diff --git a/components/chat.tsx b/components/chat.tsx index 680f1d3..ff0c718 100644 --- a/components/chat.tsx +++ b/components/chat.tsx @@ -300,26 +300,31 @@ export function Chat({ }); // 调试:监控聊天状态变化 & 持久化生成状态 + // 使用 ref 来访问最新的 modelsData,避免依赖数组问题 + const modelsDataRef = useRef(modelsData); useEffect(() => { - console.log(`[Chat] Status: ${status}`); + modelsDataRef.current = modelsData; + }, [modelsData]); + + useEffect(() => { + console.log(`[Chat] Status changed: ${status}`); // 🆕 当状态变为 submitted 时,记录生成任务 if (status === "submitted") { - // 判断模型类型 - let modelType: PendingGeneration["modelType"] = "other"; - if (currentModel?.providerType === "gemini") { - modelType = "gemini"; - } else if (currentModel?.providerType === "volcano-vision") { - modelType = "volcano-vision"; - } else if (currentModel?.providerType === "volcano-video") { - modelType = "volcano-video"; - } else if (currentModel?.providerType === "qianwen-video") { - modelType = "qianwen-video"; - } + // 判断模型类型 - 使用 ref 获取最新的 modelsData + const models = modelsDataRef.current?.models; + const model = models?.find((m: any) => m.id === currentModelId); + let modelType: PendingGeneration["modelType"] = "other"; + if (model?.providerType === "gemini") modelType = "gemini"; + else if (model?.providerType === "volcano-vision") modelType = "volcano-vision"; + else if (model?.providerType === "volcano-video") modelType = "volcano-video"; + else if (model?.providerType === "qianwen-video") modelType = "qianwen-video"; + + console.log(`[Chat] 🚀 记录生成任务: modelType=${modelType}, modelId=${currentModelId}`); startGeneration(currentModelId, modelType); } - }, [status, currentModelId, currentModel?.providerType, startGeneration]); + }, [status, currentModelId, startGeneration]); const searchParams = useSearchParams(); const query = searchParams.get("query"); diff --git a/components/messages.tsx b/components/messages.tsx index b1c42e5..db1ec73 100644 --- a/components/messages.tsx +++ b/components/messages.tsx @@ -231,10 +231,25 @@ function PureMessages({ // 恢复的待处理状态:有 pendingGeneration 且最后一条是用户消息 const hasRestoredPending = pendingGeneration && isWaitingForResponse; + // 🔍 调试日志 + console.log("[Messages] 占位图状态检查:", { + pendingGeneration, + lastMessageRole: lastMessage?.role, + isWaitingForResponse, + hasRestoredPending, + showThinkingWithDelay, + messagesCount: messages.length, + }); + // 显示条件:正常等待动画 或 恢复的待处理状态 const shouldShow = showThinkingWithDelay || hasRestoredPending; - if (!shouldShow) return null; + if (!shouldShow) { + console.log("[Messages] 不显示占位图: shouldShow=false"); + return null; + } + + console.log("[Messages] ✅ 显示占位图!"); // 优先使用恢复的状态,否则使用当前模型类型 const modelType = pendingGeneration?.modelType; diff --git a/hooks/use-pending-generation.ts b/hooks/use-pending-generation.ts index cd83e19..581643d 100644 --- a/hooks/use-pending-generation.ts +++ b/hooks/use-pending-generation.ts @@ -55,10 +55,15 @@ export function usePendingGeneration(chatId: string) { // 初始化:检查是否有当前 chatId 的未完成任务 useEffect(() => { const generations = getPendingGenerations(); + console.log("[PendingGeneration] 初始化检查, chatId:", chatId); + console.log("[PendingGeneration] localStorage 中的所有任务:", generations); + const found = generations.find((g) => g.chatId === chatId); if (found) { setPendingGeneration(found); - console.log("[PendingGeneration] 恢复处理中状态:", found); + console.log("[PendingGeneration] ✅ 恢复处理中状态:", found); + } else { + console.log("[PendingGeneration] ❌ 未找到匹配的任务"); } }, [chatId]);