message

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2026 License: GPL-3.0 Imports: 5 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 message provides message and content block types for Codex conversations.

Index

Constants

View Source
const (
	BlockTypeText       = "text"
	BlockTypeImage      = "image"
	BlockTypeLocalImage = "localImage"

	BlockTypeMention    = "mention"
	BlockTypeThinking   = "thinking"
	BlockTypeToolUse    = "tool_use"
	BlockTypeToolResult = "tool_result"
)

Block type constants.

Variables

This section is empty.

Functions

func AnnotateRawJSON added in v0.0.9

func AnnotateRawJSON(data map[string]any, raw []byte) map[string]any

AnnotateRawJSON attaches the original raw JSON bytes to a decoded payload so audit envelopes can preserve byte fidelity later in the parse pipeline.

Types

type AssistantMessage

type AssistantMessage struct {
	Type            string                 `json:"type"`
	Content         []ContentBlock         `json:"content"`
	Model           string                 `json:"model"`
	ParentToolUseID *string                `json:"parent_tool_use_id,omitempty"`
	Error           *AssistantMessageError `json:"error,omitempty"`
	Audit           *AuditEnvelope         `json:"-"`
}

AssistantMessage represents a message from the agent.

func (*AssistantMessage) MessageType

func (m *AssistantMessage) MessageType() string

MessageType implements the Message interface.

type AssistantMessageError

type AssistantMessageError string

AssistantMessageError represents error types from the assistant.

const (
	// AssistantMessageErrorAuthFailed indicates authentication failure.
	AssistantMessageErrorAuthFailed AssistantMessageError = "authentication_failed"
	// AssistantMessageErrorBilling indicates a billing error.
	AssistantMessageErrorBilling AssistantMessageError = "billing_error"
	// AssistantMessageErrorRateLimit indicates rate limiting.
	AssistantMessageErrorRateLimit AssistantMessageError = "rate_limit"
	// AssistantMessageErrorInvalidReq indicates an invalid request.
	AssistantMessageErrorInvalidReq AssistantMessageError = "invalid_request"
	// AssistantMessageErrorServer indicates a server error.
	AssistantMessageErrorServer AssistantMessageError = "server_error"
	// AssistantMessageErrorUnknown indicates an unknown error.
	AssistantMessageErrorUnknown AssistantMessageError = "unknown"
)

type AuditEnvelope added in v0.0.9

type AuditEnvelope struct {
	EventType string          `json:"event_type"`
	Subtype   string          `json:"subtype,omitempty"`
	Payload   json.RawMessage `json:"payload"`
}

AuditEnvelope contains the provider-native event metadata and canonical payload captured at the SDK boundary.

func NewAuditEnvelope added in v0.0.9

func NewAuditEnvelope(eventType, subtype string, payload any) (*AuditEnvelope, error)

NewAuditEnvelope marshals a canonical payload into an audit envelope.

func (*AuditEnvelope) GetEventType added in v0.0.9

func (a *AuditEnvelope) GetEventType() string

func (*AuditEnvelope) GetPayload added in v0.0.9

func (a *AuditEnvelope) GetPayload() json.RawMessage

func (*AuditEnvelope) GetSubtype added in v0.0.9

func (a *AuditEnvelope) GetSubtype() string

type CodexEvent

type CodexEvent struct {
	Type     EventType    `json:"type"`
	ThreadID string       `json:"thread_id,omitempty"`
	Item     *CodexItem   `json:"item,omitempty"`
	Usage    *CodexUsage  `json:"usage,omitempty"`
	Message  string       `json:"message,omitempty"`
	Error    *ErrorDetail `json:"error,omitempty"`
}

CodexEvent is a single JSONL event from the Codex CLI.

func ParseCodexEvent

func ParseCodexEvent(raw map[string]any) (*CodexEvent, error)

ParseCodexEvent converts a raw JSON map into a typed CodexEvent.

type CodexItem

