message

package
v0.35.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarshalParts

func MarshalParts(parts []ContentPart) ([]byte, error)

MarshalParts serializes a slice of ContentPart to JSON using type-tagged wrappers. Each part becomes {"type":"...", "data":{...}}.

Types

type ContentPart

type ContentPart interface {
	// contains filtered or unexported methods
}

ContentPart is the marker interface for all message content block types. A message contains a heterogeneous slice of ContentPart values, enabling rich structured messages that carry text, reasoning, tool calls, tool results, and finish markers in a single message.

func UnmarshalParts

func UnmarshalParts(data []byte) ([]ContentPart, error)

UnmarshalParts deserializes type-tagged JSON back into a slice of ContentPart.

type Finish

type Finish struct {
	Reason string `json:"reason"` // "end_turn", "tool_use", "max_tokens", etc.
}

Finish marks the end of an assistant turn, carrying the stop reason.

type ImageContent added in v0.7.0

type ImageContent struct {
	Data      []byte `json:"data"`
	MediaType string `json:"media_type"`
}

ImageContent holds image data within a message. The data is stored as raw bytes (not base64-encoded); serialization handles encoding. MediaType is a MIME type such as "image/png" or "image/jpeg".

type Message

type Message struct {
	ID        string        `json:"id"`
	Role      MessageRole   `json:"role"`
	Parts     []ContentPart `json:"parts"`
	Model     string        `json:"model,omitempty"`
	Provider  string        `json:"provider,omitempty"`
	CreatedAt time.Time     `json:"created_at"`
	UpdatedAt time.Time     `json:"updated_at"`
}

Message is a single conversation message containing a heterogeneous slice of ContentPart blocks. This design enables a single assistant message to carry text, reasoning, and multiple tool calls as discrete, typed blocks rather than flattening everything into strings.

func FromFantasyMessage deprecated

func FromFantasyMessage(msg fantasy.Message) Message

Deprecated: Use FromLLMMessage instead. FromFantasyMessage converts an LLM message into our Message type, extracting all content parts into the appropriate block types.

func FromLLMMessage added in v0.30.0

func FromLLMMessage(msg fantasy.Message) Message

FromLLMMessage converts an LLM message into our Message type, extracting all content parts into the appropriate block types.

func (*Message) AddPart

func (m *Message) AddPart(part ContentPart)

AddPart appends a content part and updates the timestamp.

func (*Message) AddToolCall

func (m *Message) AddToolCall(tc ToolCall)

AddToolCall appends or updates a ToolCall part. If a call with the same ID already exists, it is replaced (supports streaming where partial calls arrive before the final version).

func (*Message) Content

func (m *Message) Content() string

Content returns the concatenated text from all TextContent parts.

func (*Message) Images added in v0.7.0

func (m *Message) Images() []ImageContent

Images returns all ImageContent parts from this message.

func (*Message) Reasoning

func (m *Message) Reasoning() ReasoningContent

Reasoning returns the ReasoningContent if present, or a zero value.

func (*Message) ToFantasyMessages deprecated

func (m *Message) ToFantasyMessages() []fantasy.Message

Deprecated: Use ToLLMMessages instead. ToFantasyMessages converts a Message to one or more LLM message values.

func (*Message) ToLLMMessages added in v0.30.0

func (m *Message) ToLLMMessages() []fantasy.Message

ToLLMMessages converts a Message to one or more LLM message values. An assistant message with tool calls produces a single message with mixed TextPart and ToolCallPart content. Tool-role messages produce ToolResultPart entries.

func (*Message) ToolCalls

func (m *Message) ToolCalls() []ToolCall

ToolCalls returns all ToolCall parts from this message.

func (*Message) ToolResults

func (m *Message) ToolResults() []ToolResult

ToolResults returns all ToolResult parts from this message.

type MessageRole

type MessageRole string

MessageRole identifies the sender of a message.

const (
	RoleUser      MessageRole = "user"
	RoleAssistant MessageRole = "assistant"
	RoleTool      MessageRole = "tool"
	RoleSystem    MessageRole = "system"
)

type ReasoningContent

type ReasoningContent struct {
	Thinking  string `json:"thinking"`
	Signature string `json:"signature,omitempty"` // Anthropic
}

ReasoningContent holds extended thinking / reasoning output from the LLM. Provider-specific metadata (signatures, etc.) is preserved for round-trip fidelity when the conversation is sent back to the provider.

type TextContent

type TextContent struct {
	Text string `json:"text"`
}

TextContent holds plain text content within a message.

type ToolCall

type ToolCall struct {
	ID       string `json:"id"`
	Name     string `json:"name"`
	Input    string `json:"input"` // JSON string of arguments
	Finished bool   `json:"finished"`
}

ToolCall represents a tool invocation initiated by the LLM. It is stored as a content part within an assistant message, not as a separate message.

type ToolResult

type ToolResult struct {
	ToolCallID string `json:"tool_call_id"`
	Name       string `json:"name"`
	Content    string `json:"content"`
	IsError    bool   `json:"is_error"`
}

ToolResult represents the result of executing a tool. It is stored as a content part within a tool-role message, linked to a ToolCall by ID.

Jump to

Keyboard shortcuts

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