Expand description
wasi:tensor/host streaming surface (roadmap feature #2).
Lets guests emit chunks of output that the host gateway flushes to the HTTP client as SSE or chunked-transfer bytes. v0.3.7 lands the WIT contract + a buffered in-memory channel; v0.4 lands actual streaming through the axum response path.
§Surface
StreamingContext owns an optional Tokio mpsc::Sender<Vec<u8>>
channel. The host functions wrapped by add_streaming_to_linker
call into StreamingContext::emit_chunk and
StreamingContext::flush; the API gateway holds the matching
mpsc::Receiver and forwards the chunks into the axum SSE / chunked
response body.
§Caps
Two hard caps are enforced before any chunk is forwarded:
MAX_CHUNK_BYTES(64 KiB) — single-chunk size cap. Currently the parser-side check is left for the v0.4 host-fn implementation (the wasi-tensor WIT signature does not yet carry a per-call cap distinct from the total cap); the constant is exported so the host-fn wrapper and tests share a single source of truth.MAX_TOTAL_STREAM_BYTES(64 MiB) — total bytes a single invocation may emit before the host returns the documented-2 = cap exceededcode fromemit-chunk.
Caps are intentionally conservative for the scaffold so a runaway guest cannot exhaust gateway memory while v0.4 is in flight.
§Error codes
Mirrors the WIT contract in wit/wasi-tensor.wit. Acceptance is
all-or-nothing — the host forwards the whole chunk or none of it, so
success is a flat 0 rather than a byte count:
0— chunk fully accepted (the entire payload was forwarded).-1— streaming not enabled for this invocation.-2— guest tried to emit past the documented size cap.-3— downstream client disconnected (receiver dropped).
Tests in tests/streaming_scaffold.rs exercise every branch.
Structs§
- Input
Context - Per-invocation input context. Owns the bytes the host staged for the
guest to pull via the
wasi:tensor/hostinput-len/read-inputhost functions. - Streaming
Context - Per-invocation streaming context. Owns the producer end of the
mpsc::Sender<Vec<u8>>channel into which guest-emitted chunks are pushed; the API gateway holds the matching receiver and forwards the bytes onto the wire.
Constants§
- FN_
EMIT_ CHUNK - Host-function name for
emit-chunk. - FN_
FLUSH - Host-function name for
flush. - FN_
INPUT_ LEN - Host-function name for
input-len. - FN_
READ_ INPUT - Host-function name for
read-input. - MAX_
CHUNK_ BYTES - Maximum size, in bytes, of a single
emit-chunkcall. 64 KiB matches typical HTTP chunk-encoder buffer sizes; guests producing larger payloads should callemit-chunkrepeatedly. Enforced by the v0.4 host-fn wrapper; exported here so wrapper and tests share a constant. - MAX_
TOTAL_ STREAM_ BYTES - Maximum total bytes a single invocation may emit across all
emit-chunkcalls before the host starts returning the documented-2 = cap exceededcode. 64 MiB is generous for LLM-style token streams while keeping the worst-case per-invocation memory footprint bounded. - STREAMING_
MODULE - Module name used to register the wasi-tensor host functions:
wasi:tensor/host@0.1.0.
Traits§
- HasInput
- Trait implemented by store data types that can hand out an
InputContext. ParallelsHasStreaming; the executor’sInstanceStateimplements it so theinput-len/read-inputhost functions reach the per-instance staged buffer. - HasStreaming
- Trait implemented by store data types that can hand out a
StreamingContext. Parallelscrate::host::HasWasiCuda; the executor’sInstanceStatewill implement this once the v0.4 wiring lands.
Functions§
- add_
input_ to_ linker - Register the
wasi:tensor/hostguest-input host functions (input-len,read-input) on a wasmtimeLinker. - add_
streaming_ to_ linker - Register the wasi-tensor host functions on a wasmtime
Linker.