messages

package
v0.28.2 Latest Latest
Warning

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

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

Documentation

Overview

Package messages is a transport-agnostic codec for Anthropic's Messages API wire format (request, content-block response, and named-SSE-event streaming). It maps between contenox's neutral modelrepo types and Anthropic's JSON shape.

It does NO I/O. The transport (api.anthropic.com) supplies the envelope: model in the body, version via the `anthropic-version` header, auth via `x-api-key`. This lets the direct Anthropic provider stay a thin transport wrapper around the shared codec.

Index

Constants

View Source
const DefaultMaxTokens = 4096

DefaultMaxTokens is used when the caller does not set ChatConfig.MaxTokens. Anthropic requires max_tokens; it has no "unlimited" sentinel.

Variables

This section is empty.

Functions

func DecodeResponse

func DecodeResponse(raw []byte) (modelrepo.ChatResult, error)

DecodeResponse parses a non-streaming response into a neutral ChatResult.

Types

type OutputConfig

type OutputConfig struct {
	Effort string `json:"effort,omitempty"`
}

type Request

type Request struct {
	// Model is omitted for Vertex (model lives in the URL) and set for direct.
	Model string `json:"model,omitempty"`
	// AnthropicVersion is set by the Vertex transport ("vertex-2023-10-16");
	// empty for direct (sent as a header instead).
	AnthropicVersion string          `json:"anthropic_version,omitempty"`
	MaxTokens        int             `json:"max_tokens"`
	System           string          `json:"system,omitempty"`
	Messages         []wireMessage   `json:"messages"`
	Temperature      *float64        `json:"temperature,omitempty"`
	TopP             *float64        `json:"top_p,omitempty"`
	Tools            []wireTool      `json:"tools,omitempty"`
	Thinking         *ThinkingConfig `json:"thinking,omitempty"`
	OutputConfig     *OutputConfig   `json:"output_config,omitempty"`
	Stream           bool            `json:"stream,omitempty"`
}

Request is the Anthropic Messages request body.

func Build

func Build(messages []modelrepo.Message, cfg *modelrepo.ChatConfig) Request

Build converts neutral messages + config into an Anthropic Messages Request. The transport must still set Model and/or AnthropicVersion per hosting mode.

type Response

type Response struct {
	Role       string          `json:"role"`
	Content    []responseBlock `json:"content"`
	StopReason string          `json:"stop_reason"`
}

Response is the non-streaming Anthropic Messages response body.

type StreamDecoder

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

StreamDecoder assembles a streamed Messages response. Text and thinking deltas are emitted as parcels; tool_use blocks are accumulated per index (id/name from content_block_start, arguments from input_json_delta) and exposed via ToolCalls() once the stream ends.

func NewStreamDecoder

func NewStreamDecoder() *StreamDecoder

func (*StreamDecoder) DecodeLine

func (d *StreamDecoder) DecodeLine(payload []byte) (*modelrepo.StreamParcel, error)

DecodeLine parses one SSE `data:` payload (bytes after "data: "). It returns a parcel if the event carried visible text/thinking, or nil otherwise.

func (*StreamDecoder) ToolCalls

func (d *StreamDecoder) ToolCalls() []modelrepo.ToolCall

ToolCalls returns the tool calls assembled across the stream, in index order.

type ThinkingConfig

type ThinkingConfig struct {
	Type         string `json:"type"`
	BudgetTokens int    `json:"budget_tokens,omitempty"`
	Display      string `json:"display,omitempty"`
}

Jump to

Keyboard shortcuts

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