From e5c46642e90b525d2e287dbe1fa3cc87ce9f7644 Mon Sep 17 00:00:00 2001 From: lyingbug Date: Fri, 20 Mar 2026 22:37:00 +0000 Subject: [PATCH] fix: handle thinking stream edge cases (unclosed tags + missing done) Two related fixes for thinking mode streaming: 1. chat_completion_stream.go: When the response channel closes unexpectedly (context cancelled, upstream error) while thinking is in progress, the tag was left unclosed. This caused the stored message to have an incomplete tag, making the frontend show a perpetual "thinking..." state on history reload. Now we emit a closing tag when the channel ends with thinkingStarted=true but thinkingEnded=false. 2. remote_api.go: The thinking Done=true event was only sent when the first answer content chunk arrived. If the model produced only reasoning with no answer (e.g., finish_reason set with empty content), the thinking-done signal was never emitted. Now we also check isDone && state.hasThinking at the end of processStreamDelta to ensure the done event is always sent. --- .../chat_pipline/chat_completion_stream.go | 18 ++++++++++++++++++ internal/models/chat/remote_api.go | 11 +++++++++++ 2 files changed, 29 insertions(+) diff --git a/internal/application/service/chat_pipline/chat_completion_stream.go b/internal/application/service/chat_pipline/chat_completion_stream.go index f618d6c9..d2e67151 100644 --- a/internal/application/service/chat_pipline/chat_completion_stream.go +++ b/internal/application/service/chat_pipline/chat_completion_stream.go @@ -188,6 +188,24 @@ func (p *PluginChatCompletionStream) OnEvent(ctx context.Context, } } + // Compensate for unclosed tag when stream ends unexpectedly + // (e.g., context cancelled, upstream error) without an answer arriving. + if thinkingStarted && !thinkingEnded { + thinkingEnded = true + finalContent += "" + if err := eventBus.Emit(ctx, types.Event{ + ID: answerID, + Type: types.EventType(event.EventAgentFinalAnswer), + SessionID: chatManage.SessionID, + Data: event.AgentFinalAnswerData{ + Content: "", + Done: true, + }, + }); err != nil { + logger.Errorf(ctx, "Failed to emit think close tag on stream end: %v", err) + } + } + pipelineInfo(ctx, "Stream", "channel_close", map[string]interface{}{ "session_id": chatManage.SessionID, }) diff --git a/internal/models/chat/remote_api.go b/internal/models/chat/remote_api.go index ae21f8ba..7f076d01 100644 --- a/internal/models/chat/remote_api.go +++ b/internal/models/chat/remote_api.go @@ -642,6 +642,17 @@ func (c *RemoteAPIChat) processStreamDelta(ctx context.Context, choice *openai.C ToolCalls: state.buildOrderedToolCalls(), } } + + // Ensure thinking done is sent when stream finishes without any answer content + // (e.g., model only produced reasoning then hit finish_reason with empty content). + if isDone && state.hasThinking { + streamChan <- types.StreamResponse{ + ResponseType: types.ResponseTypeThinking, + Content: "", + Done: true, + } + state.hasThinking = false + } } // processToolCallsDelta 处理 tool calls 的增量更新