message

package
v0.0.5 Latest Latest
Warning

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

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

Documentation

Overview

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

Index

Constants

View Source
const (
	BlockTypeText       = "text"
	BlockTypeImage      = "image"
	BlockTypeDocument   = "document"
	BlockTypeThinking   = "thinking"
	BlockTypeToolUse    = "tool_use"
	BlockTypeToolResult = "tool_result"
	BlockTypeToolRef    = "tool_reference"
)

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

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 Base64Source added in v0.0.5

type Base64Source struct {
	Type      string `json:"type"`
	MediaType string `json:"media_type"`
	Data      string `json:"data"`
}

Base64Source contains inline base64 content for Claude CLI multimodal inputs.

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 InputDocumentBlock added in v0.0.5

type InputDocumentBlock struct {
	Type   string       `json:"type"`
	Source Base64Source `json:"source"`
}

InputDocumentBlock contains an inline PDF document input for multimodal prompts.

func (*InputDocumentBlock) BlockType added in v0.0.5

func (b *InputDocumentBlock) BlockType() string

BlockType implements the ContentBlock interface.

type InputImageBlock added in v0.0.5

type InputImageBlock struct {
	Type   string       `json:"type"`
	Source Base64Source `json:"source"`
}

InputImageBlock contains an inline image input for multimodal prompts.

func (*InputImageBlock) BlockType added in v0.0.5

func (b *InputImageBlock) BlockType() string

BlockType implements the ContentBlock interface.

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.

The logger is used to log debug information about message parsing, including warnings for unknown message types or malformed data.

Returns an error if the message type is missing, invalid, or if parsing fails.

type ResultMessage

type ResultMessage struct {
	Type             string   `json:"type"`
	Subtype          string   `json:"subtype"`
	DurationMs       int      `json:"duration_ms"`
	DurationAPIMs    int      `json:"duration_api_ms"`
	IsError          bool     `json:"is_error"`
	NumTurns         int      `json:"num_turns"`
	SessionID        string   `json:"session_id"`
	StopReason       *string  `json:"stop_reason,omitempty"`
	TotalCostUSD     *float64 `json:"total_cost_usd,omitempty"`
	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"` // Raw Anthropic API event
	ParentToolUseID *string        `json:"parent_tool_use_id,omitempty"`
}

StreamEvent represents a streaming event from the Claude API.

func (*StreamEvent) MessageType

func (m *StreamEvent) MessageType() string

MessageType implements the Message interface.

type StreamingMessage

type StreamingMessage struct {
	Type            string                  `json:"type"`                         // "user"
	Message         StreamingMessageContent `json:"message"`                      // The message content
	ParentToolUseID *string                 `json:"parent_tool_use_id,omitempty"` // Optional parent tool use ID
	SessionID       string                  `json:"session_id,omitempty"`         // Optional session ID
}

StreamingMessage represents a message sent via stdin in streaming mode. This is used with --input-format stream-json to send messages to the CLI.

type StreamingMessageContent

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

StreamingMessageContent represents the content of a streaming message.

type StructuredOutputTracker added in v0.0.2

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

StructuredOutputTracker captures StructuredOutput tool payloads so they can be promoted onto the final ResultMessage for compatibility with callers that expect structured output there.

func NewStructuredOutputTracker added in v0.0.2

func NewStructuredOutputTracker() *StructuredOutputTracker

NewStructuredOutputTracker creates a tracker for raw Claude CLI messages.

func (*StructuredOutputTracker) ObserveRaw added in v0.0.2

func (t *StructuredOutputTracker) ObserveRaw(raw map[string]any)

ObserveRaw records StructuredOutput tool invocations from raw assistant messages keyed by session ID.

func (*StructuredOutputTracker) PopulateResult added in v0.0.2

func (t *StructuredOutputTracker) PopulateResult(result *ResultMessage)

