message

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2026 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package message provides message and content block types for Codex conversations.

Index

Constants

View Source
const (
	BlockTypeText       = "text"
	BlockTypeThinking   = "thinking"
	BlockTypeToolUse    = "tool_use"
	BlockTypeToolResult = "tool_result"
)

Block type constants.

Variables

This section is empty.

Functions

This section is empty.

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"`
}

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 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"`

	// 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"`

	// 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.

type CodexUsage

type CodexUsage struct {
	InputTokens       int `json:"input_tokens"`
	CachedInputTokens int `json:"cached_input_tokens"`
	OutputTokens      int `json:"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 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 supports both legacy and newer app-server shapes for file-change kind. Some runtimes send a plain string ("create"), while newer payloads may send an object (for example {"type":"create"}).

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"
	// 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"
)

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, data map[string]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"`
	Usage            *Usage  `json:"usage,omitempty"`
	Result           *string `json:"result,omitempty"`
	StructuredOutput any     `json:"structured_output,omitempty"`
}

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"`
}

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 string `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"`
}

SystemMessage represents a system message.

func (*SystemMessage) MessageType

func (m *SystemMessage) MessageType() string

MessageType implements the Message interface.

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 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 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"`
}

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