Improve request context handling in chat streaming

- Use request context for client connection tracking
- Replace CloseNotify with context cancellation
- Enhance streaming method to handle request context termination
This commit is contained in:
Max 2025-03-04 12:03:19 +08:00
parent 709c7ec738
commit f469568d85

View file

@ -319,7 +319,7 @@ func (ast *Assistant) handleChatStream(c *gin.Context, ctx chatctx.Context, mess
var result interface{} = nil
var err error = nil
// Chat with AI in background
requestCtx := c.Request.Context()
go func() {
var res interface{} = nil
res, err = ast.streamChat(c, ctx, messages, options, clientBreak, contents, callback...)
@ -338,7 +338,8 @@ func (ast *Assistant) handleChatStream(c *gin.Context, ctx chatctx.Context, mess
return nil, err
}
return result, nil
case <-c.Writer.CloseNotify():
case <-requestCtx.Done():
clientBreak <- true
return nil, nil
}