Documentation
¶
Overview ¶
Package translate converts requests and streamed responses between the OpenAI Chat Completions, Anthropic Messages and OpenAI Responses formats.
Index ¶
- Constants
- func ApplyClaudeEffort(body []byte, model, effort string) []byte
- func ChatToClaude(body []byte, model, effort string, names *ToolNames) ([]byte, error)
- func ChatToResponses(body []byte, model, effort string, names *ToolNames) ([]byte, error)
- func ClaudeMessageToChat(msg []byte, model string, opts ChatOptions, names *ToolNames) []byte
- func ClaudeMessageToResponse(msg []byte, model string, names *ToolNames) []byte
- func ClaudeToResponses(body []byte, model, effort string, names *ToolNames) ([]byte, error)
- func CodexEffortFor(model, effort string) string
- func EffortFromBudget(budget int64) string
- func FitClaudeTokens(body []byte, model string) []byte
- func IsClaudeReasoning(enc string) bool
- func LooksLikeResponses(body []byte) bool
- func ReasoningSignature(enc string) string
- func ResponseToChat(resp []byte, model string, names *ToolNames, opts ChatOptions) []byte
- func ResponseToClaudeMessage(resp []byte, model string, names *ToolNames) []byte
- func ResponsesRequestToChat(body []byte) []byte
- func ResponsesToClaude(body []byte, model, effort string, names *ToolNames) ([]byte, error)
- type ChatOptions
- type ClaudeAccumulator
- type ClaudeToChatStream
- type ClaudeToResponsesStream
- type Converter
- type Event
- type ResponsesAccumulator
- type ResponsesToChatStream
- type ResponsesToClaudeStream
- type SSEReader
- type ToolNames
- type Usage
Constants ¶
const ClaudeReasoningPrefix = "mtk-claude:"
ClaudeReasoningPrefix marks a Responses reasoning item that carries a Claude thinking block, so it can be replayed to Claude (and kept away from Codex).
const CodexSignaturePrefix = "mtk-codex:"
CodexSignaturePrefix marks thinking signatures that carry Codex encrypted reasoning, so a Claude client can round-trip them back to Codex (and so they are dropped before reaching Claude).
Variables ¶
This section is empty.
Functions ¶
func ApplyClaudeEffort ¶
ApplyClaudeEffort sets thinking (and output_config.effort) on a Claude request from an effort level, e.g. one given as a "claude-opus-5(high)" model suffix.
func ChatToClaude ¶
ChatToClaude converts an OpenAI Chat Completions request into a Claude Messages request. Tool names are made Claude-safe through names, so responses can map them back.
func ChatToResponses ¶
ChatToResponses converts a Chat Completions request into a Responses request for Codex.
func ClaudeMessageToChat ¶
func ClaudeMessageToChat(msg []byte, model string, opts ChatOptions, names *ToolNames) []byte
ClaudeMessageToChat converts a complete Claude message into a chat.completion object.
func ClaudeMessageToResponse ¶
ClaudeMessageToResponse converts a complete Claude message into a Responses object.
func ClaudeToResponses ¶
ClaudeToResponses converts a Claude Messages request into a Responses request for Codex.
func CodexEffortFor ¶
CodexEffortFor lowers xhigh / max to the highest level the model accepts.
func EffortFromBudget ¶
EffortFromBudget maps a thinking token budget onto a reasoning effort level.
func FitClaudeTokens ¶
FitClaudeTokens keeps max_tokens within the model's output limit (defaulting it when missing) and the thinking budget below max_tokens, which Anthropic requires.
func IsClaudeReasoning ¶
IsClaudeReasoning reports whether encrypted reasoning came from Claude through maitake.
func LooksLikeResponses ¶
LooksLikeResponses reports a Responses API body sent to the Chat Completions endpoint.
func ReasoningSignature ¶
ReasoningSignature returns the provider signature inside an encrypted reasoning value.
func ResponseToChat ¶
func ResponseToChat(resp []byte, model string, names *ToolNames, opts ChatOptions) []byte
ResponseToChat converts a final Responses object into a chat.completion.
func ResponseToClaudeMessage ¶
ResponseToClaudeMessage converts a final Responses object into a Claude message.
func ResponsesRequestToChat ¶
ResponsesRequestToChat converts a Responses API request into a Chat Completions request.
Types ¶
type ChatOptions ¶
type ChatOptions struct {
IncludeUsage bool
// HideReasoning drops reasoning text (OpenRouter's reasoning.exclude).
HideReasoning bool
// ReasoningField is where reasoning text goes: "reasoning_content", "reasoning" (OpenRouter)
// or "both".
ReasoningField string
}
ChatOptions shape Chat Completions output.
type ClaudeAccumulator ¶
type ClaudeAccumulator struct {
Usage Usage
Err []byte // body of an upstream error event, if any
Done bool // the message reached a stop reason
// ServiceTier is the tier Anthropic reports serving the request at.
ServiceTier string
// contains filtered or unexported fields
}
ClaudeAccumulator rebuilds the final Claude message from its SSE stream and tracks usage.
func NewClaudeAccumulator ¶
func NewClaudeAccumulator() *ClaudeAccumulator
func (*ClaudeAccumulator) Add ¶
func (a *ClaudeAccumulator) Add(e Event)
func (*ClaudeAccumulator) Message ¶
func (a *ClaudeAccumulator) Message() []byte
Message returns the assembled message JSON.
func (*ClaudeAccumulator) Signatures ¶
func (a *ClaudeAccumulator) Signatures() []string
Signatures lists the thinking signatures in the assembled message.
type ClaudeToChatStream ¶
type ClaudeToChatStream struct {
// contains filtered or unexported fields
}
ClaudeToChatStream turns Claude Messages SSE into Chat Completions chunks.
func NewClaudeToChatStream ¶
func NewClaudeToChatStream(model string, opts ChatOptions, names *ToolNames) *ClaudeToChatStream
func (*ClaudeToChatStream) Complete ¶
func (c *ClaudeToChatStream) Complete() bool
Complete reports whether the stream reached a finish reason.
func (*ClaudeToChatStream) Convert ¶
func (c *ClaudeToChatStream) Convert(e Event) []Event
func (*ClaudeToChatStream) Finish ¶
func (c *ClaudeToChatStream) Finish() []Event
Finish ends the stream; one that stopped before a finish reason is reported as an error.
type ClaudeToResponsesStream ¶
type ClaudeToResponsesStream struct {
// contains filtered or unexported fields
}
ClaudeToResponsesStream turns Claude Messages SSE into Responses API events.
func NewClaudeToResponsesStream ¶
func NewClaudeToResponsesStream(model string, names *ToolNames) *ClaudeToResponsesStream
func (*ClaudeToResponsesStream) Convert ¶
func (c *ClaudeToResponsesStream) Convert(e Event) []Event
func (*ClaudeToResponsesStream) Finish ¶
func (c *ClaudeToResponsesStream) Finish() []Event
type ResponsesAccumulator ¶
type ResponsesAccumulator struct {
Usage Usage
Done bool
Err []byte
// ServiceTier is the tier Codex reports serving the request at.
ServiceTier string
// contains filtered or unexported fields
}
ResponsesAccumulator rebuilds the final Responses object and tracks usage.
func NewResponsesAccumulator ¶
func NewResponsesAccumulator() *ResponsesAccumulator
func (*ResponsesAccumulator) Add ¶
func (a *ResponsesAccumulator) Add(e Event)
func (*ResponsesAccumulator) Response ¶
func (a *ResponsesAccumulator) Response() []byte
Response returns the final response; output is rebuilt from streamed items when the terminal event omits it.
func (*ResponsesAccumulator) Signatures ¶
func (a *ResponsesAccumulator) Signatures() []string
Signatures lists the encrypted reasoning in the response's output items.
type ResponsesToChatStream ¶
type ResponsesToChatStream struct {
// contains filtered or unexported fields
}
ResponsesToChatStream turns Responses SSE into Chat Completions chunks.
func NewResponsesToChatStream ¶
func NewResponsesToChatStream(model string, opts ChatOptions, names *ToolNames) *ResponsesToChatStream
func (*ResponsesToChatStream) Convert ¶
func (c *ResponsesToChatStream) Convert(e Event) []Event
func (*ResponsesToChatStream) Finish ¶
func (c *ResponsesToChatStream) Finish() []Event
Finish ends the stream; one that stopped before completing is reported as an error.
type ResponsesToClaudeStream ¶
type ResponsesToClaudeStream struct {
// contains filtered or unexported fields
}
ResponsesToClaudeStream turns Responses SSE into Claude Messages SSE.
func NewResponsesToClaudeStream ¶
func NewResponsesToClaudeStream(model string, names *ToolNames) *ResponsesToClaudeStream
func (*ResponsesToClaudeStream) Convert ¶
func (c *ResponsesToClaudeStream) Convert(e Event) []Event
func (*ResponsesToClaudeStream) Finish ¶
func (c *ResponsesToClaudeStream) Finish() []Event
type SSEReader ¶
type SSEReader struct {
// contains filtered or unexported fields
}
SSEReader reads server-sent events from an upstream body.
func NewSSEReader ¶
type ToolNames ¶
type ToolNames struct {
// contains filtered or unexported fields
}
ToolNames shortens tool names to the Responses API limit (64 chars, [a-zA-Z0-9_-]) and maps them back.
func NewToolNames ¶
func NewToolNames() *ToolNames
func (*ToolNames) MarkCustom ¶
MarkCustom records a freeform tool, whose calls go back to the client as custom_tool_call.
type Usage ¶
type Usage struct {
InputTokens int64 `json:"inputTokens"`
OutputTokens int64 `json:"outputTokens"`
CacheReadTokens int64 `json:"cacheReadTokens"`
CacheWriteTokens int64 `json:"cacheWriteTokens"`
ReasoningTokens int64 `json:"reasoningTokens"`
}
Usage is token accounting normalized across providers.
func ResponsesUsage ¶
ResponsesUsage reads a Responses API usage object.