@@ -194,6 +194,12 @@ func onHttpRequestBody(ctx wrapper.HttpContext, config PluginConfig, body []byte
194
194
ctx .SetContext (StreamContextKey , struct {}{})
195
195
}
196
196
identityKey := ctx .GetStringContext (IdentityKey , "" )
197
+ question := TrimQuote (bodyJson .Get (config .QuestionFrom .RequestBody ).String ())
198
+ if question == "" {
199
+ log .Debug ("parse question from request body failed" )
200
+ return types .ActionContinue
201
+ }
202
+ ctx .SetContext (QuestionContextKey , question )
197
203
err := config .redisClient .Get (config .CacheKeyPrefix + identityKey , func (response resp.Value ) {
198
204
if err := response .Error (); err != nil {
199
205
log .Errorf ("redis get failed, err:%v" , err )
@@ -230,13 +236,6 @@ func onHttpRequestBody(ctx wrapper.HttpContext, config PluginConfig, body []byte
230
236
_ = proxywasm .SendHttpResponseWithDetail (200 , "OK" , [][2 ]string {{"content-type" , "application/json; charset=utf-8" }}, res , - 1 )
231
237
return
232
238
}
233
- question := TrimQuote (bodyJson .Get (config .QuestionFrom .RequestBody ).String ())
234
- if question == "" {
235
- log .Debug ("parse question from request body failed" )
236
- _ = proxywasm .ResumeHttpRequest ()
237
- return
238
- }
239
- ctx .SetContext (QuestionContextKey , question )
240
239
fillHistoryCnt := getIntQueryParameter ("fill_history_cnt" , path , config .FillHistoryCnt ) * 2
241
240
currJson := bodyJson .Get ("messages" ).String ()
242
241
var currMessage []ChatHistory
@@ -317,38 +316,39 @@ func getIntQueryParameter(name string, path string, defaultValue int) int {
317
316
}
318
317
319
318
func processSSEMessage (ctx wrapper.HttpContext , config PluginConfig , sseMessage string , log wrapper.Log ) string {
320
- subMessages := strings .Split (sseMessage , "\n " )
321
- var message string
322
- for _ , msg := range subMessages {
323
- if strings .HasPrefix (msg , "data:" ) {
324
- message = msg
325
- break
319
+ content := ""
320
+ for _ , chunk := range strings .Split (sseMessage , "\n \n " ) {
321
+ subMessages := strings .Split (chunk , "\n " )
322
+ var message string
323
+ for _ , msg := range subMessages {
324
+ if strings .HasPrefix (msg , "data:" ) {
325
+ message = msg
326
+ break
327
+ }
326
328
}
327
- }
328
- if len (message ) < 6 {
329
- log .Errorf ("invalid message:%s" , message )
330
- return ""
331
- }
332
- // skip the prefix "data:"
333
- bodyJson := message [5 :]
334
- if gjson .Get (bodyJson , config .AnswerStreamValueFrom .ResponseBody ).Exists () {
335
- tempContentI := ctx .GetContext (AnswerContentContextKey )
336
- if tempContentI == nil {
337
- content := TrimQuote (gjson .Get (bodyJson , config .AnswerStreamValueFrom .ResponseBody ).Raw )
338
- ctx .SetContext (AnswerContentContextKey , content )
329
+ if len (message ) < 6 {
330
+ log .Errorf ("invalid message:%s" , message )
339
331
return content
340
332
}
341
- append := TrimQuote (gjson .Get (bodyJson , config .AnswerStreamValueFrom .ResponseBody ).Raw )
342
- content := tempContentI .(string ) + append
343
- ctx .SetContext (AnswerContentContextKey , content )
344
- return content
345
- } else if gjson .Get (bodyJson , "choices.0.delta.content.tool_calls" ).Exists () {
346
- // TODO: compatible with other providers
347
- ctx .SetContext (ToolCallsContextKey , struct {}{})
348
- return ""
333
+ // skip the prefix "data:"
334
+ bodyJson := message [5 :]
335
+ if gjson .Get (bodyJson , config .AnswerStreamValueFrom .ResponseBody ).Exists () {
336
+ tempContentI := ctx .GetContext (AnswerContentContextKey )
337
+ if tempContentI == nil {
338
+ content = TrimQuote (gjson .Get (bodyJson , config .AnswerStreamValueFrom .ResponseBody ).Raw )
339
+ ctx .SetContext (AnswerContentContextKey , content )
340
+ } else {
341
+ append := TrimQuote (gjson .Get (bodyJson , config .AnswerStreamValueFrom .ResponseBody ).Raw )
342
+ content = tempContentI .(string ) + append
343
+ ctx .SetContext (AnswerContentContextKey , content )
344
+ }
345
+ } else if gjson .Get (bodyJson , "choices.0.delta.content.tool_calls" ).Exists () {
346
+ // TODO: compatible with other providers
347
+ ctx .SetContext (ToolCallsContextKey , struct {}{})
348
+ }
349
+ log .Debugf ("unknown message:%s" , bodyJson )
349
350
}
350
- log .Debugf ("unknown message:%s" , bodyJson )
351
- return ""
351
+ return content
352
352
}
353
353
354
354
func onHttpResponseHeaders (ctx wrapper.HttpContext , config PluginConfig , log wrapper.Log ) types.Action {
0 commit comments