event

package
v1.0.2 Latest Latest
Warning

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

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

Documentation

Overview

Package event defines the Event domain entity — an atomic record of activity within a Claude Code session. Events carry a typed envelope (ID, Timestamp, Kind, SessionID, ParentID) and a sealed Payload for kind-specific data.

Unknown event kinds are preserved as OpaquePayload — they are never dropped.

Package event — format.go provides the Truncate helper for normalizing and length-bounding Summary strings. This file uses only stdlib packages (strings, unicode/utf8) to satisfy the domain-pure depguard rule.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Truncate

func Truncate(s string, max int) string

Truncate normalizes whitespace and limits the string to max runes.

Rules applied in order:

  1. Replace "\r\n" with a single space (before handling lone "\r"/"\n").
  2. Replace each remaining "\r", "\n", "\t" with a single space.
  3. Collapse consecutive whitespace (space, tab) to a single space.
  4. Trim leading and trailing whitespace.
  5. Count runes via utf8.RuneCountInString.
  6. If rune count ≤ max → return as-is.
  7. Else → take first (max-1) runes and append "…" (U+2026, 1 rune). The final rune count is exactly min(len(input_runes), max).

Special cases:

  • max ≤ 0 → "" (defensive).
  • empty or all-whitespace input → "".
  • max = 1 with non-empty input → "…".

Pure stdlib: strings and unicode/utf8 only. depguard domain-pure safe.

Types

type AssistantPayload

type AssistantPayload struct {
	// Usage carries the token counts reported by the Claude API.
	Usage usage.Usage

	// Model is the Claude model ID that generated this turn, e.g.
	// "claude-opus-4-7". Populated from the JSONL "message.model" field.
	// Empty string when the field is absent (older JSONL files or opaque events).
	Model string

	// Summary is derived by the priority chain in design.md ADR-010:
	// tool_use > text > "(thinking)" > "". Bounded by Truncate().
	Summary string

	// ToolUseID is the id of the first tool_use block in this event's content.
	// Non-empty only when the event contains at least one tool_use block.
	// Used by the livesession use case for tool_use ↔ tool_result matching.
	ToolUseID string

	// ToolName is the name field of the first tool_use block (e.g. "Read", "Bash").
	// Non-empty only when ToolUseID is non-empty.
	ToolName string

	// Has1hCache is true when the API response included at least one token in
	// message.usage.cache_creation.ephemeral_1h_input_tokens.
	//
	// The 1-hour prompt cache tier is exclusively available to Claude Max plan
	// subscribers who have the 1M-token context window.  Standard plan users
	// only have access to the 5-minute cache tier.  Therefore, the presence of
	// 1h-cache tokens is a reliable indicator that the session is running under
	// the 1M-context variant of the model, even though the JSONL message.model
	// field does not carry a "[1m]" suffix.
	//
	// Used by livesession to synthesize the effective model ID
	// "claude-opus-4-7[1m]" when this field is true.
	Has1hCache bool
}

AssistantPayload is the payload for KindAssistant events. It carries the token Usage reported by the Claude API for this turn.

type Event

type Event struct {
	// ID is the globally-unique identifier (UUID) assigned by Claude Code.
	ID string

	// Timestamp is the UTC instant at which the Event was recorded.
	Timestamp time.Time

	// Kind is the category of activity. May be an unrecognized string value
	// when the adapter maps an unknown JSONL type to KindOpaque and stores
	// the original string in the Kind field of the envelope.
	Kind Kind

	// SessionID is the identifier of the Session this Event belongs to.
	SessionID string

	// ParentID is the ID of the preceding Event in the conversation DAG.
	// Empty for root Events (the first event in a session).
	ParentID string

	// Payload carries kind-specific data. Callers type-switch on the concrete
	// Payload type. Never nil after NewEvent.
	Payload Payload
}

Event is an atomic record of activity within a Session. All fields are exported and immutable by convention — nothing in the domain mutates an Event after construction.

func NewEvent

func NewEvent(
	id string,
	ts time.Time,
	kind Kind,
	sessionID, parentID string,
	payload Payload,
) Event

NewEvent constructs an Event with all envelope fields populated. parentID may be empty for root Events. payload must be one of UserPayload, AssistantPayload, or OpaquePayload.

type Kind

type Kind string

Kind identifies the category of activity recorded by an Event. It is a string-based type to allow unknown kinds to be preserved verbatim without a decode error (see OpaquePayload and KindOpaque).

const (
	// KindUser represents a turn initiated by the human user.
	KindUser Kind = "user"

	// KindAssistant represents a turn generated by the Claude assistant.
	KindAssistant Kind = "assistant"

	// KindOpaque is the canonical kind for events whose original type string
	// is not recognized by this version of the domain. The raw bytes are
	// preserved in OpaquePayload.Raw so future panels can decode them without
	// requiring a domain change.
	KindOpaque Kind = "opaque"
)

type OpaquePayload

type OpaquePayload struct {
	Raw []byte
}

OpaquePayload is the payload for any event whose kind is not recognized by this version of the domain. Raw holds the entire original JSONL line so nothing is lost. Future panels decode it themselves.

type Payload

type Payload interface {
	// contains filtered or unexported methods
}

Payload is the sealed interface for event-kind-specific data. Only types defined in this package implement Payload; callers outside the package pattern-match on the concrete variants:

switch p := ev.Payload.(type) {
case event.UserPayload:      ...
case event.AssistantPayload: ...
case event.OpaquePayload:    ...
}

type UserPayload

type UserPayload struct {
	// IsMeta is true for system-injected user events (skill prompts,
	// <local-command-caveat>, etc.). Set from the JSONL "isMeta" field.
	IsMeta bool

	// IsToolResultOnly is true when the user event's message.content is
	// an array containing exclusively tool_result blocks. Set during
	// JSONL decode by inspecting the content array.
	IsToolResultOnly bool

	// Summary is the truncated, single-line text of the typed user prompt.
	// Empty when IsMeta or IsToolResultOnly is true. Bounded by Truncate().
	Summary string

	// ToolResultIDs lists the tool_use_id values of all tool_result blocks
	// in this event's content. Non-empty only when IsToolResultOnly is true.
	// Used by the livesession use case for tool_use ↔ tool_result matching
	// (duration computation, state determination).
	ToolResultIDs []string

	// ToolResultError is true if any tool_result block in this event has
	// is_error set to true. Used to mark the corresponding tool call as failed.
	ToolResultError bool
}

UserPayload is the payload for KindUser events.

Jump to

Keyboard shortcuts

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