pub struct RunProgress { /* private fields */ }Expand description
The events of a run so far, folded into where the run is.
Feed it every event of the run in the order they are sent, with
observe or replay. It keeps no log:
what it holds is what is open, the text each open thing has produced, and
the ids the run has used.
It is also the ordering check. With the verify feature, observe
rejects what EventVerifier rejects and leaves
the fold as it was, so one RunProgress can stand where a server that
builds its own events has its verifier. Without the feature nothing is
checked and the fold takes what it is given.
A stream may carry several runs in sequence. The progress is then that
of the run that is open, or of the last one when none is: a
RUN_STARTED after a terminal event begins the next run with nothing
open. The runs before it are history, and what they produced is in the
conversation.
See the module docs for what a prelude is and how it treats the messages it is given.
Implementations§
Source§impl RunProgress
impl RunProgress
Sourcepub fn seed(&mut self, history: &[Message])
pub fn seed(&mut self, history: &[Message])
Learns the conversation the run continues — the request’s messages
— before its first event.
A result in the run may answer a call the history made, which the ordering check has to know of. The fold has to know which ids were in use before the run: a start for one of them adds to a message that already holds something.
Sourcepub fn observe(&mut self, event: &Event) -> Result<(), VerificationError>
pub fn observe(&mut self, event: &Event) -> Result<(), VerificationError>
Checks one event the run is sending, and folds it in.
§Errors
With the verify feature, the rule the event broke. A rejected event
was not sent, so it changes nothing here either.
Sourcepub fn replay<'a>(
&mut self,
events: impl IntoIterator<Item = &'a Event>,
) -> Result<(), VerificationError>
pub fn replay<'a>( &mut self, events: impl IntoIterator<Item = &'a Event>, ) -> Result<(), VerificationError>
Observes the events a run has already sent, in order.
For a server that reads them from its store: when a client joins, or
when it takes over a run another process was driving. Afterwards the
progress is where the run is, and observe carries
on from there.
§Errors
The first event that breaks a rule, as observe reports it. The
events before it stay folded in.
Sourcepub fn has_started(&self) -> bool
pub fn has_started(&self) -> bool
Whether the run has sent its RUN_STARTED: the run that is open, or
the last one of a stream that carried several.
Sourcepub fn has_ended(&self) -> bool
pub fn has_ended(&self) -> bool
Whether that run has sent a terminal event, so that no run is open.
A client that joins now is sent the prelude alone, until a
RUN_STARTED begins the next run.
Sourcepub fn open(&self) -> impl Iterator<Item = &OpenEntity>
pub fn open(&self) -> impl Iterator<Item = &OpenEntity>
What the run has open, in the order it was opened.
After RUN_ERROR this is what the run abandoned.
let mut progress = RunProgress::new();
progress.replay(&[
Event::run_started("thread-1", "run-1"),
Event::step_started("draft"),
Event::text_message_start("msg-1", TextMessageRole::Assistant),
Event::text_message_content("msg-1", "Dear "),
Event::text_message_content("msg-1", "Ada,"),
])?;
let open: Vec<&OpenEntity> = progress.open().collect();
assert!(matches!(open[0], OpenEntity::Step { start, .. } if start.step_name.as_str() == "draft"));
assert!(matches!(open[1], OpenEntity::Message { content, .. } if content == "Dear Ada,"));Sourcepub fn prelude(&self) -> Prelude<'_>
pub fn prelude(&self) -> Prelude<'_>
Starts the prelude for a client that joins the run where it now is.
See the module docs for what it holds. It is empty for a run that has not started: there is nothing to catch up on, and the client reads the run from its first event.