Skip to main content

Module stream

Module stream 

Source

Constants§

GATEWAY_SESSION_ENV
The variable visdom sets on an agent it has routed through the AI Gateway, which then records the session itself: the agent’s name (claude-code, codex). Nobody else sets it.
TRACING_MODE_ENV
The developer’s choice of tracing mode for visdom (hooks or gateway). A preference, not a fact about a running agent: an agent started without visdom may well carry gateway here and still talk to its provider directly.

Functions§

append_pending
apply_context
Convert a resolved crate::context::EffectiveContext into the three optional fields that are stamped onto a StreamEventRequest.
captured_by_gateway
Whether the session this hook of agent runs in is recorded by the AI Gateway, given the value of GATEWAY_SESSION_ENV. Hooks inherit the agent’s environment, so this is true for the events of an agent visdom routed through the gateway. The capture hook then records nothing: the same session captured twice would be a second, conflicting source.
drain_pending
inline_offset_bump
Record-count offset arithmetic for the inline (fileless) path, mirroring the byte-offset semantics of read_new_transcript_lines: returns (send_offset, next_offset) where send_offset is stamped on the request as transcript_offset (base for the server’s per-line chunk_index) and next_offset is persisted to .stream_offset.
inline_tool_is_error
Derive the event-level tool-error flag from inline transcript records. OpenCode tool events carry no tool_use_id, so extract_is_error_from_transcript can’t run; instead the plugin forwards a toolResult record whose isError signals the outcome. Amp’s records (type: "amp_message") carry Amp’s own tool_result block instead, whose status is "error" only for a tool that failed: a permission rejection and a non-zero exitCode are both "done", as Amp reports them. Returns Some(true|false) for the first tool result found, or None when there is none (non-tool events).
read_new_transcript_lines
Returns (lines, start_offset, end_offset): start_offset is the seek position read from offset_path (0 if absent), and end_offset is the byte offset after the last line read (the old bytes_read).
record_inline_tool_calls
Fileless agents (OpenCode) have no transcript for tracevault check to count tool calls from: their records ride inline on each hook event. Append one slim toolCall record per tool called in records to the session’s tool_calls.jsonl and record that file as the session’s transcript. Only the tool name is kept — arguments and outputs (file contents, bash stdout) would grow the file for nothing. Records with no tool call write nothing.
record_transcript_path
Store transcript_path in the session’s metadata.json, which is where tracevault check looks for the transcript it counts tool calls from. Without it every session reports no tool calls, and a “must call X” policy can never pass. Other keys in the file are kept. An empty path (Codex before its first record) writes nothing, and an unchanged path skips the write. The file is replaced atomically: parallel tool hooks of one session run concurrently, and check must never read a half-written file.
resolve_session_paths
Resolve the project root and session directory for a stream hook invocation.
resolve_transcript_source
Choose the transcript source for a stream event. Inline records (from a plugin-based agent like OpenCode) win over the file-read result whenever the hook event carries them — INCLUDING an empty vec, so an event with no new records (e.g. OpenCode stop) preserves the prior record-count offset instead of resetting it to the file-path’s 0.
run_stream
split_into_parts
Split a hook event whose transcript batch does not fit in one request (MAX_REQUEST_BYTES) into several requests, so no line is dropped. A request that fits is returned unchanged as the only part.
stamp_agent
Stamp the agent identity onto a request’s tool + protocol_version. Factored out so the mapping is testable without the network/FS-bound run_stream.
stream_event_type
The server event a hook’s --event is sent as.
transcript_template
A lines-only Transcript request with req’s session, agent and context fields but none of its event (type, tool payload, event_uuid), which the server handles as transcript lines only. req’s lines are not copied. Every field is listed, so a new one has to be decided on here.