Skip to content

feat(agui): project the durable Thread log to AG-UI events - #62

Open
systemfsoftware-bot wants to merge 5 commits into
mainfrom
agui-durable-projector
Open

systemfsoftware-bot wants to merge 5 commits into
mainfrom
agui-durable-projector

Conversation

@systemfsoftware-bot

@systemfsoftware-bot systemfsoftware-bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What

Adds projectDurableAgUi and toDurableAgUiEvents to @systemfsoftware/agent/agui: a pure projection of a durable Thread's canonical record log plus in-flight provisional text into cursor-indexed { sequence, event } AG-UI events, using the same event shapes toAgUiEvents emits.

  • Cursor catch-up: pass the full record log with afterSequence; the projector folds every record (rebuilding steering, subagent and committed-turn state) and emits only events whose originating sequence is after the cursor. Provisional-text events are emitted unfiltered, so live drafts keep streaming while the cursor sits at the last committed sequence.
  • Run lifecycle: every run-scoped record marks the Run started first, so a projection that begins mid-Run emits RUN_STARTED before any STEP/TEXT/TOOL event. Settled Runs end in RUN_FINISHED (completed) or RUN_ERROR (failed); aborted and host-halted (stopped) settle as a stopped RUN_FINISHED with the cancelled outcome, since AG-UI has no stopped outcome.
  • Steering: agent.steering custom events — queued when a steering UserInputRecorded commits, applied at the next committed ModelResponseRecorded for the Run (the seam that consumes it), not-joined at settlement for any still-pending steering input.
  • Tool calls and subagents: every tool-call response part maps to TOOL_CALL_START/ARGS/END (provider-executed included) and inline tool results emit TOOL_CALL_RESULT; a failed result is shaped { error, code } like the reference mapper. Subagent outcomes map completed -> SUBAGENT_FINISHED(success), failed -> SUBAGENT_ERROR, aborted/stopped -> suspended.
  • Text identity: committed and provisional text/reasoning share per-Run/Turn message ids, so a consumer that reconciles by id renders one message and a reload matches what a live viewer saw.
  • Structured output: travels in RUN_FINISHED.result (matching toAgUiEvents). The optional typed output extension decodes it through the consumer's own Schema and lets it emit extra events, fail-closed.

Extension-point placement

The typed output extension lives on the pure projectDurableAgUi. toDurableAgUiEvents is the record-only fold with a non-generic options type. Giving the stream a generic options type is rejected by the repository's missing-pipeable-signature lint (a generic that is only inferred from the options must be carried on the returned function, which forces E/R and breaks pipe(stream, toDurableAgUiEvents(options)) for errorful streams), so the typed extension stays on the pure projector, whose returned function has no E/R.

Why in durable/DurableAgUi.ts, not engine/AgUi.ts

pnpm check:exports restricts the engine layer to imports from core and engine, so a module there cannot import durable/Records. The projector lives in durable/DurableAgUi.ts, and ./agui targets a top-level barrel src/AgUi.ts that re-exports both engine/AgUi and the projector — the same top-level cross-layer pattern as src/index.ts. Every Records import in the projector is type-only (erased at runtime), so ./agui pulls no durable record schema/runtime code into a browser bundle. The existing toAgUiEvents, fromAgUiInput and makeAgUiHandler names are unchanged.

Verification (head 8dfdca1)

  • pnpm --filter @systemfsoftware/agent typecheck — exit 0
  • type-aware oxlint on the changed file — exit 0
  • pnpm run check:exports — exit 0 (11 packages, 212 entries)
  • pnpm run check:package-purity — exit 0 (198 entrypoints, 8019 bundled modules)
  • pnpm --filter @systemfsoftware/agent test — exit 0 (161 tests)
  • dprint check on changed files — exit 0

Add `projectDurableAgUi` and `toDurableAgUiEvents` to the `./agui` export: a pure projection of a durable Thread's canonical record log (plus in-flight provisional text) into cursor-indexed AG-UI events. Settled Runs end in RUN_FINISHED or RUN_ERROR, aborted and host-halted Runs end as a stopped RUN_FINISHED (cancelled), steering transitions are emitted as `agent.steering` custom events, and structured run output travels in RUN_FINISHED.result with a typed decode extension.

The projector lives in `durable/` because the `engine` layer may not import `durable`; `./agui` now targets a top-level barrel that re-exports both `engine/AgUi` and the projector, preserving the existing AG-UI helpers.
…neutral

Address review findings on the durable projector: fold every supplied record so a cursor replay rebuilds steering, subagent and committed-turn state and emits only sequences after `afterSequence`; dedupe repeated envelopes in the streaming fold; unify committed and draft message ids per Run/Turn so a live render and a reload agree; emit tool calls for provider-executed tools and `TOOL_CALL_RESULT` for inline tool results; map failed and interrupted subagents to `SUBAGENT_ERROR`/suspended rather than success; open a draft message before emitting content; emit `RUN_STARTED` before a terminal when a projection starts mid-Run; and make the projector's `Records` imports type-only so `./agui` no longer pulls durable record schemas into browser bundles.
Round-2 review fixes: (1) provisional text events are no longer filtered by afterSequence, so a cursor at the last committed sequence still streams live drafts while record-derived events stay cursor-filtered; (2) every run-scoped record marks the Run started before emitting, so a projection that begins mid-Run emits RUN_STARTED ahead of STEP/TEXT/TOOL events; (3) approval-decided, subagent-started/joined, model-aborted and compaction handlers now honor run termination; (4) a draft delta closes the opposite modality so text and reasoning blocks cannot stay interleaved. The streaming form stays the record-only fold with a non-generic options type (the typed output extension remains on the pure projector): a generic stream options type is rejected by the repository's missing-pipeable-signature lint when the generic is not reflected in the returned function, and moving it outward would break pipe inference for errorful streams.
…eering, shape inline failed tool results

The durable AG-UI stream kept every projected event in one projector for the stream's whole life; each record's events now leave the projector as they are emitted. A steering input recorded for a run whose start the slice never saw queued before RUN_STARTED; it now goes through the same live-run path as other run events, and a finished run still answers not-joined. An inline tool-result part that failed was emitted without the { error, code } shape the settled-call path gives it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant