streamjson

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 8 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 streamjson re-emits the parsed claude session-JSONL Event stream onto a writer (typically pyry's stdout) and composes a single `type:"result"` trailer line when the run terminates. Output shape mirrors `claude -p --output-format stream-json` so the dispatcher's stream-json parser keeps working unchanged after the agent-run migration.

MUST NOT log Event content. The package logs only counts, durations, and error messages — never Event.Raw bytes nor per-event Usage values.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Writer is where stream-json lines are written. Required; production
	// passes os.Stdout.
	Writer io.Writer

	// SessionID is the UUIDv4 the caller minted and passed to claude via
	// --session-id. Stamped into the trailer's `session_id` field. Required.
	SessionID string

	// Now is a clock seam; defaults to time.Now. New captures Now() at
	// construction; Close calls Now() again to compute duration_ms.
	Now func() time.Time

	// Logger is optional and defaults to slog.Default().
	Logger *slog.Logger
}

Config configures Emitter.

type Emitter

type Emitter struct {
	// contains filtered or unexported fields
}

Emitter re-emits jsonl.Events to its Writer and composes a single `type:"result"` trailer line on Close. Safe for concurrent use across Emit, SetExitReason, and Close.

func New

func New(cfg Config) (*Emitter, error)

New constructs an Emitter. Returns an error if Writer is nil or SessionID is empty.

func (*Emitter) Close

func (e *Emitter) Close() error

Close writes the final `type:"result"` trailer line. MUST be called exactly once after Emit calls have stopped; calling Close while Emit is still firing is a race.

Default exit-reason classification when SetExitReason was not called:

  • end-of-turn was observed during Emit → ExitReasonCompletion
  • end-of-turn was NOT observed → ExitReasonError

Idempotent: second and subsequent calls return the first call's error verbatim without re-writing the trailer.

func (*Emitter) Emit

func (e *Emitter) Emit(ev jsonl.Event) error

Emit re-emits ev.Raw verbatim followed by '\n', aggregates ev.Usage, and counts assistant entries. Safe for concurrent use.

Once an underlying write fails, the error is sticky: subsequent Emit calls no-op (returning nil) so the watcher can keep parsing until end-of-turn or ctx cancel without thrashing on a broken pipe. Close still attempts to write the trailer (best-effort).

Emit after Close is a no-op returning nil — late events from a slow watcher goroutine must not panic.

func (*Emitter) SetExitReason

func (e *Emitter) SetExitReason(r ExitReason)

SetExitReason overrides the default exit-reason classification used by Close. Pluggable seam for the future budget integration (#334), which calls SetExitReason(ExitReasonMaxTurns) from its Terminate hook before SIGTERMing claude.

Idempotent: only the first non-empty value sticks; later calls are no-ops. Safe for concurrent use.

type ExitReason

type ExitReason string

ExitReason classifies how the run terminated. The internal value is translated into wire fields (`subtype` / `terminal_reason` / `is_error`) at trailer-composition time; see closeLocked.

const (
	// ExitReasonCompletion signals a clean, deterministic end-of-turn
	// observation from the watcher.
	ExitReasonCompletion ExitReason = "completion"

	// ExitReasonMaxTurns signals the budget Counter forcibly stopped the
	// run. Set via SetExitReason from #334's future Terminate hook.
	ExitReasonMaxTurns ExitReason = "max_turns"

	// ExitReasonError signals an unrecoverable failure — claude exited
	// non-zero, the watcher failed, or the run was torn down before
	// end-of-turn fired.
	ExitReasonError ExitReason = "error"
)

Jump to

Keyboard shortcuts

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