sse

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ContentBlockDeltaEvent

type ContentBlockDeltaEvent struct {
	Type  string       `json:"type"`
	Index int          `json:"index"`
	Delta ContentDelta `json:"delta"`
}

ContentBlockDeltaEvent — one delta to an in-progress content block.

type ContentBlockStartEvent

type ContentBlockStartEvent struct {
	Type         string          `json:"type"`
	Index        int             `json:"index"`
	ContentBlock json.RawMessage `json:"content_block"`
}

ContentBlockStartEvent — the start of a new content block.

type ContentBlockStopEvent

type ContentBlockStopEvent struct {
	Type  string `json:"type"`
	Index int    `json:"index"`
}

ContentBlockStopEvent — content block closed.

type ContentDelta

type ContentDelta struct {
	Type        string `json:"type"`
	Text        string `json:"text,omitempty"`         // text_delta
	PartialJSON string `json:"partial_json,omitempty"` // input_json_delta
	Thinking    string `json:"thinking,omitempty"`     // thinking_delta
}

ContentDelta is one of: text_delta (most common), input_json_delta (tool inputs streaming in), thinking_delta (extended thinking).

type Error

type Error struct {
	Type    string `json:"type"`
	Message string `json:"message"`
}

Error is the typed error decoded from an SSE `error` event.

func (*Error) Error

func (e *Error) Error() string

type Event

type Event struct {
	// Type is the value of the `event:` field. For Anthropic this is one
	// of message_start, message_delta, message_stop, content_block_start,
	// content_block_delta, content_block_stop, ping, error.
	Type string
	// RawData is the concatenated `data:` lines (joined with newlines if
	// multi-line). It is the raw JSON payload — call one of the AsXxx
	// helpers to decode.
	RawData []byte
}

Event is a single Server-Sent Event from the Anthropic stream.

func (Event) AsContentBlockDelta

func (ev Event) AsContentBlockDelta() (*ContentBlockDeltaEvent, error)

AsContentBlockDelta decodes ev into a ContentBlockDeltaEvent. Returns an error when the event type doesn't match.

func (Event) AsContentBlockStart

func (ev Event) AsContentBlockStart() (*ContentBlockStartEvent, error)

AsContentBlockStart decodes ev into a ContentBlockStartEvent.

func (Event) AsContentBlockStop

func (ev Event) AsContentBlockStop() (*ContentBlockStopEvent, error)

AsContentBlockStop decodes ev into a ContentBlockStopEvent.

func (Event) AsMessageDelta

func (ev Event) AsMessageDelta() (*MessageDeltaEvent, error)

AsMessageDelta decodes ev into a MessageDeltaEvent.

func (Event) AsMessageStart

func (ev Event) AsMessageStart() (*MessageStartEvent, error)

AsMessageStart decodes ev into a MessageStartEvent.

type MessageDelta

type MessageDelta struct {
	StopReason   string `json:"stop_reason"`
	StopSequence string `json:"stop_sequence,omitempty"`
}

MessageDelta carries the stop reason at message close.

type MessageDeltaEvent

type MessageDeltaEvent struct {
	Type  string       `json:"type"`
	Delta MessageDelta `json:"delta"`
	Usage Usage        `json:"usage"`
}

MessageDeltaEvent — final usage / stop info.

type MessageMeta

type MessageMeta struct {
	ID         string            `json:"id"`
	Type       string            `json:"type"`
	Role       string            `json:"role"`
	Model      string            `json:"model"`
	Content    []json.RawMessage `json:"content"`
	StopReason string            `json:"stop_reason"`
	Usage      Usage             `json:"usage"`
}

MessageMeta is the message envelope inside message_start.

type MessageStartEvent

type MessageStartEvent struct {
	Type    string      `json:"type"`
	Message MessageMeta `json:"message"`
}

MessageStartEvent is the first event in every stream.

type Parser

type Parser struct {

	// IncludePings, when true, surfaces `ping` events to callers. Default
	// false because nothing in the agent loop needs them.
	IncludePings bool
	// contains filtered or unexported fields
}

Parser yields Events from an SSE stream. It is not safe for concurrent use; use one Parser per stream.

func NewParser

func NewParser(r io.Reader) *Parser

NewParser wraps r in a Parser. r is read once-through; the parser does not buffer beyond what bufio.Reader keeps.

func (*Parser) Next

func (p *Parser) Next() (Event, error)

Next returns the next non-skipped event. Returns io.EOF when the stream ends cleanly. Returns *Error for `error` events surfaced by the API.

Comments (lines starting with `:`), empty lines, and unknown fields are silently absorbed per the SSE spec.

type Usage

type Usage struct {
	InputTokens              int `json:"input_tokens"`
	OutputTokens             int `json:"output_tokens"`
	CacheCreationInputTokens int `json:"cache_creation_input_tokens,omitempty"`
	CacheReadInputTokens     int `json:"cache_read_input_tokens,omitempty"`
}

Usage tracks input/output tokens. cache_* fields are present when prompt caching is in play.

Jump to

Keyboard shortcuts

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