PopulateResult copies a tracked structured output payload onto a result message when the CLI emitted it through the StructuredOutput tool instead of directly on the result envelope.

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 TaskNotificationMessage added in v0.0.2

type TaskNotificationMessage struct {
	SystemMessage
	TaskID     string                 `json:"task_id"`
	Status     TaskNotificationStatus `json:"status"`
	OutputFile string                 `json:"output_file"`
	Summary    string                 `json:"summary"`
	UUID       string                 `json:"uuid"`
	SessionID  string                 `json:"session_id"`
	ToolUseID  *string                `json:"tool_use_id,omitempty"`
	Usage      *TaskUsage             `json:"usage,omitempty"`
}

TaskNotificationMessage is emitted when a task completes, fails, or is stopped.

type TaskNotificationStatus added in v0.0.2

type TaskNotificationStatus string

TaskNotificationStatus represents the final status of a task.

const (
	// TaskNotificationStatusCompleted indicates the task finished successfully.
	TaskNotificationStatusCompleted TaskNotificationStatus = "completed"
	// TaskNotificationStatusFailed indicates the task failed.
	TaskNotificationStatusFailed TaskNotificationStatus = "failed"
	// TaskNotificationStatusStopped indicates the task was stopped by control request.
	TaskNotificationStatusStopped TaskNotificationStatus = "stopped"
)

type TaskProgressMessage added in v0.0.2

type TaskProgressMessage struct {
	SystemMessage
	TaskID       string    `json:"task_id"`
	Description  string    `json:"description"`
	Usage        TaskUsage `json:"usage"`
	UUID         string    `json:"uuid"`
	SessionID    string    `json:"session_id"`
	ToolUseID    *string   `json:"tool_use_id,omitempty"`
	LastToolName *string   `json:"last_tool_name,omitempty"`
}

TaskProgressMessage is emitted while a task is in progress.

type TaskStartedMessage added in v0.0.2

type TaskStartedMessage struct {
	SystemMessage
	TaskID      string  `json:"task_id"`
	Description string  `json:"description"`
	UUID        string  `json:"uuid"`
	SessionID   string  `json:"session_id"`
	ToolUseID   *string `json:"tool_use_id,omitempty"`
	TaskType    *string `json:"task_type,omitempty"`
}

TaskStartedMessage is emitted when a task starts.

type TaskUsage added in v0.0.2

type TaskUsage struct {
	TotalTokens int `json:"total_tokens"`
	ToolUses    int `json:"tool_uses"`
	DurationMs  int `json:"duration_ms"`
}

TaskUsage contains task usage statistics from task progress events.

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 Claude's thinking process.

func (*ThinkingBlock) BlockType

func (b *ThinkingBlock) BlockType() string

BlockType implements the ContentBlock interface.

type ToolReferenceBlock added in v0.0.2

type ToolReferenceBlock struct {
	Type     string `json:"type"`
	ToolName string `json:"tool_name"`
}

ToolReferenceBlock points to a deferred tool selected by Claude tool search.

func (*ToolReferenceBlock) BlockType added in v0.0.2

func (b *ToolReferenceBlock) BlockType() string

BlockType implements the ContentBlock interface.

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. Claude CLI tool results may arrive as a plain string or structured blocks.

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 Claude using a tool.

func (*ToolUseBlock) BlockType

func (b *ToolUseBlock) BlockType() string

BlockType implements the ContentBlock interface.

type UnknownBlock added in v0.0.2

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

func (b *UnknownBlock) BlockType() string

BlockType implements the ContentBlock interface.

func (*UnknownBlock) MarshalJSON added in v0.0.2

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

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 Claude CLI user message content. Top-level user prompts are emitted as plain strings, while structured responses can use content blocks.

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. Outputs string if content is string, otherwise outputs array of blocks.

func (*UserMessageContent) String

func (c *UserMessageContent) String() string

String returns the string content if it was originally a string, or empty string.

func (*UserMessageContent) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler for the Claude CLI content shapes.

Jump to

Keyboard shortcuts

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