ExAgent.Chat.AGUI (ExAgent v3.0.0)

View Source

Pure encoder for one Server attempt in AG-UI 1.0.

Stability — Candidate.

Initialize with a chat view from an authorized history page requested with include_draft: true. Feed ordered Event v2 envelopes for one root attempt to push/2. The returned maps are JSON events. The host supplies the transport, admits turns and authorizes decisions. Start reading before the attempt and stop at finished?/1. Replays need the complete attempt from the same emitter and a fresh consumer of its text deltas.

The encoder owns the AG-UI state document. Its snapshot contains "step" (initially null), with a JSON Patch update when a run step starts. Visible answer deltas form an assistant message per step. The terminal result carries the confirmed output, which can differ from streamed text. Buffered output and structured output without visible deltas also get an assistant message. Arguments and results come from admitted tool calls. A Server supply receipt also identifies a deferred call whose result can arrive in the resumed attempt. Withheld tool results carry empty content and withheld metadata.

The encoder emits status-only activity for observed child runs, acknowledged background submissions and host job notices. It tracks up to 256 activities per attempt. Each job and each child run keeps its own activity message ID across attempts. A resumed child starts with a replacing snapshot for its activity. Open children without terminal evidence become "unknown" when the root attempt ends.

A confirmed C7 pause ends the stream with a native interrupt outcome. The exagent.approval custom event has two forms. A "requested" status carries "items", each with an "id" and any available "callRef" and "tool". A "decided" status carries the "action" and any available "approvalId", "callRef" and "tool". Input deferrals use the "input_required" interrupt reason and the exagent.input event with item identifiers. Read forms from the authorized Server. Timer deferrals use "timer_wait" and the exagent.timer event, whose items also carry the UTC millisecond "wakeAt". Display these waits as status. Recovery or the host settles them independently of client answers accepted by ExAgent.Chat.AGUI.Input.decode/2. A delegated interrupt carries its path and the child's activity "messageId" in metadata.exagent. Requested and decided review events carry the same fields for delegated items.

On resume, initialize a new encoder with the same Server run ID. Each encoder generates a fresh AG-UI run ID by default. Read after the previous attempt's terminal and include decisions published by the Server before resuming.

Summary

Functions

Whether this attempt has emitted its terminal AG-UI event.

Response headers for AG-UI over server-sent events.

Initialize an encoder for one attempt of a root Server run.

Consume one Event v2 envelope and return the encoder and its ordered events.

Encode the events returned by push/2 as complete SSE frames.

Types

t()

@opaque t()

Functions

finished?(agui)

@spec finished?(t()) :: boolean()

Whether this attempt has emitted its terminal AG-UI event.

headers()

@spec headers() :: [{String.t(), String.t()}]

Response headers for AG-UI over server-sent events.

new(chat, server_run_id, opts \\ [])

@spec new(ExAgent.Chat.t(), String.t(), keyword()) :: t()

Initialize an encoder for one attempt of a root Server run.

:thread_id defaults to the chat's agent ID. With no explicit :run_id, the encoder generates a new opaque wire ID for this attempt. Pass both IDs when they come from an authorized AG-UI request. Each attempt on a thread requires a distinct wire run ID.

Candidate :reasoning defaults to false. Pass a two-argument redactor fn :reasoning_summary, text -> {:ok, reviewed_text} | :drop end for provider-supplied summaries on unguarded turns. A stream or final-output guardrail suppresses these summaries. The callback sees only bounded input. Its UTF-8 result must fit 4096 bytes as JSON. A dropped, invalid or oversized value emits no reasoning events.

push(state, event)

@spec push(t(), ExAgent.Event.t()) :: {t(), [map()]}

Consume one Event v2 envelope and return the encoder and its ordered events.

Ignores events outside the observed run tree, duplicates and older events. A replay gap must be handled by the host before feeding the encoder.

sse(events)

@spec sse([map()]) :: iodata()

Encode the events returned by push/2 as complete SSE frames.