From 082e2800a80b78468368a8b40d2b45bb5fc2d93f Mon Sep 17 00:00:00 2001 From: sp mac bookpro 2605 Date: Fri, 29 May 2026 21:15:44 +0800 Subject: [PATCH] refactor(chat): load history without sessionId --- src/pages/ChatPage.tsx | 31 +++++++++---------------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/src/pages/ChatPage.tsx b/src/pages/ChatPage.tsx index 7d702d0..20af8af 100644 --- a/src/pages/ChatPage.tsx +++ b/src/pages/ChatPage.tsx @@ -14,7 +14,6 @@ import { ImageAPI, ModelOverrides, RetrievedSnippet, - SessionAPI, regenerateMessage, streamChat, ToolCallTrace @@ -73,7 +72,6 @@ export default function ChatPage() { }); const bodyRef = useRef(null); const abortRef = useRef(null); - const creatingSessionRef = useRef(false); const autoScrollRef = useRef(true); // URL 参数 ?session=xxx&msg=yyy @@ -134,8 +132,8 @@ export default function ChatPage() { }; const loadMessages = async () => { - if (!id || !sessionId) return; - const his = await ChatAPI.history(id, sessionId); + if (!id) return; + const his = await ChatAPI.history(id, sessionId || undefined); setMessages(Array.isArray(his.messages) ? his.messages : []); setBranches(his.branches || {}); requestAnimationFrame(() => { @@ -169,17 +167,6 @@ export default function ChatPage() { // eslint-disable-next-line react-hooks/exhaustive-deps }, [id]); - useEffect(() => { - if (!id || sessionId || creatingSessionRef.current) return; - creatingSessionRef.current = true; - SessionAPI.create(id) - .then((s) => setSessionId(s.id)) - .catch(() => msg.error('创建会话失败')) - .finally(() => { - creatingSessionRef.current = false; - }); - }, [id, msg, sessionId]); - useEffect(() => { if (!id) return; loadMessages(); @@ -198,7 +185,7 @@ export default function ChatPage() { }; const handleSendStream = async (text: string) => { - if (!id || !sessionId) return; + if (!id) return; const tempUser: ChatMessage = { id: 'tmp-' + Date.now(), role: 'user', @@ -340,7 +327,7 @@ export default function ChatPage() { } }, ctrl.signal, - sessionId, + sessionId || undefined, model, imageUrls ); @@ -362,7 +349,7 @@ export default function ChatPage() { }; const handleSendNonStream = async (text: string) => { - if (!id || !sessionId) return; + if (!id) return; const tempUser: ChatMessage = { id: 'tmp-' + Date.now(), role: 'user', @@ -378,7 +365,7 @@ export default function ChatPage() { const res = await ChatAPI.send( id, content, - sessionId, + sessionId || undefined, model, imageUrls ); @@ -395,7 +382,7 @@ export default function ChatPage() { const handleSend = async () => { const text = input.trim(); - if (!text || !id || sending || !sessionId) return; + if (!text || !id || sending) return; setInput(''); setSending(true); try { @@ -412,8 +399,8 @@ export default function ChatPage() { }; const handleClear = async () => { - if (!id || !sessionId) return; - await ChatAPI.clear(id, sessionId); + if (!id) return; + await ChatAPI.clear(id, sessionId || undefined); setMessages([]); setBranches({}); msg.success('对话已清空');