diff --git a/app/components/chat/index.tsx b/app/components/chat/index.tsx index e2ad579..f91edcd 100644 --- a/app/components/chat/index.tsx +++ b/app/components/chat/index.tsx @@ -60,7 +60,11 @@ export default function Chat() { } = useChatMessage({ onUpdateConversationList: updateConversationInList, onConversationIdChange: async (conversationId: string) => { - // console.log('🔄 收到会话ID变更通知:', conversationId); + console.log('🔄 [Chat] 收到会话ID变更通知:', { + oldConversationId: currConversationId, + newConversationId: conversationId, + willUpdateLocalStorage: true + }); // 设置当前会话ID(这会触发localStorage更新) setCurrConversationId(conversationId, CHAT_CONFIG.APP_ID); @@ -302,7 +306,12 @@ export default function Chat() { return; } - // console.log('📤 发送消息:', { message, conversationId: currConversationId }); + console.log('📤 [Chat] 发送消息:', { + message: message.substring(0, 50) + (message.length > 50 ? '...' : ''), + currConversationId, + isNewConversation, + willSendConversationId: isNewConversation ? null : currConversationId + }); try { // 准备输入数据 @@ -414,8 +423,12 @@ export default function Chat() { const _conversationId = getConversationIdFromStorage(CHAT_CONFIG.APP_ID); const isNotNewConversation = conversations.some((item: ConversationItem) => item.id === _conversationId); - // console.log('💾 本地存储的会话ID:', _conversationId); - // console.log('🔍 是否为已存在的会话:', isNotNewConversation); + console.log('💾 [Chat] 初始化 - 本地存储的会话ID:', { + conversationId: _conversationId, + isNotNewConversation, + conversationsCount: conversations.length, + conversationIds: conversations.map((c: ConversationItem) => c.id) + }); // 获取新会话信息 const { user_input_form, opening_statement: introduction } = (appParams as any).data || {}; @@ -436,11 +449,11 @@ export default function Chat() { // 如果存在有效的会话ID,则设置为当前会话 if (isNotNewConversation) { - // console.log('🎯 设置当前会话ID:', _conversationId); + console.log('🎯 [Chat] 初始化 - 设置当前会话ID:', _conversationId); setCurrConversationId(_conversationId, CHAT_CONFIG.APP_ID, false); } else { // 如果localStorage为空或会话不存在,自动创建新会话 - console.log('🆕 localStorage为空或会话不存在,创建新会话'); + console.log('🆕 [Chat] 初始化 - localStorage为空或会话不存在,创建新会话'); setCurrConversationId('-1', CHAT_CONFIG.APP_ID, false); } diff --git a/app/hooks/use-chat-message.ts b/app/hooks/use-chat-message.ts index 7fcfa6a..9d60603 100644 --- a/app/hooks/use-chat-message.ts +++ b/app/hooks/use-chat-message.ts @@ -183,7 +183,12 @@ export default function useChatMessage({ 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); setResponding(); @@ -310,7 +315,10 @@ export default function useChatMessage({ // 重要:确保正确获取新会话ID if (newConversationId && !tempNewConversationId) { tempNewConversationId = newConversationId; - // console.log('🆔 获取到新会话ID:', tempNewConversationId); + console.log('🆔 [useChatMessage] 首次获取到新会话ID:', { + newConversationId: tempNewConversationId, + originalConversationId: conversationId + }); } setMessageTaskId(taskId || ''); @@ -383,13 +391,16 @@ export default function useChatMessage({ // 如果是新会话,处理会话ID更新和名称生成 // 检查原始传入的conversationId是否为新会话标识 const isNewConversation = conversationId === '-1' || conversationId === null; + console.log('🔍 [useChatMessage] 检查是否需要处理新会话:', { + tempNewConversationId, + originalConversationId: conversationId, + isNewConversation, + willProcess: !!(tempNewConversationId && isNewConversation) + }); + if (tempNewConversationId && isNewConversation) { try { - // console.log('🆕 处理新会话:', { - // tempNewConversationId, - // originalConversationId: conversationId, - // isNewConversation - // }); + console.log('🆕 [useChatMessage] 处理新会话,调用onConversationIdChange:', tempNewConversationId); // 通知会话ID变更(这会触发localStorage更新) onConversationIdChange?.(tempNewConversationId); @@ -398,18 +409,12 @@ export default function useChatMessage({ const res = await generateConversationName(tempNewConversationId); const { data } = res as any; if (data?.name) { - // console.log('📝 生成会话名称:', data.name); + console.log('📝 [useChatMessage] 生成会话名称:', data.name); onUpdateConversationList?.(tempNewConversationId, { name: data.name }); } } catch (err) { - console.error('生成会话名称失败:', err); + console.error('❌ [useChatMessage] 生成会话名称失败:', err); } - } else { - // console.log('🔍 不是新会话,跳过处理:', { - // tempNewConversationId, - // conversationId, - // isNewConversation - // }); } setNotResponding(); diff --git a/app/hooks/use-conversation.ts b/app/hooks/use-conversation.ts index e569264..bd81dac 100644 --- a/app/hooks/use-conversation.ts +++ b/app/hooks/use-conversation.ts @@ -113,6 +113,13 @@ export default function useConversation() { isSetToLocalStorage = true, newConversationName = '' ) => { + console.log('💾 [useConversation] setCurrConversationId:', { + id, + appId, + isSetToLocalStorage, + willSaveToStorage: isSetToLocalStorage && id !== '-1' + }); + doSetCurrConversationId(id); if (isSetToLocalStorage && id !== '-1') { @@ -128,8 +135,14 @@ export default function useConversation() { globalThis.localStorage?.setItem(storageConversationIdKey, JSON.stringify(conversationIdInfo)); + console.log('✅ [useConversation] 已保存到localStorage:', { + appUrlKey, + conversationId: id, + allKeys: Object.keys(conversationIdInfo) + }); + } catch (error) { - console.error('保存会话ID到本地存储失败:', error); + console.error('❌ [useConversation] 保存会话ID到本地存储失败:', error); } } };