type CodexItem struct {
	ID   string         `json:"id"`
	Type ItemType       `json:"type"`
	Raw  map[string]any `json:"-"`

	// agent_message / reasoning
	Text string `json:"text,omitempty"`

	// command_execution
	Command          string `json:"command,omitempty"`
	AggregatedOutput string `json:"aggregated_output,omitempty"`
	ExitCode         *int   `json:"exit_code,omitempty"`
	Status           string `json:"status,omitempty"`

	// file_change
	Changes []FileChange `json:"changes,omitempty"`

	// mcp_tool_call
	Server string `json:"server,omitempty"`
	Tool   string `json:"tool,omitempty"`
	Result any    `json:"result,omitempty"`
	Error  any    `json:"error,omitempty"`

	// dynamic_tool_call
	Name         string         `json:"name,omitempty"`
	Arguments    map[string]any `json:"arguments,omitempty"`
	ContentItems []ContentItem  `json:"contentItems,omitempty"`
	Success      *bool          `json:"success,omitempty"`

	// plan / image_view / review / collaboration
	Path              string   `json:"path,omitempty"`
	Review            string   `json:"review,omitempty"`
	Prompt            string   `json:"prompt,omitempty"`
	SenderThreadID    string   `json:"senderThreadId,omitempty"`
	ReceiverThreadIDs []string `json:"receiverThreadIds,omitempty"`

	// web_search
	Query string `json:"query,omitempty"`

	// todo_list
	Items []TodoItem `json:"items,omitempty"`

	// error
	Message string `json:"message,omitempty"`
}

CodexItem represents an item in a Codex event.

func (*CodexItem) UnmarshalJSON added in v0.0.7

func (c *CodexItem) UnmarshalJSON(data []byte) error

UnmarshalJSON preserves the full item payload and normalizes fields whose shape differs across Codex backends and protocol versions.

type CodexUsage

type CodexUsage struct {
	InputTokens           int `json:"input_tokens"`
	CachedInputTokens     int `json:"cached_input_tokens"`
	OutputTokens          int `json:"output_tokens"`
	ReasoningOutputTokens int `json:"reasoning_output_tokens"`
}

CodexUsage contains token consumption metrics from Codex.

type ContentBlock

type ContentBlock interface {
	BlockType() string
}

ContentBlock represents a block of content within a message.

func UnmarshalContentBlock

func UnmarshalContentBlock(data []byte) (ContentBlock, error)

UnmarshalContentBlock unmarshals a single content block from JSON.

type ContentItem added in v0.0.2

type ContentItem struct {
	Type string         `json:"type"`
	Text string         `json:"text,omitempty"`
	Raw  map[string]any `json:"-"`
}

ContentItem represents a tool result content item from app-server.

func (*ContentItem) UnmarshalJSON added in v0.0.2

func (c *ContentItem) UnmarshalJSON(data []byte) error

UnmarshalJSON preserves the full content item payload so non-text tool results can be surfaced without losing fields.

type ErrorDetail

type ErrorDetail struct {
	Message string `json:"message"`
}

ErrorDetail contains error information from turn.failed events.

type EventType

type EventType string

EventType represents the type of a Codex CLI JSONL event.

const (
	// EventThreadStarted is emitted when a new thread begins.
	EventThreadStarted EventType = "thread.started"

	// EventTurnStarted is emitted at the start of each agent turn.
	EventTurnStarted EventType = "turn.started"

	// EventTurnCompleted is emitted when a turn finishes successfully.
	EventTurnCompleted EventType = "turn.completed"

	// EventTurnFailed is emitted when a turn fails.
	EventTurnFailed EventType = "turn.failed"

	// EventItemStarted is emitted when an item begins processing.
	EventItemStarted EventType = "item.started"

	// EventItemUpdated is emitted when an item is incrementally updated.
	EventItemUpdated EventType = "item.updated"

	// EventItemCompleted is emitted when an item finishes processing.
	EventItemCompleted EventType = "item.completed"

	// EventError is emitted for top-level errors.
	EventError EventType = "error"
)

