What is a runtime stream?
A runtime stream is a live, two-way byte stream between agent code and the thread’s execution host. The execution host is the client that runs work for the thread outside the Worker, such as a process on a workstation. Agent code opens a stream withstate.runtimeStreams.open(). The runtime carries the bytes and does not read them. The capability name and the metadata have meaning only to your agent code and your execution host.
Use a runtime stream when a tool must send or receive a large or continuous byte sequence, such as media, an archive, or the output of a long process.
Feature detection
runtimeStreams is an optional member of ThreadState in the Standard Agent Specification. Standard Agents Builder provides it on every ThreadState. Check for it before use, so the same code runs on a runtime that omits it:
Opening a stream
open() does not throw for a refused or unavailable stream. It resolves to a result:
Failure codes
When the thread has a recorded execution host whose connection is closed, Standard Agents Builder signals that host to reconnect and waits up to the time limit. When the thread has no recorded execution host,
open() returns unavailable at once.
The stream
readablecarries bytes from the execution host to your code.writablecarries bytes from your code to the execution host.- Bytes arrive in the order they were written, in both directions.
- Closing
writableends your side of the stream. The host can continue to send bytes throughreadable. close(reason?)aborts the stream in both directions. A second call does nothing.closedsettles once with the final outcome of the stream.
Lifetime
A runtime stream is a live resource. Standard Agents Builder does not store it:- A stream ends with an error when the execution host disconnects.
- A stream does not continue after the thread’s Durable Object restarts.
- A stream opened by a child thread travels through the nearest parent thread that has an execution host.
stream.close() in a finally block, so a stream never stays open after your code returns.
Example
This tool sends a file from the thread’s file system to a host transcoder and stores the result:Next steps
Tools
Open runtime streams from tool code
ThreadState
See the other members of the state object