@@ -44,9 +44,10 @@ func parseGlobalConfig(json gjson.Result, pluginConfig *config.PluginConfig, log
4444 if err := pluginConfig .Validate (); err != nil {
4545 return err
4646 }
47- if err := pluginConfig .Complete (); err != nil {
47+ if err := pluginConfig .Complete (log ); err != nil {
4848 return err
4949 }
50+
5051 return nil
5152}
5253
@@ -59,9 +60,10 @@ func parseOverrideRuleConfig(json gjson.Result, global config.PluginConfig, plug
5960 if err := pluginConfig .Validate (); err != nil {
6061 return err
6162 }
62- if err := pluginConfig .Complete (); err != nil {
63+ if err := pluginConfig .Complete (log ); err != nil {
6364 return err
6465 }
66+
6567 return nil
6668}
6769
@@ -80,7 +82,13 @@ func onHttpRequestHeader(ctx wrapper.HttpContext, pluginConfig config.PluginConf
8082 path , _ := url .Parse (rawPath )
8183 apiName := getOpenAiApiName (path .Path )
8284 providerConfig := pluginConfig .GetProviderConfig ()
83- if apiName == "" && ! providerConfig .IsOriginal () {
85+ if providerConfig .IsOriginal () {
86+ if handler , ok := activeProvider .(provider.ApiNameHandler ); ok {
87+ apiName = handler .GetApiName (path .Path )
88+ }
89+ }
90+
91+ if apiName == "" {
8492 log .Debugf ("[onHttpRequestHeader] unsupported path: %s" , path .Path )
8593 // _ = util.SendResponse(404, "ai-proxy.unknown_api", util.MimeTypeTextPlain, "API not found: "+path.Path)
8694 log .Debugf ("[onHttpRequestHeader] no send response" )
@@ -89,8 +97,11 @@ func onHttpRequestHeader(ctx wrapper.HttpContext, pluginConfig config.PluginConf
8997 ctx .SetContext (ctxKeyApiName , apiName )
9098
9199 if handler , ok := activeProvider .(provider.RequestHeadersHandler ); ok {
92- // Disable the route re-calculation since the plugin may modify some headers related to the chosen route.
100+ // Disable the route re-calculation since the plugin may modify some headers related to the chosen route.
93101 ctx .DisableReroute ()
102+ // Set the apiToken for the current request.
103+ providerConfig .SetApiTokenInUse (ctx , log )
104+
94105 hasRequestBody := wrapper .HasRequestBody ()
95106 action , err := handler .OnRequestHeaders (ctx , apiName , log )
96107 if err == nil {
@@ -102,6 +113,7 @@ func onHttpRequestHeader(ctx wrapper.HttpContext, pluginConfig config.PluginConf
102113 }
103114 return action
104115 }
116+
105117 _ = util .SendResponse (500 , "ai-proxy.proc_req_headers_failed" , util .MimeTypeTextPlain , fmt .Sprintf ("failed to process request headers: %v" , err ))
106118 return types .ActionContinue
107119 }
@@ -156,15 +168,24 @@ func onHttpResponseHeaders(ctx wrapper.HttpContext, pluginConfig config.PluginCo
156168
157169 log .Debugf ("[onHttpResponseHeaders] provider=%s" , activeProvider .GetProviderType ())
158170
171+ providerConfig := pluginConfig .GetProviderConfig ()
172+ apiTokenInUse := providerConfig .GetApiTokenInUse (ctx )
173+
159174 status , err := proxywasm .GetHttpResponseHeader (":status" )
160175 if err != nil || status != "200" {
161176 if err != nil {
162177 log .Errorf ("unable to load :status header from response: %v" , err )
163178 }
164179 ctx .DontReadResponseBody ()
180+ providerConfig .OnRequestFailed (ctx , apiTokenInUse , log )
181+
165182 return types .ActionContinue
166183 }
167184
185+ // Reset ctxApiTokenRequestFailureCount if the request is successful,
186+ // the apiToken is removed only when the number of consecutive request failures exceeds the threshold.
187+ providerConfig .ResetApiTokenRequestFailureCount (apiTokenInUse , log )
188+
168189 if handler , ok := activeProvider .(provider.ResponseHeadersHandler ); ok {
169190 apiName , _ := ctx .GetContext (ctxKeyApiName ).(provider.ApiName )
170191 action , err := handler .OnResponseHeaders (ctx , apiName , log )
@@ -233,16 +254,6 @@ func onHttpResponseBody(ctx wrapper.HttpContext, pluginConfig config.PluginConfi
233254 return types .ActionContinue
234255}
235256
236- func getOpenAiApiName (path string ) provider.ApiName {
237- if strings .HasSuffix (path , "/v1/chat/completions" ) {
238- return provider .ApiNameChatCompletion
239- }
240- if strings .HasSuffix (path , "/v1/embeddings" ) {
241- return provider .ApiNameEmbeddings
242- }
243- return ""
244- }
245-
246257func checkStream (ctx * wrapper.HttpContext , log * wrapper.Log ) {
247258 contentType , err := proxywasm .GetHttpResponseHeader ("Content-Type" )
248259 if err != nil || ! strings .HasPrefix (contentType , "text/event-stream" ) {
@@ -252,3 +263,13 @@ func checkStream(ctx *wrapper.HttpContext, log *wrapper.Log) {
252263 (* ctx ).BufferResponseBody ()
253264 }
254265}
266+
267+ func getOpenAiApiName (path string ) provider.ApiName {
268+ if strings .HasSuffix (path , "/v1/chat/completions" ) {
269+ return provider .ApiNameChatCompletion
270+ }
271+ if strings .HasSuffix (path , "/v1/embeddings" ) {
272+ return provider .ApiNameEmbeddings
273+ }
274+ return ""
275+ }
0 commit comments