message

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package message defines the core message types used in AI conversations. This mirrors the ai-sdk ModelMessage types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarshalOutput added in v0.1.4

func MarshalOutput(o ToolResultOutput) (json.RawMessage, error)

MarshalOutput serializes a ToolResultOutput as a self-describing object (with the "type" discriminant). Exported for stream-event and wire serializers that embed an Output outside a ToolResultPart.

func ToolOutcome added in v0.1.4

func ToolOutcome(o ToolResultOutput) string

ToolOutcome classifies a ToolResultOutput as "success", "error" or "denied" — the persisted, heuristic-free tool status.

func ToolOutputIsError added in v0.1.4

func ToolOutputIsError(o ToolResultOutput) bool

ToolOutputIsError reports whether the output is an error variant — the single source of truth for a provider's wire is_error flag.

func ToolOutputText added in v0.1.4

func ToolOutputText(o ToolResultOutput) string

ToolOutputText renders any ToolResultOutput to a single string — the model-facing text for that result. JSON variants are compact-encoded; execution-denied falls back to a default sentence when Reason is empty; content concatenates its text items.

func ToolOutputWire added in v0.1.4

func ToolOutputWire(o ToolResultOutput) string

ToolOutputWire renders a ToolResultOutput to the single string most providers put in their tool message (no is_error field): text/error-text → value; execution-denied → reason or default; json/error-json/content → compact JSON. Mirrors ai-sdk's convert-to-openai-chat-messages behavior.

Types

type Content

type Content struct {
	Text  string // Simple text content
	Parts []Part // Multi-part content
}

Content represents message content - either a string or parts.

func (Content) IsMultiPart

func (c Content) IsMultiPart() bool

IsMultiPart returns true if content has multiple parts.

func (Content) MarshalJSON

func (c Content) MarshalJSON() ([]byte, error)

MarshalJSON serializes Content. Text-only content serializes as a plain string. Multi-part content serializes as an array of typed objects.

func (*Content) UnmarshalJSON

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

UnmarshalJSON deserializes Content from either a plain string or a typed array.

type ContentOutput added in v0.1.4

type ContentOutput struct {
	Value []ToolContentItem `json:"value"`
}

ContentOutput is a multipart successful result (type:"content"): text and file/image items. Mirrors ai-sdk's content variant inner union.

type ContentType

type ContentType string

ContentType represents the type of content in a message part.

const (
	ContentTypeText                 ContentType = "text"
	ContentTypeFile                 ContentType = "file"
	ContentTypeToolCall             ContentType = "tool-call"
	ContentTypeToolResult           ContentType = "tool-result"
	ContentTypeReasoning            ContentType = "reasoning"
	ContentTypeToolApprovalRequest  ContentType = "tool-approval-request"
	ContentTypeToolApprovalResponse ContentType = "tool-approval-response"
)

type ErrorJSONOutput added in v0.1.4

type ErrorJSONOutput struct {
	Value           any            `json:"value"`
	ProviderOptions map[string]any `json:"providerOptions,omitempty"`
}

ErrorJSONOutput is a failed tool execution with a structured payload (type:"error-json"). Drives the provider wire is_error flag.

type ErrorTextOutput added in v0.1.4

type ErrorTextOutput struct {
	Value           string         `json:"value"`
	ProviderOptions map[string]any `json:"providerOptions,omitempty"`
}

ErrorTextOutput is a failed tool execution with a text message (type:"error-text"). Drives the provider wire is_error flag.

type ExecutionDeniedOutput added in v0.1.4

type ExecutionDeniedOutput struct {
	Reason          string         `json:"reason,omitempty"`
	ProviderOptions map[string]any `json:"providerOptions,omitempty"`
}

ExecutionDeniedOutput is a tool call the user/policy refused to run (type:"execution-denied"). Distinct from an error so the agent re-reasons and the UI can show it differently. Reason is optional.

type FileData added in v0.1.4

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

FileData is the tagged-union payload of a FilePart: the concrete variant says how the file bytes are carried. JSON encodes the variant as the nested "type" field. Mirrors ai-sdk's file part data union (references/ai-sdk/packages/provider-utils/src/types/content-part.ts).

