translate

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: AGPL-3.0 Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package translate converts requests and streamed responses between the OpenAI Chat Completions, Anthropic Messages and OpenAI Responses formats.

Index

Constants

View Source
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).

View Source
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

func ApplyClaudeEffort(body []byte, model, effort string) []byte

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

func ChatToClaude(body []byte, model, effort string, names *ToolNames) ([]byte, error)

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

func ChatToResponses(body []byte, model, effort string, names *ToolNames) ([]byte, error)

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

func ClaudeMessageToResponse(msg []byte, model string, names *ToolNames) []byte

ClaudeMessageToResponse converts a complete Claude message into a Responses object.

func ClaudeToResponses

func ClaudeToResponses(body []byte, model, effort string, names *ToolNames) ([]byte, error)

ClaudeToResponses converts a Claude Messages request into a Responses request for Codex.

func CodexEffortFor

func CodexEffortFor(model, effort string) string

CodexEffortFor lowers xhigh / max to the highest level the model accepts.

func EffortFromBudget

func EffortFromBudget(budget int64) string

EffortFromBudget maps a thinking token budget onto a reasoning effort level.

func FitClaudeTokens

func FitClaudeTokens(body []byte, model string) []byte

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

func IsClaudeReasoning(enc string) bool

IsClaudeReasoning reports whether encrypted reasoning came from Claude through maitake.

func LooksLikeResponses

func LooksLikeResponses(body []byte) bool

LooksLikeResponses reports a Responses API body sent to the Chat Completions endpoint.

func ReasoningSignature

func ReasoningSignature(enc string) string

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

func ResponseToClaudeMessage(resp []byte, model string, names *ToolNames) []byte

ResponseToClaudeMessage converts a final Responses object into a Claude message.

func ResponsesRequestToChat

func ResponsesRequestToChat(body []byte) []byte

ResponsesRequestToChat converts a Responses API request into a Chat Completions request.

func ResponsesToClaude

func ResponsesToClaude(body []byte, model, effort string, names *ToolNames) ([]byte, error)

ResponsesToClaude converts a Responses API request into a Claude Messages 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 Converter

type Converter interface {
	Convert(e Event) []Event
	Finish() []Event
}

Converter rewrites a stream of upstream events into client events.

type Event

type Event struct {
	Name string
	Data []byte
}

Event is one server-sent event. Name may be empty (OpenAI chat chunks).

func (Event) Encode

func (e Event) Encode() []byte

Encode renders an event in SSE wire format.

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

func NewSSEReader(r io.Reader) *SSEReader

func (*SSEReader) Next

func (s *SSEReader) Next() (Event, error)

Next returns the next event, or io.EOF when the stream ends.

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) IsCustom

func (t *ToolNames) IsCustom(name string) bool

func (*ToolNames) MarkCustom

func (t *ToolNames) MarkCustom(name string)

MarkCustom records a freeform tool, whose calls go back to the client as custom_tool_call.

func (*ToolNames) Original

func (t *ToolNames) Original(short string) string

func (*ToolNames) Short

func (t *ToolNames) Short(name string) string

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

func ResponsesUsage(u gjson.Result) Usage

ResponsesUsage reads a Responses API usage object.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL