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 ¶
- type Codec
- func (c *Codec) DecomposeRequest(body []byte, scope string, cfg llm.DecomposeConfig) ([]keep.Call, any, error)
- func (c *Codec) DecomposeResponse(body []byte, scope string, cfg llm.DecomposeConfig) ([]keep.Call, any, error)
- func (c *Codec) ReassembleRequest(handle any, results []keep.EvalResult) ([]byte, error)
- func (c *Codec) ReassembleResponse(handle any, results []keep.EvalResult) ([]byte, error)
- func (c *Codec) ReassembleStream(events []sse.Event) ([]byte, error)
- func (c *Codec) SynthesizeEvents(patchedBody []byte) ([]sse.Event, error)
- type ContentBlock
- type Message
- type MessagesRequest
- type MessagesResponse
- type Usage
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 (*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 ¶
ReassembleRequest patches mutations from eval results back into the request. Returns the original body if no mutations are present.
func (*Codec) ReassembleResponse ¶
ReassembleResponse patches mutations from eval results back into the response. Returns the original body if no mutations are present.
func (*Codec) ReassembleStream ¶
ReassembleStream reassembles SSE events into a complete 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.