Expand description
A local conversation: independent history, state and human input over a shared transport.
Thread owns its transport handle; dropping the agent that created it is safe.
It does not automatically fetch server history or synchronize other local copies.
Each request borrows the thread until dropped. Requests are dispatched on first poll.
§Several runs on one stream
A request asks for one run, and AG-UI 1.0 lets the stream that answers it
carry several in sequence: the runs the producer replays, then the
requested one, which echoes the request’s run id. A RunStream reads
to the end of the stream. Messages accumulate and state persists across
the runs; each run yields one Update::RunEnded, and is listed in
RunReport::runs; one Update::Done ends the stream.
What a run’s end means for the thread depends on where the run stands:
| Run | Ends | The thread |
|---|---|---|
| replayed, before the requested run | any way | keeps what it delivered and reports it; owes nothing new |
| requested | success, interrupt | confirms a resume submission; pending interrupts cleared, or set |
| requested | cancelled | settles a resume submission; pending interrupts cleared |
| requested | RUN_ERROR, cut short | leaves a submission unconfirmed, and the interrupts as they were |
| after the requested run | success, cancelled | pending interrupts cleared |
| after the requested run | interrupt | pending interrupts set |
| after the requested run | RUN_ERROR, cut short | pending interrupts left |
| after the requested run, a submission unconfirmed | any RUN_FINISHED | pending interrupts left, with an Update::Warning |
A requested run that ends cancelled took the answers of a resume and
reported how it ended, so the submission is settled like a confirmed one.
AG-UI 1.0 (events/lifecycle, “Cancelled runs”): “A cancelled run waits
for nothing. Its outcome carries no interrupts, and the next run on the
thread is an ordinary new run, not a resume.” It did not complete, so a
tool call one of the answers approved is still in
Thread::unanswered_tool_calls when the run left it without a result.
A RUN_ERROR that opens the stream belongs to no run. With nothing after
it, it is the requested run failing before it began, and is read as the
requested run above. With a run after it, it is history, like a replayed
run: at most one run of a stream is the requested one.
The request went the way the last run from the requested one on ended:
that is Update::Done, RunReport::end and Thread::last_run_end.
1.0 makes the requested run the last of its stream, so on a conforming
stream it is the requested run’s end. A producer that keeps the stream
open may send a run after it all the same, and the agent then stands
where that run left it. A stream that ends without the requested run does
not answer the request, and fails it.
Thread::unanswered_tool_calls reads the conversation, so a call any
run left without a result is owed.
§Two runs at once
A request borrows its thread until its stream ends, so one Thread has
one request in flight. A second one does not compile:
use ag_ui::client::{Thread, transport::ReplayTransport};
let mut thread = Thread::new(ReplayTransport::new([]), "thread-1");
let long = thread.send("Write the report").unwrap();
let quick = thread.send("What time is it?").unwrap(); // `thread` is still borrowed
drop((long, quick));That is deliberate. A thread tracks what one request owes: its resume submission, the interrupts it left pending, how it ended. AG-UI 1.0 describes a thread as runs in sequence and does not say how two runs open at once reconcile their messages or the state they share.
For a quick question while a long run works on the same conversation, use two threads with the same thread id, each holding the conversation as it stood, and merge their messages once both streams have ended:
use ag_ui::client::{RunEnd, Thread, merge_messages, transport::ReplayTransport};
use futures_util::StreamExt;
let mut main = Thread::new(transport.clone(), "thread-1");
// A second thread on the same conversation, holding the same messages.
let mut side = Thread::<_>::restore(transport.clone(), main.snapshot())?;
// The long run starts, and borrows `main` until its stream ends ...
let mut long = main.send("Write the report")?;
long.next().await;
// ... so the quick question goes to `side`, and is answered meanwhile.
let quick = side.send("What time is it?")?.collect_report().await;
assert_eq!(quick.end, RunEnd::Success { result: None });
// Both can be drawn as one list at any moment.
let on_screen = merge_messages(long.thread().messages(), side.messages());
assert_eq!(on_screen.len(), 4);
long.collect_report().await;
// Both streams have ended: `main` takes the messages it lacks ...
let merged = main.merge_messages_from(&side)?;
assert_eq!(merged.added.len(), 2);
assert!(merged.differing.is_empty());
// ... and its next request carries both runs.
main.send("Thanks")?.collect_report().await;
assert_eq!(transport.last_request().unwrap().messages.len(), 5);Make the second thread from a snapshot taken before
the long run is polled, as above, so that both start from the same
messages; with HttpAgent that is agent.restore_thread(main.snapshot()).
A restored thread has the defaults for what a snapshot does not hold: set
its tools, context and forwarded properties again. A second thread made
later, from long.thread().snapshot() while the long run is being read,
holds what had arrived by then. A message it holds only in part stays as
the thread that finished it has it, and is named in
MergeReport::differing.
Two things the long run had in flight then are the copy’s to settle, as
on any restored thread. A tool call the run made and has not had the
result of yet is unanswered on the copy, which sends nothing until it has
an answer: give the call up there first, with
abandon_tool_call. When the long run then
receives the result, merging the copy into main leaves the copy’s
stand-in out and names it in MergeReport::superseded. Merging main
into the copy is refused: a merge removes nothing of the thread it adds
to, and the copy gave the call up. And a copy
taken while a resume is in flight holds that
submission unconfirmed, and runs and merges once
the application has reconciled it.
Thread::merge_messages_from says what the merge takes, leaves and
refuses, and merge_messages has the rules for the list. In short:
messages are merged by id, this thread’s copy of a message stays, and a
tool result is put behind the message that holds its call. State is not
merged. Pending interrupts, a resume submission and unanswered tool calls
are not carried over: a thread that has any is not merged until it has
settled them itself.
Structs§
- Abort
Handle - Cloneable cancellation for one local request. Wakes a pending poll and drops its connection/response stream. It does not confirm remote cancellation.
- Merge
Report - What
Thread::merge_messages_fromadded to a thread, and what it left as it was. - Message
Update - A message that changed, and the message as it now stands.
- Reasoning
Update - Reasoning that changed, and the reasoning as it now stands.
- Resume
Submission - Local observation of an interrupt response submission.
- RunDiagnostic
- One retained diagnostic, independent of the terminal outcome.
- RunReport
- Results from the whole stream, including updates consumed before
collect_report. - RunStream
- A lazily dispatched request, read to the end of the stream that answers
it. It borrows the thread until dropped. Every consumed stream yields one
Update::RunEndedper run it carried and then oneUpdate::Done; dropping an unpolled one leaves the thread unchanged. - RunSummary
- One run a stream carried, and how it ended.
- State
View Error - A typed view of current raw state could not be constructed.
- Subagent
Update - A subagent that changed, and the subagent as it now stands.
- Thread
- One local conversation with an agent.
Tonly supplies communication.Sis the current state view, defaulting to JSON. - Thread
Builder - Configures a thread.
buildchecks history IDs and typed initial state. - Thread
Snapshot - A versioned local snapshot. It contains conversation data, never a transport,
observer or running future. Validate it through
restorebefore using it.
Enums§
- RunEnd
- How the remote run ended, or how local consumption stopped.
- Submission
Status - A submission is never retried automatically.
- Update
- Something a view should react to.
Functions§
- merge_
messages - Merges two copies of one conversation’s messages, by message id.