zidane/ output/ stream-json
A JSONL writer that emits one line per persisted turn, for pipes, logs and other programs.
import { … } from 'zidane/output/stream-json'src/output/stream-json.ts
Use zidane/output/stream-json when another process reads the run: a wrapper script, a log shipper or an eval harness. setupStreamJsonOutput() writes one JSON line per turn the agent persists, either as zidane event envelopes or as raw provider messages. The format helpers build the same lines without attaching to an agent, and createStreamJsonOutputHandler() fits the headless runner.
import { createAgent } from 'zidane'
import { setupStreamJsonOutput } from 'zidane/output/stream-json'
import { basic } from 'zidane/presets'
import { anthropic } from 'zidane/providers/anthropic'
const agent = createAgent({ ...basic, provider: anthropic() })
const lines: string[] = []
const stop = setupStreamJsonOutput(agent, { format: 'zidane', write: line => lines.push(line) })
await agent.run({ prompt: 'Say hello.' })
stop()
console.log(lines.length)Functions#
createStreamJsonOutputHandlerFunction#
Creates a headless event handler that writes one JSONL line per turn event and ignores other events.
function createStreamJsonOutputHandler(
options?: StreamJsonOutputOptions,
): (event: HeadlessEvent) => voidParameters
| Name | Type | Default | Description |
|---|---|---|---|
| options? | StreamJsonOutputOptions | {} | Output and line shape options. |
Returns
(event: HeadlessEvent) => voidformatStreamJsonResultFunction#
Formats a final headless result as a JSON-serializable value.
function formatStreamJsonResult(
result: HeadlessResult,
options?: StreamJsonFormatOptions,
): FormattedHeadlessResultParameters
| Name | Type | Default | Description |
|---|---|---|---|
| result | HeadlessResult | The headless run result. | |
| options? | StreamJsonFormatOptions | {} | Line shape options. |
Returns
formatStreamJsonTurnEventFunction#
Formats one turn event as a JSON-serializable value.
function formatStreamJsonTurnEvent(
event: { index: number; turn: SessionTurn; type: 'turn' },
options?: StreamJsonFormatOptions,
): FormattedHeadlessTurnEventParameters
| Name | Type | Default | Description |
|---|---|---|---|
| event | { index: number; turn: SessionTurn; type: 'turn' } | The turn event. | |
| options? | StreamJsonFormatOptions | {} | Line shape options. |
Returns
setupStreamJsonOutputFunction#
Writes a JSONL line for every turn the agent persists.
Returns a function that detaches the output from the agent.
function setupStreamJsonOutput(
agent: Agent,
options?: StreamJsonOutputOptions,
): () => voidParameters
| Name | Type | Default | Description |
|---|---|---|---|
| agent | Agent | Agent to listen to. | |
| options? | StreamJsonOutputOptions | {} | Output and line shape options. |
Returns
() => voidExample
import { createAgent } from 'zidane'
import { basic } from 'zidane/presets'
import { anthropic } from 'zidane/providers'
import { setupStreamJsonOutput } from 'zidane/output/stream-json'
const agent = createAgent({ ...basic, provider: anthropic({ apiKey: 'sk-ant-...' }) })
const stop = setupStreamJsonOutput(agent, { format: 'zidane' })
await agent.run({ prompt: 'build a REST API' })
stop()streamJsonResultToJsonlFunction#
Formats a final headless result as a JSONL line.
function streamJsonResultToJsonl(
result: HeadlessResult,
options?: StreamJsonFormatOptions,
): stringParameters
| Name | Type | Default | Description |
|---|---|---|---|
| result | HeadlessResult | The headless run result. | |
| options? | StreamJsonFormatOptions | {} | Line shape options. |
Returns
stringstreamJsonTurnEventToJsonlFunction#
Formats one turn event as a JSONL line.
function streamJsonTurnEventToJsonl(
event: { index: number; turn: SessionTurn; type: 'turn' },
options?: StreamJsonFormatOptions,
): stringParameters
| Name | Type | Default | Description |
|---|---|---|---|
| event | { index: number; turn: SessionTurn; type: 'turn' } | The turn event. | |
| options? | StreamJsonFormatOptions | {} | Line shape options. |
Returns
stringInterfaces#
StreamJsonFormatOptionsInterface#
Options that control the shape of each emitted line.
interface StreamJsonFormatOptionsProperties
| Name | Type | Default | Description |
|---|---|---|---|
| format? | HeadlessOutputFormat | 'zidane' | Line shape. |
| providerName? | string | 'openai' | Provider name that selects the message shape when |
StreamJsonOutputOptionsInterface#
Options for createStreamJsonOutputHandler and setupStreamJsonOutput.
interface StreamJsonOutputOptions extends StreamJsonFormatOptionsProperties
| Name | Type | Default | Description |
|---|---|---|---|
format?from StreamJsonFormatOptions | HeadlessOutputFormat | 'zidane' | Line shape. |
providerName?from StreamJsonFormatOptions | string | 'openai' | Provider name that selects the message shape when |
| write? | (line: string) => void | Receives each JSONL line. Default: writes to |
Types#
StreamJsonOutputFormatType#
Line shape: zidane event envelopes or raw provider messages.
type StreamJsonOutputFormat = HeadlessOutputFormatStreamJsonTurnEventType#
A complete turn event, the only event this output emits.
type StreamJsonTurnEvent = Extract<HeadlessEvent, { type: 'turn' }>