Documentation
¶
Overview ¶
Package translator provides types and functions for converting chat requests and responses between different schemas.
Index ¶
- func HasNonStreamResponseTransformer(from, to Format) bool
- func HasNonStreamResponseTransformerByFormatName(from, to Format) bool
- func HasRequestTransformer(from, to Format) bool
- func HasRequestTransformerByFormatName(from, to Format) bool
- func HasResponseTransformer(from, to Format) bool
- func HasResponseTransformerByFormatName(from, to Format) bool
- func HasStreamResponseTransformer(from, to Format) bool
- func HasStreamResponseTransformerByFormatName(from, to Format) bool
- func Register(from, to Format, request RequestTransform, response ResponseTransform)
- func SetPluginHooks(hooks PluginHooks)
- func TranslateNonStream(ctx context.Context, from, to Format, model string, ...) []byte
- func TranslateNonStreamByFormatName(ctx context.Context, from, to Format, model string, ...) []byte
- func TranslateRequest(from, to Format, model string, rawJSON []byte, stream bool) []byte
- func TranslateRequestByFormatName(from, to Format, model string, rawJSON []byte, stream bool) []byte
- func TranslateStream(ctx context.Context, from, to Format, model string, ...) [][]byte
- func TranslateStreamByFormatName(ctx context.Context, from, to Format, model string, ...) [][]byte
- func TranslateTokenCount(ctx context.Context, from, to Format, count int64, rawJSON []byte) []byte
- func TranslateTokenCountByFormatName(ctx context.Context, from, to Format, count int64, rawJSON []byte) []byte
- type Format
- type Pipeline
- func (p *Pipeline) TranslateRequest(ctx context.Context, from, to Format, req RequestEnvelope) (RequestEnvelope, error)
- func (p *Pipeline) TranslateResponse(ctx context.Context, from, to Format, resp ResponseEnvelope, ...) (ResponseEnvelope, error)
- func (p *Pipeline) UseRequest(mw RequestMiddleware)
- func (p *Pipeline) UseResponse(mw ResponseMiddleware)
- type PluginHooks
- type Registry
- func (r *Registry) HasNonStreamResponseTransformer(from, to Format) bool
- func (r *Registry) HasRequestTransformer(from, to Format) bool
- func (r *Registry) HasResponseTransformer(from, to Format) bool
- func (r *Registry) HasStreamResponseTransformer(from, to Format) bool
- func (r *Registry) Register(from, to Format, request RequestTransform, response ResponseTransform)
- func (r *Registry) SetPluginHooks(hooks PluginHooks)
- func (r *Registry) TranslateNonStream(ctx context.Context, from, to Format, model string, ...) []byte
- func (r *Registry) TranslateRequest(from, to Format, model string, rawJSON []byte, stream bool) []byte
- func (r *Registry) TranslateStream(ctx context.Context, from, to Format, model string, ...) [][]byte
- func (r *Registry) TranslateTokenCount(ctx context.Context, from, to Format, count int64, rawJSON []byte) []byte
- type RequestEnvelope
- type RequestHandler
- type RequestMiddleware
- type RequestTransform
- type ResponseEnvelope
- type ResponseHandler
- type ResponseMiddleware
- type ResponseNonStreamTransform
- type ResponseStreamTransform
- type ResponseTokenCountTransform
- type ResponseTransform
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HasNonStreamResponseTransformer ¶
HasNonStreamResponseTransformer inspects the default registry for a non-streaming response translator.
func HasNonStreamResponseTransformerByFormatName ¶
HasNonStreamResponseTransformerByFormatName reports whether a non-streaming response translator exists between two schemas.
func HasRequestTransformer ¶
HasRequestTransformer inspects the default registry.
func HasRequestTransformerByFormatName ¶
HasRequestTransformerByFormatName reports whether a request translator exists between two schemas.
func HasResponseTransformer ¶
HasResponseTransformer inspects the default registry.
func HasResponseTransformerByFormatName ¶
HasResponseTransformerByFormatName reports whether a response translator exists between two schemas.
func HasStreamResponseTransformer ¶
HasStreamResponseTransformer inspects the default registry for a streaming response translator.
func HasStreamResponseTransformerByFormatName ¶
HasStreamResponseTransformerByFormatName reports whether a streaming response translator exists between two schemas.
func Register ¶
func Register(from, to Format, request RequestTransform, response ResponseTransform)
Register attaches transforms to the default registry.
func SetPluginHooks ¶
func SetPluginHooks(hooks PluginHooks)
SetPluginHooks stores plugin hooks on the default registry.
func TranslateNonStream ¶
func TranslateNonStream(ctx context.Context, from, to Format, model string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) []byte
TranslateNonStream is a helper on the default registry.
func TranslateNonStreamByFormatName ¶
func TranslateNonStreamByFormatName(ctx context.Context, from, to Format, model string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) []byte
TranslateNonStreamByFormatName converts non-streaming responses between schemas by their string identifiers.
func TranslateRequest ¶
TranslateRequest is a helper on the default registry.
func TranslateRequestByFormatName ¶
func TranslateRequestByFormatName(from, to Format, model string, rawJSON []byte, stream bool) []byte
TranslateRequestByFormatName converts a request payload between schemas by their string identifiers.
func TranslateStream ¶
func TranslateStream(ctx context.Context, from, to Format, model string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) [][]byte
TranslateStream is a helper on the default registry.
func TranslateStreamByFormatName ¶
func TranslateStreamByFormatName(ctx context.Context, from, to Format, model string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) [][]byte
TranslateStreamByFormatName converts streaming responses between schemas by their string identifiers.
func TranslateTokenCount ¶
TranslateTokenCount is a helper on the default registry.
Types ¶
type Format ¶
type Format string
Format identifies a request/response schema used inside the proxy.
const ( FormatOpenAI Format = "openai" FormatOpenAIResponse Format = "openai-response" FormatClaude Format = "claude" FormatGemini Format = "gemini" FormatGeminiCLI Format = "gemini-cli" FormatCodex Format = "codex" FormatAntigravity Format = "antigravity" )
Common format identifiers exposed for SDK users.
func FromString ¶
FromString converts an arbitrary identifier to a translator format.
type Pipeline ¶
type Pipeline struct {
// contains filtered or unexported fields
}
Pipeline orchestrates request/response transformation with middleware support.
func NewPipeline ¶
NewPipeline constructs a pipeline bound to the provided registry.
func (*Pipeline) TranslateRequest ¶
func (p *Pipeline) TranslateRequest(ctx context.Context, from, to Format, req RequestEnvelope) (RequestEnvelope, error)
TranslateRequest applies middleware and registry transformations.
func (*Pipeline) TranslateResponse ¶
func (p *Pipeline) TranslateResponse(ctx context.Context, from, to Format, resp ResponseEnvelope, originalReq, translatedReq []byte, param *any) (ResponseEnvelope, error)
TranslateResponse applies middleware and registry transformations.
func (*Pipeline) UseRequest ¶
func (p *Pipeline) UseRequest(mw RequestMiddleware)
UseRequest adds request middleware executed in registration order.
func (*Pipeline) UseResponse ¶
func (p *Pipeline) UseResponse(mw ResponseMiddleware)
UseResponse adds response middleware executed in registration order.
type PluginHooks ¶
type PluginHooks interface {
NormalizeRequest(ctx context.Context, from, to Format, model string, body []byte, stream bool) []byte
TranslateRequest(ctx context.Context, from, to Format, model string, body []byte, stream bool) ([]byte, bool)
NormalizeResponseBefore(ctx context.Context, from, to Format, model string, originalRequestRawJSON, requestRawJSON, body []byte, stream bool) []byte
TranslateResponse(ctx context.Context, from, to Format, model string, originalRequestRawJSON, requestRawJSON, body []byte, stream bool) ([]byte, bool)
NormalizeResponseAfter(ctx context.Context, from, to Format, model string, originalRequestRawJSON, requestRawJSON, body []byte, stream bool) []byte
}
PluginHooks defines optional translator extension hooks provided by plugins. The plugin host implements this interface and installs it via Registry.SetPluginHooks so that plugin-supplied translators participate in the regular translation pipeline.
Normalize* methods always run and may rewrite the body in place. The Translate* methods are fallbacks consulted only when no native translator is registered for the (from, to) format pair; the bool result reports whether the plugin supplied a translation. This split keeps plugin translators from overriding native behaviour while still allowing plugins to introduce new format pairs.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry manages translation functions across schemas.
func NewRegistry ¶
func NewRegistry() *Registry
NewRegistry constructs an empty translator registry.
func (*Registry) HasNonStreamResponseTransformer ¶
HasNonStreamResponseTransformer indicates whether a non-streaming response translator exists.
func (*Registry) HasRequestTransformer ¶
HasRequestTransformer indicates whether a native request translator exists. Plugin-supplied translators are intentionally not reported here so callers can detect "no native translator registered" without consulting the plugin host.
func (*Registry) HasResponseTransformer ¶
HasResponseTransformer indicates whether a response translator exists.
func (*Registry) HasStreamResponseTransformer ¶
HasStreamResponseTransformer indicates whether a streaming response translator exists.
func (*Registry) Register ¶
func (r *Registry) Register(from, to Format, request RequestTransform, response ResponseTransform)
Register stores request/response transforms between two formats.
func (*Registry) SetPluginHooks ¶
func (r *Registry) SetPluginHooks(hooks PluginHooks)
SetPluginHooks stores translator plugin hooks for this registry. Passing nil clears any previously-installed hooks. The plugin host calls this at startup and whenever the plugin set changes so that plugin-supplied translators stay in sync with the active plugin generation.
func (*Registry) TranslateNonStream ¶
func (r *Registry) TranslateNonStream(ctx context.Context, from, to Format, model string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) []byte
TranslateNonStream applies the registered non-stream response translator.
func (*Registry) TranslateRequest ¶
func (r *Registry) TranslateRequest(from, to Format, model string, rawJSON []byte, stream bool) []byte
TranslateRequest converts a payload between schemas, returning the original payload if no translator is registered. When falling back to the original payload, the "model" field is still updated to match the resolved model name so that client-side prefixes (e.g. "copilot/gpt-5-mini") are not leaked upstream.
When plugin hooks are installed the request body is normalized through them and, if no native translator exists, the hooks get a chance to supply a plugin-provided translation. This lets plugins add new format pairs (for example a custom enterprise schema) without modifying the core registry.
type RequestEnvelope ¶
RequestEnvelope represents a request in the translation pipeline.
type RequestHandler ¶
type RequestHandler func(ctx context.Context, req RequestEnvelope) (RequestEnvelope, error)
RequestHandler performs request translation between formats.
type RequestMiddleware ¶
type RequestMiddleware func(ctx context.Context, req RequestEnvelope, next RequestHandler) (RequestEnvelope, error)
RequestMiddleware decorates request translation.
type RequestTransform ¶
RequestTransform is a function type that converts a request payload from a source schema to a target schema. It takes the model name, the raw JSON payload of the request, and a boolean indicating if the request is for a streaming response. It returns the converted request payload as a byte slice.
type ResponseEnvelope ¶
ResponseEnvelope represents a response in the translation pipeline.
type ResponseHandler ¶
type ResponseHandler func(ctx context.Context, resp ResponseEnvelope) (ResponseEnvelope, error)
ResponseHandler performs response translation between formats.
type ResponseMiddleware ¶
type ResponseMiddleware func(ctx context.Context, resp ResponseEnvelope, next ResponseHandler) (ResponseEnvelope, error)
ResponseMiddleware decorates response translation.
type ResponseNonStreamTransform ¶
type ResponseNonStreamTransform func(ctx context.Context, model string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) []byte
ResponseNonStreamTransform is a function type that converts a non-streaming response from a source schema to a target schema. It takes a context, the model name, the raw JSON of the original and converted requests, the raw JSON of the response, and an optional parameter. It returns the converted response as a single byte slice.
type ResponseStreamTransform ¶
type ResponseStreamTransform func(ctx context.Context, model string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) [][]byte
ResponseStreamTransform is a function type that converts a streaming response from a source schema to a target schema. It takes a context, the model name, the raw JSON of the original and converted requests, the raw JSON of the current response chunk, and an optional parameter. It returns a slice of byte chunks containing the converted streaming response.
type ResponseTokenCountTransform ¶
ResponseTokenCountTransform is a function type that transforms a token count from a source format to a target format. It takes a context and the token count as an int64, and returns the transformed token count as bytes.
type ResponseTransform ¶
type ResponseTransform struct {
// Stream is the function for transforming streaming responses.
Stream ResponseStreamTransform
// NonStream is the function for transforming non-streaming responses.
NonStream ResponseNonStreamTransform
// TokenCount is the function for transforming token counts.
TokenCount ResponseTokenCountTransform
}
ResponseTransform is a struct that groups together the functions for transforming streaming and non-streaming responses, as well as token counts.