fix: stabilize auth sync and stream recovery for ji

This commit is contained in:
吉鑫升 committed 2026-08-17 19:27:39 +08:00
1 parent 5bf08c83a6
commit 68c4896088
6 files changed
+118 -50

No files matched your search

+26 -23
View File
@@ -472,13 +472,11 @@ export function useThreadStream({
const lastStreamActivityAtRef = useRef(0);
const streamReconciliationInFlightRef = useRef(false);
const streamReconnectAttemptsRef = useRef(0);
const streamReconnectToastShownRef = useRef(false);
const streamReconnectFailureToastShownRef = useRef(false);
const markStreamActivity = useCallback(() => {
lastStreamActivityAtRef.current = Date.now();
streamReconnectAttemptsRef.current = 0;
streamReconnectToastShownRef.current = false;
streamReconnectFailureToastShownRef.current = false;
}, []);
@@ -650,19 +648,20 @@ export function useThreadStream({
return;
}
if (
typeof event === "object" &&
event !== null &&
"type" in event &&
event.type === "llm_retry" &&
if (
typeof event === "object" &&
event !== null &&
"type" in event &&
event.type === "llm_retry" &&
"message" in event &&
typeof event.message === "string" &&
event.message.trim()
) {
const e = event as { type: "llm_retry"; message: string };
toast(e.message);
return;
}
typeof event.message === "string" &&
event.message.trim()
) {
// The backend is already handling this with bounded retry/backoff.
// Surfacing every transient model retry as a toast made normal model
// stalls look like repeated "network fluctuation" failures.
return;
}
// Context compression about to run a (blocking) summarization LLM call —
// surface a transient hint so the wait before the answer streams isn't a
@@ -752,7 +751,7 @@ export function useThreadStream({
const [optimisticMessages, setOptimisticMessages] = useState<Message[]>([]);
const [isUploading, setIsUploading] = useState(false);
const [threadValueOverrides, setThreadValueOverrides] = useState<
Partial<ThreadTitleRefreshState>
Partial<AgentThreadState>
>({});
const sendInFlightRef = useRef(false);
// Carries the ask time of an in-flight send until the server-assigned human
@@ -824,10 +823,6 @@ export function useThreadStream({
// enough time to receive its first event. A genuine event resets
// the attempt counter through ``markStreamActivity``.
lastStreamActivityAtRef.current = Date.now();
if (!streamReconnectToastShownRef.current) {
streamReconnectToastShownRef.current = true;
toast.info("回答连接出现波动,正在自动恢复…");
}
const stream = threadStreamRef.current;
void Promise.resolve(stream.stop())
@@ -858,12 +853,14 @@ export function useThreadStream({
storage?.removeItem(`lg:stream:${activeThreadId}`);
streamReconnectAttemptsRef.current = 0;
streamReconnectToastShownRef.current = false;
streamReconnectFailureToastShownRef.current = false;
if (run.status === "success") {
setOptimisticMessages([]);
setThreadValueOverrides(finalState.values as Partial<AgentThreadState>);
listeners.current.onFinish?.(finalState.values as AgentThreadState);
void queryClient.invalidateQueries({ queryKey: ["threads", "search"] });
} else {
setThreadValueOverrides(finalState.values as Partial<AgentThreadState>);
toast.error("本轮生成未能完成,已同步当前对话内容,可重新发起请求。");
}
@@ -960,10 +957,11 @@ export function useThreadStream({
return;
}
setThreadValueOverrides({
setThreadValueOverrides((prev) => ({
...prev,
title: nextState.title,
title_provisional: nextState.title_provisional,
});
}));
if (nextState.title) {
updateCachedThreadLists(queryClient, (threads) =>
@@ -1329,12 +1327,17 @@ export function useThreadStream({
}
: thread.values;
const mergedMessages =
Array.isArray(mergedValues.messages)
? (mergedValues.messages as Message[])
: thread.messages;
const mergedThread =
optimisticMessages.length > 0 || mergedValues !== thread.values
? ({
...thread,
values: mergedValues,
messages: [...thread.messages, ...optimisticMessages],
messages: [...mergedMessages, ...optimisticMessages],
} as typeof thread)
: thread;