fix(webui): bind landing first message to created chat

This commit is contained in:
sid
2026-07-10 20:19:10 +08:00
committed by Xubin Ren
parent 45a6466c2c
commit d32f8961b1
2 changed files with 80 additions and 3 deletions
+15 -3
View File
@@ -339,6 +339,7 @@ export function ThreadShell({
const filePreviewWidthRef = useRef(FILE_PREVIEW_DEFAULT_WIDTH);
const filePreviewCloseTimerRef = useRef<number | null>(null);
const pendingFirstRef = useRef<PendingFirstMessage | null>(null);
const [pendingFirstTargetChatId, setPendingFirstTargetChatId] = useState<string | null>(null);
const viewportRef = useRef<ThreadViewportHandle | null>(null);
const messageCacheRef = useRef<Map<string, UIMessage[]>>(new Map());
/** Last chatId we associated with the in-memory thread (for cache-on-switch). */
@@ -555,15 +556,22 @@ export function ThreadShell({
messageCacheRef.current.set(chatId, projectWebuiThreadMessages(messages));
}, [chatId, loading, messages]);
// The landing composer queues the first message while `new_chat` is in flight.
// Only the chat created for that send may consume it; selecting another chat
// while creation is pending must not leak the message there.
useEffect(() => {
if (!chatId) return;
if (!chatId || pendingFirstTargetChatId !== chatId) return;
const pending = pendingFirstRef.current;
if (!pending) return;
if (!pending) {
setPendingFirstTargetChatId(null);
return;
}
pendingFirstRef.current = null;
setPendingFirstTargetChatId(null);
setScrollToLatestUserPromptSignal((value) => value + 1);
send(pending.content, pending.images, pending.options);
setBooting(false);
}, [chatId, send]);
}, [chatId, pendingFirstTargetChatId, send]);
useEffect(() => {
let cancelled = false;
@@ -585,11 +593,15 @@ export function ThreadShell({
if (booting) return;
setBooting(true);
pendingFirstRef.current = { content, images, options: withWorkspaceScope(options) };
setPendingFirstTargetChatId(null);
const newId = await onCreateChat?.(workspaceScope);
if (!newId) {
pendingFirstRef.current = null;
setPendingFirstTargetChatId(null);
setBooting(false);
return;
}
setPendingFirstTargetChatId(newId);
},
[booting, onCreateChat, withWorkspaceScope, workspaceScope],
);