Expand description
Where a run is: what it has open, and what a client that joins it is sent first.
A run that outlives the response that started it is read by clients that
were not there when it began: a second window on the same thread, a client
that lost its connection and came back. Such a client cannot be handed the
run’s stream from where it now stands. A stream begins with RUN_STARTED,
or with RUN_ERROR for a run that failed before it began, and a client
rejects any other first event. It rejects what continues or closes
something it never saw open in the same way: the arguments or the end of
a tool call, the end of a step, reasoning content.
RunProgress folds the events of a run, as they are sent, into what the
run has open and what each open thing holds so far. From that it builds
the Prelude: the events that take a client to where the run is. The
run’s later events then follow as they are.
It needs no Runner. A server that builds its
own events observes each one here as it sends it, or replays the ones it
stored.
// The run so far, as stored. The answer is half written.
let stored = [
Event::run_started("thread-1", "run-1"),
Event::step_started("answer"),
Event::text_message_start("msg-2", TextMessageRole::Assistant),
Event::text_message_content("msg-2", "The capital "),
Event::text_message_content("msg-2", "of France"),
];
let mut progress = RunProgress::new();
progress.replay(&stored)?;
// The messages that are complete: here, the question the run answers.
let question = Message::user("msg-1", "What is the capital of France?");
let prelude = progress.prelude().messages(vec![question.clone()]).into_events();
assert_eq!(
prelude,
[
Event::run_started("thread-1", "run-1"),
Event::messages_snapshot(vec![question]),
Event::step_started("answer"),
Event::text_message_start("msg-2", TextMessageRole::Assistant),
Event::text_message_content("msg-2", "The capital of France"),
]
);
// Then every event the run sends from here on: " is Paris.", the end of
// the message, the end of the step, `RUN_FINISHED`.§What a prelude holds
In this order, and nothing else:
RUN_STARTEDas the run sent it, without the input echo. A prelude always begins with it.MESSAGES_SNAPSHOT, whenPrelude::messageswas given the conversation, andSTATE_SNAPSHOT, whenPrelude::statewas given the state. Neither is sent unless asked for.SUBAGENT_STARTEDfor each subagent that is active. A finished one is restated, with the event that closed it, only when an active one names it as its parent: a client rejects an announcement whose parent it never saw.- Each thing that is open, in the order they were opened. A step is its
STEP_STARTEDand a reasoning block itsREASONING_START. A text message, a tool call and a reasoning message are their start and one content event carrying everything produced since. - The terminal event, once the run has ended.
A tool call that has ended and waits for its result adds nothing. It is
in the conversation, and a client accepts a TOOL_CALL_RESULT for a call
it did not see open.
One thing comes before the snapshot. A MESSAGES_SNAPSHOT the run sent
while a message, a reasoning message or a tool call was open, and that
left it out, dropped it on every client, and a snapshot closes nothing:
what the entity sends next has nowhere to go and is dropped too. So with
Prelude::messages such an entity is opened right after RUN_STARTED
(and the subagents, which then come before it), by its start alone, the
snapshot that follows drops it again, and it is taken out of the
messages given. A client ends holding what one that read the run holds.
An entity that a *_CHUNK event opened is restated as one chunk, not as
a start and a content event. A chunk stream ends when something else
arrives in its lane, and an entity opened explicitly would not: the
client would still hold it open at RUN_FINISHED.
Every restated event carries what the event it stands for carried: the
subagentRunId, the timestamp, and the metadata of the start merged with
that of the content events since, as a client merges them. rawEvent is
left out.
§The prelude or the stored events, never both
Replaying is not idempotent. A client appends: a *_START for an id it
already holds creates nothing, and the content event after it is added to
what is there. So a client is sent the prelude and then the events that
follow it, or the stored events from the first, and never the stored
events after a prelude that already accounts for them.
§The conversation is yours to supply
A prelude restates what is open. What is closed — the earlier turns, the
messages and reasoning messages this run has finished, the tool calls
that ended and their results — is the conversation, and the server holds
it: this module does not rebuild a message list from events. Give it to
Prelude::messages.
A server that stores events and not messages folds them with the
client’s Applier (ag_ui::client::apply::Applier, behind the client
feature), the one implementation of that fold:
use ag_ui::client::apply::Applier;
use ag_ui::server::RunProgress;
use ag_ui::{Event, Message, TextMessageRole, ToolCallStartEvent};
// The request's history, and the run so far: the assistant said what it
// would do, and is half way through the arguments of a search.
let history = vec![Message::user("msg-1", "Find me a flight to Lisbon.")];
let call = ToolCallStartEvent::new("call-1", "search").with_parent_message_id("msg-2");
let stored = [
Event::run_started("thread-1", "run-1"),
Event::text_message_start("msg-2", TextMessageRole::Assistant),
Event::text_message_content("msg-2", "Searching."),
Event::text_message_end("msg-2"),
Event::ToolCallStart(call.clone()),
Event::tool_call_args("call-1", r#"{"to":"#),
];
// The messages, as a client that read the run from the start holds them.
let mut applier = Applier::new().with_messages(history.clone());
let mut progress = RunProgress::new();
progress.seed(&history);
for event in &stored {
applier.apply(event)?;
progress.observe(event)?;
}
let prelude = progress.prelude().messages(applier.messages()).into_events();
// The snapshot holds the call without its arguments, and the stream
// restates them: nothing reaches the client twice.
let Event::MessagesSnapshot(snapshot) = &prelude[1] else { unreachable!() };
let Message::Assistant(assistant) = &snapshot.messages[1] else { unreachable!() };
assert_eq!(assistant.content.as_deref(), Some("Searching."));
assert_eq!(assistant.tool_calls.as_ref().unwrap()[0].function.arguments, "");
assert_eq!(
prelude[2..],
[Event::ToolCallStart(call), Event::tool_call_args("call-1", r#"{"to":"#)]
);An Applier applies the explicit form. A stream that carries *_CHUNK
events goes through the client’s ChunkNormalizer first.
§What is open is taken out of the messages
A message list folded from the events holds what is open as well: the message being written, the reasoning message being thought, the tool call with part of its arguments. Sent as it is and followed by each start and everything so far, it would give the client that text twice.
So a prelude does one thing with the messages it is given, whatever they hold: it takes out what it restates. An open text message or reasoning message keeps its place in the list and whatever else it carries — the tool calls it made that have ended — and loses its text. An open tool call stays in its assistant message, without its arguments. The start and the content event that follow then fill them in, on every client and from any list: one that holds the open entities as they stand, one that holds an earlier copy, one that leaves them out. A message the list leaves out is created by its start, after the messages of the snapshot.
The other way round was possible: keep the open entities in the snapshot, set to what the run has produced, and send each start alone. That works only for an entity the list already holds. One it leaves out would have to be put in, which needs a place in the list, for a tool call with no parent a message id that clients derive each in their own way, and for reasoning would turn a snapshot that says nothing about reasoning into one that declares all of it. Restating needs none of that, so it is the only behaviour.
A client that already holds a different copy of an open entity ends in the same place. The snapshot replaces its copy of a message the snapshot carries and drops one it leaves out, and the restated content then lands on an empty entity. Three cases are beyond what a prelude can do:
- An entity that held something before. A start for an id that was
in use before, in the history or earlier in the run, adds to what the
entity held. So does content that follows a
MESSAGES_SNAPSHOTwhich carried the entity while it was open. The fold knows what was produced since and not what was there before, so it takes that ending out of the supplied text, where the text ends with it. A list folded from the same events does; an earlier copy keeps what it has. - Reasoning the snapshot is silent about. A client keeps its
reasoning messages through a snapshot that carries none, so a client
that holds part of an open reasoning message keeps that part, and the
restated content is added to it. Supply the reasoning messages in the
list when clients come back holding some. A list folded with the
Applierhas them. - An entity a snapshot dropped, with no snapshot to drop it again.
Without
Prelude::messages, and withPrelude::reopen_only, it is restated by its start alone, and a client holds it again from there, with what the run sends next.
§A client that holds the run up to a point
Prelude::reopen_only is for a client whose position in the stream is
known, such as one that reconnects naming the last event it received.
Replay the stored events up to that point into a RunProgress and it
gives each start alone, with no snapshot: the client holds what is open
exactly as it stood there, and is sent the events it missed next.
§What is not restated
State, unless Prelude::state is given it: a STATE_DELTA that
follows needs the state it patches. Activities, which are messages and
belong in the list. A reasoning message that has ended inside a block
that has not: it is closed, and so part of the conversation. A subagent
that has finished, unless an active one descends from it: what it said
is in the conversation under its id, and its announcement and outcome
are not sent again.
Structs§
- Prelude
- The events that take a joining client to where a run is.
- RunProgress
- The events of a run so far, folded into where the run is.
Enums§
- Open
Entity - Something a run has opened and not closed.