Skip to main content

Module stream

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 Chunk and 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::recorded returns 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.
StreamBatch
One read’s answer: the durable chunks from from on, and the end once the reader has every chunk.
StreamEnd
How a stream ended.
StreamReader
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::offset is where a reader that reconnects continues.
StreamWriter
A writer of one of the run’s streams (Ctx::stream). Opened in workflow code, it writes the run’s stream, which close() or the run’s end completes; opened inside a step’s function, the step’s stream, which the step’s final result completes.

Enums§

ChunkValue
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).