Documentation
¶
Overview ¶
Package anthropic2openai translates between the OpenAI Responses API and the Anthropic Messages API.
The Codex CLI only speaks the OpenAI Responses protocol, while several upstreams (Command Code, Anthropic-native gateways) only expose the native Anthropic Messages protocol (/v1/messages). This package converts the Responses request sent by such a client into an Anthropic Messages request, then converts the Anthropic response (JSON or SSE stream) back into the Responses shape the client understands:
Codex CLI ──/v1/responses──▶ bridge ──/v1/messages──▶ Anthropic upstream Codex CLI ◀─Responses SSE── bridge ◀──Anthropic SSE── upstream
It is a Go port of the MIT-licensed bridge in farion1231/cc-switch (src-tauri/src/proxy/providers/{transform_codex_anthropic, streaming_codex_anthropic, codex_responses_sse, reasoning_bridge}.rs and the helper modules they share), with the upstream tests ported alongside the code. Like the original, values are dynamic (map[string]any) so unknown fields pass through untouched and the upstream test expectations carry over verbatim.
Direction summary:
ResponsesToAnthropic OpenAI Responses request body → Anthropic Messages request body AnthropicToResponses Anthropic Messages JSON response → OpenAI Responses JSON response AnthropicSSEToMessage full Anthropic SSE stream → one aggregated Responses message value NewAnthropicToResponsesStream incremental Anthropic SSE bytes → Responses SSE bytes
The package uses only the Go standard library. JSON decoding preserves number literals via json.Number; construct values with the provided helpers or use json.Number when precision matters.
Index ¶
- func AnthropicSSEToMessage(body string) (map[string]any, error)
- func AnthropicToResponses(body map[string]any) (map[string]any, error)
- func BridgeHandler(inner http.Handler, options BridgeOptions) http.Handler
- func IsInvalidRequest(err error) bool
- func ResponsesSSEEventsFromAnthropicMessage(body map[string]any, toolContext *codexToolContext) [][]byte
- func ResponsesToAnthropic(body map[string]any, options Options) (map[string]any, error)
- func ResponsesToAnthropicWithContext(body map[string]any, options Options) (map[string]any, *codexToolContext, error)
- type BridgeOptions
- type EffortMode
- type Error
- type Options
- type StreamTranslator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AnthropicSSEToMessage ¶
Aggregates an Anthropic Messages SSE stream back into a single Anthropic non-streaming message JSON.
Used as a fallback: the upstream returned an SSE body for a `stream:false` request but without the `text/event-stream` header. The aggregated message can be handed directly to AnthropicToResponses. It also tolerates the last event missing a trailing blank line (truncated stream). Ported from cc-switch transform_codex_anthropic.rs anthropic_sse_to_message_value.
func AnthropicToResponses ¶
AnthropicToResponses converts a non-streaming Anthropic Messages JSON response into an OpenAI Responses JSON response. It returns an *Error of kind "transform" when the upstream returned an error envelope.
func BridgeHandler ¶
func BridgeHandler(inner http.Handler, options BridgeOptions) http.Handler
BridgeHandler wraps an Anthropic Messages http.Handler (such as the commandcode provider handler) so it also serves OpenAI Responses requests: the request body is translated with ResponsesToAnthropic, dispatched to the inner handler at /v1/messages, and the Anthropic response (JSON or SSE) is translated back into the Responses shape. Non-200 responses pass through unchanged.
func IsInvalidRequest ¶
IsInvalidRequest reports whether err is a client-request error.
func ResponsesSSEEventsFromAnthropicMessage ¶
func ResponsesSSEEventsFromAnthropicMessage(body map[string]any, toolContext *codexToolContext) [][]byte
ResponsesSSEEventsFromAnthropicMessage converts a complete non-streaming Anthropic message (or error envelope) into the same Responses SSE lifecycle emitted by the live stream converter. Some compatible gateways ignore `stream:true` and return JSON with HTTP 200.
func ResponsesToAnthropic ¶
ResponsesToAnthropic converts an OpenAI Responses request body into an Anthropic Messages request body. It returns an *Error wrapping invalid_request for client errors.
func ResponsesToAnthropicWithContext ¶
func ResponsesToAnthropicWithContext(body map[string]any, options Options) (map[string]any, *codexToolContext, error)
ResponsesToAnthropicWithContext is ResponsesToAnthropic, additionally returning the tool context built from the request (callers streaming the response back through NewStreamTranslatorWithContext should share it so namespaced/custom tool calls round-trip).
Types ¶
type BridgeOptions ¶
type BridgeOptions struct {
// DefaultMaxTokens is injected when the Responses request carries no
// max_output_tokens (Anthropic's max_tokens is required).
DefaultMaxTokens int
// EffortMode selects the effort transport; llm-proxy's commandcode
// provider uses EffortOutputConfig.
EffortMode EffortMode
}
BridgeOptions configures a BridgeHandler.
type EffortMode ¶
type EffortMode int
EffortMode selects how the client's reasoning effort is transported to the Anthropic Messages upstream.
const ( // EffortThinkingBudget reproduces cc-switch behavior: extended thinking is // enabled with a token budget derived from the effort (adaptive Claude // models use thinking: adaptive + output_config.effort instead). EffortThinkingBudget EffortMode = iota // EffortOutputConfig transports the effort as output_config.effort // (low/medium/high/max) without injecting Anthropic thinking. This is the // convention llm-proxy's commandcode provider already speaks: its handler // maps output_config.effort through the route's effortMapping. EffortOutputConfig )
type Error ¶
Error kinds produced by the transforms, mirroring cc-switch's ProxyError::InvalidRequest (client's fault, HTTP 400) and ProxyError::TransformError (upstream payload trouble, HTTP 502).
type Options ¶
type Options struct {
// DefaultMaxTokens is injected when the Responses body has no
// max_output_tokens (Anthropic's max_tokens is required; missing it yields
// a 400).
DefaultMaxTokens int
// EffortMode selects the effort transport. Zero value is
// EffortThinkingBudget (faithful cc-switch behavior).
EffortMode EffortMode
}
Options configures ResponsesToAnthropic.
type StreamTranslator ¶
type StreamTranslator struct {
// contains filtered or unexported fields
}
StreamTranslator incrementally converts Anthropic SSE bytes into Responses SSE bytes. Feed upstream chunks via Write and read translated events from Pending; Flush processes a trailing event missing its blank-line delimiter and completes the response lifecycle at EOF.
func NewStreamTranslator ¶
func NewStreamTranslator() *StreamTranslator
NewStreamTranslator builds a translator with a default tool context.
func NewStreamTranslatorWithContext ¶
func NewStreamTranslatorWithContext(toolContext *codexToolContext) *StreamTranslator
NewStreamTranslatorWithContext builds a translator sharing the given tool context (built from the request so tool names round-trip).
func (*StreamTranslator) Flush ¶
func (t *StreamTranslator) Flush()
Flush finishes the stream at upstream EOF.
func (*StreamTranslator) Pending ¶
func (t *StreamTranslator) Pending() []byte
Pending returns the translated Responses SSE bytes accumulated so far.
func (*StreamTranslator) Write ¶
func (t *StreamTranslator) Write(chunk []byte)
Write consumes one upstream chunk and buffers translated Responses SSE.