diff --git a/components/chat.tsx b/components/chat.tsx index 7bd8692..84ccb6d 100644 --- a/components/chat.tsx +++ b/components/chat.tsx @@ -301,22 +301,14 @@ export function Chat({ // 🆕 收到消息,清除持久化状态 completeGeneration(); - // 🆕 更新 URL(不跳转,只改变地址栏) + // 🆕 如果当前在首页,更新 URL 为 /chat/${id},确保刷新后不会丢失消息 if (window.location.pathname === "/" || window.location.pathname === "") { - console.log("[Chat] 更新 URL 到 /chat/" + id + "(不刷新页面)"); + console.log("[Chat] 更新 URL 到 /chat/" + id); window.history.replaceState({}, "", `/chat/${id}`); } setMessages((prevMessages) => { - // 🔍 调试日志:检查当前消息列表 - console.log("[Chat] setMessages 前的消息列表:", prevMessages.map(m => ({ - id: m.id?.substring(0, 8), - role: m.role, - hasContent: m.parts?.length > 0 - }))); - if (prevMessages.some(m => m.id === messageData.id)) { - console.log("[Chat] 消息已存在,跳过添加"); return prevMessages; } @@ -331,14 +323,7 @@ export function Chat({ attachments: [], }; - const updatedMessages = [...prevMessages, newMessage]; - console.log("[Chat] setMessages 后的消息列表:", updatedMessages.map(m => ({ - id: m.id?.substring(0, 8), - role: m.role, - hasContent: m.parts?.length > 0 - }))); - - return updatedMessages; + return [...prevMessages, newMessage]; }); } catch (e) { console.error("[Chat] Failed to parse data-appendMessage:", e); @@ -350,7 +335,7 @@ export function Chat({ // 🆕 生成完成,清除持久化状态 completeGeneration(); - // 🆕 如果当前在首页,更新 URL(不刷新页面) + // 🆕 如果当前在首页,更新 URL 为 /chat/${id},确保刷新后不会丢失消息 if (window.location.pathname === "/" || window.location.pathname === "") { console.log("[Chat] onFinish: 更新 URL 到 /chat/" + id); window.history.replaceState({}, "", `/chat/${id}`); diff --git a/components/messages.tsx b/components/messages.tsx index 3db33ed..471a504 100644 --- a/components/messages.tsx +++ b/components/messages.tsx @@ -152,22 +152,12 @@ function PureMessages({ }); } - // 对于图片模型,检查是否有图片内容(更全面的匹配) + // 对于图片模型,检查是否有图片内容(包含 ![thumb| 或 ![thumb||| 标记) const hasImageContent = lastMessage?.parts && lastMessage.parts.some( (part: any) => part.type === "text" && part.text && ( part.text.includes("![thumb|") || part.text.includes("![thumb|||") || - part.text.includes("![Generated Image") || - part.text.includes("![image") || - part.text.includes("![Image") || - // 🆕 匹配更多图片格式 - part.text.includes("](http") || // Markdown 图片链接 - part.text.includes(".png)") || - part.text.includes(".jpg)") || - part.text.includes(".jpeg)") || - part.text.includes(".webp)") || - part.text.includes("图片生成") || - part.text.includes("已生成") + part.text.includes("![Generated Image") ) ); @@ -192,66 +182,26 @@ function PureMessages({ // 根据模型类型设置延迟时间(视频模型 60 秒,图片模型 4 秒,其他 6 秒) const delayTime = (isQianwenVideoModel || isVideoModel) ? 60000 : isImageGenModel ? 4000 : 6000; - // 🆕 超时保护:如果占位图显示超过 3 分钟,强制隐藏(防止卡住) - const maxShowTime = 3 * 60 * 1000; // 3 分钟 - - // 用于存储需要清理的定时器 - let delayTimer: ReturnType | null = null; - let timeoutTimer: ReturnType | null = null; - // 如果是 submitted 状态,立即显示 if (status === "submitted") { setShowThinkingWithDelay(true); - thinkingStartTimeRef.current = Date.now(); - } - // 🆕 如果是 streaming 状态但最后一条是用户消息(还没有 AI 回复),也显示占位图 - else if (status === "streaming" && lastMessage?.role === "user") { - setShowThinkingWithDelay(true); - if (!thinkingStartTimeRef.current) { - thinkingStartTimeRef.current = Date.now(); - } } // 如果是 streaming 状态且没有内容,显示 else if (status === "streaming" && lastMessage?.role === "assistant" && !hasContent) { setShowThinkingWithDelay(true); - if (!thinkingStartTimeRef.current) { - thinkingStartTimeRef.current = Date.now(); - } } // 如果 AI 开始有内容了,延迟后隐藏 else if (status === "streaming" && lastMessage?.role === "assistant" && hasContent) { - delayTimer = setTimeout(() => { + const timer = setTimeout(() => { setShowThinkingWithDelay(false); - thinkingStartTimeRef.current = null; }, delayTime); - } - // 🆕 如果状态是 ready(生成完成),立即隐藏 - else if (status === "ready") { - setShowThinkingWithDelay(false); - thinkingStartTimeRef.current = null; + + return () => clearTimeout(timer); } // 其他情况立即隐藏 else { setShowThinkingWithDelay(false); - thinkingStartTimeRef.current = null; } - - // 🆕 超时保护定时器(只在显示占位图时启动) - if (status === "submitted" || status === "streaming") { - timeoutTimer = setTimeout(() => { - if (thinkingStartTimeRef.current && Date.now() - thinkingStartTimeRef.current > maxShowTime) { - console.warn("[占位图] ⚠️ 超时保护触发:占位图显示超过 3 分钟,强制隐藏"); - setShowThinkingWithDelay(false); - thinkingStartTimeRef.current = null; - } - }, maxShowTime); - } - - // 清理所有定时器 - return () => { - if (delayTimer) clearTimeout(delayTimer); - if (timeoutTimer) clearTimeout(timeoutTimer); - }; }, [status, messages, isGeminiModel, isQianwenVideoModel, isVolcanoVisionModel, isVideoModel, isAlibabaImageModel]); // 🆕 监听新消息添加,当用户发送带图片的消息或添加加载消息时,强制滚动到底部