stream

package
v0.9.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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.

View Source
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

func LimitName

func LimitName(kind string) string

LimitName names a usage limit as the agent reports its type: "five_hour" is the five-hour limit, "seven_day" the weekly one, and "seven_day_opus" the weekly Opus one.

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

type MCPServer struct {
	Name   string `json:"name"`
	Status string `json:"status"`
}

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 Claude

func Claude(dir string) *Parser

Claude returns a parser of Claude Code's stream, for a run in dir.

func Codex

func Codex(dir string) *Parser

Codex returns a parser of Codex's stream, for a run in dir.

func (*Parser) AgentSessionID

func (s *Parser) AgentSessionID() string

AgentSessionID returns the agent's own ID of the conversation, once its stream names it (Codex's thread.started), or "".

func (*Parser) Events

func (s *Parser) Events() []runevent.Event

Events returns every event the parser keeps, oldest first.

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

func (s *Parser) From(offset int64) *Parser

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

func (s *Parser) Init() (Init, bool)

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

func (s *Parser) LastWords() string

LastWords returns the last thing the agent said, or its latest step: what a run that didn't report shows.

func (*Parser) Limit

func (s *Parser) Limit() *Limit

Limit returns the provider's usage limit as the agent last reported it, or nil.

func (*Parser) Over

func (s *Parser) Over() bool

Over reports that the agent's result line came: the run is done, whatever of it is left running.

func (*Parser) Page

func (s *Parser) Page(after int64) runevent.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.

func (*Parser) Progress

func (s *Parser) Progress() runevent.Progress

Progress returns how far the run has got.

func (*Parser) Result

func (s *Parser) Result() (Result, bool)

Result returns the agent's own end, once its result line came.

func (*Parser) Write

func (s *Parser) Write(p []byte) (int, error)

Write feeds the parser more of the log. It never fails.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL