translator

package
v6.2.6 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package translator provides types and functions for converting chat requests and responses between different schemas.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HasResponseTransformer

func HasResponseTransformer(from, to Format) bool

HasResponseTransformer inspects the default registry.

func Register

func Register(from, to Format, request RequestTransform, response ResponseTransform)

Register attaches transforms to the default registry.

func TranslateNonStream

func TranslateNonStream(ctx context.Context, from, to Format, model string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) string

TranslateNonStream is a helper on the default registry.

func TranslateRequest

func TranslateRequest(from, to Format, model string, rawJSON []byte, stream bool) []byte

TranslateRequest is a helper on the default registry.

func TranslateStream

func TranslateStream(ctx context.Context, from, to Format, model string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) []string

TranslateStream is a helper on the default registry.

func TranslateTokenCount

func TranslateTokenCount(ctx context.Context, from, to Format, count int64, rawJSON []byte) string

TranslateTokenCount is a helper on the default registry.

Types

type Format

type Format string

Format identifies a request/response schema used inside the proxy.

func FromString

func FromString(v string) Format

FromString converts an arbitrary identifier to a translator format.

func (Format) String

func (f Format) String() string

String returns the raw schema identifier.

type Pipeline

type Pipeline struct {
	// contains filtered or unexported fields
}

Pipeline orchestrates request/response transformation with middleware support.

func NewPipeline

func NewPipeline(registry *Registry) *Pipeline

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 Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry manages translation functions across schemas.

func Default

func Default() *Registry

Default exposes the package-level registry for shared use.

func NewRegistry

func NewRegistry() *Registry

NewRegistry constructs an empty translator registry.

func (*Registry) HasResponseTransformer

func (r *Registry) HasResponseTransformer(from, to Format) bool

HasResponseTransformer indicates whether a 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) TranslateNonStream

func (r *Registry) TranslateNonStream(ctx context.Context, from, to Format, model string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) string

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.

func (*Registry) TranslateStream

func (r *Registry) TranslateStream(ctx context.Context, from, to Format, model string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) []string

TranslateStream applies the registered streaming response translator.

func (*Registry) TranslateTokenCount

func (r *Registry) TranslateTokenCount(ctx context.Context, from, to Format, count int64, rawJSON []byte) string

TranslateNonStream applies the registered non-stream response translator.

type RequestEnvelope

type RequestEnvelope struct {
	Format Format
	Model  string
	Stream bool
	Body   []byte
}

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

type RequestTransform func(model string, rawJSON []byte, stream bool) []byte

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

type ResponseEnvelope struct {
	Format Format
	Model  string
	Stream bool
	Body   []byte
	Chunks []string
}

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

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 string.

type ResponseStreamTransform

type ResponseStreamTransform func(ctx context.Context, model string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) []string

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 strings, where each string is a chunk of the converted streaming response.

type ResponseTokenCountTransform

type ResponseTokenCountTransform func(ctx context.Context, count int64) string

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 a string.

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.

Jump to

Keyboard shortcuts

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