anthropic

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecomposeRequest

func DecomposeRequest(req *MessagesRequest, scope string, cfg config.DecomposeConfig) []keep.Call

DecomposeRequest breaks an Anthropic Messages API request into flat Keep calls. Returns one llm.request summary + one call per content block (based on decompose config).

func DecomposeResponse

func DecomposeResponse(resp *MessagesResponse, scope string, cfg config.DecomposeConfig) []keep.Call

DecomposeResponse breaks an Anthropic Messages API response into flat Keep calls.

func SynthesizeEvents

func SynthesizeEvents(resp *MessagesResponse) []sse.Event

SynthesizeEvents produces a minimal valid Anthropic SSE event sequence from a MessagesResponse. Each content block gets one start + one delta + one stop. This is used when the response was redacted and the original events can't be replayed.

Types

type BlockPosition

type BlockPosition struct {
	MessageIndex int
	BlockIndex   int
	Block        ContentBlock
	Role         string // role of the containing message
	CallIndex    int    // index into the calls slice returned by DecomposeRequest
}

BlockPosition identifies a content block within a request.

func WalkRequestBlocks

func WalkRequestBlocks(req *MessagesRequest, cfg config.DecomposeConfig) []BlockPosition

WalkRequestBlocks returns the positions of all blocks that DecomposeRequest would emit calls for.

func WalkResponseBlocks

func WalkResponseBlocks(resp *MessagesResponse, cfg config.DecomposeConfig) []BlockPosition

WalkResponseBlocks returns the positions of all blocks that DecomposeResponse would emit calls for.

type BlockResult

type BlockResult struct {
	// MessageIndex is the index into req.Messages.
	MessageIndex int
	// BlockIndex is the index into the message's content blocks.
	BlockIndex int
	// Result is the Keep evaluation result.
	Result keep.EvalResult
}

BlockResult pairs a decomposed call's block index with its evaluation result.

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.

func ReassembleRequest

func ReassembleRequest(req *MessagesRequest, results []BlockResult) *MessagesRequest

ReassembleRequest patches redaction mutations back into an Anthropic request. Returns a deep copy with mutated content. The original request is not modified.

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.

func ReassembleFromEvents

func ReassembleFromEvents(events []sse.Event) (*MessagesResponse, error)

ReassembleFromEvents builds a MessagesResponse from a sequence of Anthropic streaming SSE events. It accumulates text deltas and input_json_delta fragments into complete content blocks.

func ReassembleResponse

func ReassembleResponse(resp *MessagesResponse, results []BlockResult) *MessagesResponse

ReassembleResponse patches mutations into a response. Returns a deep copy with mutated content.

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