pub struct StreamGrammar { /* private fields */ }Expand description
Incremental validator for exactly one Client event stream.
Implementations§
Source§impl StreamGrammar
impl StreamGrammar
Sourcepub fn accept(&mut self, event: &Event) -> Result<(), StreamError>
pub fn accept(&mut self, event: &Event) -> Result<(), StreamError>
Validates one event without taking ownership from its consumer.
§Errors
A rejected event means the provider’s stream is not a well-formed sequence, which the caller must treat as a failed generation rather than as a recoverable gap: the accumulated state is no longer a faithful picture of what the provider sent.
EventAfterDonefor anything at all after the terminal event. Checked before the event’s own shape, because after a terminal there is no lifecycle left to be consistent with.EmptyItemIdfor an event with no identity, andEmptyToolNamefor a tool event that does not name a tool. Neither can be correlated with anything.DuplicateItemIdif an identity that already completed is opened again. Identities are single-use for the whole stream, so a reused one is a provider defect rather than a second item.InvalidLifecycleif the event does not fit the identity’s active state — content deltas for a tool call, a tool final for an identity opened as content, or a content kind that changed mid-item.FinalPayloadMismatchif a final payload is not exactly what the deltas accumulated. This is the check that makes incremental output trustworthy: a consumer that rendered the deltas has already shown the user something, so a final that disagrees cannot be silently preferred.UnfinishedItemsif the terminal event arrives while any identity is still open.
§Deltas are optional
A final may open its own item. Event::ContentFinal for an identity no
delta ever opened, and Event::ToolCallFinal whose call accumulated no
ToolCallArgumentsDelta — whether or not
a ToolCallStart announced it — are both
well-formed, and the final’s payload is the item. A provider that answers
in one frame is not required to synthesise a delta equal to its own
payload, which is the workaround every adapter for such a provider would
otherwise have to invent.
Nothing about the mismatch rule is relaxed by this. What
FinalPayloadMismatch protects is a consumer that has already rendered
something, so it applies exactly when deltas were sent: a final that
contradicts a non-empty accumulation is still rejected. With no deltas
there is nothing rendered for the final to contradict.
Identities are spent either way. A single-frame final inserts its
identity, so reusing it afterwards is
DuplicateItemId exactly as reusing a
streamed one is.
Sourcepub fn finish(self) -> Result<(), StreamError>
pub fn finish(self) -> Result<(), StreamError>
Verifies that the producer ended after exactly one terminal event.
§Errors
MissingTerminal if the stream ended
without one. This is the one violation accept cannot
report, because it is the absence of an event rather than a bad one: a
stream that simply stops is indistinguishable, event by event, from one
that is still going.
Trait Implementations§
Source§impl Debug for StreamGrammar
impl Debug for StreamGrammar
Source§fn fmt(&self, formatter: &mut Formatter<'_>) -> Result
fn fmt(&self, formatter: &mut Formatter<'_>) -> Result
Reports the validator’s position in the stream, not the stream.
The open map accumulates the exact text and argument bytes each item has produced so far, so it is counted rather than shown; the counts and the terminal flag are what a grammar violation is actually diagnosed from.