@@ -20,24 +20,13 @@ import (
2020 "fmt"
2121 "slices"
2222
23- "github.com/openai/openai-go/v3" //imported as openai
2423 bbrplugins "sigs.k8s.io/gateway-api-inference-extension/pkg/bbr/plugins"
2524)
2625
2726// -------------------- INTERFACES -----------------------------------------------------------------------
2827// Interfaces are defined in "sigs.k8s.io/gateway-api-inference-extension/pkg/bbr/framework/interfaces.go"
2928
3029// --------------------- PluginRegistry implementation ---------------------------------------------------
31- // --------------------- Constructors lookup map ---------------------------------------------------------
32- // This lookup map should be extended when a new concrete plugin implementation is added
33- /* var pluginConstructors = map[string]PluginFactoryFunc{
34- "simple-model-extractor": NewSimpleModelExtractor,
35- "semantic-model-selector": NewSemanticModelSelector,
36- "bad-words-blocker": NewBadWordsBlocker,
37- "pid-disclosure-blocker": NewPidDisclosureBlocker,
38- "extract-metadata-bykeys": NewExtractMetadataByKeys,
39- } */
40- // Registration: of constructors for implementations is done in the main's initPlugins based on the ConfigMap
4130
4231// pluginRegistry implements PluginRegistry
4332type pluginRegistry struct {
@@ -123,15 +112,6 @@ func (r *pluginRegistry) UnregisterFactory(typeKey string) error {
123112 return fmt .Errorf ("plugin (%s) not found" , typeKey )
124113}
125114
126- // Removes a plugin instance by type key
127- func (r * pluginRegistry ) UnregisterPlugin (typeKey string ) error {
128- if _ , ok := r .plugins [typeKey ]; ok {
129- delete (r .plugins , typeKey )
130- return nil
131- }
132- return fmt .Errorf ("plugin (%s) not found" , typeKey )
133- }
134-
135115// ListPlugins lists all registered plugins
136116func (r * pluginRegistry ) ListPlugins () []string {
137117 typeKeys := make ([]string , 0 , len (r .plugins ))
@@ -160,12 +140,6 @@ func (r *pluginRegistry) GetPlugins() map[string]bbrplugins.BBRPlugin {
160140 return r .plugins
161141}
162142
163- // Clear removes all registered factories and plugins
164- func (r * pluginRegistry ) Clear () {
165- r .pluginsFactory = make (map [string ]PluginFactoryFunc )
166- r .plugins = make (map [string ]bbrplugins.BBRPlugin )
167- }
168-
169143// Checks for presense of a factory in this registry
170144func (r * pluginRegistry ) ContainsFactory (typeKey string ) bool {
171145 _ , exists := r .pluginsFactory [typeKey ]
@@ -186,17 +160,13 @@ func (r *pluginRegistry) String() string {
186160
187161// PluginsChain is a sequence of plugins to be executed in order inside the ext_proc server
188162type pluginsChain struct {
189- plugins []string
190- sharedChatCompletion openai.ChatCompletionNewParams //will be nil if an instance of pluginsChain does not contain a plugin that requires full parsing
191- sharedCompletion openai.CompletionNewParams //likewise
163+ plugins []string
192164}
193165
194166// NewPluginsChain creates a new PluginsChain instance
195167func NewPluginsChain () PluginsChain {
196168 return & pluginsChain {
197- plugins : []string {},
198- sharedChatCompletion : openai.ChatCompletionNewParams {},
199- sharedCompletion : openai.CompletionNewParams {},
169+ plugins : []string {},
200170 }
201171}
202172
@@ -212,17 +182,6 @@ func (pc *pluginsChain) AddPlugin(typeKey string, r PluginRegistry) error {
212182 return nil
213183}
214184
215- // DeletePlugin deletes a plugin from the chain
216- func (pc * pluginsChain ) DeletePlugin (p string ) error {
217- for i := range len (pc .plugins ) {
218- if pc .plugins [i ] == p {
219- pc .plugins = append (pc .plugins [:i ], pc .plugins [i + 1 :]... )
220- return nil
221- }
222- }
223- return fmt .Errorf ("plugin %s not found in chain" , p )
224- }
225-
226185// GetPlugin retrieves the next plugin in the chain by index
227186func (pc * pluginsChain ) GetPlugin (index int , r PluginRegistry ) (bbrplugins.BBRPlugin , error ) {
228187 if index < 0 || index >= len (pc .plugins ) {
@@ -255,36 +214,8 @@ func (pc *pluginsChain) AddPluginAtInd(typeKey string, i int, r PluginRegistry)
255214 return nil
256215}
257216
258- func (pc * pluginsChain ) ParseChatCompletion (data []byte ) (openai.ChatCompletionNewParams , error ) {
259- if err := pc .sharedChatCompletion .UnmarshalJSON (data ); err != nil {
260- return pc .sharedChatCompletion , err
261- }
262- return pc .sharedChatCompletion , nil
263- }
264-
265- func (pc * pluginsChain ) ParseCompletion (data []byte ) (openai.CompletionNewParams , error ) {
266- if err := pc .sharedCompletion .UnmarshalJSON (data ); err != nil {
267- return pc .sharedCompletion , err
268- }
269- return pc .sharedCompletion , nil
270- }
271-
272- func (pc * pluginsChain ) GetSharedChatCompletion () openai.ChatCompletionNewParams {
273- return pc .sharedChatCompletion
274- }
275-
276- func (pc * pluginsChain ) GetSharedCompletion () openai.CompletionNewParams {
277- return pc .sharedCompletion
278- }
279-
280- func (pc * pluginsChain ) GetSharedMemory (which string ) interface {} {
281- if which == "/v1/completions" {
282- return pc .sharedCompletion
283- }
284- if which == "/v1/chat/completions" {
285- return pc .sharedChatCompletion
286- }
287- return nil
217+ func (pc * pluginsChain ) GetPlugins () []string {
218+ return pc .plugins
288219}
289220
290221// MergeMaps copies all key/value pairs from src into dst and returns dst.
@@ -315,7 +246,7 @@ func (pc *pluginsChain) Run(
315246 bodyBytes []byte ,
316247 metaDataKeys []string ,
317248 r PluginRegistry ,
318- ) (mutateBodyBytes [] byte , headers map [ string ] string , err error ) {
249+ ) (headers map [ string ] string , mutateBodyBytes [] byte , err error ) {
319250
320251 allHeaders := make (map [string ]string )
321252 mutatedBodyBytes := bodyBytes
@@ -327,20 +258,20 @@ func (pc *pluginsChain) Run(
327258 metExtPlugin , err := r .GetPlugin (pluginType )
328259
329260 if err != nil {
330- return bodyBytes , allHeaders , err
261+ return allHeaders , bodyBytes , err
331262 }
332263
333264 // The plugin i in the chain receives the (potentially mutated) body from plugin i-1 in the chain
334265 headers , mutatedBodyBytes , err := metExtPlugin .Execute (mutatedBodyBytes , metaDataKeys )
335266
336267 if err != nil {
337- return mutatedBodyBytes , headers , err
268+ return headers , mutatedBodyBytes , err
338269 }
339270
340271 //note that the existing overlapping keys are NOT over-written by merge
341272 MergeMaps (allHeaders , headers )
342273 }
343- return
274+ return allHeaders , mutatedBodyBytes , nil
344275}
345276
346277func (pc * pluginsChain ) String () string {
0 commit comments