Skip to content

feat(agui): split the AG-UI encoder into a per-run type shared by stdout and the WebSocket binding - #1407

Merged
edenreich merged 2 commits into
mainfrom
fix/issue-1397
Sep 29, 2026
Merged

edenreich merged 2 commits into
mainfrom
fix/issue-1397

Conversation

@inference-gateway-maintainer

@inference-gateway-maintainer inference-gateway-maintainer Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #1397

Summary

agui.Render was the one AG-UI encoder, but it only ran as a whole-stream loop with one RUN_STARTED at the start and one terminal event when the channel closed. This splits the encoder into a small per-run RunEncoder type (Start/Handle/Finish) that the stdout transport and the WebSocket binding share. It uses the split to close the stdout contract gaps that docs/ag-ui-output.md already promised but nothing delivered: the resume MESSAGES_SNAPSHOT and the AG-UI cancelled run outcome.

Changes

  • internal/protocols/agui exposes the per-run RunEncoder: Start(threadID, runID), Handle(event), Finish() error. Render stays as the whole-stream composition of start, range and finish. Its Go signature is unchanged, so existing consumers keep working, and the daemon binding and session worker from [FEATURE] EPIC - Consolidate events and external communication behind infer daemon #1396 can now drive one run per turn.
  • RunEncoder.emit writes every event with exactly one Write (one JSON event plus newline), so a transport can map one Write to one frame. The extension bridge chatEventWriter dropped its line splitter and now maps each write to one chat_event frame.
  • headless --format ag-ui --session-id <id> on an existing conversation now emits the MESSAGES_SNAPSHOT that docs/ag-ui-output.md already promised, right after RUN_STARTED. The new snapshotMessages helper builds it from the conversation entries: id, role, text content, assistant toolCalls, tool toolCallId, and error when the tool execution failed. Hidden entries (system reminders, resume and compaction prompts) are skipped. Fresh runs pass no history and stay byte-identical.
  • A ChatCompleteEvent{Cancelled: true} now ends the run with RUN_FINISHED carrying outcome cancelled. The Go SDK has no constant yet, so RunFinishedOutcome{Type: "cancelled"} passes through WithOutcome. Finish derives the outcome from the run error, so a later ComputerUseResumedEvent keeps a computer-use pause and resume one successful run, and a failure after a cancelled turn still ends in RUN_ERROR. TestRender_ComputerUsePauseResume and all existing agui_test.go expectations pass unedited.
  • render.ApprovalAction(ipc.ApprovalResponse) is now the single mapping from a response to a decision (approved with scope: "always" auto-accepts, plain approved approves, anything else rejects), and render.AnswerApproval uses it.
  • The encoder gained EmitApprovalResolved(toolCallID) for the CUSTOM approval_resolved {tool_call_id} event that the per-tool_call_id approval contract needs. Stdout keeps emitting only approval_request, so the documented wire bytes stay limited to the two contract changes.
  • docs/ag-ui-output.md documents the cancelled outcome and the resume snapshot behaviour.

Notes

New behaviour coverage lives in internal/protocols/agui/run_encoder_test.go (terminal event table covering success, cancelled, resume and error after cancel, snapshot ordering and mapping including hidden entries, one Write per event, approval_resolved) plus TestApprovalAction in internal/platform/render. Taskfile.yml needs no new mocks since no domain interface changed.

…nsports

RunEncoder (Start/Handle/Finish) is now the one AG-UI encoder shared by stdout and the WebSocket binding. RunEncoder.emit writes each event with exactly one Write, so a transport can map one Write to one frame without a line splitter, and the extension bridge chat mirror drops its line splitter.

headless --format ag-ui --session-id on an existing conversation emits the MESSAGES_SNAPSHOT the docs already promise right after RUN_STARTED, built by one snapshotMessages helper from the conversation entries (id, role, text content, assistant toolCalls, tool toolCallId, error when the execution failed).

A cancelled turn ends with RUN_FINISHED outcome cancelled instead of RUN_ERROR, and a later computer-use resume keeps it one successful run. render.ApprovalAction is now the single mapping from an IPC approval response to a decision, and the encoder gained EmitApprovalResolved for the CUSTOM approval_resolved event keyed by tool_call_id.
…cancelled outcome from the run error

snapshotMessages mapped every conversation entry, so a resumed run's MESSAGES_SNAPSHOT carried hidden system reminders, resume prompts and compaction prompts as user turns. It now skips hidden entries like every other history reader.

The cancelled flag latched until a computer-use resume, so a failure after a cancelled turn still finished as RUN_FINISHED cancelled. Finish now derives the outcome from the run error itself. The run encoder tests drop the cases already covered by agui_test.go and gain a table for the terminal event.
@edenreich
edenreich merged commit d0ec300 into main Sep 29, 2026
12 checks passed
@edenreich
edenreich deleted the fix/issue-1397 branch September 29, 2026 10:10
@inference-gateway-releaser

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.219.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Labels

released Shipped in a release

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

[TASK] Refactor the AG-UI encoder into a per-run type shared by stdout and the WebSocket binding

1 participant