fix: 修复流式对话日志丢失问题

This commit is contained in:
fuzhongyun 2026-01-07 10:31:20 +08:00
parent 3a7925fb55
commit 4452fb1568
1 changed files with 29 additions and 1 deletions

View File

@ -284,14 +284,42 @@ func (d *Do) startMessageHandler(
}() }()
streamText := ""
streamIndex := ""
for v := range requireData.Ch { // 自动检测通道关闭 for v := range requireData.Ch { // 自动检测通道关闭
if err := sendWithTimeout(client, v, 10*time.Second); err != nil { if err := sendWithTimeout(client, v, 10*time.Second); err != nil {
log.Errorf("Send error: %v", err) log.Errorf("Send error: %v", err)
return return
} }
if v.Type == entitys.ResponseText || v.Type == entitys.ResponseStream || v.Type == entitys.ResponseJson { // 文本+卡片
if v.Type == entitys.ResponseText || v.Type == entitys.ResponseJson {
chat = append(chat, v) chat = append(chat, v)
} }
// 流式-追加
if v.Type == entitys.ResponseStream {
streamText += v.Content
streamIndex = v.Index
}
// 流式-阶段结束|对话结束
if streamText != "" && v.Type != entitys.ResponseStream {
chat = append(chat, entitys.Response{
Content: streamText,
Type: entitys.ResponseText,
Index: streamIndex,
})
streamText = ""
streamIndex = ""
}
}
// 流式结束
if streamText != "" {
chat = append(chat, entitys.Response{
Content: streamText,
Type: entitys.ResponseText,
Index: streamIndex,
})
streamText = ""
streamIndex = ""
} }
}() }()