添加对话记录保存详细日志,诊断conversation_id传递问题
添加日志位置: 1. chat/index.tsx: 发送消息、接收会话ID变更、初始化读取localStorage 2. use-chat-message.ts: 发送消息、接收新会话ID、处理新会话 3. use-conversation.ts: setCurrConversationId保存到localStorage 帮助诊断为什么对话记录没有固定(每次都创建新会话) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -60,7 +60,11 @@ export default function Chat() {
|
|||||||
} = useChatMessage({
|
} = useChatMessage({
|
||||||
onUpdateConversationList: updateConversationInList,
|
onUpdateConversationList: updateConversationInList,
|
||||||
onConversationIdChange: async (conversationId: string) => {
|
onConversationIdChange: async (conversationId: string) => {
|
||||||
// console.log('🔄 收到会话ID变更通知:', conversationId);
|
console.log('🔄 [Chat] 收到会话ID变更通知:', {
|
||||||
|
oldConversationId: currConversationId,
|
||||||
|
newConversationId: conversationId,
|
||||||
|
willUpdateLocalStorage: true
|
||||||
|
});
|
||||||
|
|
||||||
// 设置当前会话ID(这会触发localStorage更新)
|
// 设置当前会话ID(这会触发localStorage更新)
|
||||||
setCurrConversationId(conversationId, CHAT_CONFIG.APP_ID);
|
setCurrConversationId(conversationId, CHAT_CONFIG.APP_ID);
|
||||||
@@ -302,7 +306,12 @@ export default function Chat() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// console.log('📤 发送消息:', { message, conversationId: currConversationId });
|
console.log('📤 [Chat] 发送消息:', {
|
||||||
|
message: message.substring(0, 50) + (message.length > 50 ? '...' : ''),
|
||||||
|
currConversationId,
|
||||||
|
isNewConversation,
|
||||||
|
willSendConversationId: isNewConversation ? null : currConversationId
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 准备输入数据
|
// 准备输入数据
|
||||||
@@ -414,8 +423,12 @@ export default function Chat() {
|
|||||||
const _conversationId = getConversationIdFromStorage(CHAT_CONFIG.APP_ID);
|
const _conversationId = getConversationIdFromStorage(CHAT_CONFIG.APP_ID);
|
||||||
const isNotNewConversation = conversations.some((item: ConversationItem) => item.id === _conversationId);
|
const isNotNewConversation = conversations.some((item: ConversationItem) => item.id === _conversationId);
|
||||||
|
|
||||||
// console.log('💾 本地存储的会话ID:', _conversationId);
|
console.log('💾 [Chat] 初始化 - 本地存储的会话ID:', {
|
||||||
// console.log('🔍 是否为已存在的会话:', isNotNewConversation);
|
conversationId: _conversationId,
|
||||||
|
isNotNewConversation,
|
||||||
|
conversationsCount: conversations.length,
|
||||||
|
conversationIds: conversations.map((c: ConversationItem) => c.id)
|
||||||
|
});
|
||||||
|
|
||||||
// 获取新会话信息
|
// 获取新会话信息
|
||||||
const { user_input_form, opening_statement: introduction } = (appParams as any).data || {};
|
const { user_input_form, opening_statement: introduction } = (appParams as any).data || {};
|
||||||
@@ -436,11 +449,11 @@ export default function Chat() {
|
|||||||
|
|
||||||
// 如果存在有效的会话ID,则设置为当前会话
|
// 如果存在有效的会话ID,则设置为当前会话
|
||||||
if (isNotNewConversation) {
|
if (isNotNewConversation) {
|
||||||
// console.log('🎯 设置当前会话ID:', _conversationId);
|
console.log('🎯 [Chat] 初始化 - 设置当前会话ID:', _conversationId);
|
||||||
setCurrConversationId(_conversationId, CHAT_CONFIG.APP_ID, false);
|
setCurrConversationId(_conversationId, CHAT_CONFIG.APP_ID, false);
|
||||||
} else {
|
} else {
|
||||||
// 如果localStorage为空或会话不存在,自动创建新会话
|
// 如果localStorage为空或会话不存在,自动创建新会话
|
||||||
console.log('🆕 localStorage为空或会话不存在,创建新会话');
|
console.log('🆕 [Chat] 初始化 - localStorage为空或会话不存在,创建新会话');
|
||||||
setCurrConversationId('-1', CHAT_CONFIG.APP_ID, false);
|
setCurrConversationId('-1', CHAT_CONFIG.APP_ID, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -183,7 +183,12 @@ export default function useChatMessage({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// console.log('📤 发送消息:', { message, conversationId });
|
console.log('📤 [useChatMessage] 发送消息:', {
|
||||||
|
messageLength: message.length,
|
||||||
|
messagePreview: message.substring(0, 50) + (message.length > 50 ? '...' : ''),
|
||||||
|
conversationId,
|
||||||
|
isNewConversation: conversationId === null || conversationId === '-1'
|
||||||
|
});
|
||||||
|
|
||||||
setUserQuery(message);
|
setUserQuery(message);
|
||||||
setResponding();
|
setResponding();
|
||||||
@@ -310,7 +315,10 @@ export default function useChatMessage({
|
|||||||
// 重要:确保正确获取新会话ID
|
// 重要:确保正确获取新会话ID
|
||||||
if (newConversationId && !tempNewConversationId) {
|
if (newConversationId && !tempNewConversationId) {
|
||||||
tempNewConversationId = newConversationId;
|
tempNewConversationId = newConversationId;
|
||||||
// console.log('🆔 获取到新会话ID:', tempNewConversationId);
|
console.log('🆔 [useChatMessage] 首次获取到新会话ID:', {
|
||||||
|
newConversationId: tempNewConversationId,
|
||||||
|
originalConversationId: conversationId
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
setMessageTaskId(taskId || '');
|
setMessageTaskId(taskId || '');
|
||||||
@@ -383,13 +391,16 @@ export default function useChatMessage({
|
|||||||
// 如果是新会话,处理会话ID更新和名称生成
|
// 如果是新会话,处理会话ID更新和名称生成
|
||||||
// 检查原始传入的conversationId是否为新会话标识
|
// 检查原始传入的conversationId是否为新会话标识
|
||||||
const isNewConversation = conversationId === '-1' || conversationId === null;
|
const isNewConversation = conversationId === '-1' || conversationId === null;
|
||||||
|
console.log('🔍 [useChatMessage] 检查是否需要处理新会话:', {
|
||||||
|
tempNewConversationId,
|
||||||
|
originalConversationId: conversationId,
|
||||||
|
isNewConversation,
|
||||||
|
willProcess: !!(tempNewConversationId && isNewConversation)
|
||||||
|
});
|
||||||
|
|
||||||
if (tempNewConversationId && isNewConversation) {
|
if (tempNewConversationId && isNewConversation) {
|
||||||
try {
|
try {
|
||||||
// console.log('🆕 处理新会话:', {
|
console.log('🆕 [useChatMessage] 处理新会话,调用onConversationIdChange:', tempNewConversationId);
|
||||||
// tempNewConversationId,
|
|
||||||
// originalConversationId: conversationId,
|
|
||||||
// isNewConversation
|
|
||||||
// });
|
|
||||||
|
|
||||||
// 通知会话ID变更(这会触发localStorage更新)
|
// 通知会话ID变更(这会触发localStorage更新)
|
||||||
onConversationIdChange?.(tempNewConversationId);
|
onConversationIdChange?.(tempNewConversationId);
|
||||||
@@ -398,18 +409,12 @@ export default function useChatMessage({
|
|||||||
const res = await generateConversationName(tempNewConversationId);
|
const res = await generateConversationName(tempNewConversationId);
|
||||||
const { data } = res as any;
|
const { data } = res as any;
|
||||||
if (data?.name) {
|
if (data?.name) {
|
||||||
// console.log('📝 生成会话名称:', data.name);
|
console.log('📝 [useChatMessage] 生成会话名称:', data.name);
|
||||||
onUpdateConversationList?.(tempNewConversationId, { name: data.name });
|
onUpdateConversationList?.(tempNewConversationId, { name: data.name });
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('生成会话名称失败:', err);
|
console.error('❌ [useChatMessage] 生成会话名称失败:', err);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// console.log('🔍 不是新会话,跳过处理:', {
|
|
||||||
// tempNewConversationId,
|
|
||||||
// conversationId,
|
|
||||||
// isNewConversation
|
|
||||||
// });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setNotResponding();
|
setNotResponding();
|
||||||
|
|||||||
@@ -113,6 +113,13 @@ export default function useConversation() {
|
|||||||
isSetToLocalStorage = true,
|
isSetToLocalStorage = true,
|
||||||
newConversationName = ''
|
newConversationName = ''
|
||||||
) => {
|
) => {
|
||||||
|
console.log('💾 [useConversation] setCurrConversationId:', {
|
||||||
|
id,
|
||||||
|
appId,
|
||||||
|
isSetToLocalStorage,
|
||||||
|
willSaveToStorage: isSetToLocalStorage && id !== '-1'
|
||||||
|
});
|
||||||
|
|
||||||
doSetCurrConversationId(id);
|
doSetCurrConversationId(id);
|
||||||
|
|
||||||
if (isSetToLocalStorage && id !== '-1') {
|
if (isSetToLocalStorage && id !== '-1') {
|
||||||
@@ -128,8 +135,14 @@ export default function useConversation() {
|
|||||||
|
|
||||||
globalThis.localStorage?.setItem(storageConversationIdKey, JSON.stringify(conversationIdInfo));
|
globalThis.localStorage?.setItem(storageConversationIdKey, JSON.stringify(conversationIdInfo));
|
||||||
|
|
||||||
|
console.log('✅ [useConversation] 已保存到localStorage:', {
|
||||||
|
appUrlKey,
|
||||||
|
conversationId: id,
|
||||||
|
allKeys: Object.keys(conversationIdInfo)
|
||||||
|
});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('保存会话ID到本地存储失败:', error);
|
console.error('❌ [useConversation] 保存会话ID到本地存储失败:', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user