formatter

package
v0.137.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2026 License: BSD-2-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package formatter implements a Go port of the claude-yolo Python v2 stream-json formatter. It reads newline-delimited Claude Code stream-json from an io.Reader, writes every raw line verbatim to a raw writer, and renders a human-readable formatted line to a formatted writer.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AssistantMessage

type AssistantMessage struct {
	Content []ContentBlock `json:"content"`
}

AssistantMessage is the decoded form of StreamMessage.Message for type == "assistant".

type ContentBlock

type ContentBlock struct {
	Type     string `json:"type"`
	Text     string `json:"text"`
	Thinking string `json:"thinking"`
	// tool_use fields:
	ID    string          `json:"id"`
	Name  string          `json:"name"`
	Input json.RawMessage `json:"input"`
	// tool_result fields:
	ToolUseID string          `json:"tool_use_id"`
	IsError   *bool           `json:"is_error"`
	Content   json.RawMessage `json:"content"`
}

ContentBlock represents one element of a message's content array.

type Formatter

type Formatter interface {
	// ProcessStream reads newline-delimited stream-json from r until EOF or ctx cancellation.
	// For each line it writes the raw bytes (including the trailing newline) to rawWriter,
	// and a formatted human-readable line (with timestamp prefix, trailing newline) to formattedWriter.
	// Lines that are not valid JSON are written verbatim to both outputs.
	// ProcessStream returns nil on clean EOF. It returns a non-nil error only when rawWriter
	// or formattedWriter return a write error.
	ProcessStream(
		ctx context.Context,
		r io.Reader,
		rawWriter io.Writer,
		formattedWriter io.Writer,
	) error
}

Formatter processes a Claude Code stream-json stdout stream.

func NewFormatter

func NewFormatter() Formatter

NewFormatter creates a new Formatter.

type RateLimitInfo added in v0.120.0

type RateLimitInfo struct {
	// Status is the rate-limit status string, e.g. "allowed_warning" or "blocked".
	Status string `json:"status"`
	// ResetsAt is the unix epoch (seconds) when the bucket resets. Zero means unknown.
	ResetsAt int64 `json:"resetsAt"`
	// RateLimitType identifies the bucket, e.g. "seven_day" or "five_hour".
	RateLimitType string `json:"rateLimitType"`
	// Utilization is the fraction of the bucket consumed, 0.0–1.0.
	Utilization float64 `json:"utilization"`
	// IsUsingOverage indicates overage consumption.
	IsUsingOverage bool `json:"isUsingOverage"`
	// SurpassedThreshold indicates the threshold has been crossed.
	SurpassedThreshold bool `json:"surpassedThreshold"`
}

RateLimitInfo contains the details of a rate_limit_event message.

type StreamMessage

type StreamMessage struct {
	Type    string          `json:"type"`
	Subtype string          `json:"subtype"`
	Message json.RawMessage `json:"message"`
	// Result event fields (type == "result"):
	DurationMs   *float64 `json:"duration_ms"`
	TotalCostUSD *float64 `json:"total_cost_usd"`
	Usage        *Usage   `json:"usage"`
	Result       *string  `json:"result"`
	IsError      *bool    `json:"is_error"`
	// RateLimitInfo is populated when Type == "rate_limit_event".
	RateLimitInfo *RateLimitInfo `json:"rate_limit_info"`
	// System init fields (type == "system", subtype == "init"):
	SessionID string   `json:"session_id"`
	Model     string   `json:"model"`
	Cwd       string   `json:"cwd"`
	Tools     []string `json:"tools"`
}

StreamMessage is the top-level envelope for every newline-delimited event.

type Usage

type Usage struct {
	InputTokens          *int `json:"input_tokens"`
	OutputTokens         *int `json:"output_tokens"`
	CacheReadInputTokens *int `json:"cache_read_input_tokens"`
}

Usage captures the token counts from the final result event.

type UserMessage

type UserMessage struct {
	Content []ContentBlock `json:"content"`
}

UserMessage is the decoded form of StreamMessage.Message for type == "user".

Jump to

Keyboard shortcuts

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