Documentation
¶
Overview ¶
Package translate converts Anthropic Messages API requests/responses to and from OpenAI ChatCompletions shape. Pure: no I/O, no upstream calls. Adapter-specific quirks live in internal/adapter/<provider>.
Index ¶
- type AnthropicBlock
- type AnthropicImageSource
- type AnthropicMessage
- type AnthropicRequest
- type AnthropicResponse
- type AnthropicTool
- type AnthropicUsage
- type OpenAIChoice
- type OpenAIContentPart
- type OpenAIImageURL
- type OpenAIMessage
- type OpenAIRequest
- type OpenAIResponse
- type OpenAITool
- type OpenAIToolCall
- type OpenAIToolCallBody
- type OpenAIToolFunction
- type OpenAIUsage
- type SSEEvent
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AnthropicBlock ¶
type AnthropicBlock struct {
Type string `json:"type"`
// type: "text"
Text string `json:"text,omitempty"`
// type: "image"
Source *AnthropicImageSource `json:"source,omitempty"`
// type: "tool_use"
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Input json.RawMessage `json:"input,omitempty"`
// type: "tool_result"
ToolUseID string `json:"tool_use_id,omitempty"`
// Content on tool_result is either a string or a content[] of text/image
// blocks. We keep RawMessage for the boundary.
ToolContent json.RawMessage `json:"content,omitempty"`
IsError bool `json:"is_error,omitempty"`
}
AnthropicBlock is a tagged union of content block types: text, image, tool_use, tool_result, and thinking (Stage 0 rejects thinking loudly).
type AnthropicImageSource ¶
type AnthropicImageSource struct {
Type string `json:"type"` // "base64" | "url"
MediaType string `json:"media_type"` // e.g. "image/png"
Data string `json:"data,omitempty"`
URL string `json:"url,omitempty"`
}
AnthropicImageSource carries either base64 data with a media_type or a URL. Anthropic supports both; OpenAI takes a single URL form (data: for base64).
type AnthropicMessage ¶
type AnthropicMessage struct {
Role string `json:"role"`
Content json.RawMessage `json:"content"`
}
AnthropicMessage holds a role and content. Content is either a string or an array of typed blocks — we keep RawMessage for the boundary and parse in helpers.
type AnthropicRequest ¶
type AnthropicRequest struct {
Model string `json:"model"`
Messages []AnthropicMessage `json:"messages"`
System json.RawMessage `json:"system,omitempty"` // string | content[]
MaxTokens int `json:"max_tokens"`
Stream bool `json:"stream,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
TopP *float64 `json:"top_p,omitempty"`
StopSequences []string `json:"stop_sequences,omitempty"`
Tools []AnthropicTool `json:"tools,omitempty"`
ToolChoice json.RawMessage `json:"tool_choice,omitempty"`
}
AnthropicRequest mirrors the documented Anthropic Messages request body. Fields irrelevant to Stage 0 are intentionally omitted (e.g. metadata, service_tier) — they round-trip via json.Unmarshal's ignored-field path.
type AnthropicResponse ¶
type AnthropicResponse struct {
ID string `json:"id"`
Type string `json:"type"` // always "message"
Role string `json:"role"` // always "assistant"
Model string `json:"model"`
Content []AnthropicBlock `json:"content"`
StopReason string `json:"stop_reason"`
StopSequence *string `json:"stop_sequence,omitempty"`
Usage AnthropicUsage `json:"usage"`
}
AnthropicResponse is what we send back to Claude Code.
func OpenAIToAnthropic ¶
func OpenAIToAnthropic(resp *OpenAIResponse, originalModel string) (*AnthropicResponse, error)
OpenAIToAnthropic converts an OpenAI ChatCompletions response back to Anthropic Messages shape. Only choice[0] is consumed (Stage 0; n=1).
originalModel is the model name the client sent (passed through into the response so Claude Code sees what it asked for).
type AnthropicTool ¶
type AnthropicTool struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
InputSchema json.RawMessage `json:"input_schema"`
}
AnthropicTool is the tool declaration shape on the request side. Schema stays a RawMessage so we don't validate JSON Schema here.
type AnthropicUsage ¶
type AnthropicUsage struct {
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
}
AnthropicUsage mirrors Anthropic's usage shape.
type OpenAIChoice ¶
type OpenAIChoice struct {
Index int `json:"index"`
Message OpenAIMessage `json:"message"`
FinishReason string `json:"finish_reason"`
}
OpenAIChoice is one completion option. We only consume index 0.
type OpenAIContentPart ¶
type OpenAIContentPart struct {
Type string `json:"type"` // "text" | "image_url"
Text string `json:"text,omitempty"`
ImageURL *OpenAIImageURL `json:"image_url,omitempty"`
}
OpenAIContentPart represents a single chunk within a multi-modal user message: text or image_url.
type OpenAIImageURL ¶
type OpenAIImageURL struct {
URL string `json:"url"`
}
OpenAIImageURL accepts http(s) URLs or data: URLs for inline base64.
type OpenAIMessage ¶
type OpenAIMessage struct {
Role string `json:"role"`
Content json.RawMessage `json:"content,omitempty"`
ToolCalls []OpenAIToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
}
OpenAIMessage covers system, user, assistant, and tool roles. Content is string for simple cases or an array of parts when mixed text+image.
type OpenAIRequest ¶
type OpenAIRequest struct {
Model string `json:"model"`
Messages []OpenAIMessage `json:"messages"`
MaxTokens int `json:"max_tokens,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
TopP *float64 `json:"top_p,omitempty"`
Stop []string `json:"stop,omitempty"`
Tools []OpenAITool `json:"tools,omitempty"`
ToolChoice json.RawMessage `json:"tool_choice,omitempty"`
Stream bool `json:"stream,omitempty"`
}
OpenAIRequest is the body we send to {adapter.BaseURL}/chat/completions.
func AnthropicToOpenAI ¶
func AnthropicToOpenAI(req *AnthropicRequest) (*OpenAIRequest, error)
AnthropicToOpenAI converts an Anthropic Messages request to an OpenAI ChatCompletions request. Tools-related fields are delegated to tools.go (step 5) but the wiring lives here.
Model name mapping is delegated to the Adapter — translate stays pure.
type OpenAIResponse ¶
type OpenAIResponse struct {
ID string `json:"id"`
Model string `json:"model"`
Choices []OpenAIChoice `json:"choices"`
Usage OpenAIUsage `json:"usage"`
}
OpenAIResponse is the upstream response we receive.
type OpenAITool ¶
type OpenAITool struct {
Type string `json:"type"` // always "function"
Function OpenAIToolFunction `json:"function"`
}
OpenAITool is the function-wrapped tool declaration.
type OpenAIToolCall ¶
type OpenAIToolCall struct {
ID string `json:"id"`
Type string `json:"type"` // always "function"
Function OpenAIToolCallBody `json:"function"`
}
OpenAIToolCall is one tool invocation on the assistant side.
type OpenAIToolCallBody ¶
type OpenAIToolCallBody struct {
Name string `json:"name"`
Arguments string `json:"arguments"` // JSON-encoded string per spec
}
OpenAIToolCallBody is the name + serialised JSON arguments.
type OpenAIToolFunction ¶
type OpenAIToolFunction struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Parameters json.RawMessage `json:"parameters"`
}
OpenAIToolFunction inner declaration.
type OpenAIUsage ¶
type OpenAIUsage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
}
OpenAIUsage carries prompt/completion token counts.
type SSEEvent ¶
SSEEvent is one Anthropic-shaped SSE event, ready to wire-format. Marshal as `event: <Name>\ndata: <JSON(Data)>\n\n`.
func ToAnthropicSSE ¶
func ToAnthropicSSE(resp *OpenAIResponse, originalModel string) ([]SSEEvent, error)
ToAnthropicSSE converts a complete OpenAI response into an ordered sequence of Anthropic SSE events. Stage 0 emits the sequence in a single burst (buffer-then-restream) so callers see the canonical event order even though no real per-token streaming happens. Tool-use blocks emit one content_block_start + a single input_json_delta carrying the full arguments + content_block_stop.