fix(chat): include history in model fallback

This commit is contained in:
c
2026-05-01 13:23:50 +00:00
committed by lyingbug
parent 8955bf1aa8
commit 48aeafcc3f
3 changed files with 162 additions and 10 deletions
@@ -740,11 +740,7 @@ func (s *sessionService) handleModelFallback(ctx context.Context, chatManage *ty
}
// Start streaming response
userMsg := chat.Message{Role: "user", Content: promptContent}
if chatManage.ChatModelSupportsVision && len(chatManage.Images) > 0 {
userMsg.Images = chatManage.Images
}
responseChan, err := chatModel.ChatStream(ctx, []chat.Message{userMsg}, opt)
responseChan, err := chatModel.ChatStream(ctx, buildFallbackMessages(chatManage, promptContent), opt)
if err != nil {
logger.Errorf(ctx, "Failed to start streaming fallback response: %v, falling back to fixed response", err)
s.handleFixedFallback(ctx, chatManage)
@@ -761,6 +757,18 @@ func (s *sessionService) handleModelFallback(ctx context.Context, chatManage *ty
go s.consumeFallbackStream(ctx, chatManage, responseChan)
}
func buildFallbackMessages(chatManage *types.ChatManage, promptContent string) []chat.Message {
messages := make([]chat.Message, 0, len(chatManage.History)*2+1)
messages = chatpipeline.AppendHistoryMessages(messages, chatManage.History)
userMsg := chat.Message{Role: "user", Content: promptContent}
if chatManage.ChatModelSupportsVision && len(chatManage.Images) > 0 {
userMsg.Images = chatManage.Images
}
return append(messages, userMsg)
}
// renderFallbackPrompt renders the fallback prompt template with query and image context.
func (s *sessionService) renderFallbackPrompt(ctx context.Context, chatManage *types.ChatManage) (string, error) {
query := chatManage.Query