type FileDataBytes added in v0.1.4

type FileDataBytes struct {
	Data string `json:"data"` // base64-encoded
}

FileDataBytes carries inline base64-encoded bytes (type:"data").

type FileDataReference added in v0.1.4

type FileDataReference struct {
	Reference map[string]any `json:"reference"`
}

FileDataReference is a provider-side handle to a previously uploaded file (type:"reference"), e.g. an OpenAI/Anthropic Files API id.

type FileDataText added in v0.1.4

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

FileDataText carries the file content as text (type:"text").

type FileDataURL added in v0.1.4

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

FileDataURL points to a remote file the provider fetches (type:"url").

type FilePart

type FilePart struct {
	Data            FileData       `json:"data"`
	MimeType        string         `json:"mimeType"`           // e.g., "application/pdf", "image/png"
	Filename        string         `json:"filename,omitempty"` // optional filename
	ProviderOptions map[string]any `json:"providerOptions,omitempty"`
}

FilePart represents file content, including images. Data is a tagged union (inline bytes / remote URL / provider reference / text) mirroring ai-sdk's unified FilePart. Images are file parts whose MimeType has an "image/" prefix.

func (FilePart) MarshalJSON added in v0.1.4

func (p FilePart) MarshalJSON() ([]byte, error)

MarshalJSON serializes the part, encoding the nested data union with its "type" discriminant.

func (*FilePart) UnmarshalJSON added in v0.1.4

func (p *FilePart) UnmarshalJSON(data []byte) error

UnmarshalJSON deserializes the part, decoding the discriminated data union.

type JSONOutput added in v0.1.4

type JSONOutput struct {
	Value           any            `json:"value"`
	ProviderOptions map[string]any `json:"providerOptions,omitempty"`
}

JSONOutput is a successful structured result (type:"json").

type Message

type Message struct {
	Role            Role           `json:"role"`
	Content         Content        `json:"content"`
	ProviderOptions map[string]any `json:"providerOptions,omitempty"`
}

Message represents a single message in a conversation.

func NewAssistantMessage

func NewAssistantMessage(text string) Message

NewAssistantMessage creates an assistant message.

func NewAssistantMessageWithParts

func NewAssistantMessageWithParts(parts ...Part) Message

NewAssistantMessageWithParts creates an assistant message with parts (e.g., tool calls).

func NewSystemMessage

func NewSystemMessage(text string) Message

NewSystemMessage creates a system message.

func NewToolMessage

func NewToolMessage(toolCallID, toolName string, output ToolResultOutput) Message

NewToolMessage creates a tool result message wrapping the given discriminated output (success / error / denied).

func NewToolResultDenied added in v0.1.4

func NewToolResultDenied(toolCallID, toolName, reason string) Message

NewToolResultDenied creates a tool result message marking the call as execution-denied. reason may be empty.

func NewToolResultJSON added in v0.1.4

func NewToolResultJSON(toolCallID, toolName string, value any) Message

NewToolResultJSON creates a tool result message with a structured success output.

func NewToolResultText added in v0.1.4

func NewToolResultText(toolCallID, toolName, value string) Message

NewToolResultText creates a tool result message with a text success output.

func NewUserMessage

func NewUserMessage(text string) Message

NewUserMessage creates a user message with text.

func NewUserMessageWithParts

func NewUserMessageWithParts(parts ...Part) Message

NewUserMessageWithParts creates a user message with multiple parts.

type Part

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

Part represents a single part of message content.

type ReasoningPart

type ReasoningPart struct {
	Text            string         `json:"text"`
	ProviderOptions map[string]any `json:"providerOptions,omitempty"`
}

ReasoningPart represents reasoning/thinking content from the model. Source: ai-sdk/packages/provider-utils/src/message.ts

type Role

type Role string

Role represents the role of a message sender.

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

type TextOutput added in v0.1.4

type TextOutput struct {
	Value           string         `json:"value"`
	ProviderOptions map[string]any `json:"providerOptions,omitempty"`
}

TextOutput is a successful text result (type:"text").

type TextPart

