fix(webui): share sidebar and Composer run state (#6337)
This commit is contained in:
1 parent
3d0f8feb77
commit
5ae49a43e7
13 files changed
+378
-374
No files matched your search
@@ -15,9 +15,9 @@ import type {
|
||||
} from "./types";
|
||||
import { createHostWebSocket } from "./runtime";
|
||||
import { ClientRunLedger } from "./client-run-ledger";
|
||||
import type { CanonicalRunSnapshot, RunEffect, RunObservation } from "./client-run-ledger";
|
||||
import type { CanonicalRunSnapshot, RunEffect, RunObservation, RunState } from "./client-run-ledger";
|
||||
|
||||
export type { CanonicalRunSnapshot } from "./client-run-ledger";
|
||||
export type { CanonicalRunSnapshot, RunState } from "./client-run-ledger";
|
||||
|
||||
/** WebSocket readyState constants, referenced by value to stay portable
|
||||
* across runtimes that don't expose a global ``WebSocket`` (tests, SSR). */
|
||||
@@ -87,7 +87,7 @@ type SessionUpdateHandler = (
|
||||
workspaceScope?: WorkspaceScopePayload,
|
||||
) => void;
|
||||
type SidebarStateUpdateHandler = (state: SidebarStatePayload) => void;
|
||||
type RunStatusHandler = (chatId: string, startedAt: number | null) => void;
|
||||
type RunStatusHandler = (chatId: string, state: RunState) => void;
|
||||
|
||||
/** Structured errors surfaced to the UI.
|
||||
*
|
||||
@@ -181,6 +181,9 @@ function runObservation(event: InboundEvent): RunObservation | null {
|
||||
case "message_accepted":
|
||||
case "user_message": {
|
||||
const activeTurnId = event.active_turn_id ?? (event.starts_turn ? event.turn_id : undefined);
|
||||
if (event.event === "user_message" && event.provenance?.session_message && !activeTurnId) {
|
||||
return { ...base, kind: "activity" };
|
||||
}
|
||||
return {
|
||||
...base,
|
||||
kind: event.event === "message_accepted" ? "admission" : "input",
|
||||
@@ -193,6 +196,22 @@ function runObservation(event: InboundEvent): RunObservation | null {
|
||||
return { ...base, kind: "completed" };
|
||||
case "error":
|
||||
return { ...base, kind: "rejection" };
|
||||
case "delta":
|
||||
case "reasoning_delta":
|
||||
return { ...base, kind: event.text ? "activity" : "progress" };
|
||||
case "stream_end":
|
||||
return { ...base, kind: "activity" };
|
||||
case "retry_status":
|
||||
return { ...base, kind: event.state === "recovered" || event.state === "cleared" ? "progress" : "activity" };
|
||||
case "recovery_state":
|
||||
// Supersession resolves the old recovery, not the new user turn that caused it.
|
||||
return event.status === "recovered" && event.reason === "superseded"
|
||||
? { ...base, kind: "progress" }
|
||||
: { ...base, kind: "recovery", active: event.status === "resuming" };
|
||||
case "attached":
|
||||
return event.recovery_state && event.recovery_state.status !== "recovered"
|
||||
? { ...base, kind: "recovery", active: event.recovery_state.status === "resuming" }
|
||||
: { ...base, kind: "progress" };
|
||||
case "message":
|
||||
return { ...base, kind: "message" };
|
||||
default:
|
||||
@@ -307,8 +326,8 @@ export class NanobotClient {
|
||||
|
||||
onRunStatus(handler: RunStatusHandler): Unsubscribe {
|
||||
this.runStatusHandlers.add(handler);
|
||||
for (const { chatId, startedAt } of this.runs.activeClocks()) {
|
||||
handler(chatId, startedAt);
|
||||
for (const { chatId, state } of this.runs.states()) {
|
||||
handler(chatId, state);
|
||||
}
|
||||
return () => {
|
||||
this.runStatusHandlers.delete(handler);
|
||||
@@ -329,6 +348,15 @@ export class NanobotClient {
|
||||
};
|
||||
}
|
||||
|
||||
getRunState(chatId: string): RunState {
|
||||
return this.runs.view(chatId);
|
||||
}
|
||||
|
||||
/** Restore pending history only within its original lifecycle generation. */
|
||||
restorePendingRun(chatId: string, generation: number, turnId: string | null): void {
|
||||
this.applyRunEffects(this.runs.transition({ kind: "restore-pending", chatId, generation, turnId }));
|
||||
}
|
||||
|
||||
/** Last ``goal_status`` ``started_at`` (unix sec) for *chatId*, if the turn is running. */
|
||||
getRunStartedAt(chatId: string): number | null {
|
||||
return this.runs.view(chatId).startedAt;
|
||||
@@ -428,7 +456,7 @@ export class NanobotClient {
|
||||
for (const effect of effects) {
|
||||
switch (effect.kind) {
|
||||
case "run-status":
|
||||
this.emitRunStatus(effect.chatId, effect.startedAt);
|
||||
this.emitRunStatus(effect.chatId, effect.state);
|
||||
break;
|
||||
case "completion":
|
||||
for (const handler of this.runCompletionHandlers) handler(effect.chatId);
|
||||
@@ -703,6 +731,10 @@ export class NanobotClient {
|
||||
const turnId = options?.turnId ?? crypto.randomUUID();
|
||||
const stillConnected = () => this.socket === socket
|
||||
&& socket?.readyState === WS_OPEN && this.upload === capability;
|
||||
this.applyRunEffects(this.runs.transition({
|
||||
kind: "submit", chatId, turnId,
|
||||
role: options?.startsNewRun === false ? "supplement" : "candidate-owner",
|
||||
}));
|
||||
try {
|
||||
const references = await uploadAttachments(media, capability,
|
||||
typeof window !== "undefined" ? window.location.href : this.currentUrl,
|
||||
@@ -714,6 +746,7 @@ export class NanobotClient {
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
this.applyRunEffects(this.runs.transition({ kind: "reject", chatId, turnId }));
|
||||
this.emitError({ kind: "turn_rejected", chatId, turnId, detail: "attachment_rejected",
|
||||
reason: error instanceof Error ? error.message : "Attachment send failed" });
|
||||
throw error;
|
||||
@@ -1037,9 +1070,9 @@ export class NanobotClient {
|
||||
}
|
||||
}
|
||||
|
||||
private emitRunStatus(chatId: string, startedAt: number | null): void {
|
||||
private emitRunStatus(chatId: string, state: RunState): void {
|
||||
for (const handler of this.runStatusHandlers) {
|
||||
handler(chatId, startedAt);
|
||||
handler(chatId, state);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user