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>
impl<S: RenderSink> StreamRenderer<S>
Sourcepub fn set_replay(&mut self, replay: bool)
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.
Sourcepub fn set_tool_names(&mut self, names: Vec<String>)
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.
Sourcepub fn set_show_tool_calls(&mut self, show: bool)
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.
Sourcepub fn set_show_thinking(&mut self, show: bool)
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.
Sourcepub fn set_freeze_on_error(&mut self, freeze: bool)
pub fn set_freeze_on_error(&mut self, freeze: bool)
Sets whether a DSML error freezes all further output (default false).
Opt-in, and deliberately so: freezing is only correct for a renderer whose life is one generation pass. plank builds one per pass, so the freeze keeps raw tool-call markup off the user’s screen for the rest of a doomed stanza and ends with the pass. A renderer that outlives a pass – the debug-console mirror keeps one per connection, fed a raw byte tee with no pass boundaries in it – would instead discard every byte of every later pass, and the console window went dead after the first bad stanza while plank itself recovered normally. Defaulting to false means a consumer gets the safe behaviour without knowing this flag exists; the one consumer that wants the freeze asks for it.
Independent of error reporting: Self::finished’s error is set
either way.
Sourcepub fn set_thinking_tool_calls(&mut self, allow: bool)
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.
Sourcepub fn set_preflight(
&mut self,
f: impl FnMut(&ToolCall) -> Result<(), String> + 'static,
)
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.
Sourcepub fn preflight_error(&self) -> Option<&str>
pub fn preflight_error(&self) -> Option<&str>
The first mid-stream preflight failure, if any (model-facing text).
Sourcepub fn begin_in_think(&mut self)
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.
Sourcepub fn finish(&mut self)
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.
Sourcepub fn finished(&self) -> Finished<'_>
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.
Sourcepub fn wants_greedy_sampling(&self) -> bool
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.