anthropic

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package anthropic implements the llm.Codec interface for the Anthropic Messages API (https://docs.anthropic.com/en/api/messages).

It decomposes Messages API requests and responses into keep.Call objects for policy evaluation, and reassembles mutations back into the wire format. It also handles SSE stream reassembly and event synthesis for streaming responses.

This package is used internally by the gateway proxy and is also available to library consumers via the NewCodec constructor:

codec := anthropic.NewCodec()
result, err := llm.EvaluateRequest(engine, codec, body, scope, cfg)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Codec

type Codec struct{}

Codec implements llm.Codec for the Anthropic Messages API. It is stateless and safe for concurrent use.

func NewCodec

func NewCodec() *Codec

NewCodec returns a new Anthropic codec.

func (*Codec) DecomposeRequest

func (c *Codec) DecomposeRequest(body []byte, scope string, cfg llm.DecomposeConfig) ([]keep.Call, any, error)

DecomposeRequest parses an Anthropic request body and returns policy calls plus an opaque handle for ReassembleRequest.

func (*Codec) DecomposeResponse

func (c *Codec) DecomposeResponse(body []byte, scope string, cfg llm.DecomposeConfig) ([]keep.Call, any, error)

DecomposeResponse parses an Anthropic response body and returns policy calls plus an opaque handle for ReassembleResponse.

func (*Codec) ReassembleRequest

func (c *Codec) ReassembleRequest(handle any, results []keep.EvalResult) ([]byte, error)

ReassembleRequest patches mutations from eval results back into the request. Returns the original body if no mutations are present.

func (*Codec) ReassembleResponse

func (c *Codec) ReassembleResponse(handle any, results []keep.EvalResult) ([]byte, error)

ReassembleResponse patches mutations from eval results back into the response. Returns the original body if no mutations are present.

func (*Codec) ReassembleStream

func (c *Codec) ReassembleStream(events []sse.Event) ([]byte, error)

ReassembleStream reassembles SSE events into a complete response body.

func (*Codec) SynthesizeEvents

func (c *Codec) SynthesizeEvents(patchedBody []byte) ([]sse.Event, error)

SynthesizeEvents creates replacement SSE events from a patched response body.

type ContentBlock

type ContentBlock struct {
	Type      string         `json:"type"` // "text", "tool_use", "tool_result", "image"
	Text      string         `json:"text,omitempty"`
	ID        string         `json:"id,omitempty"`          // tool_use ID
	Name      string         `json:"name,omitempty"`        // tool_use name
	Input     map[string]any `json:"input,omitempty"`       // tool_use input
	ToolUseID string         `json:"tool_use_id,omitempty"` // tool_result reference to tool_use ID
	Content   any            `json:"content,omitempty"`     // tool_result content: string or []ContentBlock
	IsError   bool           `json:"is_error,omitempty"`    // tool_result error flag
}

ContentBlock represents a single content block in a message or response.

func (*ContentBlock) ToolResultContent

func (b *ContentBlock) ToolResultContent() string

ToolResultContent returns the text content of a tool_result block. It handles both string and array-of-blocks content formats.

type Message

type Message struct {
	Role    string `json:"role"`    // "user" or "assistant"
	Content any    `json:"content"` // string or []ContentBlock
}

Message is a single turn in a conversation.

func (*Message) ContentBlocks

func (m *Message) ContentBlocks() []ContentBlock

ContentBlocks extracts content blocks from a Message. If Content is a string, it returns a single text block. If Content is a []ContentBlock (or a []any from JSON unmarshal), it returns the parsed blocks.

type MessagesRequest

type MessagesRequest struct {
	Model     string    `json:"model"`
	System    any       `json:"system,omitempty"` // string or []ContentBlock
	Messages  []Message `json:"messages"`
	MaxTokens int       `json:"max_tokens"`
	Stream    bool      `json:"stream,omitempty"`
	Tools     any       `json:"tools,omitempty"`    // passthrough
	Metadata  any       `json:"metadata,omitempty"` // passthrough
}

MessagesRequest is the Anthropic /v1/messages request body.

type MessagesResponse

type MessagesResponse struct {
	ID         string         `json:"id"`
	Type       string         `json:"type"` // "message"
	Role       string         `json:"role"` // "assistant"
	Content    []ContentBlock `json:"content"`
	Model      string         `json:"model"`
	StopReason string         `json:"stop_reason"` // "end_turn", "tool_use", etc.
	Usage      *Usage         `json:"usage,omitempty"`
}

MessagesResponse is the Anthropic /v1/messages response body.

type Usage

type Usage struct {
	InputTokens  int `json:"input_tokens"`
	OutputTokens int `json:"output_tokens"`
}

Usage tracks token consumption for a request/response pair.

Jump to

Keyboard shortcuts

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