@@ -102,21 +102,21 @@ func onHttpRequestHeader(ctx wrapper.HttpContext, pluginConfig config.PluginConf
102
102
103
103
// Always remove the Accept-Encoding header to prevent the LLM from sending compressed responses,
104
104
// allowing plugins to inspect or modify the response correctly
105
- proxywasm .RemoveHttpRequestHeader ("Accept-Encoding" )
105
+ _ = proxywasm .RemoveHttpRequestHeader ("Accept-Encoding" )
106
106
107
107
if handler , ok := activeProvider .(provider.RequestHeadersHandler ); ok {
108
108
// Set the apiToken for the current request.
109
109
providerConfig .SetApiTokenInUse (ctx , log )
110
110
111
111
err := handler .OnRequestHeaders (ctx , apiName , log )
112
112
if err != nil {
113
- util .ErrorHandler ("ai-proxy.proc_req_headers_failed" , fmt .Errorf ("failed to process request headers: %v" , err ))
113
+ _ = util .ErrorHandler ("ai-proxy.proc_req_headers_failed" , fmt .Errorf ("failed to process request headers: %v" , err ))
114
114
return types .ActionContinue
115
115
}
116
116
117
117
hasRequestBody := wrapper .HasRequestBody ()
118
118
if hasRequestBody {
119
- proxywasm .RemoveHttpRequestHeader ("Content-Length" )
119
+ _ = proxywasm .RemoveHttpRequestHeader ("Content-Length" )
120
120
ctx .SetRequestBodyBufferLimit (defaultMaxBodyBytes )
121
121
// Delay the header processing to allow changing in OnRequestBody
122
122
return types .HeaderStopIteration
@@ -143,7 +143,7 @@ func onHttpRequestBody(ctx wrapper.HttpContext, pluginConfig config.PluginConfig
143
143
144
144
newBody , settingErr := pluginConfig .GetProviderConfig ().ReplaceByCustomSettings (body )
145
145
if settingErr != nil {
146
- util .ErrorHandler (
146
+ _ = util .ErrorHandler (
147
147
"ai-proxy.proc_req_body_failed" ,
148
148
fmt .Errorf ("failed to replace request body by custom settings: %v" , settingErr ),
149
149
)
@@ -156,7 +156,7 @@ func onHttpRequestBody(ctx wrapper.HttpContext, pluginConfig config.PluginConfig
156
156
if err == nil {
157
157
return action
158
158
}
159
- util .ErrorHandler ("ai-proxy.proc_req_body_failed" , fmt .Errorf ("failed to process request body: %v" , err ))
159
+ _ = util .ErrorHandler ("ai-proxy.proc_req_body_failed" , fmt .Errorf ("failed to process request body: %v" , err ))
160
160
}
161
161
return types .ActionContinue
162
162
}
@@ -205,7 +205,11 @@ func onHttpResponseHeaders(ctx wrapper.HttpContext, pluginConfig config.PluginCo
205
205
206
206
checkStream (ctx , log )
207
207
_ , needHandleBody := activeProvider .(provider.TransformResponseBodyHandler )
208
- _ , needHandleStreamingBody := activeProvider .(provider.StreamingResponseBodyHandler )
208
+ var needHandleStreamingBody bool
209
+ _ , needHandleStreamingBody = activeProvider .(provider.StreamingResponseBodyHandler )
210
+ if ! needHandleStreamingBody {
211
+ _ , needHandleStreamingBody = activeProvider .(provider.StreamingEventHandler )
212
+ }
209
213
if ! needHandleBody && ! needHandleStreamingBody {
210
214
ctx .DontReadResponseBody ()
211
215
} else if ! needHandleStreamingBody {
@@ -224,7 +228,7 @@ func onStreamingResponseBody(ctx wrapper.HttpContext, pluginConfig config.Plugin
224
228
}
225
229
226
230
log .Debugf ("[onStreamingResponseBody] provider=%s" , activeProvider .GetProviderType ())
227
- log .Debugf ("isLastChunk=%v chunk: %s" , isLastChunk , string (chunk ))
231
+ log .Debugf ("[onStreamingResponseBody] isLastChunk=%v chunk: %s" , isLastChunk , string (chunk ))
228
232
229
233
if handler , ok := activeProvider .(provider.StreamingResponseBodyHandler ); ok {
230
234
apiName , _ := ctx .GetContext (provider .CtxKeyApiName ).(provider.ApiName )
@@ -234,6 +238,38 @@ func onStreamingResponseBody(ctx wrapper.HttpContext, pluginConfig config.Plugin
234
238
}
235
239
return chunk
236
240
}
241
+ if handler , ok := activeProvider .(provider.StreamingEventHandler ); ok {
242
+ apiName , _ := ctx .GetContext (provider .CtxKeyApiName ).(provider.ApiName )
243
+ events := provider .ExtractStreamingEvents (ctx , chunk , log )
244
+ log .Debugf ("[onStreamingResponseBody] %d events received" , len (events ))
245
+ if len (events ) == 0 {
246
+ // No events are extracted, return the original chunk
247
+ return chunk
248
+ }
249
+ var responseBuilder strings.Builder
250
+ for _ , event := range events {
251
+ log .Debugf ("processing event: %v" , event )
252
+
253
+ if event .IsEndData () {
254
+ responseBuilder .WriteString (event .ToHttpString ())
255
+ continue
256
+ }
257
+
258
+ outputEvents , err := handler .OnStreamingEvent (ctx , apiName , event , log )
259
+ if err != nil {
260
+ log .Errorf ("[onStreamingResponseBody] failed to process streaming event: %v\n %s" , err , chunk )
261
+ return chunk
262
+ }
263
+ if outputEvents == nil || len (outputEvents ) == 0 {
264
+ responseBuilder .WriteString (event .ToHttpString ())
265
+ } else {
266
+ for _ , outputEvent := range outputEvents {
267
+ responseBuilder .WriteString (outputEvent .ToHttpString ())
268
+ }
269
+ }
270
+ }
271
+ return []byte (responseBuilder .String ())
272
+ }
237
273
return chunk
238
274
}
239
275
@@ -251,11 +287,11 @@ func onHttpResponseBody(ctx wrapper.HttpContext, pluginConfig config.PluginConfi
251
287
apiName , _ := ctx .GetContext (provider .CtxKeyApiName ).(provider.ApiName )
252
288
body , err := handler .TransformResponseBody (ctx , apiName , body , log )
253
289
if err != nil {
254
- util .ErrorHandler ("ai-proxy.proc_resp_body_failed" , fmt .Errorf ("failed to process response body: %v" , err ))
290
+ _ = util .ErrorHandler ("ai-proxy.proc_resp_body_failed" , fmt .Errorf ("failed to process response body: %v" , err ))
255
291
return types .ActionContinue
256
292
}
257
293
if err = provider .ReplaceResponseBody (body , log ); err != nil {
258
- util .ErrorHandler ("ai-proxy.replace_resp_body_failed" , fmt .Errorf ("failed to replace response body: %v" , err ))
294
+ _ = util .ErrorHandler ("ai-proxy.replace_resp_body_failed" , fmt .Errorf ("failed to replace response body: %v" , err ))
259
295
}
260
296
}
261
297
return types .ActionContinue
0 commit comments