Skip to main content

Module apply

Module apply 

Source
Expand description

Turning a stream of events into materialised state.

Consuming AG-UI is not “read a response”. The agent sends deltas — a message opens, text arrives a fragment at a time, tool arguments accumulate as unparseable JSON fragments, state moves by RFC 6902 patch. Applier is the state machine that folds all of that back into something a UI can draw: a message list, reasoning and activities included, and a JSON state document.

It is a plain synchronous struct. No async, no runtime, no I/O — feed it events from anywhere.

use ag_ui::client::apply::Applier;
use ag_ui::{Event, TextMessageRole};

let mut applier = Applier::new();
for event in [
    Event::run_started("thread-1", "run-1"),
    Event::text_message_start("msg-1", TextMessageRole::Assistant),
    Event::text_message_content("msg-1", "Hello, "),
    Event::text_message_content("msg-1", "world"),
    Event::text_message_end("msg-1"),
    Event::run_finished_success("thread-1", "run-1"),
] {
    applier.apply(&event)?;
}

assert_eq!(applier.messages().len(), 1);
assert_eq!(applier.text_of("msg-1"), Some("Hello, world"));

§What it does not do

The applier is tolerant: an orphan TEXT_MESSAGE_CONTENT opens a message rather than failing, because a half-drawn conversation beats a blank screen. Catching the producer’s mistake is crate::client::verify’s job, and crate::client::Thread runs both. The one thing the applier refuses to do quietly is corrupt state: a patch that does not apply is an error.

It applies the explicit form of the protocol only. A *_CHUNK event stands for a start, content and an end that depend on what came before it, and a retired THINKING_* event for a REASONING_* one; expanding the first is crate::client::chunks’s job and translating the second is the compatibility boundary’s (TransportStream::into_events, enforce_events). Both run before the applier in a Thread. Handed one directly, Applier::apply returns Error::Unsupported and changes nothing.

Structs§

Applier
The materialised view of a run.
MessageChange
Which message changed, and how.
ReasoningChange
Which reasoning message changed, and how.
Subagent
One subagent invocation the run has announced, as the applier tracks it.
SubagentChange
Which subagent changed, and how.

Enums§

Changed
What one event changed.
MessageChangeKind
What happened to a message.
ReasoningChangeKind
What happened to a reasoning message.
SubagentChangeKind
What happened to a subagent invocation.
SubagentStatus
Where a subagent invocation stands.