refactor(chat): load history without sessionId

main
sp mac bookpro 2605 2026-05-29 21:15:44 +08:00
parent b6090f0897
commit 082e2800a8
1 changed files with 9 additions and 22 deletions

View File

@ -14,7 +14,6 @@ import {
ImageAPI, ImageAPI,
ModelOverrides, ModelOverrides,
RetrievedSnippet, RetrievedSnippet,
SessionAPI,
regenerateMessage, regenerateMessage,
streamChat, streamChat,
ToolCallTrace ToolCallTrace
@ -73,7 +72,6 @@ export default function ChatPage() {
}); });
const bodyRef = useRef<HTMLDivElement>(null); const bodyRef = useRef<HTMLDivElement>(null);
const abortRef = useRef<AbortController | null>(null); const abortRef = useRef<AbortController | null>(null);
const creatingSessionRef = useRef(false);
const autoScrollRef = useRef(true); const autoScrollRef = useRef(true);
// URL 参数 ?session=xxx&msg=yyy // URL 参数 ?session=xxx&msg=yyy
@ -134,8 +132,8 @@ export default function ChatPage() {
}; };
const loadMessages = async () => { const loadMessages = async () => {
if (!id || !sessionId) return; if (!id) return;
const his = await ChatAPI.history(id, sessionId); const his = await ChatAPI.history(id, sessionId || undefined);
setMessages(Array.isArray(his.messages) ? his.messages : []); setMessages(Array.isArray(his.messages) ? his.messages : []);
setBranches(his.branches || {}); setBranches(his.branches || {});
requestAnimationFrame(() => { requestAnimationFrame(() => {
@ -169,17 +167,6 @@ export default function ChatPage() {
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [id]); }, [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(() => { useEffect(() => {
if (!id) return; if (!id) return;
loadMessages(); loadMessages();
@ -198,7 +185,7 @@ export default function ChatPage() {
}; };
const handleSendStream = async (text: string) => { const handleSendStream = async (text: string) => {
if (!id || !sessionId) return; if (!id) return;
const tempUser: ChatMessage = { const tempUser: ChatMessage = {
id: 'tmp-' + Date.now(), id: 'tmp-' + Date.now(),
role: 'user', role: 'user',
@ -340,7 +327,7 @@ export default function ChatPage() {
} }
}, },
ctrl.signal, ctrl.signal,
sessionId, sessionId || undefined,
model, model,
imageUrls imageUrls
); );
@ -362,7 +349,7 @@ export default function ChatPage() {
}; };
const handleSendNonStream = async (text: string) => { const handleSendNonStream = async (text: string) => {
if (!id || !sessionId) return; if (!id) return;
const tempUser: ChatMessage = { const tempUser: ChatMessage = {
id: 'tmp-' + Date.now(), id: 'tmp-' + Date.now(),
role: 'user', role: 'user',
@ -378,7 +365,7 @@ export default function ChatPage() {
const res = await ChatAPI.send( const res = await ChatAPI.send(
id, id,
content, content,
sessionId, sessionId || undefined,
model, model,
imageUrls imageUrls
); );
@ -395,7 +382,7 @@ export default function ChatPage() {
const handleSend = async () => { const handleSend = async () => {
const text = input.trim(); const text = input.trim();
if (!text || !id || sending || !sessionId) return; if (!text || !id || sending) return;
setInput(''); setInput('');
setSending(true); setSending(true);
try { try {
@ -412,8 +399,8 @@ export default function ChatPage() {
}; };
const handleClear = async () => { const handleClear = async () => {
if (!id || !sessionId) return; if (!id) return;
await ChatAPI.clear(id, sessionId); await ChatAPI.clear(id, sessionId || undefined);
setMessages([]); setMessages([]);
setBranches({}); setBranches({});
msg.success('对话已清空'); msg.success('对话已清空');