fix(ui): P1-1 彻底修复线程切换竞态 — 抑制 useStream 旧数据
useStream 在 threadId 变更后仍有一个渲染周期保留旧线程的 messages。 新增 threadJustSwitched 标记,切换时忽略 thread.messages,强制走 historicalMessages(新线程为空),保证欢迎页必然显示。 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
c610e23931
commit
ac1eaf63c3
@@ -155,16 +155,20 @@ function App() {
|
||||
// ── Active messages ──────────────────────────────────────────────────────
|
||||
const lastMessagesRef = useRef<Message[]>([]);
|
||||
const lastThreadIdRef = useRef<string | null>(null);
|
||||
// Clear stale messages when thread switches to avoid showing previous thread's content
|
||||
if (currentThreadId !== lastThreadIdRef.current) {
|
||||
// Detect thread switch — when threadId changes, thread.messages still holds stale data
|
||||
// for the current render cycle (useStream is async). Track this to suppress stale data.
|
||||
const threadJustSwitched = currentThreadId !== lastThreadIdRef.current;
|
||||
if (threadJustSwitched) {
|
||||
lastMessagesRef.current = [];
|
||||
lastThreadIdRef.current = currentThreadId;
|
||||
}
|
||||
if (thread.messages.length > 0) {
|
||||
// Only trust thread.messages when useStream has caught up to the current threadId
|
||||
const threadMessagesValid = !threadJustSwitched && thread.messages.length > 0;
|
||||
if (threadMessagesValid) {
|
||||
lastMessagesRef.current = thread.messages;
|
||||
}
|
||||
const activeMessages: Message[] = deduplicateMessages(
|
||||
thread.messages.length > 0
|
||||
threadMessagesValid
|
||||
? thread.messages
|
||||
: lastMessagesRef.current.length > 0
|
||||
? lastMessagesRef.current
|
||||
|
||||
Reference in New Issue
Block a user