type TextPart struct {
	Text            string         `json:"text"`
	ProviderOptions map[string]any `json:"providerOptions,omitempty"`
}

TextPart represents text content.

type ToolApprovalRequestPart

type ToolApprovalRequestPart struct {
	ApprovalID      string         `json:"approvalId"`
	ToolCallID      string         `json:"toolCallId"`
	ToolName        string         `json:"toolName"`
	Input           any            `json:"input"`
	ProviderOptions map[string]any `json:"providerOptions,omitempty"`
}

ToolApprovalRequestPart represents a request for tool execution approval. Source: ai-sdk/packages/provider-utils/src/message.ts

type ToolApprovalResponsePart

type ToolApprovalResponsePart struct {
	ApprovalID       string         `json:"approvalId"`
	Approved         bool           `json:"approved"`
	Reason           string         `json:"reason,omitempty"`
	ProviderExecuted bool           `json:"providerExecuted,omitempty"`
	ProviderOptions  map[string]any `json:"providerOptions,omitempty"`
}

ToolApprovalResponsePart represents a response to tool execution approval. Source: ai-sdk/packages/provider-utils/src/message.ts

type ToolCallPart

type ToolCallPart struct {
	ID              string          `json:"toolCallId"`
	Name            string          `json:"toolName"`
	Input           json.RawMessage `json:"args"`
	ProviderOptions map[string]any  `json:"providerOptions,omitempty"`
}

ToolCallPart represents a tool invocation by the assistant.

type ToolContentItem added in v0.1.4

type ToolContentItem struct {
	Type              string            `json:"type"`
	Text              string            `json:"text,omitempty"`
	Data              string            `json:"data,omitempty"`      // file-data / image-data: base64
	MediaType         string            `json:"mediaType,omitempty"` // file-data / file-url
	Filename          string            `json:"filename,omitempty"`  // file-data
	URL               string            `json:"url,omitempty"`       // file-url / image-url
	FileID            any               `json:"fileId,omitempty"`    // string | map[string]string
	ProviderReference map[string]string `json:"providerReference,omitempty"`
	ProviderOptions   map[string]any    `json:"providerOptions,omitempty"`
}

ToolContentItem is one item of a ContentOutput. Type discriminates which fields are meaningful, mirroring ai-sdk's inner union (text | file-data | file-url | file-id | file-reference | image-data | image-url | image-file-id | image-file-reference | custom).

type ToolResultOutput added in v0.1.4

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

ToolResultOutput is the discriminated output of a tool result, mirroring ai-sdk's ToolResultOutput union. The Go concrete type is the discriminant; JSON carries it as the nested "type" field.

func UnmarshalOutput added in v0.1.4

func UnmarshalOutput(raw json.RawMessage) (ToolResultOutput, error)

UnmarshalOutput decodes a ToolResultOutput produced by MarshalOutput.

type ToolResultPart

type ToolResultPart struct {
	ToolCallID       string           `json:"toolCallId"`
	ToolName         string           `json:"toolName"`
	Output           ToolResultOutput `json:"output"`
	ProviderExecuted bool             `json:"providerExecuted,omitempty"`
	ProviderOptions  map[string]any   `json:"providerOptions,omitempty"`
}

ToolResultPart represents the result of a tool invocation. Output is a discriminated union mirroring ai-sdk's ToolResultOutput (references/ai-sdk/ packages/provider-utils/src/types/content-part.ts): the concrete variant is the outcome (success text/json/content vs error vs execution-denied).

func (ToolResultPart) MarshalJSON added in v0.1.4

func (p ToolResultPart) MarshalJSON() ([]byte, error)

MarshalJSON serializes the part, injecting the discriminated "type" into the nested output object.

func (*ToolResultPart) UnmarshalJSON added in v0.1.4

func (p *ToolResultPart) UnmarshalJSON(data []byte) error

UnmarshalJSON deserializes the part. The output is the discriminated ToolResultOutput union; a missing or malformed output is a hard error (fail loud — the data migration converts all history to this shape, so there is no legacy {result,isError} path to tolerate).

Jump to

Keyboard shortcuts

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