event

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package event defines the unified event model for LLM streaming in deepwork. All 9 Portals (Chat, Browser Sidebar, Open Design, Studio, Council, Claw, Companion, Workspace Run, CLI Escape) consume the same Event type regardless of source family (CLI Observer / LLM API Orchestrator / WebChat DOM Polling).

Design: TH-0503-k8v (8 rounds, 12 Codex reviews, 15 DDC, 7 BRR, 2 EUREKA).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DoneData

type DoneData struct {
	Reason       string `json:"reason,omitempty"`        // stop / tool_use / length / cancel / error
	InputTokens  int    `json:"input_tokens,omitempty"`  // LLM: prompt tokens
	OutputTokens int    `json:"output_tokens,omitempty"` // LLM: completion tokens
}

DoneData holds stream completion information including usage.

type Emitter

type Emitter func(Event) bool

─── Emitter ────────────────────────────────────────── Emitter is the universal callback for streaming events. Returns false to signal the producer should stop (backpressure / client disconnect).

func Collect

func Collect(events *[]Event) Emitter

Collect returns an Emitter that appends events to the given slice. For testing.

func Discard

func Discard() Emitter

Discard returns an Emitter that silently drops all events. For benchmarks.

func NewSSEEmitter

func NewSSEEmitter(w *ssekit.Writer) Emitter

NewSSEEmitter creates an Emitter that writes each Event as an SSE data line. Returns false (backpressure) when the SSE write fails (client disconnected).

func SafeEmitter

func SafeEmitter(emit Emitter) Emitter

SafeEmitter wraps an Emitter with a mutex for concurrent use. Use this when multiple goroutines emit to the same downstream (e.g., Council fan-in).

func SeqEmitter

func SeqEmitter(emit Emitter) Emitter

SeqEmitter wraps an Emitter to stamp monotonic sequence numbers into Meta["seq"].

func TimestampEmitter

func TimestampEmitter(emit Emitter) Emitter

TimestampEmitter wraps an Emitter to stamp current time into Meta["ts"].

type Event

type Event struct {
	Kind     Kind            `json:"kind"`
	Status   string          `json:"status,omitempty"`  // Status: waiting / running / tool / etc.
	Content  string          `json:"content,omitempty"` // Text/Thinking/Error: direct string
	Tool     *ToolData       `json:"tool,omitempty"`    // ToolStart/ToolResult: structured
	Usage    *UsageData      `json:"usage,omitempty"`   // Usage: token accounting update
	DoneInfo *DoneData       `json:"done,omitempty"`    // Done: usage + reason
	Source   string          `json:"source,omitempty"`  // Multi-source attribution (Council)
	RawData  json.RawMessage `json:"raw,omitempty"`     // Raw: passthrough unknown events
	Meta     map[string]any  `json:"meta,omitempty"`    // Application extensions (seq, cost_usd, duration_ms)
}

Event is the universal streaming event for all deepwork Portals. Exactly one of Content/Tool/DoneInfo/RawData is meaningful per Kind.

func Burst

func Burst(content string) []Event

Burst converts a complete (non-streaming) response into a sequence of events. Useful for WebChat DOM polling, non-streaming API fallback, cached responses.

func DoneEvent

func DoneEvent(reason string, inputTokens, outputTokens int) Event

DoneEvent creates a stream completion event.

func ErrorEvent

func ErrorEvent(msg string) Event

ErrorEvent creates an error event.

func RawEvent

func RawEvent(data json.RawMessage) Event

RawEvent creates a passthrough event for unknown provider data.

func StatusEvent

func StatusEvent(status string) Event

StatusEvent creates a non-content progress event.

func TextEvent

func TextEvent(content string) Event

TextEvent creates a text delta event.

func ThinkingEvent

func ThinkingEvent(content string) Event

ThinkingEvent creates a thinking/reasoning delta event.

func ToolResultEvent

func ToolResultEvent(id, name, output string, isErr bool, durMs int) Event

ToolResultEvent creates a tool execution result event.

func ToolStartEvent

func ToolStartEvent(id, name string, input json.RawMessage) Event

ToolStartEvent creates a tool call event with complete arguments.

func UsageEvent

func UsageEvent(data UsageData) Event

UsageEvent creates a token accounting update.

func (Event) WithMeta

func (e Event) WithMeta(key string, value any) Event

WithMeta returns a copy of the event with Meta entries added. Existing Meta entries are preserved; new entries override on collision.

func (Event) WithSource

func (e Event) WithSource(source string) Event

WithSource returns a copy of the event with Source set. Used by Council to tag which LLM participant produced this event.

type Kind

type Kind string

Kind classifies the semantic content of an LLM streaming event.

const (
	// Status is a non-content progress signal for transport/agent lifecycle.
	Status Kind = "status"
	// Text is an incremental text content delta (打字机效果).
	Text Kind = "text"
	// Thinking is an incremental reasoning/CoT delta.
	// Maps to: OpenAI reasoning, Anthropic thinking_delta, DeepSeek/GLM reasoning_content.
	Thinking Kind = "thinking"
	// ToolStart signals a complete tool call (provider buffers incremental args internally).
	ToolStart Kind = "tool_start"
	// ToolResult signals a tool execution result.
	ToolResult Kind = "tool_result"
	// Usage reports token counters while a stream is still running or after a
	// provider emits cumulative accounting outside the final done frame.
	Usage Kind = "usage"
	// Done signals stream completion with usage/cost/reason.
	Done Kind = "done"
	// Error signals a recoverable error (stream may continue).
	Error Kind = "error"
	// Raw is an escape hatch for unknown/new event types from providers.
	Raw Kind = "raw"
)

type ToolData

type ToolData struct {
	ID         string          `json:"id"`
	Name       string          `json:"name"`
	Input      json.RawMessage `json:"input,omitempty"`       // ToolStart: complete arguments JSON
	Output     string          `json:"output,omitempty"`      // ToolResult: execution output
	IsError    bool            `json:"is_error,omitempty"`    // ToolResult: tool failed
	DurationMs int             `json:"duration_ms,omitempty"` // ToolResult: execution time
}

ToolData holds tool call or tool result information.

type UsageData

type UsageData struct {
	InputTokens              int  `json:"input_tokens,omitempty"`
	ThinkingTokens           int  `json:"thinking_tokens,omitempty"`
	OutputTokens             int  `json:"output_tokens,omitempty"`
	CacheReadInputTokens     int  `json:"cache_read_input_tokens,omitempty"`
	CacheCreationInputTokens int  `json:"cache_creation_input_tokens,omitempty"`
	TotalTokens              int  `json:"total_tokens,omitempty"`
	Estimated                bool `json:"estimated,omitempty"`
}

UsageData holds provider token accounting. Providers may emit this incrementally, cumulatively, or only at completion.

Jump to

Keyboard shortcuts

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