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,
+22 -7
View File
@@ -12,6 +12,14 @@ import { type PromptAnchor, userPromptAnchors } from "@/components/thread/prompt
interface HistoryPage {
messages: UIMessage[];
body: WebuiThreadPersistedPayload;
details: Map<string, UIMessage[]>;
}
function expandDetails(messages: UIMessage[], details: ReadonlyMap<string, UIMessage[]>): UIMessage[] {
return messages.flatMap((message) => {
const rows = message.traceDetail && details.get(message.traceDetail.ref);
return rows ? rows.map((row) => ({ ...row, activitySegmentId: message.activitySegmentId })) : [message];
});
}
const HISTORY_PAGE_LIMIT = 3;
@@ -36,7 +44,7 @@ export interface PromptHistoryNavigation {
cancel: () => void;
latest: () => void;
retry: () => void;
updateMessages: (update: (messages: UIMessage[]) => UIMessage[]) => void;
expandTraceDetail: (ref: string, rows: UIMessage[]) => void;
}
/** Browsing pages never replace the canonical latest page or live turn state. */
@@ -140,8 +148,12 @@ export function usePromptHistory(key: string | null, version: number, messages:
return merged;
}, [currentScope, enabled, index, indexed, key, messages]);
const updateMessages = useCallback((update: (messages: UIMessage[]) => UIMessage[]) => {
setPages((current) => current.map((page) => ({ ...page, messages: update(page.messages) })));
const expandTraceDetail = useCallback((ref: string, rows: UIMessage[]) => {
setPages((current) => current.map((page) => {
if (!page.messages.some((message) => message.traceDetail?.ref === ref)) return page;
const details = new Map(page.details).set(ref, rows);
return { ...page, messages: expandDetails(page.messages, details), details };
}));
}, []);
const read = useCallback(async (cursor: string, direction: "before" | "after", id?: string, automatic = false) => {
@@ -166,7 +178,7 @@ export function usePromptHistory(key: string | null, version: number, messages:
if (!id && generation && previousGeneration && generation !== previousGeneration) {
throw new Error("History changed; select the message again");
}
const page = { body, messages: projectWebuiThreadMessages(projectThreadEvents(body.events)) };
const page = { body, messages: projectWebuiThreadMessages(projectThreadEvents(body.events)), details: new Map<string, UIMessage[]>() };
if (id && !page.messages.some((message) => message.id === id)) {
setRefresh((value) => value + 1);
throw new Error("History changed; select the message again");
@@ -198,11 +210,14 @@ export function usePromptHistory(key: string | null, version: number, messages:
const range = body.page?.record_range;
if (range) {
let joined = joinThreadFragments([], body);
const details = new Map(page.details);
while (pages[index + 1]?.body.page?.record_range?.turn === range.turn) {
joined = joinThreadFragments(joined.pages, pages[++index].body);
const next = pages[++index];
joined = joinThreadFragments(joined.pages, next.body);
for (const [ref, detail] of next.details) details.set(ref, detail);
}
body = joined.body;
rows = projectWebuiThreadMessages(projectThreadEvents(body.events));
rows = expandDetails(projectWebuiThreadMessages(projectThreadEvents(body.events)), details);
}
const eventIndex = body.fork_boundary_event_index;
if (eventIndex !== undefined) boundary = messages.length + projectWebuiThreadMessages(projectThreadEvents(body.events.slice(0, eventIndex))).length;
@@ -228,7 +243,7 @@ export function usePromptHistory(key: string | null, version: number, messages:
return { prompts, window, target: currentScope ? target : null, loading: currentScope && loading,
operation: currentScope && (loading || error) ? operation : null,
error: currentScope ? error ?? indexError : null, cancel, latest, updateMessages,
error: currentScope ? error ?? indexError : null, cancel, latest, expandTraceDetail,
retry: () => error ? retry.current() : setRefresh((value) => value + 1),
jump: (id) => {
const prompt = prompts.find((item) => item.id === id);