type FileChange

type FileChange struct {
	Path string `json:"path"`
	Kind string `json:"kind"`
}

FileChange represents a single file modification.

func (*FileChange) UnmarshalJSON

func (f *FileChange) UnmarshalJSON(data []byte) error

UnmarshalJSON accepts the current file-change kind shape.

type InputImageBlock added in v0.0.6

type InputImageBlock struct {
	Type     string `json:"type"`
	URL      string `json:"url,omitempty"`
	ImageURL string `json:"image_url,omitempty"`
}

InputImageBlock contains an app-server image input reference.

func (*InputImageBlock) BlockType added in v0.0.6

func (b *InputImageBlock) BlockType() string

BlockType implements the ContentBlock interface.

func (*InputImageBlock) MarshalJSON added in v0.0.6

func (b *InputImageBlock) MarshalJSON() ([]byte, error)

MarshalJSON emits `url`, which matches the turn/start request schema.

func (*InputImageBlock) UnmarshalJSON added in v0.0.6

func (b *InputImageBlock) UnmarshalJSON(data []byte) error

UnmarshalJSON accepts both `url` and `image_url` field names used across Codex app-server schemas.

type InputLocalImageBlock added in v0.0.6

type InputLocalImageBlock struct {
	Type string `json:"type"`
	Path string `json:"path"`
}

InputLocalImageBlock contains a local image-path input reference.

func (*InputLocalImageBlock) BlockType added in v0.0.6

func (b *InputLocalImageBlock) BlockType() string

BlockType implements the ContentBlock interface.

func (*InputLocalImageBlock) MarshalJSON added in v0.0.6

func (b *InputLocalImageBlock) MarshalJSON() ([]byte, error)

MarshalJSON emits local_image, which matches the live app-server transport.

func (*InputLocalImageBlock) UnmarshalJSON added in v0.0.6

func (b *InputLocalImageBlock) UnmarshalJSON(data []byte) error

UnmarshalJSON accepts both localImage and local_image spellings.

type InputMentionBlock added in v0.0.6

type InputMentionBlock struct {
	Type string `json:"type"`
	Name string `json:"name"`
	Path string `json:"path"`
}

InputMentionBlock contains a generic local path mention.

func (*InputMentionBlock) BlockType added in v0.0.6

func (b *InputMentionBlock) BlockType() string

BlockType implements the ContentBlock interface.

type ItemType

type ItemType string

ItemType represents the type of an item in an event.

const (
	// ItemTypeAgentMessage is a text response from the agent.
	ItemTypeAgentMessage ItemType = "agent_message"
	// ItemTypeReasoning is internal chain-of-thought reasoning.
	ItemTypeReasoning ItemType = "reasoning"
	// ItemTypeCommandExec is a shell command execution.
	ItemTypeCommandExec ItemType = "command_execution"
	// ItemTypeFileChange is a file modification.
	ItemTypeFileChange ItemType = "file_change"
	// ItemTypeMCPToolCall is a Model Context Protocol tool invocation.
	ItemTypeMCPToolCall ItemType = "mcp_tool_call"
	// ItemTypeDynamicToolCall is an SDK dynamic tool invocation.
	ItemTypeDynamicToolCall ItemType = "dynamic_tool_call"
	// ItemTypeWebSearch is a web search operation.
	ItemTypeWebSearch ItemType = "web_search"
	// ItemTypeTodoList is a todo list update.
	ItemTypeTodoList ItemType = "todo_list"
	// ItemTypeError is an error item.
	ItemTypeError ItemType = "error"
	// ItemTypeToolSearch is a tool search operation.
	ItemTypeToolSearch ItemType = "tool_search_call"
	// ItemTypeImageGeneration is an image generation operation.
	ItemTypeImageGeneration ItemType = "image_generation_call"
	// ItemTypeCustomToolCall is a custom tool call.
	ItemTypeCustomToolCall ItemType = "custom_tool_call"
)

