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,
ModelOverrides,
RetrievedSnippet,
SessionAPI,
regenerateMessage,
streamChat,
ToolCallTrace
@ -73,7 +72,6 @@ export default function ChatPage() {
});
const bodyRef = useRef<HTMLDivElement>(null);
const abortRef = useRef<AbortController | null>(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('对话已清空');