|
| 1 | +/* |
| 2 | +Copyright 2026. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package api_translation |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "fmt" |
| 23 | + |
| 24 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/bbr/framework" |
| 25 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/framework/interface/plugin" |
| 26 | + |
| 27 | + "github.com/opendatahub-io/ai-gateway-payload-processing/pkg/plugins/api-translation/translator" |
| 28 | + "github.com/opendatahub-io/ai-gateway-payload-processing/pkg/plugins/api-translation/translator/anthropic" |
| 29 | + "github.com/opendatahub-io/ai-gateway-payload-processing/pkg/plugins/api-translation/translator/azureopenai" |
| 30 | + "github.com/opendatahub-io/ai-gateway-payload-processing/pkg/plugins/api-translation/translator/vertex" |
| 31 | + "github.com/opendatahub-io/ai-gateway-payload-processing/pkg/plugins/common/provider" |
| 32 | + "github.com/opendatahub-io/ai-gateway-payload-processing/pkg/plugins/common/state" |
| 33 | +) |
| 34 | + |
| 35 | +const ( |
| 36 | + APITranslationPluginType = "api-translation" |
| 37 | +) |
| 38 | + |
| 39 | +// compile-time type validation |
| 40 | +var _ framework.RequestProcessor = &APITranslationPlugin{} |
| 41 | +var _ framework.ResponseProcessor = &APITranslationPlugin{} |
| 42 | + |
| 43 | +// APITranslationFactory defines the factory function for APITranslationPlugin. |
| 44 | +func APITranslationFactory(name string, _ json.RawMessage, _ framework.Handle) (framework.BBRPlugin, error) { |
| 45 | + return NewAPITranslationPlugin().WithName(name), nil |
| 46 | +} |
| 47 | + |
| 48 | +// NewAPITranslationPlugin creates a new plugin instance with all registered providers. |
| 49 | +func NewAPITranslationPlugin() *APITranslationPlugin { |
| 50 | + return &APITranslationPlugin{ |
| 51 | + typedName: plugin.TypedName{ |
| 52 | + Type: APITranslationPluginType, |
| 53 | + Name: APITranslationPluginType, |
| 54 | + }, |
| 55 | + providers: map[string]translator.Translator{ |
| 56 | + provider.Anthropic: anthropic.NewAnthropicTranslator(), |
| 57 | + provider.AzureOpenAI: azureopenai.NewAzureOpenAITranslator(), |
| 58 | + provider.Vertex: vertex.NewVertexTranslator(), |
| 59 | + }, |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// APITranslationPlugin translates inference API requests and responses between |
| 64 | +// OpenAI Chat Completions format and provider-native formats (e.g., Anthropic Messages API). |
| 65 | +type APITranslationPlugin struct { |
| 66 | + typedName plugin.TypedName |
| 67 | + providers map[string]translator.Translator // map from provider name to translator interface |
| 68 | +} |
| 69 | + |
| 70 | +// TypedName returns the type and name tuple of this plugin instance. |
| 71 | +func (p *APITranslationPlugin) TypedName() plugin.TypedName { |
| 72 | + return p.typedName |
| 73 | +} |
| 74 | + |
| 75 | +// WithName sets the name of the plugin instance. |
| 76 | +func (p *APITranslationPlugin) WithName(name string) *APITranslationPlugin { |
| 77 | + p.typedName.Name = name |
| 78 | + return p |
| 79 | +} |
| 80 | + |
| 81 | +// ProcessRequest reads the provider from CycleState (set by an upstream plugin) and translates |
| 82 | +// the request body from OpenAI format to the provider's native format if needed. |
| 83 | +func (p *APITranslationPlugin) ProcessRequest(ctx context.Context, cycleState *framework.CycleState, request *framework.InferenceRequest) error { |
| 84 | + if request == nil || request.Headers == nil || request.Body == nil { |
| 85 | + return fmt.Errorf("invalid inference request: request/headers/body must be non-nil") |
| 86 | + } |
| 87 | + |
| 88 | + providerName, err := framework.ReadCycleStateKey[string](cycleState, state.ProviderKey) // err if not found |
| 89 | + if err != nil || providerName == "" || providerName == "openai" { // empty provider means no translation needed |
| 90 | + return nil |
| 91 | + } |
| 92 | + |
| 93 | + translator, ok := p.providers[providerName] |
| 94 | + if !ok { |
| 95 | + return fmt.Errorf("unsupported provider - '%s'", providerName) |
| 96 | + } |
| 97 | + |
| 98 | + translatedBody, headersToMutate, headersToRemove, err := translator.TranslateRequest(request.Body) |
| 99 | + if err != nil { |
| 100 | + return fmt.Errorf("request translation failed for provider '%s' - %w", providerName, err) |
| 101 | + } |
| 102 | + |
| 103 | + if translatedBody != nil { |
| 104 | + request.SetBody(translatedBody) |
| 105 | + } |
| 106 | + |
| 107 | + for key, value := range headersToMutate { |
| 108 | + request.SetHeader(key, value) |
| 109 | + } |
| 110 | + for _, key := range headersToRemove { |
| 111 | + request.RemoveHeader(key) |
| 112 | + } |
| 113 | + |
| 114 | + // authorization is a special header removed by the plugin, no matter which provider is used. |
| 115 | + // The api-key is expected to be set by the the api-key injection plugin. |
| 116 | + request.RemoveHeader("authorization") |
| 117 | + |
| 118 | + // content-length is another special header that will be set automatically by the pluggable framework when the body is mutated. |
| 119 | + |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +// ProcessResponse reads the provider from CycleState and translates the response |
| 124 | +// back to OpenAI Chat Completions format if needed. |
| 125 | +func (p *APITranslationPlugin) ProcessResponse(ctx context.Context, cycleState *framework.CycleState, response *framework.InferenceResponse) error { |
| 126 | + if response == nil || response.Headers == nil || response.Body == nil { |
| 127 | + return fmt.Errorf("invalid inference response: response/headers/body must be non-nil") |
| 128 | + } |
| 129 | + |
| 130 | + providerName, err := framework.ReadCycleStateKey[string](cycleState, state.ProviderKey) // err if not found |
| 131 | + if err != nil || providerName == "" || providerName == "openai" { // empty provider means no translation needed |
| 132 | + return nil |
| 133 | + } |
| 134 | + |
| 135 | + translator, ok := p.providers[providerName] |
| 136 | + if !ok { |
| 137 | + return fmt.Errorf("unsupported provider - '%s'", providerName) |
| 138 | + } |
| 139 | + |
| 140 | + model, _ := framework.ReadCycleStateKey[string](cycleState, state.ModelKey) |
| 141 | + |
| 142 | + translatedBody, err := translator.TranslateResponse(response.Body, model) |
| 143 | + if err != nil { |
| 144 | + return fmt.Errorf("response translation failed for provider '%s' - %w", providerName, err) |
| 145 | + } |
| 146 | + |
| 147 | + if translatedBody != nil { |
| 148 | + response.SetBody(translatedBody) |
| 149 | + } |
| 150 | + |
| 151 | + return nil |
| 152 | +} |
0 commit comments