feat(webui): show when the agent picks up queued messages (#6719)

This commit is contained in:
chengyongru authored and GitHub committed 2026-11-19 16:03:16 +08:00
1 parent 3025a01cf0
commit b453de9cf6
26 files changed
+642 -29

No files matched your search

+48 -1
View File
@@ -183,6 +183,12 @@ export function useNanobotStream(
historyRunGenerationRef.current = { chatId, generation: chatId ? client.getRunGeneration(chatId) : 0 };
}
const [messages, setRenderedMessages] = useState<UIMessage[]>(initialTranscript.messages);
const [liveInputs, setLiveInputs] = useState<{
chatId: string;
client: typeof client;
waiting: ReadonlySet<string>;
consumed: ReadonlySet<string>;
} | null>(null);
const [execSourceMessages, setExecSourceMessages] = useState<UIMessage[]>(initialTranscript.messages);
const [modelRequests, setModelRequests] = useState(initialTranscript.modelRequests);
const messagesRef = useRef(messages);
@@ -686,6 +692,8 @@ export function useNanobotStream(
useEffect(() => {
if (!chatId) return;
// A request may start before its acceptance frame reaches this tab.
const startedInputs = new Set<string>();
const handle = (ev: InboundEvent) => {
if (ev.event === "error") {
if (ev.detail === "message_too_big") {
@@ -716,6 +724,15 @@ export function useNanobotStream(
if (turnId) {
setMessages((prev) => transitionTurnDelivery(prev, turnId, "accepted"));
}
if ((ev.event === "message_accepted" || ev.event === "user_message") && ev.waiting_for_model === true) {
const id = ev.event === "user_message" ? ev.input_id : `user:${ev.turn_id}`;
if (id && !startedInputs.has(id)) {
setLiveInputs((prev) => {
const current = prev?.chatId === chatId && prev.client === client ? prev : null;
return { chatId, client, waiting: new Set([...(current?.waiting ?? []), id]), consumed: new Set() };
});
}
}
if (ev.event === "message_accepted") {
if (ev.media_urls?.length) {
setMessages((prev) => prev.map((message) => (
@@ -732,6 +749,16 @@ export function useNanobotStream(
return;
}
if (ev.event === "model_request") {
if (ev.phase === "started" && Array.isArray(ev.response_input_ids)) {
const ids = ev.response_input_ids;
ids.forEach((id) => startedInputs.add(id));
setLiveInputs((prev) => {
if (prev?.chatId !== chatId || prev.client !== client) return prev;
const consumed = new Set(ids.filter((id) => prev.waiting.has(id)));
if (!consumed.size) return prev;
return { ...prev, waiting: new Set([...prev.waiting].filter((id) => !consumed.has(id))), consumed };
});
}
flushPendingStreamEvents();
applyProjectionEvent(ev);
return;
@@ -844,6 +871,9 @@ export function useNanobotStream(
}
if (ev.event === "turn_end") {
if (!sideChannelEvent && (ev.outcome === "cancelled" || ev.outcome === "failed" || ev.outcome === "interrupted")) {
setLiveInputs(null);
}
if (typeof ev.turn_id === "string") transcriptRef.current.sideChannelTurnIds.delete(ev.turn_id);
if ("goal_state" in ev && ev.goal_state != null && typeof ev.goal_state === "object") {
setGoalState(ev.goal_state);
@@ -939,9 +969,14 @@ export function useNanobotStream(
}
};
const unsubStatus = client.onStatus((status) => {
if (status !== "open") setLiveInputs(null);
});
const unsub = client.onTranscript(chatId, handle);
return () => {
unsubStatus();
unsub();
setLiveInputs(null);
// Navigation may happen before the throttled paint. Snapshot the pending
// deltas synchronously while they still belong to this subscription.
const pending = pendingStreamEventsRef.current;
@@ -1061,6 +1096,7 @@ export function useNanobotStream(
const stop = useCallback(() => {
if (!chatId) return;
flushPendingStreamEvents();
setLiveInputs(null);
setRetryStatus(null);
setMessages((prev) => {
const settled = prev.map((m) => ({ ...m, isStreaming: false }));
@@ -1097,8 +1133,19 @@ export function useNanobotStream(
[recoveryAction],
);
const visibleMessages = useMemo(() => {
if (!liveInputs || liveInputs.chatId !== messageScope.chatId || liveInputs.client !== messageScope.client
|| (!liveInputs.waiting.size && !liveInputs.consumed.size)) return messages;
return messages.map((row): UIMessage => {
if (row.role !== "user" || !row.inputId) return row;
if (liveInputs.waiting.has(row.inputId)) return { ...row, processingStatus: "pending" };
if (liveInputs.consumed.has(row.inputId)) return { ...row, processingStatus: "consumed" };
return row;
});
}, [liveInputs, messageScope, messages]);
return {
messages,
messages: visibleMessages,
execSourceMessages,
modelRequests,
messagesKey: messageScope.historyKey ?? messageScope.chatId,