mirror of
https://github.com/router-for-me/CLIProxyAPIPlus.git
synced 2026-04-26 21:05:46 +00:00
remove all
This commit is contained in:
@@ -1,14 +0,0 @@
|
||||
package translator
|
||||
|
||||
// Format identifies a request/response schema used inside the proxy.
|
||||
type Format string
|
||||
|
||||
// FromString converts an arbitrary identifier to a translator format.
|
||||
func FromString(v string) Format {
|
||||
return Format(v)
|
||||
}
|
||||
|
||||
// String returns the raw schema identifier.
|
||||
func (f Format) String() string {
|
||||
return string(f)
|
||||
}
|
||||
@@ -1,106 +0,0 @@
|
||||
package translator
|
||||
|
||||
import "context"
|
||||
|
||||
// RequestEnvelope represents a request in the translation pipeline.
|
||||
type RequestEnvelope struct {
|
||||
Format Format
|
||||
Model string
|
||||
Stream bool
|
||||
Body []byte
|
||||
}
|
||||
|
||||
// ResponseEnvelope represents a response in the translation pipeline.
|
||||
type ResponseEnvelope struct {
|
||||
Format Format
|
||||
Model string
|
||||
Stream bool
|
||||
Body []byte
|
||||
Chunks []string
|
||||
}
|
||||
|
||||
// RequestMiddleware decorates request translation.
|
||||
type RequestMiddleware func(ctx context.Context, req RequestEnvelope, next RequestHandler) (RequestEnvelope, error)
|
||||
|
||||
// ResponseMiddleware decorates response translation.
|
||||
type ResponseMiddleware func(ctx context.Context, resp ResponseEnvelope, next ResponseHandler) (ResponseEnvelope, error)
|
||||
|
||||
// RequestHandler performs request translation between formats.
|
||||
type RequestHandler func(ctx context.Context, req RequestEnvelope) (RequestEnvelope, error)
|
||||
|
||||
// ResponseHandler performs response translation between formats.
|
||||
type ResponseHandler func(ctx context.Context, resp ResponseEnvelope) (ResponseEnvelope, error)
|
||||
|
||||
// Pipeline orchestrates request/response transformation with middleware support.
|
||||
type Pipeline struct {
|
||||
registry *Registry
|
||||
requestMiddleware []RequestMiddleware
|
||||
responseMiddleware []ResponseMiddleware
|
||||
}
|
||||
|
||||
// NewPipeline constructs a pipeline bound to the provided registry.
|
||||
func NewPipeline(registry *Registry) *Pipeline {
|
||||
if registry == nil {
|
||||
registry = Default()
|
||||
}
|
||||
return &Pipeline{registry: registry}
|
||||
}
|
||||
|
||||
// UseRequest adds request middleware executed in registration order.
|
||||
func (p *Pipeline) UseRequest(mw RequestMiddleware) {
|
||||
if mw != nil {
|
||||
p.requestMiddleware = append(p.requestMiddleware, mw)
|
||||
}
|
||||
}
|
||||
|
||||
// UseResponse adds response middleware executed in registration order.
|
||||
func (p *Pipeline) UseResponse(mw ResponseMiddleware) {
|
||||
if mw != nil {
|
||||
p.responseMiddleware = append(p.responseMiddleware, mw)
|
||||
}
|
||||
}
|
||||
|
||||
// TranslateRequest applies middleware and registry transformations.
|
||||
func (p *Pipeline) TranslateRequest(ctx context.Context, from, to Format, req RequestEnvelope) (RequestEnvelope, error) {
|
||||
terminal := func(ctx context.Context, input RequestEnvelope) (RequestEnvelope, error) {
|
||||
translated := p.registry.TranslateRequest(from, to, input.Model, input.Body, input.Stream)
|
||||
input.Body = translated
|
||||
input.Format = to
|
||||
return input, nil
|
||||
}
|
||||
|
||||
handler := terminal
|
||||
for i := len(p.requestMiddleware) - 1; i >= 0; i-- {
|
||||
mw := p.requestMiddleware[i]
|
||||
next := handler
|
||||
handler = func(ctx context.Context, r RequestEnvelope) (RequestEnvelope, error) {
|
||||
return mw(ctx, r, next)
|
||||
}
|
||||
}
|
||||
|
||||
return handler(ctx, req)
|
||||
}
|
||||
|
||||
// TranslateResponse applies middleware and registry transformations.
|
||||
func (p *Pipeline) TranslateResponse(ctx context.Context, from, to Format, resp ResponseEnvelope, originalReq, translatedReq []byte, param *any) (ResponseEnvelope, error) {
|
||||
terminal := func(ctx context.Context, input ResponseEnvelope) (ResponseEnvelope, error) {
|
||||
if input.Stream {
|
||||
input.Chunks = p.registry.TranslateStream(ctx, from, to, input.Model, originalReq, translatedReq, input.Body, param)
|
||||
} else {
|
||||
input.Body = []byte(p.registry.TranslateNonStream(ctx, from, to, input.Model, originalReq, translatedReq, input.Body, param))
|
||||
}
|
||||
input.Format = to
|
||||
return input, nil
|
||||
}
|
||||
|
||||
handler := terminal
|
||||
for i := len(p.responseMiddleware) - 1; i >= 0; i-- {
|
||||
mw := p.responseMiddleware[i]
|
||||
next := handler
|
||||
handler = func(ctx context.Context, r ResponseEnvelope) (ResponseEnvelope, error) {
|
||||
return mw(ctx, r, next)
|
||||
}
|
||||
}
|
||||
|
||||
return handler(ctx, resp)
|
||||
}
|
||||
@@ -1,142 +0,0 @@
|
||||
package translator
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Registry manages translation functions across schemas.
|
||||
type Registry struct {
|
||||
mu sync.RWMutex
|
||||
requests map[Format]map[Format]RequestTransform
|
||||
responses map[Format]map[Format]ResponseTransform
|
||||
}
|
||||
|
||||
// NewRegistry constructs an empty translator registry.
|
||||
func NewRegistry() *Registry {
|
||||
return &Registry{
|
||||
requests: make(map[Format]map[Format]RequestTransform),
|
||||
responses: make(map[Format]map[Format]ResponseTransform),
|
||||
}
|
||||
}
|
||||
|
||||
// Register stores request/response transforms between two formats.
|
||||
func (r *Registry) Register(from, to Format, request RequestTransform, response ResponseTransform) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
if _, ok := r.requests[from]; !ok {
|
||||
r.requests[from] = make(map[Format]RequestTransform)
|
||||
}
|
||||
if request != nil {
|
||||
r.requests[from][to] = request
|
||||
}
|
||||
|
||||
if _, ok := r.responses[from]; !ok {
|
||||
r.responses[from] = make(map[Format]ResponseTransform)
|
||||
}
|
||||
r.responses[from][to] = response
|
||||
}
|
||||
|
||||
// TranslateRequest converts a payload between schemas, returning the original payload
|
||||
// if no translator is registered.
|
||||
func (r *Registry) TranslateRequest(from, to Format, model string, rawJSON []byte, stream bool) []byte {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
if byTarget, ok := r.requests[from]; ok {
|
||||
if fn, isOk := byTarget[to]; isOk && fn != nil {
|
||||
return fn(model, rawJSON, stream)
|
||||
}
|
||||
}
|
||||
return rawJSON
|
||||
}
|
||||
|
||||
// HasResponseTransformer indicates whether a response translator exists.
|
||||
func (r *Registry) HasResponseTransformer(from, to Format) bool {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
if byTarget, ok := r.responses[from]; ok {
|
||||
if _, isOk := byTarget[to]; isOk {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// TranslateStream applies the registered streaming response translator.
|
||||
func (r *Registry) TranslateStream(ctx context.Context, from, to Format, model string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) []string {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
if byTarget, ok := r.responses[to]; ok {
|
||||
if fn, isOk := byTarget[from]; isOk && fn.Stream != nil {
|
||||
return fn.Stream(ctx, model, originalRequestRawJSON, requestRawJSON, rawJSON, param)
|
||||
}
|
||||
}
|
||||
return []string{string(rawJSON)}
|
||||
}
|
||||
|
||||
// TranslateNonStream applies the registered non-stream response translator.
|
||||
func (r *Registry) TranslateNonStream(ctx context.Context, from, to Format, model string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) string {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
if byTarget, ok := r.responses[to]; ok {
|
||||
if fn, isOk := byTarget[from]; isOk && fn.NonStream != nil {
|
||||
return fn.NonStream(ctx, model, originalRequestRawJSON, requestRawJSON, rawJSON, param)
|
||||
}
|
||||
}
|
||||
return string(rawJSON)
|
||||
}
|
||||
|
||||
// TranslateNonStream applies the registered non-stream response translator.
|
||||
func (r *Registry) TranslateTokenCount(ctx context.Context, from, to Format, count int64, rawJSON []byte) string {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
if byTarget, ok := r.responses[to]; ok {
|
||||
if fn, isOk := byTarget[from]; isOk && fn.TokenCount != nil {
|
||||
return fn.TokenCount(ctx, count)
|
||||
}
|
||||
}
|
||||
return string(rawJSON)
|
||||
}
|
||||
|
||||
var defaultRegistry = NewRegistry()
|
||||
|
||||
// Default exposes the package-level registry for shared use.
|
||||
func Default() *Registry {
|
||||
return defaultRegistry
|
||||
}
|
||||
|
||||
// Register attaches transforms to the default registry.
|
||||
func Register(from, to Format, request RequestTransform, response ResponseTransform) {
|
||||
defaultRegistry.Register(from, to, request, response)
|
||||
}
|
||||
|
||||
// TranslateRequest is a helper on the default registry.
|
||||
func TranslateRequest(from, to Format, model string, rawJSON []byte, stream bool) []byte {
|
||||
return defaultRegistry.TranslateRequest(from, to, model, rawJSON, stream)
|
||||
}
|
||||
|
||||
// HasResponseTransformer inspects the default registry.
|
||||
func HasResponseTransformer(from, to Format) bool {
|
||||
return defaultRegistry.HasResponseTransformer(from, to)
|
||||
}
|
||||
|
||||
// TranslateStream is a helper on the default registry.
|
||||
func TranslateStream(ctx context.Context, from, to Format, model string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) []string {
|
||||
return defaultRegistry.TranslateStream(ctx, from, to, model, originalRequestRawJSON, requestRawJSON, rawJSON, param)
|
||||
}
|
||||
|
||||
// TranslateNonStream is a helper on the default registry.
|
||||
func TranslateNonStream(ctx context.Context, from, to Format, model string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) string {
|
||||
return defaultRegistry.TranslateNonStream(ctx, from, to, model, originalRequestRawJSON, requestRawJSON, rawJSON, param)
|
||||
}
|
||||
|
||||
// TranslateTokenCount is a helper on the default registry.
|
||||
func TranslateTokenCount(ctx context.Context, from, to Format, count int64, rawJSON []byte) string {
|
||||
return defaultRegistry.TranslateTokenCount(ctx, from, to, count, rawJSON)
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
// Package translator provides types and functions for converting chat requests and responses between different schemas.
|
||||
package translator
|
||||
|
||||
import "context"
|
||||
|
||||
// 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 RequestTransform func(model string, rawJSON []byte, stream bool) []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 strings, where each string is a chunk of the converted streaming response.
|
||||
type ResponseStreamTransform 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 ResponseNonStreamTransform func(ctx context.Context, model string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) 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 ResponseTokenCountTransform func(ctx context.Context, count int64) string
|
||||
|
||||
// ResponseTransform is a struct that groups together the functions for transforming streaming and non-streaming responses,
|
||||
// as well as token counts.
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user