fix(webui): clarify command session activity

This commit is contained in:
chengyongru authored and chengyongru committed 2026-11-13 22:30:27 +08:00
1 parent c80ae65746
commit de9cce45b4
28 files changed
+1174 -68

No files matched your search

+26 -5
View File
@@ -126,6 +126,8 @@ export function useNanobotStream(
historyActiveTurnId: string | null = null,
): {
messages: UIMessage[];
/** Stable across text-only updates; refreshed at exec/history boundaries. */
execSourceMessages: UIMessage[];
modelRequests: ReadonlyMap<string, UIModelRequestSnapshot>;
/** Conversation owning these messages, including while a selection is handing over. */
messagesKey: string | null;
@@ -178,6 +180,7 @@ export function useNanobotStream(
historyRunGenerationRef.current = { chatId, generation: chatId ? client.getRunGeneration(chatId) : 0 };
}
const [messages, setRenderedMessages] = useState<UIMessage[]>(initialTranscript.messages);
const [execSourceMessages, setExecSourceMessages] = useState<UIMessage[]>(initialTranscript.messages);
const [modelRequests, setModelRequests] = useState(initialTranscript.modelRequests);
const messagesRef = useRef(messages);
// Keep received state ahead of React's batched render: navigation can unmount
@@ -292,6 +295,7 @@ export function useNanobotStream(
: projection.fileEditSegmentId,
};
transcriptRef.current.rejectTurn(rejectedTurnId);
setExecSourceMessages(nextMessages);
return nextMessages;
});
@@ -484,6 +488,7 @@ export function useNanobotStream(
useEffect(() => {
transcriptRef.current = createTranscript(initialMessages, chatId ? messageCache?.get(chatId) : undefined, historyKey, temporary);
setMessages(transcriptRef.current.messages);
setExecSourceMessages(transcriptRef.current.messages);
setMessageScope({ chatId, historyKey, client });
setRunState(chatId ? client.getRunState(chatId) : null);
setStreamError(null);
@@ -504,8 +509,14 @@ export function useNanobotStream(
) => {
setMessages((prev) => {
transcriptRef.current.projection = { ...transcriptRef.current.projection, messages: prev };
return transcriptRef.current.project(event, { createId: () => crypto.randomUUID(), now: Date.now(),
const next = transcriptRef.current.project(event, { createId: () => crypto.randomUUID(), now: Date.now(),
sideChannel: options?.sideChannel });
if (event.event === "message" && event.tool_events?.some((tool) => (
(tool.name === "exec" || (tool as { function?: { name?: unknown } }).function?.name === "exec")
&& tool.phase === "end" && typeof tool.result === "string"
&& tool.result.endsWith("\nInitial wait ended; session remains active.")
))) setExecSourceMessages(next);
return next;
});
}, [setMessages]);
@@ -520,7 +531,7 @@ export function useNanobotStream(
pending.owner.finishRead(pending.read);
if (transcriptRef.current === pending.owner) publishHistory();
}, [publishHistory]);
const detailRequestsRef = useRef(new Map<string, Promise<{ ref: string; events: ThreadProjectionEvent[] } | null>>());
const detailRequestsRef = useRef(new WeakMap<ThreadTranscript, Map<string, Promise<{ ref: string; events: ThreadProjectionEvent[] } | null>>>());
const reconcileCanonical = useCallback((generation: number, canonical: ReturnType<typeof persistedCanonicalRunState>, accepted: boolean) => {
if (!chatId) return;
const settle = !canonical.snapshot.hasPendingToolCalls
@@ -552,6 +563,7 @@ export function useNanobotStream(
const accepted = owner.accept(body, decision);
// Commit the received owner before cache listeners/ledger callbacks can reenter.
setMessages(owner.messages);
setExecSourceMessages(owner.messages);
if (accepted && chatId) messageCache?.set(chatId, owner.save());
if (ownRead) {
publishing = true;
@@ -605,6 +617,7 @@ export function useNanobotStream(
flushPendingStreamEvents();
const accepted = owner.accept(body, read);
setMessages(owner.messages);
setExecSourceMessages(owner.messages);
if (accepted) {
if (chatId) messageCache?.set(chatId, owner.save());
settleFragment(owner);
@@ -629,18 +642,25 @@ export function useNanobotStream(
const loadTraceDetails = useCallback(async (refs: string[]) => {
const owner = transcriptRef.current;
if (!historyKey) return [];
let requests = detailRequestsRef.current.get(owner);
if (!requests) {
requests = new Map();
detailRequestsRef.current.set(owner, requests);
}
const ownerRequests = requests;
const details = await Promise.all(refs.map((ref) => {
const id = `${historyKey}:${ref}`;
const existing = detailRequestsRef.current.get(id);
const existing = ownerRequests.get(id);
if (existing) return existing;
const request = fetchWebuiThreadTraceDetail(getToken(), historyKey, ref).then((detail) => {
if (transcriptRef.current !== owner) return null;
flushPendingStreamEvents();
owner.expandDetail(ref, detail.events);
setMessages(owner.messages);
setExecSourceMessages(owner.messages);
return { ref, events: detail.events };
}).finally(() => detailRequestsRef.current.delete(id));
detailRequestsRef.current.set(id, request);
}).finally(() => ownerRequests.delete(id));
ownerRequests.set(id, request);
return request;
}));
return details.filter((detail) => detail !== null);
@@ -1062,6 +1082,7 @@ export function useNanobotStream(
return {
messages,
execSourceMessages,
modelRequests,
messagesKey: messageScope.historyKey ?? messageScope.chatId,
messagesReady: messageScope.chatId === chatId && messageScope.historyKey === historyKey && messageScope.client === client,