Documentation
¶
Overview ¶
Package translate converts between the Anthropic Messages API that Claude Code speaks and the OpenAI Responses API exposed by the Codex backend.
Index ¶
Constants ¶
const SignatureMarker = "ccmp1:"
SignatureMarker prefixes every thinking signature the proxy hands to Claude Code. Codex reasoning state and Anthropic reasoning signatures are not interchangeable, so the marker lets both directions recognise their own blocks and drop the other's instead of failing a request upstream.
Variables ¶
This section is empty.
Functions ¶
func EffortFrom ¶
EffortFrom reads the client's reasoning preference out of an Anthropic Messages request and maps it onto a Responses reasoning effort. fallback is returned when the request states no preference.
An explicit output_config.effort wins over everything else: it is what Claude Code's /effort and --effort set, and it says exactly what the user asked for rather than implying it through a token budget.
func MarkSignature ¶
MarkSignature tags Codex reasoning state as this proxy's own.
func StripProxySignatures ¶
StripProxySignatures removes assistant thinking blocks whose signature this proxy issued for a Codex turn.
Claude Code replays prior assistant turns verbatim, so a session that switched from a Codex model back to a Claude model would otherwise send Codex reasoning state to Anthropic, which rejects the request. The check is a byte scan first, so requests that never touched a Codex model are returned untouched and pass through unmodified.
Types ¶
type MessageAssembler ¶
type MessageAssembler struct {
// contains filtered or unexported fields
}
MessageAssembler folds the Anthropic SSE frames a StreamTranslator produces back into a single non-streaming message.
A client that did not ask for streaming still has to be answered from a stream, because that is all the Codex backend serves. Rebuilding the message from the translated frames rather than from the terminal upstream payload keeps one translation path for both cases: the real backend does not repeat the output items in response.completed, so anything reading only that event would return an empty message.
func NewMessageAssembler ¶
func NewMessageAssembler() *MessageAssembler
NewMessageAssembler returns an assembler for one response.
func (*MessageAssembler) Err ¶
func (a *MessageAssembler) Err() (kind string, message string, ok bool)
Err reports the error type and message if the stream carried one.
func (*MessageAssembler) Message ¶
func (a *MessageAssembler) Message() []byte
Message renders the assembled Anthropic message.
func (*MessageAssembler) Write ¶
func (a *MessageAssembler) Write(frames []byte)
Write consumes SSE frames emitted by a StreamTranslator.
type Options ¶
type Options struct {
// Model is the Codex slug to request.
Model string
// Effort overrides the reasoning effort. When empty the effort is derived
// from the Claude thinking configuration.
Effort string
// DefaultEffort is used when neither Effort nor the request implies one.
DefaultEffort string
}
Options controls the translation.
type Request ¶
type Request struct {
// Body is the JSON payload to POST to the Codex Responses endpoint.
Body []byte
// ToolNames maps the shortened names sent upstream back to the names the
// client used, so tool_use blocks can be reported under the original name.
ToolNames map[string]string
}
Request is the outcome of translating one Claude request.
type StreamTranslator ¶
type StreamTranslator struct {
// contains filtered or unexported fields
}
StreamTranslator turns the Codex Responses event stream into the Anthropic Messages SSE stream Claude Code consumes. One instance handles one response and is not safe for concurrent use.
func NewStreamTranslator ¶
func NewStreamTranslator(clientModel string, toolNames map[string]string) *StreamTranslator
NewStreamTranslator returns a translator for one response. clientModel is the model name the client asked for; toolNames maps upstream (shortened) tool names to the client's original names.
func (*StreamTranslator) Close ¶
func (t *StreamTranslator) Close() []byte
Close emits whatever is needed to leave the client with a well-formed stream when upstream ends without a terminal event.
func (*StreamTranslator) Error ¶
func (t *StreamTranslator) Error(kind, message string) []byte
Error renders an Anthropic error frame.
func (*StreamTranslator) Event ¶
func (t *StreamTranslator) Event(payload []byte) []byte
Event translates a single Codex SSE payload (the JSON after "data:") into zero or more Anthropic SSE frames.
func (*StreamTranslator) Started ¶
func (t *StreamTranslator) Started() bool
Started reports whether a message_start has been emitted, which tells the caller whether an error can still be reported as an HTTP status.