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 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) -> &[ReasoningMessage]
pub fn reasoning(&self) -> &[ReasoningMessage]
The reasoning messages, oldest first.
Reasoning is deliberately not in Applier::messages: a UI shows it in
a separate pane, or not at all, and folding it into the transcript would
make “the assistant’s reply” ambiguous.
Sourcepub fn reasoning_text(&self, id: &MessageId) -> Option<&str>
pub fn reasoning_text(&self, id: &MessageId) -> Option<&str>
The accumulated reasoning text for one reasoning message.
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.
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.
Chunk events are accepted here as well as their expanded form, so an
applier driven directly from a raw stream still assembles correctly —
but crate::client::chunks is the place that turns them into the explicit
triples the rest of the protocol is written in.