import { acceptsCompactionPhase } from "../../../packages/client-events/notifications"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { useStreamBatch } from "@/hooks/useStreamBatch"; import { useClient } from "@/providers/ClientProvider"; import { hasPendingAgentActivity } from "@/lib/activity-timeline"; import { ApiError, fetchWebuiThread, fetchWebuiThreadTraceDetail } from "@/lib/api"; import { fetchLatestWebuiThread } from "@/lib/latest-thread-request"; import { isImageRequestFailure } from "@/lib/model-request-failure"; import { persistedCanonicalRunState } from "@/lib/canonical-run-snapshot"; import { historyContentSnapshot, sameHistoryContent } from "@/lib/thread-display-projection"; import { webuiThreadCache } from "@/lib/webui-thread-cache"; import { ThreadTranscript, type ThreadPaging, type ThreadRead, type ThreadTranscriptSnapshot } from "@/lib/thread-transcript"; import type { ThreadMessageCache } from "@/lib/thread-message-cache"; import type { StreamError } from "@/lib/nanobot-client"; import { clearThreadProjectionActivity, closeThreadProjectionAnswer, finalizeStreamedTurn, resetThreadProjectionCursor, turnFieldsFromEvent, } from "@/lib/thread-event-projection"; import type { ThreadProjectionEvent, UIMessageTurnFields, } from "@/lib/thread-event-projection"; import { formatQuotedUserMessage } from "@/lib/user-message-quote"; import { playTurnCompleteSound } from "@/lib/notification-sound"; import type { InboundEvent, OutboundCliAppMention, OutboundMcpPresetMention, OutboundMedia, SessionMention, SessionModelOptions, GoalStateWsPayload, MessageDeliveryStatus, RecoveryState, RetryStatus, UIMediaAttachment, UIMessage, UIModelRequestSnapshot, WorkspaceScopePayload, } from "@/lib/types"; type PendingStreamEvent = | { kind: "delta"; text: string; turn: UIMessageTurnFields; streamId?: string; source?: UIMessage["source"]; responseSources?: UIMessage["responseSources"]; responseInputIds?: string[] } | { kind: "reasoning"; text: string; turn: UIMessageTurnFields; streamId?: string }; const EMPTY_INITIAL_MESSAGES: UIMessage[] = []; const EMPTY_COMPLETED_TURN_IDS: readonly string[] = []; /** Payload passed to ``send`` when the user attaches one or more files. * * ``media`` is handed to the wire client verbatim; ``preview`` powers the * optimistic user bubble. Keeping the two separate lets the bubble re-use the * local data URL even after the server persists the file under a different * name. */ export interface SendAttachment { media: OutboundMedia; preview: UIMediaAttachment; } export interface SendOptions { modelOptions?: SessionModelOptions; intent?: "create_automation"; cliApps?: OutboundCliAppMention[]; mcpPresets?: OutboundMcpPresetMention[]; sessionMentions?: SessionMention[]; quotedContext?: string; workspaceScope?: WorkspaceScopePayload | null; sideChannel?: boolean; userShell?: boolean; finalizeActiveTurn?: boolean; /** Append guidance to the running turn without detaching its active answer segment. */ continueActiveTurn?: boolean; } export interface SubmittedTurn { delivery?: Promise; turnId: string; userMessageId: string; sideChannel: boolean; } function eventTurnId(ev: InboundEvent): string | undefined { return "turn_id" in ev && typeof ev.turn_id === "string" ? ev.turn_id : undefined; } function transitionTurnDelivery( messages: UIMessage[], turnId: string, status: MessageDeliveryStatus, ): UIMessage[] { let changed = false; const next = messages.map((message) => { if ( message.role !== "user" || message.turnId !== turnId || message.deliveryStatus === status || (status === "accepted" && message.deliveryStatus !== "sending") ) { return message; } changed = true; return { ...message, deliveryStatus: status }; }); return changed ? next : messages; } function createTranscript(messages: UIMessage[], saved: ThreadTranscriptSnapshot | undefined, historyKey: string | null, temporary: boolean) { const owner = new ThreadTranscript(messages, saved, { recordLiveContributions: !temporary }); // The first paint must not enter the welcome layout while history is pending. owner.paging = { ...owner.paging, loading: !!historyKey && owner.paging.version === 0, hasMoreBefore: historyKey ? owner.paging.hasMoreBefore : false }; return owner; } export function useNanobotStream( chatId: string | null, initialMessages: UIMessage[] = EMPTY_INITIAL_MESSAGES, hasPendingToolCalls = false, onTurnEnd?: () => void, onStreamDetach?: (messages: UIMessage[]) => void, completedTurnIds: readonly string[] = EMPTY_COMPLETED_TURN_IDS, history?: { key: string | null; cache?: ThreadMessageCache; temporary?: boolean }, historyActiveTurnId: string | null = null, ): { messages: UIMessage[]; /** Stable across text-only updates; refreshed at exec/history boundaries. */ execSourceMessages: UIMessage[]; modelRequests: ReadonlyMap; /** Conversation owning these messages, including while a selection is handing over. */ messagesKey: string | null; /** Whether ``messages`` belongs to the current ``chatId`` after a session switch. */ messagesReady: boolean; isStreaming: boolean; /** Unix epoch seconds when the current user turn started (WebSocket ``goal_status``). */ runStartedAt: number | null; retryStatus: RetryStatus | null; /** Latest sustained goal for this ``chatId`` (``goal_state`` WS events). */ goalState: GoalStateWsPayload | undefined; recoveryState: RecoveryState | null; continueRecovery: () => Promise; dismissRecovery: () => Promise; send: ( content: string, images?: SendAttachment[], options?: SendOptions, ) => SubmittedTurn | null; transcribeAudio: (dataUrl: string, options?: { durationMs?: number }) => Promise; stop: () => void; history: ThreadPaging; refreshHistory: () => void; loadOlder: () => Promise; loadTraceDetails: (refs: string[]) => Promise>; /** Latest transport-level fault raised since the last ``dismissStreamError``. * ``null`` when there is nothing to show. */ streamError: StreamError | null; dismissStreamError: () => void; } { const { client, getToken } = useClient(); const historyKey = history?.key ?? null; const temporary = history?.temporary === true; const messageCache = history?.cache; const [initialTranscript] = useState(() => createTranscript(initialMessages, chatId ? messageCache?.get(chatId) : undefined, historyKey, temporary)); const transcriptRef = useRef(initialTranscript); const [historyState, setHistoryState] = useState(initialTranscript.paging); const publishHistory = useCallback(() => setHistoryState(transcriptRef.current.paging), []); const [refreshSeq, setRefreshSeq] = useState(0); const refreshHistory = useCallback(() => setRefreshSeq((value) => value + 1), []); const [runState, setRunState] = useState(() => chatId ? client.getRunState(chatId) : null); const isStreaming = runState?.active ?? false; const runStartedAt = runState?.startedAt ?? null; const historyRunGenerationRef = useRef({ chatId, generation: chatId ? client.getRunGeneration(chatId) : 0 }); if (historyRunGenerationRef.current.chatId !== chatId) { historyRunGenerationRef.current = { chatId, generation: chatId ? client.getRunGeneration(chatId) : 0 }; } const [messages, setRenderedMessages] = useState(initialTranscript.messages); const [liveInputs, setLiveInputs] = useState<{ chatId: string; client: typeof client; waiting: ReadonlySet; consumed: ReadonlySet; } | null>(null); const [execSourceMessages, setExecSourceMessages] = useState(initialTranscript.messages); const [modelRequests, setModelRequests] = useState(initialTranscript.modelRequests); const messagesRef = useRef(messages); // Keep received state ahead of React's batched render: navigation can unmount // this hook after a stream flush but before the queued render commits. const setMessages = useCallback>>((update) => { const next = typeof update === "function" ? update(messagesRef.current) : update; messagesRef.current = next; transcriptRef.current.projection = { ...transcriptRef.current.projection, messages: next }; setRenderedMessages(next); setModelRequests(transcriptRef.current.modelRequests); setHistoryState(transcriptRef.current.paging); }, []); const [messageScope, setMessageScope] = useState({ chatId, historyKey, client }); const initialStreaming = useMemo(() => { if (completedTurnIds.length === 0) return hasPendingAgentActivity(initialMessages); const completedInitialTurns = new Set(completedTurnIds); return hasPendingAgentActivity(initialMessages.filter((message) => ( !message.turnId || !completedInitialTurns.has(message.turnId) ))); }, [completedTurnIds, initialMessages]); const [retryStatus, setRetryStatus] = useState(null); const [goalState, setGoalState] = useState(undefined); const [recoveryState, setRecoveryState] = useState(null); const [streamError, setStreamError] = useState(null); const dismissStreamError = useCallback(() => setStreamError(null), []); const isSideChannelEvent = useCallback((ev: InboundEvent) => { const turnId = eventTurnId(ev); return turnId !== undefined && transcriptRef.current.sideChannelTurnIds.has(turnId); }, []); const clearActivitySegment = useCallback(() => { transcriptRef.current.projection = clearThreadProjectionActivity(transcriptRef.current.projection); }, []); const closeActiveAssistantStream = useCallback(() => { const hadActiveAssistant = transcriptRef.current.projection.activeAssistantId !== null; transcriptRef.current.projection = closeThreadProjectionAnswer(transcriptRef.current.projection); return hadActiveAssistant; }, []); const applyPendingStreamEvents = useCallback( (prev: UIMessage[], events: PendingStreamEvent[]): UIMessage[] => { transcriptRef.current.projection = { ...transcriptRef.current.projection, messages: prev }; for (let index = 0; index < events.length; index++) { const event = events[index]; const chunks = [event.text]; const parts = [event]; const members = event.turn.turnSeq === undefined ? [] : [event.turn.turnSeq]; let turn = event.turn; while (index + 1 < events.length) { const nextEvent = events[index + 1]; if (nextEvent.kind !== event.kind || nextEvent.turn.turnId !== event.turn.turnId || nextEvent.turn.turnPhase !== event.turn.turnPhase || nextEvent.streamId !== event.streamId || (nextEvent.kind === "delta" && event.kind === "delta" && (nextEvent.source !== event.source || JSON.stringify(nextEvent.responseSources) !== JSON.stringify(event.responseSources) || JSON.stringify(nextEvent.responseInputIds) !== JSON.stringify(event.responseInputIds)))) break; chunks.push(nextEvent.text); parts.push(nextEvent); if (nextEvent.turn.turnSeq !== undefined) members.push(nextEvent.turn.turnSeq); turn = { ...turn, ...nextEvent.turn }; index++; } const text = chunks.join(""); const projectedEvent: ThreadProjectionEvent = event.kind === "delta" ? { event: "delta", chat_id: chatId ?? "", text, turn_id: turn.turnId, turn_phase: turn.turnPhase, turn_seq: turn.turnSeq, source: event.source, response_sources: event.responseSources, response_input_ids: event.responseInputIds, stream_id: event.streamId, } : { event: "reasoning_delta", chat_id: chatId ?? "", text, turn_id: turn.turnId, turn_phase: turn.turnPhase, turn_seq: turn.turnSeq, }; projectedEvent.stream_id = event.streamId; if (turn.turnId && members.length) { const ranges: Array<[number, number]> = []; for (const seq of members) { const last = ranges[ranges.length - 1]; if (last && last[1] + 1 === seq) last[1] = seq; else ranges.push([seq, seq]); } projectedEvent.projection_receipts = [{ turn_id: turn.turnId, ranges }]; } const contributions = parts.map((part) => ({ ...projectedEvent, text: part.text, turn_id: part.turn.turnId, turn_phase: part.turn.turnPhase, turn_seq: part.turn.turnSeq, stream_id: part.streamId, projection_receipts: undefined, })); transcriptRef.current.project(projectedEvent, { createId: () => crypto.randomUUID(), now: Date.now(), startSeq: event.turn.turnSeq, contributions, }); } return transcriptRef.current.messages; }, [chatId], ); const publishStreamBatch = useCallback((events: PendingStreamEvent[]) => { setMessages((prev) => applyPendingStreamEvents(prev, events)); }, [applyPendingStreamEvents, setMessages]); const { enqueue: enqueueStreamEvent, drain: drainStreamEvents, discard: discardStreamEvents } = useStreamBatch(publishStreamBatch); const applyStreamError = useCallback((err: StreamError) => { // One multiplexed client serves every thread. A correlated send fault // belongs only to its target chat. An uncorrelated transport close can // still be shown in the mounted thread, but cannot roll back any turn. if (!chatId || (err.chatId && err.chatId !== chatId)) return; setStreamError(err); if (err.kind === "model_request_failed") return; if (!err.turnId) return; const rejectedTurnId = err.turnId; discardStreamEvents((event) => event.turn.turnId === rejectedTurnId); transcriptRef.current.sideChannelTurnIds.delete(rejectedTurnId); setMessages((prev) => { const rejectedRows = prev.filter((message) => message.turnId === rejectedTurnId); if (rejectedRows.length === 0) return prev; const rejectedIds = new Set(rejectedRows.map((message) => message.id)); const rejectedSegments = new Set( rejectedRows .map((message) => message.activitySegmentId) .filter((segmentId): segmentId is string => typeof segmentId === "string"), ); const projection = transcriptRef.current.projection; const closedAssistantIds = new Set(projection.closedAssistantIds); for (const id of rejectedIds) closedAssistantIds.delete(id); const nextMessages = prev.flatMap((message) => { if (message.turnId !== rejectedTurnId) return [message]; if (message.role !== "user") return []; return [{ ...message, deliveryStatus: "failed" as const, deliveryErrorKind: err.kind, }]; }); transcriptRef.current.projection = { ...projection, messages: nextMessages, activeAssistantId: projection.activeAssistantId && rejectedIds.has(projection.activeAssistantId) ? null : projection.activeAssistantId, closedAssistantIds, activitySegmentId: projection.activitySegmentId && rejectedSegments.has(projection.activitySegmentId) ? null : projection.activitySegmentId, fileEditSegmentId: projection.fileEditSegmentId && rejectedSegments.has(projection.fileEditSegmentId) ? null : projection.fileEditSegmentId, }; transcriptRef.current.rejectTurn(rejectedTurnId); setExecSourceMessages(nextMessages); return nextMessages; }); if (!client.getRunState(chatId).active) { transcriptRef.current.projection = { ...transcriptRef.current.projection, suppressUntilTurnEnd: false, }; } }, [chatId, client, discardStreamEvents, setMessages]); useEffect(() => client.onError(applyStreamError), [applyStreamError, client]); const flushPendingStreamEvents = useCallback((end?: Extract) => { const events = drainStreamEvents(); if (events.length === 0 && !end) return; setMessages((prev) => { const nextMessages = events.length > 0 ? applyPendingStreamEvents(prev, events) : prev; transcriptRef.current.projection = { ...transcriptRef.current.projection, messages: nextMessages }; if (end) transcriptRef.current.project(end, { createId: () => crypto.randomUUID(), now: Date.now(), }); return transcriptRef.current.messages; }); }, [applyPendingStreamEvents, drainStreamEvents, setMessages]); useEffect(() => { if (!chatId) return; return client.onRunStatus((runChatId, state) => { if (runChatId !== chatId) return; setRunState(state); if (state.active) return; flushPendingStreamEvents(); setMessages((prev) => { const settled = prev.map((message) => ( message.isStreaming ? { ...message, isStreaming: false } : message )); transcriptRef.current.projection = resetThreadProjectionCursor( transcriptRef.current.projection, settled, ); return settled; }); }); }, [chatId, client, clearActivitySegment, flushPendingStreamEvents, setMessages]); // Reset local state when switching chats. Do not reset on every // ``initialMessages`` update: a brand-new chat can receive an empty/404 // history response after the optimistic first message has already rendered. 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); setRetryStatus(null); setGoalState(chatId ? client.getGoalState(chatId) : undefined); setRecoveryState(null); drainStreamEvents(); }, [chatId, client, historyKey, temporary, clearActivitySegment, drainStreamEvents, setMessages]); useEffect(() => { if (!chatId || (!hasPendingToolCalls && !initialStreaming)) return; client.restorePendingRun(chatId, historyRunGenerationRef.current.generation, historyActiveTurnId); }, [chatId, client, hasPendingToolCalls, historyActiveTurnId, initialStreaming]); const applyProjectionEvent = useCallback(( event: ThreadProjectionEvent, options?: { sideChannel?: boolean }, ) => { setMessages((prev) => { transcriptRef.current.projection = { ...transcriptRef.current.projection, messages: prev }; 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]); const olderReadRef = useRef<{ owner: ThreadTranscript; read: ThreadRead; controller: AbortController; } | null>(null); const cancelOlderRead = useCallback(() => { const pending = olderReadRef.current; if (!pending) return; olderReadRef.current = null; pending.controller.abort(); pending.owner.finishRead(pending.read); if (transcriptRef.current === pending.owner) publishHistory(); }, [publishHistory]); const detailRequestsRef = useRef(new WeakMap>>()); const reconcileCanonical = useCallback((generation: number, canonical: ReturnType, accepted: boolean) => { if (!chatId) return; const settle = !canonical.snapshot.hasPendingToolCalls && client.canReconcileCanonicalCompletion(chatId, generation, canonical.completedTurnIds, canonical.snapshot); if (accepted && settle && client.reconcileCanonicalCompletion(chatId, generation, canonical.completedTurnIds, canonical.snapshot)) { setRetryStatus(null); } else client.fenceCanonicalCompletedTurns(chatId, canonical.completedTurnIds); }, [chatId, client]); const settleFragment = useCallback((owner: ThreadTranscript) => { if (!chatId || !owner.fragmentCompletedTurnIds.length) return; client.settleCompletedRunLifecycle(chatId, owner.fragmentCompletedTurnIds); if (!client.getRunState(chatId)?.active) setRetryStatus(null); }, [chatId, client]); useEffect(() => { if (!historyKey) return; const owner = transcriptRef.current; const controller = new AbortController(); const generation = chatId ? client.getRunGeneration(chatId) : 0; let disposed = false; let superseded = false; let publishing = false; const install = (body: Awaited>, ownRead: boolean, seed = false) => { if (disposed || (ownRead && superseded) || transcriptRef.current !== owner) return; flushPendingStreamEvents(); const decision = ownRead ? read : owner.begin(seed ? "initial" : "latest"); const canonical = body ? persistedCanonicalRunState(body) : null; 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; if (body) webuiThreadCache.set(historyKey, body); else webuiThreadCache.delete(historyKey); publishing = false; } if (chatId && canonical && (ownRead || !seed)) { if (body?.page?.record_range) { if (accepted) settleFragment(owner); } else if (!owner.hasPendingFragmentGap) { // A published reconnect page supersedes this owner's shared read. // The owner must reconcile its completion facts after that handoff too. reconcileCanonical(generation, canonical, accepted); } } }; const cached = webuiThreadCache.get(historyKey); if (cached && owner.paging.version === 0) install(cached, false, true); // A restored projection or cached seed already has history. Revalidate it // as latest so a safe disjoint window can replace its durable baseline. const read = owner.begin(owner.paging.version > 0 ? "latest" : "initial"); publishHistory(); const unsubscribe = webuiThreadCache.subscribe(historyKey, (body) => { if (publishing) return; superseded = true; controller.abort(); cancelOlderRead(); install(body, false); }); void fetchLatestWebuiThread(client, getToken(), historyKey, controller.signal) .then((body) => install(body, true)) .catch((error: unknown) => { if (disposed || superseded || controller.signal.aborted || transcriptRef.current !== owner) return; if (error instanceof ApiError && error.status === 404) install(null, true); else { owner.finishRead(read, (error as Error).message); publishHistory(); } }); return () => { disposed = true; controller.abort(); cancelOlderRead(); unsubscribe(); }; }, [cancelOlderRead, chatId, client, flushPendingStreamEvents, getToken, historyKey, messageCache, publishHistory, reconcileCanonical, refreshSeq, setMessages, settleFragment]); const loadOlder = useCallback(async () => { const owner = transcriptRef.current; const before = owner.paging.beforeCursor; if (!historyKey || !before || !owner.paging.hasMoreBefore || owner.paging.loadingOlder) return; const read = owner.begin("older"); const controller = new AbortController(); const operation = { owner, read, controller }; olderReadRef.current = operation; publishHistory(); try { while (true) { const body = await fetchWebuiThread(getToken(), historyKey, { before: operation.read.before!, limit: 120, signal: controller.signal, }); if (olderReadRef.current !== operation || transcriptRef.current !== owner) return; flushPendingStreamEvents(); const previousContent = historyContentSnapshot(owner.messages, owner.paging.forkBoundaryMessageCount); const accepted = owner.accept(body, operation.read); const needsContent = accepted && owner.paging.hasMoreBefore && (owner.hasPendingFragmentGap || sameHistoryContent(previousContent, historyContentSnapshot(owner.messages, owner.paging.forkBoundaryMessageCount))); // Keep one pull pending across bookkeeping pages and staged fragments. // Begin the continuation before publishing accept()'s intermediate state. if (needsContent) operation.read = owner.begin("older"); setMessages(owner.messages); setExecSourceMessages(owner.messages); if (accepted) { if (chatId) messageCache?.set(chatId, owner.save()); settleFragment(owner); } if (!needsContent) break; } } catch (error) { if (olderReadRef.current === operation && transcriptRef.current === owner) { owner.finishRead(operation.read, (error as Error).message); publishHistory(); } } finally { if (olderReadRef.current === operation) olderReadRef.current = null; } }, [chatId, client, flushPendingStreamEvents, getToken, historyKey, messageCache, publishHistory, setMessages, settleFragment]); useEffect(() => { const bridging = transcriptRef.current.hasPendingFragmentGap; if (historyKey && historyState.version > 0 && historyState.hasMoreBefore && !historyState.loading && !historyState.loadingOlder && !historyState.olderError && (bridging || (messages.length === 0 && !historyState.error))) void loadOlder(); }, [historyKey, historyState, loadOlder, messages.length]); 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 = 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(() => ownerRequests.delete(id)); ownerRequests.set(id, request); return request; })); return details.filter((detail) => detail !== null); }, [flushPendingStreamEvents, getToken, historyKey, setMessages]); useEffect(() => { if (!chatId) return; // A request may start before its acceptance frame reaches this tab. const startedInputs = new Set(); const handle = (ev: InboundEvent) => { if (ev.event === "error") { if (ev.detail === "message_too_big") { applyStreamError({ kind: "message_too_big", chatId, turnId: ev.turn_id, }); } else if (ev.detail === "workspace_scope_rejected") { applyStreamError({ kind: "workspace_scope_rejected", reason: ev.reason, chatId, turnId: ev.turn_id, }); } else if (ev.turn_id) { applyStreamError({ kind: "turn_rejected", detail: ev.detail, reason: ev.reason, chatId, turnId: ev.turn_id, }); } return; } const turnId = eventTurnId(ev); 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) => ( message.role === "user" && message.turnId === ev.turn_id ? { ...message, media: ev.media_urls } : message ))); } return; } if (ev.event === "user_message") { flushPendingStreamEvents(); applyProjectionEvent(ev); 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; } const sideChannelEvent = isSideChannelEvent(ev); if (ev.event === "context_compaction") { flushPendingStreamEvents(); closeActiveAssistantStream(); clearActivitySegment(); const previous = transcriptRef.current.messages.find((row) => row.id === `compaction-${ev.compaction_id}`); if (acceptsCompactionPhase(previous?.compaction?.phase, ev.phase)) applyProjectionEvent(ev); return; } if ( ev.event === "delta" || ev.event === "message" || ev.event === "file_edit" || ev.event === "reasoning_delta" || ev.event === "stream_end" ) setRetryStatus(null); if (ev.event === "delta") { if (transcriptRef.current.projection.suppressUntilTurnEnd || transcriptRef.current.represented(ev)) return; const chunk = typeof ev.text === "string" ? ev.text : ""; if (!chunk) return; enqueueStreamEvent({ kind: "delta", text: chunk, turn: turnFieldsFromEvent(ev, "answer"), streamId: ev.stream_id, source: ev.source, responseSources: ev.response_sources, responseInputIds: ev.response_input_ids, }); return; } if (ev.event === "reasoning_delta") { if (transcriptRef.current.projection.suppressUntilTurnEnd || transcriptRef.current.represented(ev)) return; const chunk = ev.text; if (!chunk) return; enqueueStreamEvent({ kind: "reasoning", text: chunk, turn: turnFieldsFromEvent(ev, "reasoning"), streamId: ev.stream_id, }); return; } if (ev.event === "stream_end") { if (transcriptRef.current.represented(ev)) return; flushPendingStreamEvents(ev); if (transcriptRef.current.projection.suppressUntilTurnEnd) return; if (ev.resuming) { return; } // ``stream_end`` closes the current answer segment, not the turn. // Tools and follow-up model segments may still arrive before the // definitive ``turn_end`` event. return; } flushPendingStreamEvents(); if (ev.event === "reasoning_end") { if (transcriptRef.current.projection.suppressUntilTurnEnd) return; applyProjectionEvent(ev); return; } if (ev.event === "goal_state") { setGoalState(ev.goal_state); return; } if (ev.event === "goal_status") { if (ev.status === "running") setStreamError(null); else setRetryStatus(null); return; } if (ev.event === "retry_status") { const activeTurnId = client.getRunTurnId(chatId); if (ev.turn_id && activeTurnId && ev.turn_id !== activeTurnId) return; if (ev.state === "recovered" || ev.state === "cleared") { setRetryStatus(null); } else { const retryAfterSeconds = typeof ev.retry_after_s === "number" && Number.isFinite(ev.retry_after_s) && ev.retry_after_s >= 0 ? ev.retry_after_s : undefined; setRetryStatus({ state: ev.state, attempt: ev.attempt, error_kind: ev.error_kind, ...(typeof ev.max_attempts === "number" ? { max_attempts: ev.max_attempts } : {}), ...(retryAfterSeconds !== undefined ? { next_retry_at: Date.now() / 1000 + retryAfterSeconds } : {}), ...(ev.turn_id ? { turn_id: ev.turn_id } : {}), }); } return; } 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); } setRetryStatus(null); const modelRequestFailed = ev.outcome === "failed" && ev.failure_kind === "model"; const failureAttempts = typeof ev.failure_attempts === "number" && Number.isInteger(ev.failure_attempts) && ev.failure_attempts > 0 ? ev.failure_attempts : undefined; const modelFailure = modelRequestFailed ? { kind: "model_request_failed" as const, chatId, ...(ev.turn_id ? { turnId: ev.turn_id } : {}), ...(typeof ev.failure_error_kind === "string" ? { errorKind: ev.failure_error_kind } : {}), ...(failureAttempts !== undefined ? { attempts: failureAttempts } : {}), } : null; // Recovery decides whether an image failure needs an action card or just a toast. if (modelFailure && !isImageRequestFailure(modelFailure.errorKind)) setStreamError(modelFailure); applyProjectionEvent(ev); playTurnCompleteSound(); onTurnEnd?.(); return; } if (ev.event === "recovery_state") { const next: RecoveryState = { status: ev.status, recovery_id: ev.recovery_id, ...(ev.reason ? { reason: ev.reason } : {}), ...(typeof ev.attempts === "number" ? { attempts: ev.attempts } : {}), ...(typeof ev.can_continue === "boolean" ? { can_continue: ev.can_continue } : {}), ...(typeof ev.failure_error_kind === "string" ? { failure_error_kind: ev.failure_error_kind } : {}), ...(typeof ev.reply_preserved === "boolean" ? { reply_preserved: ev.reply_preserved } : {}), }; const inputRejected = ev.status === "recovered" && ev.reason === "input_rejected"; setRecoveryState(inputRejected ? null : next); if (inputRejected) { setStreamError({ kind: "model_request_failed", chatId, errorKind: next.failure_error_kind }); } else if (ev.reason === "model_request_failed" || ev.status === "resuming") { setStreamError(null); } if ( ev.status === "awaiting_user" || ev.status === "recovered" || ev.status === "failed" ) { setRetryStatus(null); drainStreamEvents(); closeActiveAssistantStream(); clearActivitySegment(); } return; } if (ev.event === "attached") { setRecoveryState(ev.recovery_state?.reason === "input_rejected" ? null : ev.recovery_state ?? null); if ( ev.recovery_state?.reason === "model_request_failed" || ev.recovery_state?.status === "resuming" ) { setStreamError(null); } if ( ev.recovery_state?.status === "awaiting_user" || ev.recovery_state?.status === "failed" ) { drainStreamEvents(); closeActiveAssistantStream(); clearActivitySegment(); } return; } if (ev.event === "message") { applyProjectionEvent(ev, { sideChannel: sideChannelEvent }); if (sideChannelEvent && typeof ev.turn_id === "string") { transcriptRef.current.sideChannelTurnIds.delete(ev.turn_id); } return; } if (ev.event === "file_edit") { applyProjectionEvent(ev); return; } }; 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 = drainStreamEvents(); const snapshot = pending.length > 0 ? applyPendingStreamEvents(messagesRef.current, pending) : messagesRef.current; onStreamDetach?.(snapshot); if (pending.length > 0) setMessages(snapshot); messageCache?.set(chatId, transcriptRef.current.save()); }; }, [ applyProjectionEvent, applyPendingStreamEvents, applyStreamError, chatId, closeActiveAssistantStream, client, clearActivitySegment, drainStreamEvents, flushPendingStreamEvents, isSideChannelEvent, onTurnEnd, onStreamDetach, messageCache, enqueueStreamEvent, setMessages, ]); const send = useCallback( (content: string, images?: SendAttachment[], options?: SendOptions) => { if (!chatId) return null; const hasAttachments = !!images && images.length > 0; // Text is optional when files are attached — the agent will still see // them via ``media`` paths. if (!hasAttachments && !content.trim()) return null; setStreamError(null); const sideChannel = options?.sideChannel === true; const finalizeActiveTurn = options?.finalizeActiveTurn === true; const continueActiveTurn = options?.continueActiveTurn === true; const outboundContent = options?.quotedContext ? formatQuotedUserMessage(content, options.quotedContext) : content; flushPendingStreamEvents(); if (finalizeActiveTurn) { client.finishRunLocally(chatId); setRetryStatus(null); } const turnId = crypto.randomUUID(); const userMessageId = crypto.randomUUID(); if (sideChannel) transcriptRef.current.sideChannelTurnIds.add(turnId); const previews = hasAttachments ? images!.map(({ preview }) => ( preview.kind === "video" && preview.url?.startsWith("data:") ? { kind: preview.kind, name: preview.name } : preview )) : undefined; setMessages((prev) => { let projection = { ...transcriptRef.current.projection, messages: prev }; if ((!sideChannel && !continueActiveTurn) || finalizeActiveTurn) { projection = resetThreadProjectionCursor(projection, prev); } else if (continueActiveTurn) { // Guidance belongs to the active backend turn. Preserve the answer // cursor so its resuming stream_end can finalize the text already // shown before the new user row, while starting fresh activity after it. projection = clearThreadProjectionActivity(projection); } const base = finalizeActiveTurn ? finalizeStreamedTurn(prev) : prev; const next: UIMessage[] = [ ...base, { id: userMessageId, inputId: `user:${turnId}`, role: "user" as const, content: outboundContent, turnId, turnPhase: "user", turnSeq: 0, deliveryStatus: "sending" as const, createdAt: Date.now(), ...(previews ? { media: previews } : {}), ...(options?.cliApps?.length ? { cliApps: options.cliApps } : {}), ...(options?.mcpPresets?.length ? { mcpPresets: options.mcpPresets } : {}), ...(options?.sessionMentions?.length ? { sessionMentions: options.sessionMentions } : {}), }, ]; transcriptRef.current.projection = { ...projection, messages: next }; transcriptRef.current.registerInput(next[next.length - 1]); return next; }); const wireMedia = hasAttachments ? images!.map((i) => i.media) : undefined; const clientOptions = { ...options, turnId, ...((sideChannel || continueActiveTurn) ? { startsNewRun: false } : {}), }; delete clientOptions.quotedContext; delete clientOptions.sideChannel; delete clientOptions.finalizeActiveTurn; delete clientOptions.continueActiveTurn; const delivery = options?.userShell ? client.sendUserShellCommand(chatId, outboundContent, clientOptions) : wireMedia?.length ? client.sendAttachments(chatId, outboundContent, wireMedia, clientOptions) : undefined; if (!delivery) client.sendMessage(chatId, outboundContent, undefined, clientOptions); // Every caller may keep this promise to retain its draft until accepted. // Also observe it here for queued/landing sends without an active composer. void delivery?.catch(() => {}); return { turnId, userMessageId, sideChannel, delivery }; }, [chatId, clearActivitySegment, client, flushPendingStreamEvents, setMessages], ); const stop = useCallback(() => { if (!chatId) return; flushPendingStreamEvents(); setLiveInputs(null); setRetryStatus(null); setMessages((prev) => { const settled = prev.map((m) => ({ ...m, isStreaming: false })); const projection = resetThreadProjectionCursor(transcriptRef.current.projection, settled); const modelRequests = new Map(projection.modelRequests); for (const [turn, snapshot] of modelRequests) modelRequests.set(turn, { ...snapshot, ended: true }); transcriptRef.current.projection = { ...projection, modelRequests }; return settled; }); client.finishRunLocally(chatId); client.sendMessage(chatId, "/stop"); }, [chatId, clearActivitySegment, client, flushPendingStreamEvents, setMessages]); const transcribeAudio = useCallback( (dataUrl: string, options?: { durationMs?: number }) => client.transcribeAudio(dataUrl, options), [client], ); const recoveryAction = useCallback(async (action: "continue" | "dismiss") => { if (!chatId || !recoveryState) return; await client.requestMutation(`recovery.${action}`, { chat_id: chatId, recovery_id: recoveryState.recovery_id, }); }, [chatId, client, recoveryState]); const continueRecovery = useCallback( () => recoveryAction("continue"), [recoveryAction], ); const dismissRecovery = useCallback( () => recoveryAction("dismiss"), [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: visibleMessages, execSourceMessages, modelRequests, messagesKey: messageScope.historyKey ?? messageScope.chatId, messagesReady: messageScope.chatId === chatId && messageScope.historyKey === historyKey && messageScope.client === client, isStreaming, runStartedAt, retryStatus, goalState, recoveryState, continueRecovery, dismissRecovery, send, transcribeAudio, stop, history: historyState, refreshHistory, loadOlder, loadTraceDetails, streamError, dismissStreamError, }; }