Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
refactor(chat): the client reads only the run events the server sends
trohitg43f56f0d575c
diff --git a/client/src/features/chat/data/runs.ts b/client/src/features/chat/data/runs.tsindex 29c31122..0a1e7b36 100644--- a/client/src/features/chat/data/runs.ts+++ b/client/src/features/chat/data/runs.ts@@ -98,11 +98,11 @@ export function useSessionRuns(sessionId: string | null): Record<string, RunSnap}/** The run holding the session's lane: the live run the owner's last- * message started (approved sends running as `resume` runs never hold it). */+ * message started. */export function laneRun(runs: Record<string, RunSnapshot>): RunSnapshot | null {let lane: RunSnapshot | null = null;for (const run of Object.values(runs)) {- if (!isLiveRun(run) || run.kind === 'resume') continue;+ if (!isLiveRun(run)) continue;if (!lane || (run.createdAt ?? run.startedAt ?? '') > (lane.createdAt ?? lane.startedAt ?? '')) lane = run;}return lane;diff --git a/client/src/features/chat/data/stop.ts b/client/src/features/chat/data/stop.tsindex f60e39d3..b489ed63 100644--- a/client/src/features/chat/data/stop.ts+++ b/client/src/features/chat/data/stop.ts@@ -31,7 +31,7 @@ export function useStopChatRun(sessionId: string, onFailed?: () => void) {const current = store.sessions[sessionId]?.runs[run.runId] ?? run;if (!isLiveRun(current)) return;if (state === 'stopped') {- store.upsertRun({ ...current, state: 'stopped', outcome: { type: 'stopped', interrupts: [] }, result: { noReply: true } });+ store.upsertRun({ ...current, state: 'stopped', outcome: { type: 'stopped' }, result: { noReply: true } });} else if (state === 'stopping' && current.state !== 'stopping') {store.upsertRun({ ...current, state: 'stopping' });}diff --git a/client/src/features/chat/thread/model.ts b/client/src/features/chat/thread/model.tsindex 1b0723cb..113de9ae 100644--- a/client/src/features/chat/thread/model.ts+++ b/client/src/features/chat/thread/model.ts@@ -70,9 +70,7 @@ function fromMessage(message: ChatMessage, run: MessageRun, sessionId: string):...emptyRun(run.runId, sessionId),state: run.state,userMessageId: message.role === 'user' ? message.id : null,- outcome: run.outcome === 'success' || run.outcome === 'interrupt' || run.outcome === 'stopped'- ? { type: run.outcome, interrupts: [] }- : null,+ outcome: run.outcome === 'success' || run.outcome === 'stopped' ? { type: run.outcome } : null,error: run.error,steps: run.steps,durationMs: run.durationMs,diff --git a/client/src/lib/agui/__tests__/reduceRun.test.ts b/client/src/lib/agui/__tests__/reduceRun.test.tsindex ab4603c8..5858cab5 100644--- a/client/src/lib/agui/__tests__/reduceRun.test.ts+++ b/client/src/lib/agui/__tests__/reduceRun.test.ts@@ -42,6 +42,8 @@ describe('parseRunFrame', () => {expect(parseRunFrame(envelope('started', { hub_epoch: undefined }))).toBeNull();expect(parseRunFrame(envelope('started', { seq: 0 }, 0))).toBeNull();expect(parseRunFrame(envelope('nonsense', {}))).toBeNull();+ // AG-UI events the server does not send.+ expect(parseRunFrame(envelope('tool_call.started', { tool_call_id: 'c1', tool_call_name: 'whatsapp_send' }))).toBeNull();});});diff --git a/client/src/lib/agui/events.ts b/client/src/lib/agui/events.tsindex 63e9dc4e..28baf302 100644--- a/client/src/lib/agui/events.ts+++ b/client/src/lib/agui/events.ts@@ -9,8 +9,8 @@* a typed event with camelCase fields, a session's resync request, or null.** Fields beyond the scope are read one at a time: a field that cannot be read- * is left out, never the whole event. Suffixes later phases render- * (tool calls, reasoning) come back as `unhandled`.+ * is left out, never the whole event. A suffix the server does not send is+ * null.*/import type { PatchOp } from './patch';@@ -18,21 +18,12 @@ import type { PatchOp } from './patch';export const RUN_SOURCE = 'opencompany://services/chat';export const RUN_TYPE_PREFIX = 'com.opencompany.chat.run.';-export type RunKind = 'message' | 'edit' | 'regenerate' | 'action' | 'resume';+export type RunKind = 'message' | 'edit' | 'regenerate' | 'action';export type RunState = 'queued' | 'pending' | 'running' | 'stopping' | 'finished' | 'error' | 'stopped';export type StepState = 'running' | 'done' | 'failed' | 'skipped';-export interface RunInterrupt {- id: string;- reason: string;- message: string;- toolCallId?: string;- expiresAt?: string;-}-export interface RunOutcome {- type: 'success' | 'interrupt' | 'stopped';- interrupts: RunInterrupt[];+ type: 'success' | 'stopped';}export interface RunResult {@@ -84,8 +75,7 @@ export type RunEventBody =| { type: 'text.ended'; messageId: string; final: boolean; replyMessageId?: string }| { type: 'activity.snapshot'; messageId: string; activityType: string; content: unknown; replace: boolean }| { type: 'activity.delta'; messageId: string; activityType: string; patch: PatchOp[] }- | { type: 'custom'; name: string; value: Record<string, unknown> }- | { type: 'unhandled'; suffix: string };+ | { type: 'custom'; name: string; value: Record<string, unknown> };export type RunEvent = RunEventScope & RunEventBody;@@ -99,17 +89,9 @@ export interface ResyncRequest {export type RunFrame = RunEvent | ResyncRequest;-const RUN_KINDS: readonly RunKind[] = ['message', 'edit', 'regenerate', 'action', 'resume'];+export const RUN_KINDS: readonly RunKind[] = ['message', 'edit', 'regenerate', 'action'];const FINISHED_STEP_STATES = ['done', 'failed', 'skipped'] as const;-const OUTCOME_TYPES = ['success', 'interrupt', 'stopped'] as const;-const UNHANDLED_SUFFIXES = new Set([- 'tool_call.started',- 'tool_call.args',- 'tool_call.ended',- 'tool_call.result',- 'reasoning.started',- 'reasoning.ended',-]);+const OUTCOME_TYPES = ['success', 'stopped'] as const;/** Patch operations kept per `activity.delta`. */const MAX_PATCH_OPS = 64;@@ -136,28 +118,10 @@ function defined<T extends object>(fields: T): T {return Object.fromEntries(Object.entries(fields).filter(([, value]) => value !== undefined)) as T;}-function interrupts(raw: unknown): RunInterrupt[] {- if (!Array.isArray(raw)) return [];- return raw.flatMap((item) => {- if (!isRecord(item)) return [];- const id = text(item.id);- if (!id) return [];- return [- defined({- id,- reason: text(item.reason) ?? 'tool_call',- message: text(item.message) ?? '',- toolCallId: text(item.tool_call_id),- expiresAt: text(item.expires_at),- }),- ];- });-}-export function parseOutcome(raw: unknown): RunOutcome | null {if (!isRecord(raw)) return null;const type = oneOf(raw.type, OUTCOME_TYPES);- return type ? { type, interrupts: interrupts(raw.interrupts) } : null;+ return type ? { type } : null;}export function parseResult(raw: unknown): RunResult {@@ -191,7 +155,7 @@ function eventFields(suffix: string, data: Data): RunEventBody | null {case 'finished':return {type: 'finished',- outcome: parseOutcome(data.outcome) ?? { type: 'success', interrupts: [] },+ outcome: parseOutcome(data.outcome) ?? { type: 'success' },result: parseResult(data.result),durationMs: count(data.duration_ms) ?? 0,stepCount: count(data.step_count) ?? 0,@@ -251,7 +215,7 @@ function eventFields(suffix: string, data: Data): RunEventBody | null {return name ? { type: 'custom', name, value: isRecord(data.value) ? data.value : {} } : null;}default:- return UNHANDLED_SUFFIXES.has(suffix) ? { type: 'unhandled', suffix } : null;+ return null;}}diff --git a/client/src/lib/agui/reduceRun.ts b/client/src/lib/agui/reduceRun.tsindex ed173feb..cc97b89d 100644--- a/client/src/lib/agui/reduceRun.ts+++ b/client/src/lib/agui/reduceRun.ts@@ -9,6 +9,7 @@*/import {+ RUN_KINDS,parseError,parseOutcome,parseResult,@@ -46,7 +47,7 @@ export interface RunActivity {messageId: string;activityType: string;content: unknown;- /** Patches applied since the snapshot ("Rendering UI · patch k"). */+ /** Patches applied since the snapshot (development builds show the count). */patches: number;}@@ -115,7 +116,6 @@ export function workedMs(run: Pick<RunSnapshot, 'durationMs' | 'startedAt' | 'fireturn Number.isFinite(span) && span >= 0 ? span : null;}-const RUN_KINDS: readonly RunKind[] = ['message', 'edit', 'regenerate', 'action', 'resume'];const RUN_STATES: readonly RunState[] = ['queued', 'pending', 'running', 'stopping', 'finished', 'error', 'stopped'];const STEP_STATES: readonly StepState[] = ['running', 'done', 'failed', 'skipped'];@@ -327,8 +327,6 @@ export function applyRunEvent(run: RunSnapshot, event: RunEvent): RunSnapshot {}if (event.name === 'opencompany.stopping') return { ...next, state: 'stopping' };return next;- case 'unhandled':- return next;}}