message

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 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"
	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 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 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 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"`
	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 string `json:"content"` // The message text
}

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

func (*ThinkingBlock) BlockType

func (b *ThinkingBlock) 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. Handles both string content and array content.

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 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 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. 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. Accepts both string and array of content blocks.

Jump to

Keyboard shortcuts

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