pub struct Applier { /* private fields */ }Expand description
The materialised view of a run.
See the module docs for the shape of the problem this solves.
Implementations§
Source§impl Applier
impl Applier
Sourcepub fn new() -> Self
pub fn new() -> Self
An empty applier: no messages, and {} for state.
The state starts as an empty object rather than null so that the first
STATE_DELTA of a run has something to patch.
Sourcepub fn with_messages(self, messages: impl Into<Vec<Message>>) -> Self
pub fn with_messages(self, messages: impl Into<Vec<Message>>) -> Self
Seeds the applier with an existing conversation.
Sourcepub fn with_state(self, state: impl Into<Value>) -> Self
pub fn with_state(self, state: impl Into<Value>) -> Self
Seeds the applier with an existing state document.
Sourcepub fn messages(&self) -> &[Message]
pub fn messages(&self) -> &[Message]
The assembled conversation, oldest first: every role, reasoning and activity included, each message where it arrived.
Sourcepub fn message(&self, id: &MessageId) -> Option<&Message>
pub fn message(&self, id: &MessageId) -> Option<&Message>
The message with this id, if the applier has seen one.
Sourcepub fn text_of(&self, id: impl Into<MessageId>) -> Option<&str>
pub fn text_of(&self, id: impl Into<MessageId>) -> Option<&str>
The text of the message with this id, for the roles that carry plain
text. Multimodal user messages return only their text parts’ first
fragment; use Applier::message for the whole payload.
Sourcepub fn state(&self) -> &Value
pub fn state(&self) -> &Value
The application state, as the JSON document the protocol carries.
This is the authority: snapshots replace it and deltas patch it. A typed
view is a projection of this — see Applier::state_as.
Sourcepub fn state_as<T: for<'de> Deserialize<'de>>(&self) -> Result<T>
pub fn state_as<T: for<'de> Deserialize<'de>>(&self) -> Result<T>
Deserializes the application state into a caller-defined type.
#[derive(Deserialize)]
struct Ui {
step: u32,
}
let mut applier = Applier::new();
applier.apply(&Event::state_snapshot(serde_json::json!({ "step": 2 })))?;
assert_eq!(applier.state_as::<Ui>()?.step, 2);Sourcepub fn set_state(&mut self, state: impl Into<Value>)
pub fn set_state(&mut self, state: impl Into<Value>)
Replaces the application state without going through an event.
Sourcepub fn reasoning(&self) -> impl Iterator<Item = &ReasoningMessage>
pub fn reasoning(&self) -> impl Iterator<Item = &ReasoningMessage>
The reasoning messages of the conversation, oldest first.
A view over Applier::messages, not a second list: a reasoning
message is held there, where it arrived, and is sent back to the agent
with the rest. This is for a UI that shows reasoning in a pane of its
own.
Sourcepub fn reasoning_text(&self, id: &MessageId) -> Option<&str>
pub fn reasoning_text(&self, id: &MessageId) -> Option<&str>
The accumulated text of one reasoning message. None for an id that
names no message, or a message of another role.
Sourcepub fn interrupts(&self) -> &[Interrupt]
pub fn interrupts(&self) -> &[Interrupt]
The interrupts the last RUN_FINISHED paused on. Empty unless the run
is waiting for human input; cleared when the next run starts, and by
RUN_ERROR — a failed run waits for nothing.
Sourcepub fn subagents(&self) -> &[Subagent]
pub fn subagents(&self) -> &[Subagent]
The subagent invocations the run has announced, in announcement order.
Kept across runs on purpose: a subagent suspended in one run is announced again by the run that resumes it, and the entry it left is what makes that read as a continuation rather than a duplicate.
Sourcepub fn subagent(&self, run_id: &SubagentRunId) -> Option<&Subagent>
pub fn subagent(&self, run_id: &SubagentRunId) -> Option<&Subagent>
One subagent invocation, by id.
Sourcepub fn push_message(&mut self, message: Message) -> usize
pub fn push_message(&mut self, message: Message) -> usize
Appends a message the client itself produced — typically the user’s turn, before starting a run.
Returns its index in Applier::messages.
Sourcepub fn apply(&mut self, event: &Event) -> Result<Changed>
pub fn apply(&mut self, event: &Event) -> Result<Changed>
Applies one event and reports what it changed.
An event the conversation has no place for changes nothing and is not
an error: Changed::Nothing is returned for text, reasoning or
tool-call arguments whose message or call the conversation does not
hold, or holds as an activity message, which takes no text. That is
what follows a MESSAGES_SNAPSHOT that removed a message or a call
the run still had open: the snapshot is the new baseline, and nothing
is made again from the rest of the item. A
Thread reports each such item once per run,
as a warning.
§Errors
Error::Patch or Error::InvalidPatchDocument for a state or
activity patch that does not apply, Error::Protocol for a
RUN_FINISHED whose interrupt outcome carries no interrupt, and
Error::Unsupported for a
*_CHUNK or a retired THINKING_* event, which this applies only in
their explicit form. Run a raw stream through a
ChunkNormalizer first, and read
one that may carry THINKING_* through the compatibility boundary;
see the module docs.