type Message

type Message interface {
	MessageType() string
}

Message represents any message in the conversation. Use type assertion or type switch to determine the concrete type.

func Parse

func Parse(log *slog.Logger, payload any) (Message, error)

Parse converts a raw JSON map into a typed Message.

This function handles both Claude-style messages (with "type": "user"|"assistant"|etc.) and Codex-style events (with "type": "thread.started"|"item.completed"|etc.).

type ResultMessage

type ResultMessage struct {
	Type             string         `json:"type"`
	Subtype          string         `json:"subtype"`
	IsError          bool           `json:"is_error"`
	SessionID        string         `json:"session_id"`
	StopReason       *string        `json:"stop_reason,omitempty"`
	DurationMs       int            `json:"duration_ms"`
	NumTurns         int            `json:"num_turns"`
	TotalCostUSD     *float64       `json:"total_cost_usd,omitempty"`
	Usage            *Usage         `json:"usage,omitempty"`
	Result           *string        `json:"result,omitempty"`
	StructuredOutput any            `json:"structured_output,omitempty"`
	Audit            *AuditEnvelope `json:"-"`
}

ResultMessage represents the final result of a query.

func (*ResultMessage) MessageType

func (m *ResultMessage) MessageType() string

MessageType implements the Message interface.

type StreamEvent

type StreamEvent struct {
	UUID            string         `json:"uuid"`
	SessionID       string         `json:"session_id"`
	Event           map[string]any `json:"event"`
	ParentToolUseID *string        `json:"parent_tool_use_id,omitempty"`
	Audit           *AuditEnvelope `json:"-"`
}

StreamEvent represents a streaming event from the API.

func (*StreamEvent) MessageType

func (m *StreamEvent) MessageType() string

MessageType implements the Message interface.

type StreamingMessage

type StreamingMessage struct {
	Type            string                  `json:"type"`
	Message         StreamingMessageContent `json:"message"`
	ParentToolUseID *string                 `json:"parent_tool_use_id,omitempty"`
	SessionID       string                  `json:"session_id,omitempty"`
}

StreamingMessage represents a message sent via stdin in streaming mode.

type StreamingMessageContent

type StreamingMessageContent struct {
	Role    string             `json:"role"`
	Content UserMessageContent `json:"content"`
}

StreamingMessageContent represents the content of a streaming message.

type SystemMessage

type SystemMessage struct {
	Type    string         `json:"type"`
	Subtype string         `json:"subtype,omitempty"`
	Data    map[string]any `json:"data,omitempty"`
	Audit   *AuditEnvelope `json:"-"`
}

SystemMessage represents a system message.

func (*SystemMessage) MessageType

func (m *SystemMessage) MessageType() string

MessageType implements the Message interface.

type TaskCompleteMessage added in v0.0.2

type TaskCompleteMessage struct {
	SystemMessage
	TurnID           string  `json:"turn_id"`
	LastAgentMessage *string `json:"last_agent_message,omitempty"`
}

TaskCompleteMessage is emitted when a Codex turn completes.

type TaskStartedMessage added in v0.0.2

type TaskStartedMessage struct {
	SystemMessage
	TurnID                string `json:"turn_id"`
	CollaborationModeKind string `json:"collaboration_mode_kind,omitempty"`
	ModelContextWindow    *int64 `json:"model_context_window,omitempty"`
}

TaskStartedMessage is emitted when a Codex turn starts.

type TextBlock

type TextBlock struct {
	Type string `json:"type"`
	Text string `json:"text"`
}

TextBlock contains plain text content.

func (*TextBlock) BlockType

func (b *TextBlock) BlockType() string

BlockType implements the ContentBlock interface.

type ThinkingBlock

type ThinkingBlock struct {
	Type      string `json:"type"`
	Thinking  string `json:"thinking"`
	Signature string `json:"signature"`
}

ThinkingBlock contains the agent's thinking process.

func (*ThinkingBlock) BlockType

