Skip to main content

StreamRenderer

Struct StreamRenderer 

Source
pub struct StreamRenderer<S> { /* private fields */ }
Expand description

Streaming display state machine for assistant output.

Feed model text with push and call finish once when the stream ends. Ordinary prose passes through to the sink; raw DSML is hidden and replaced by tool banners. Partial <|DSML| prefixes are held back until disambiguated, then either consumed (real tool call) or flushed verbatim (false alarm).

§Examples

use trace_stream::viz::{RenderSink, StreamRenderer};

struct Stdout;
impl RenderSink for Stdout {
    fn visible_text(&mut self, t: &str) { print!("{t}"); }
    fn think_text(&mut self, t: &str) { eprint!("{t}"); }
}

let mut sr = StreamRenderer::new(Stdout);
sr.push("Hello ");
sr.push("world");
sr.finish();
assert!(sr.finished().calls.is_empty());

Implementations§

Source§

impl<S: RenderSink> StreamRenderer<S>

Source

pub fn new(sink: S) -> Self

Creates a renderer that writes rendered output to sink.

Source

pub fn set_replay(&mut self, replay: bool)

Sets whether this renderer is replaying already-diagnosed text from a stored transcript (default false). Replayed text was already streamed and diagnosed the first time it was produced, so re-reporting it here would both double-log to ~/.plank/tool-call-errors.log and truncate the rest of the replayed message via stream_error. Wire this at the replay construction site only; live generation must leave it false.

Source

pub fn set_tool_names(&mut self, names: Vec<String>)

Sets the tool names the pseudo-tool detector recognizes (default none).

Production wires this from the live registry; with an empty list only the generic wrappers (<tool_call>, <function_call>, <invoke ) are matched, which is what unit tests and echo paths get.

Source

pub fn set_show_tool_calls(&mut self, show: bool)

Sets whether tool-call visualization is shown (default true). Production wires this from ui.showToolCalls; when false the DSML is still parsed and hidden but no banner, param, read, or diff line is emitted.

Source

pub fn set_show_thinking(&mut self, show: bool)

Sets whether thinking text is displayed (default true). Production wires this from ui.showThinking; when false the model still produces its thinking (and <think> tags still drive parser state) but none of it is emitted to the sink.

Source

pub fn set_thinking_tool_calls(&mut self, allow: bool)

Sets whether tool calls emitted inside <think></think> are dispatched (default false = strict C parity). Production wires this from engine.thinkingToolCalls. When false an in-think stanza is discarded with a [tool call ignored: ...] notice, exactly as the C agent does.

Source

pub fn set_preflight( &mut self, f: impl FnMut(&ToolCall) -> Result<(), String> + 'static, )

Installs the mid-stream preflight hook: called with the pending call each time an edit invoke’s old parameter closes. A returned error records preflight_error; the caller is expected to stop generation and feed the error back to the model.

Source

pub fn preflight_error(&self) -> Option<&str>

The first mid-stream preflight failure, if any (model-facing text).

Source

pub fn begin_in_think(&mut self)

Starts the stream already inside a <think> block.

Use when the chat template opened thinking in the prefill prefix, so the model streams thinking content before any </think> and without an opening tag of its own.

Source

pub fn push(&mut self, text: impl AsRef<str>)

Feeds one streamed chunk of model output.

Source

pub fn finish(&mut self)

Signals end of stream, flushing held-back bytes and open banners.

An interrupted tool call is closed with a [tool call interrupted] status line; DSML seen inside thinking is reported as ignored.

Source

pub fn finished(&self) -> Finished<'_>

Results after the stream ends: completed calls and error state.

A stream that ends mid-stanza (parser still structural or inside a parameter value) reports incomplete DSML tool call, like the C’s worker loop; callers must check user interruption first, since an interrupted stanza is not a model error.

Source

pub fn wants_greedy_sampling(&self) -> bool

True while the sampler should run greedy (argmax), mirroring agent_stream_wants_greedy_sampling: inside a tool-call stanza’s structural markup, while a parameter close tag is streaming, or once the start detector holds a DSML-shaped prefix longer than one byte. Derived purely from per-round parser state, so EOS, errors, or the next turn can never leave sampling stuck greedy.

Source

pub fn sink(&self) -> &S

Borrows the underlying sink.

Source

pub fn sink_mut(&mut self) -> &mut S

Mutable access to the sink, for callers that must drain it mid-stream.

Source

pub fn into_sink(self) -> S

Consumes the renderer, returning the sink.

Trait Implementations§

Source§

impl<S: Debug> Debug for StreamRenderer<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<S> !RefUnwindSafe for StreamRenderer<S>

§

impl<S> !Send for StreamRenderer<S>

§

impl<S> !Sync for StreamRenderer<S>

§

impl<S> !UnwindSafe for StreamRenderer<S>

§

impl<S> Freeze for StreamRenderer<S>
where S: Freeze,

§

impl<S> Unpin for StreamRenderer<S>
where S: Unpin,

§

impl<S> UnsafeUnpin for StreamRenderer<S>
where S: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.