@@ -11,6 +11,7 @@ import (
1111 cohereschema "github.com/envoyproxy/ai-gateway/internal/apischema/cohere"
1212 "github.com/envoyproxy/ai-gateway/internal/apischema/openai"
1313 "github.com/envoyproxy/ai-gateway/internal/dynamic_module/sdk"
14+ "github.com/envoyproxy/ai-gateway/internal/filterapi"
1415 "github.com/envoyproxy/ai-gateway/internal/internalapi"
1516 openaisdk "github.com/openai/openai-go/v2"
1617)
3334 originalRequestBody interface {}
3435 originalRequestBodyRaw []byte
3536 }
36-
37- requestBodyParserFn func (body []byte ) (parsed interface {}, modelName string , err error )
3837)
3938
4039func newRouterFilterConfig (fcr * filterConfigReceiverImpl ) * routerFilterConfig {
@@ -59,6 +58,11 @@ func (f *routerFilterConfig) NewFilter() sdk.HTTPFilter {
5958 return & routerFilter {fc : f }
6059}
6160
61+ // filterConfig is a helper that returns the filter config for the current stream.
62+ func (f * routerFilter ) filterConfig () * filterapi.RuntimeConfig {
63+ return f .fc .fcr .fc
64+ }
65+
6266// RequestHeaders implements [sdk.HTTPFilter].
6367func (f * routerFilter ) RequestHeaders (e sdk.EnvoyHTTPFilter , _ bool ) sdk.RequestHeadersStatus {
6468 p , _ := e .GetRequestHeader (":path" ) // The :path pseudo header is always present.
@@ -110,24 +114,24 @@ func (f *routerFilter) RequestBody(e sdk.EnvoyHTTPFilter, endOfStream bool) sdk.
110114 return sdk .RequestBodyStatusStopIterationAndBuffer
111115 }
112116 f .originalRequestBodyRaw = raw
113- var parserFn requestBodyParserFn
117+ var parsed any
118+ var modelName string
114119 switch f .endpoint {
115120 case chatCompletionsEndpoint :
116- parserFn = chatCompletionsBodyParser
121+ parsed , modelName , err = modelBodyParser ( raw , func ( req * openai. ChatCompletionRequest ) string { return req . Model })
117122 case completionsEndpoint :
118- parserFn = completionsBodyParser
123+ parsed , modelName , err = modelBodyParser ( raw , func ( req * openai. CompletionRequest ) string { return req . Model })
119124 case embeddingsEndpoint :
120- parserFn = embeddingsBodyParser
125+ parsed , modelName , err = modelBodyParser ( raw , func ( req * openai. EmbeddingRequest ) string { return req . Model })
121126 case imagesGenerationsEndpoint :
122- parserFn = imagesGenerationsBodyParser
127+ parsed , modelName , err = modelBodyParser ( raw , func ( req * openaisdk. ImageGenerateParams ) string { return req . Model })
123128 case rerankEndpoint :
124- parserFn = rerankBodyParser
129+ parsed , modelName , err = modelBodyParser ( raw , func ( req * cohereschema. RerankV2Request ) string { return req . Model })
125130 case messagesEndpoint :
126- parserFn = messagesBodyParser
131+ parsed , modelName , err = modelBodyParser ( raw , func ( req * anthropic. MessagesRequest ) string { return req . GetModel () })
127132 default :
128133 e .SendLocalReply (500 , nil , []byte ("BUG: unsupported endpoint at body parsing: " + fmt .Sprintf ("%d" , f .endpoint )))
129134 }
130- parsed , modelName , err := parserFn (raw )
131135 if err != nil {
132136 e .SendLocalReply (400 , nil , []byte ("failed to parse request body: " + err .Error ()))
133137 return sdk .RequestBodyStatusStopIterationAndBuffer
@@ -164,50 +168,10 @@ func (f *routerFilter) handleModelsEndpoint(e sdk.EnvoyHTTPFilter) sdk.RequestHe
164168 return sdk .RequestHeadersStatusStopIteration
165169}
166170
167- func chatCompletionsBodyParser (body []byte ) (interface {}, string , error ) {
168- var req openai.ChatCompletionRequest
169- if err := json .Unmarshal (body , & req ); err != nil {
170- return nil , "" , fmt .Errorf ("failed to unmarshal body: %w" , err )
171- }
172- return req , req .Model , nil
173- }
174-
175- func completionsBodyParser (body []byte ) (interface {}, string , error ) {
176- var req openai.CompletionRequest
171+ func modelBodyParser [T any ](body []byte , modelExtractFn func (req * T ) string ) (interface {}, string , error ) {
172+ var req T
177173 if err := json .Unmarshal (body , & req ); err != nil {
178174 return nil , "" , fmt .Errorf ("failed to unmarshal body: %w" , err )
179175 }
180- return req , req .Model , nil
181- }
182-
183- func embeddingsBodyParser (body []byte ) (interface {}, string , error ) {
184- var req openai.EmbeddingRequest
185- if err := json .Unmarshal (body , & req ); err != nil {
186- return nil , "" , fmt .Errorf ("failed to unmarshal body: %w" , err )
187- }
188- return req , req .Model , nil
189- }
190-
191- func imagesGenerationsBodyParser (body []byte ) (interface {}, string , error ) {
192- var req openaisdk.ImageGenerateParams
193- if err := json .Unmarshal (body , & req ); err != nil {
194- return nil , "" , fmt .Errorf ("failed to unmarshal body: %w" , err )
195- }
196- return req , req .Model , nil
197- }
198-
199- func rerankBodyParser (body []byte ) (interface {}, string , error ) {
200- var req cohereschema.RerankV2Request
201- if err := json .Unmarshal (body , & req ); err != nil {
202- return nil , "" , fmt .Errorf ("failed to unmarshal body: %w" , err )
203- }
204- return req , req .Model , nil
205- }
206-
207- func messagesBodyParser (body []byte ) (interface {}, string , error ) {
208- var anthropicReq anthropic.MessagesRequest
209- if err := json .Unmarshal (body , & anthropicReq ); err != nil {
210- return nil , "" , fmt .Errorf ("failed to unmarshal body: %w" , err )
211- }
212- return anthropicReq , anthropicReq .GetModel (), nil
176+ return req , modelExtractFn (& req ), nil
213177}
0 commit comments