func (b *ThinkingBlock) BlockType() string

BlockType implements the ContentBlock interface.

type ThreadRolledBackMessage added in v0.0.2

type ThreadRolledBackMessage struct {
	SystemMessage
	NumTurns int `json:"num_turns"`
}

ThreadRolledBackMessage is emitted when a thread is rolled back.

type TodoItem

type TodoItem struct {
	Text      string `json:"text"`
	Completed bool   `json:"completed"`
}

TodoItem represents an item in a todo list.

type ToolResultBlock

type ToolResultBlock struct {
	Type      string         `json:"type"`
	ToolUseID string         `json:"tool_use_id"`
	Content   []ContentBlock `json:"content,omitempty"`
	IsError   bool           `json:"is_error,omitempty"`
}

ToolResultBlock contains the result of a tool execution.

func (*ToolResultBlock) BlockType

func (b *ToolResultBlock) BlockType() string

BlockType implements the ContentBlock interface.

func (*ToolResultBlock) UnmarshalJSON

func (b *ToolResultBlock) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler for ToolResultBlock.

type ToolUseBlock

type ToolUseBlock struct {
	Type  string         `json:"type"`
	ID    string         `json:"id"`
	Name  string         `json:"name"`
	Input map[string]any `json:"input"`
}

ToolUseBlock represents the agent using a tool.

func (*ToolUseBlock) BlockType

func (b *ToolUseBlock) BlockType() string

BlockType implements the ContentBlock interface.

type UnknownBlock added in v0.0.6

type UnknownBlock struct {
	Type string         `json:"type"`
	Raw  map[string]any `json:"raw,omitempty"`
}

UnknownBlock preserves unrecognized content block payloads without failing parsing.

func (*UnknownBlock) BlockType added in v0.0.6

func (b *UnknownBlock) BlockType() string

BlockType implements the ContentBlock interface.

func (*UnknownBlock) MarshalJSON added in v0.0.6

func (b *UnknownBlock) MarshalJSON() ([]byte, error)

MarshalJSON preserves the original block payload for round-tripping.

type Usage

type Usage struct {
	InputTokens           int `json:"input_tokens"`
	OutputTokens          int `json:"output_tokens"`
	CachedInputTokens     int `json:"cached_input_tokens"`
	ReasoningOutputTokens int `json:"reasoning_output_tokens"`
}

Usage contains token usage information.

type UserMessage

type UserMessage struct {
	Type            string             `json:"type"`
	Content         UserMessageContent `json:"content"`
	UUID            *string            `json:"uuid,omitempty"`
	ParentToolUseID *string            `json:"parent_tool_use_id,omitempty"`
	ToolUseResult   map[string]any     `json:"tool_use_result,omitempty"`
	Audit           *AuditEnvelope     `json:"-"`
}

UserMessage represents a message from the user.

func (*UserMessage) MessageType

func (m *UserMessage) MessageType() string

MessageType implements the Message interface.

type UserMessageContent

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

UserMessageContent represents content that can be either a string or []ContentBlock.

func NewUserMessageContent

func NewUserMessageContent(text string) UserMessageContent

NewUserMessageContent creates UserMessageContent from a string.

func NewUserMessageContentBlocks

func NewUserMessageContentBlocks(blocks []ContentBlock) UserMessageContent

NewUserMessageContentBlocks creates UserMessageContent from blocks.

func (*UserMessageContent) Blocks

func (c *UserMessageContent) Blocks() []ContentBlock

Blocks returns content as []ContentBlock (normalizes string to TextBlock).

func (*UserMessageContent) IsString

func (c *UserMessageContent) IsString() bool

IsString returns true if content was originally a string.

func (UserMessageContent) MarshalJSON

func (c UserMessageContent) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*UserMessageContent) String

func (c *UserMessageContent) String() string

String returns the string content if it was originally a string.

func (*UserMessageContent) UnmarshalJSON

func (c *UserMessageContent) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

Jump to

Keyboard shortcuts

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