Module stream
Expand description
Durable streams: chunks a run appends to a named stream (tokens from a model, progress events) and readers that follow them from an offset.
use fusee::{Ctx, Engine, Error};
engine.register("answer", |ctx: Ctx, prompt: String| async move {
let c = ctx.clone();
ctx.step("generate", move || {
let c = c.clone();
let prompt = prompt.clone();
async move {
let out = c.stream("tokens")?;
// Chunks an earlier attempt recorded: continue after them.
let mut text: String = out.recorded::<String>()?.concat();
for token in [" a", " b"].into_iter().skip(text.len() / 2) {
out.append(token)?;
text.push_str(token);
}
let _ = prompt;
Ok::<_, Error>(text)
}
})
.await
});
let mut tokens = engine.run::<String>("q-1").stream("tokens");
while let Some(chunk) = tokens.next().await? {
print!("{}", chunk.json::<String>()?);
}The rules (docs/features.md#streams):
- Appending records a
Chunkand returns at once; chunks go to the log with whatever else flushes next. A reader sees a chunk only once it is durable (committed, when replicated), so a crash never takes back a chunk a reader has seen. - Inside a step, a chunk records the step’s attempt. A step that runs
again (a retry, or after a crash) keeps the chunks earlier attempts
recorded and continues after them:
StreamWriter::recordedreturns them. The stream a step opened is completed by the step’s final result. - In workflow code, appends are journaled once: a replay of the code
matches its appends to the recorded ones in order and appends nothing.
close()completes the stream, as the run’s end does.
Structs§
- Chunk
- One chunk as a reader receives it.
- Stream
Batch - One read’s answer: the durable chunks from
fromon, and the end once the reader has every chunk. - Stream
End - How a stream ended.
- Stream
Reader - A reader of a run’s stream (
crate::RunHandle::stream): yields each chunk once it is durable, in order, and ends once the stream is complete.Self::offsetis where a reader that reconnects continues. - Stream
Writer - A writer of one of the run’s streams (
Ctx::stream). Opened in workflow code, it writes the run’s stream, whichclose()or the run’s end completes; opened inside a step’s function, the step’s stream, which the step’s final result completes.
Enums§
- Chunk
Value - A chunk’s value: JSON, or bytes.
Constants§
- BATCH_
BYTES - Most payload bytes one read returns (at least one chunk, whatever its size).
- BATCH_
CHUNKS - Most chunks one read returns.
- WAIT_MS
- How long a read waits for a chunk by default before it answers with none (a long poll; the reader then asks again).