message

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2026 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package message provides internal message and content block types.

Index

Constants

View Source
const (
	BlockTypeText       = "text"
	BlockTypeImage      = "image"
	BlockTypeImageURL   = "image_url"
	BlockTypeFile       = "file"
	BlockTypeInputAudio = "input_audio"
	BlockTypeVideoURL   = "video_url"
	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 model.

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 ImageBlock

type ImageBlock struct {
	Type      string `json:"type"`
	URL       string `json:"url"`
	MediaType string `json:"media_type,omitempty"`
}

ImageBlock contains a generated image reference.

func (*ImageBlock) BlockType

func (b *ImageBlock) BlockType() string

BlockType implements the ContentBlock interface.

func (*ImageBlock) Decode

func (b *ImageBlock) Decode() ([]byte, string, error)

Decode returns the raw bytes and media type for data-url-backed image blocks.

func (*ImageBlock) FileExtension

func (b *ImageBlock) FileExtension() string

FileExtension returns a best-effort file extension for the image.

func (*ImageBlock) Save

func (b *ImageBlock) Save(path string) error

Save writes a data-url-backed image to disk.

type InputAudioBlock

type InputAudioBlock struct {
	Type       string        `json:"type"`
	InputAudio InputAudioRef `json:"input_audio"`
}

InputAudioBlock contains an input audio payload for multimodal prompts.

func (*InputAudioBlock) BlockType

func (b *InputAudioBlock) BlockType() string

BlockType implements the ContentBlock interface.

type InputAudioRef

type InputAudioRef struct {
	Data   string `json:"data"`
	Format string `json:"format"`
}

InputAudioRef contains base64-encoded input audio plus its format.

type InputFileBlock

type InputFileBlock struct {
	Type string       `json:"type"`
	File InputFileRef `json:"file"`
}

InputFileBlock contains an input file reference for multimodal prompts.

func (*InputFileBlock) BlockType

func (b *InputFileBlock) BlockType() string

BlockType implements the ContentBlock interface.

type InputFileRef

type InputFileRef struct {
	Filename string `json:"filename,omitempty"`
	FileData string `json:"file_data"`
}

InputFileRef identifies an input file URL or data URL.

type InputImageBlock

type InputImageBlock struct {
	Type     string        `json:"type"`
	ImageURL InputImageRef `json:"image_url"`
}

InputImageBlock contains an input image reference for multimodal prompts.

func (*InputImageBlock) BlockType

func (b *InputImageBlock) BlockType() string

BlockType implements the ContentBlock interface.

type InputImageRef

type InputImageRef struct {
	URL string `json:"url"`
}

InputImageRef identifies an input image URL or data URL.

type InputVideoBlock

type InputVideoBlock struct {
	Type     string        `json:"type"`
	VideoURL InputVideoRef `json:"video_url"`
}

InputVideoBlock contains an input video reference for multimodal prompts.

func (*InputVideoBlock) BlockType

func (b *InputVideoBlock) BlockType() string

BlockType implements the ContentBlock interface.

type InputVideoRef

type InputVideoRef struct {
	URL string `json:"url"`
}

InputVideoRef identifies an input video URL or data URL.

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.

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 raw streaming event payload.

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 in the SDK's streaming input format.

type StreamingMessageContent

type StreamingMessageContent struct {
	Role    string             `json:"role"`    // "user"
	Content UserMessageContent `json:"content"` // The message 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 model reasoning text.

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 a model tool call.

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) HasBlocks

func (c *UserMessageContent) HasBlocks() bool

HasBlocks reports whether the content is represented as a block array.

func (*UserMessageContent) HasNonTextBlocks

func (c *UserMessageContent) HasNonTextBlocks() bool

HasNonTextBlocks reports whether the content contains multimodal input blocks.

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 the concatenated text blocks.

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