Documentation
¶
Overview ¶
Package stream reads a coding agent's stream as its run writes it: Claude Code's `claude -p --output-format stream-json --verbose` and Codex's `codex exec --json`. Each agent's parser turns its own messages into the one event model (runevent), and keeps how far the run has got, the agent's own end (Result), the provider's usage limit, and, for Claude Code, what its init line says of its own setup (Init).
It is a leaf: it imports only runevent and the standard library, so the background runner (internal/agentrun) and anything else that follows an agent's stream read it without reaching a run engine or the store.
Index ¶
- Constants
- func LimitName(kind string) string
- type Init
- type Limit
- type MCPServer
- type Parser
- func (s *Parser) AgentSessionID() string
- func (s *Parser) Events() []runevent.Event
- func (s *Parser) Flush()
- func (s *Parser) From(offset int64) *Parser
- func (s *Parser) Init() (Init, bool)
- func (s *Parser) LastWords() string
- func (s *Parser) Limit() *Limit
- func (s *Parser) Over() bool
- func (s *Parser) Page(after int64) runevent.Page
- func (s *Parser) Progress() runevent.Progress
- func (s *Parser) Result() (Result, bool)
- func (s *Parser) Write(p []byte) (int, error)
- type Result
Constants ¶
const ( LimitRejected = "rejected" LimitWarning = "allowed_warning" )
The statuses of a usage limit the agent reports: LimitRejected refused the run, and LimitWarning lets it go on near the limit.
const ( // KeepEvents is how many events a parser keeps in memory; the run view of a run with // more reads the rest from its log. KeepEvents = 2000 )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Init ¶
type Init struct {
// Model is the model it runs, Version its own version, and APIKeySource where its API
// key came from ("none" for a sign-in with the user's plan).
Model string `json:"model"`
Version string `json:"version"`
APIKeySource string `json:"apiKeySource"`
// SessionID is its ID of the conversation.
SessionID string `json:"sessionId"`
// Tools are every tool it may use, and MCPServers the MCP servers it started, each as
// it came up; neither is ever nil.
Tools []string `json:"tools"`
MCPServers []MCPServer `json:"mcpServers"`
}
Init is what an agent's init line says of its own setup as its run starts, Claude Code's system/init: a turn that runs only the app's own tools checks them here before it goes on.
type Limit ¶
type Limit struct {
// Status is allowed, allowed_warning, or rejected (LimitRejected); Type is which window
// ("five_hour", "seven_day", ...); ResetsAt is when it resets, in Unix seconds.
Status string `json:"status"`
Type string `json:"type"`
ResetsAt int64 `json:"resetsAt"`
}
Limit is the provider's usage limit, as the agent last reported it.
type MCPServer ¶
MCPServer is an MCP server as an agent's init line names it: its name, and its status ("connected", "failed", ...).
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser is a run's parser of its agent's stream (Claude or Codex): an io.Writer fed the run's log in order, from one goroutine at a time, which any goroutine may read.
func (*Parser) AgentSessionID ¶
AgentSessionID returns the agent's own ID of the conversation, once its stream names it (Codex's thread.started), or "".
func (*Parser) Flush ¶
func (s *Parser) Flush()
Flush reads a last line the log never ended, as a run's that has ended may be: the log won't grow, so the line is whole.
func (*Parser) From ¶
From starts the parser at offset in the log, a line's start, as for a page read from its middle: its events' offsets count from there. It returns the parser, which must not have read anything yet.
func (*Parser) Init ¶
Init returns what the agent's init line said of its own setup, once it came: only Claude Code's stream has one. A turn reads it to check its own tools and MCP servers before it goes on.
func (*Parser) LastWords ¶
LastWords returns the last thing the agent said, or its latest step: what a run that didn't report shows.
func (*Parser) Limit ¶
Limit returns the provider's usage limit as the agent last reported it, or nil.
func (*Parser) Over ¶
Over reports that the agent's result line came: the run is done, whatever of it is left running.
func (*Parser) Page ¶
Page returns the events after the one numbered after, and how far the run has got. The events it no longer keeps in memory are the log's, before the first one's line.
type Result ¶
type Result struct {
// Subtype is the agent's word for how it ended ("success", "error_max_budget_usd",
// "error_during_execution"), and IsError whether it ended in error.
Subtype string `json:"subtype"`
IsError bool `json:"isError"`
// Message is its final message, at most 50 KB: a read-only run's notes.
Message string `json:"message"`
// Report is its structured output, as it gave it, or nil.
Report json.RawMessage `json:"report"`
// Cost is what it reports the run cost, in dollars, nil when it reports none: for a run
// that resumed a conversation, Claude Code reports the whole conversation's cost so far
// (total_cost_usd). Estimated says Cost and TurnCost are estimates of what the API would
// have charged, as for a run signed in with the user's plan rather than an API key.
Cost *float64 `json:"cost"`
Estimated bool `json:"estimated"`
// TurnCost is what this run alone cost, in dollars, nil when the agent reports none:
// the sum of the costs in Claude Code's model_usage_since_last_result, which counts only
// the run's own turn, where Cost counts every turn of a conversation it resumed. A run
// that starts its conversation costs the same in both.
TurnCost *float64 `json:"turnCost"`
// TokensIn, TokensOut, and CacheRead are the tokens it used, Turns its turns, and
// DurationMS how long it ran.
TokensIn int64 `json:"tokensIn"`
TokensOut int64 `json:"tokensOut"`
CacheRead int64 `json:"cacheRead"`
Turns int64 `json:"turns"`
DurationMS int64 `json:"durationMs"`
// Models are the models it actually used: plan mode runs on another model than asked.
// For a run that resumed a conversation, they are every turn's.
Models []string `json:"models"`
// TerminalReason and Errors say why it ended, as the agent does, and Denials counts
// the tool uses its permissions denied.
TerminalReason string `json:"terminalReason"`
Errors []string `json:"errors"`
Denials int `json:"denials"`
}
Result is the agent's own end of a run, from its result line.