diff --git a/frontend/src/views/chat/index.vue b/frontend/src/views/chat/index.vue index 538b57ee..64acfa0e 100644 --- a/frontend/src/views/chat/index.vue +++ b/frontend/src/views/chat/index.vue @@ -609,18 +609,19 @@ const handleMsgList = async (data, isScrollType = false, newScrollHeight) => { } if (item.content) { - if (!item.content.includes('') && !item.content.includes('<\/think>')) { + const thinkCloseTag = ''; + if (!item.content.includes('') && !item.content.includes(thinkCloseTag)) { item.thinkContent = ""; item.content = item.content; item.showThink = false; item.thinking = false; - } else if (item.content.includes('<\/think>')) { + } else if (item.content.includes(thinkCloseTag)) { // 历史消息中包含完整的 ... 标签,说明 thinking 已完成 item.showThink = true; item.thinking = false; // 关键:标记 thinking 已完成,使 deepThink 默认折叠 - const index = item.content.trim().lastIndexOf('<\/think>'); + const index = item.content.trim().lastIndexOf(thinkCloseTag); item.thinkContent = item.content.trim().substring(0, index).replace('', '').trim(); - item.content = item.content.trim().substring(index + 8); + item.content = item.content.trim().substring(index + thinkCloseTag.length); } else if (item.content.includes('')) { // 内容包含 但没有 ,说明 thinking 还在进行中(不太可能出现在历史消息中) item.showThink = true; @@ -1018,19 +1019,20 @@ onChunk((data) => { obj.is_fallback = true; } - if (fullContent.value.includes('') && !fullContent.value.includes('<\/think>')) { + const thinkCloseTag = ''; + if (fullContent.value.includes('') && !fullContent.value.includes(thinkCloseTag)) { obj.thinking = true; obj.showThink = true; obj.content = ''; obj.thinkContent = fullContent.value.replace('', '').trim(); - } else if (fullContent.value.includes('') && fullContent.value.includes('<\/think>')) { + } else if (fullContent.value.includes('') && fullContent.value.includes(thinkCloseTag)) { obj.thinking = false; obj.showThink = true; // Use lastIndexOf to handle edge cases with multiple occurrences, // consistent with history loading logic (line 280) - const index = fullContent.value.lastIndexOf('<\/think>'); + const index = fullContent.value.lastIndexOf(thinkCloseTag); obj.thinkContent = fullContent.value.substring(0, index).replace('', '').trim(); - obj.content = fullContent.value.substring(index + 8).trim(); + obj.content = fullContent.value.substring(index + thinkCloseTag.length).trim(); } else { obj.content = fullContent.value; } diff --git a/internal/application/service/chat_pipeline/chat_completion_stream.go b/internal/application/service/chat_pipeline/chat_completion_stream.go index f18a8edd..9f519568 100644 --- a/internal/application/service/chat_pipeline/chat_completion_stream.go +++ b/internal/application/service/chat_pipeline/chat_completion_stream.go @@ -100,29 +100,34 @@ func (p *PluginChatCompletionStream) OnEvent(ctx context.Context, }) // Start goroutine to consume channel and emit events directly. - // For non-agent mode, thinking content is embedded in answer stream with tags. + // reasoning_content is routed to EventAgentThought (SSE response_type=thinking) + // and plain answer text to EventAgentFinalAnswer, matching the Agent pipeline. // The goroutine monitors ctx.Done() to avoid leaking when the context is cancelled // and the upstream channel is not closed promptly. go func() { + thinkingID := fmt.Sprintf("%s-thinking", uuid.New().String()[:8]) answerID := fmt.Sprintf("%s-answer", uuid.New().String()[:8]) - var finalContent string - var thinkingStarted bool - var thinkingEnded bool + thinkingOpen := false + + closeThinking := func() { + if !thinkingOpen { + return + } + eventBus.Emit(ctx, types.Event{ + ID: thinkingID, + Type: types.EventType(event.EventAgentThought), + SessionID: chatManage.SessionID, + Data: event.AgentThoughtData{ + Done: true, + }, + }) + thinkingOpen = false + } for { select { case <-ctx.Done(): - if thinkingStarted && !thinkingEnded { - eventBus.Emit(ctx, types.Event{ - ID: answerID, - Type: types.EventType(event.EventAgentFinalAnswer), - SessionID: chatManage.SessionID, - Data: event.AgentFinalAnswerData{ - Content: "", - Done: true, - }, - }) - } + closeThinking() pipelineInfo(ctx, "Stream", "context_cancelled", map[string]interface{}{ "session_id": chatManage.SessionID, }) @@ -130,18 +135,7 @@ func (p *PluginChatCompletionStream) OnEvent(ctx context.Context, case response, ok := <-responseChan: if !ok { - if thinkingStarted && !thinkingEnded { - finalContent += "" - eventBus.Emit(ctx, types.Event{ - ID: answerID, - Type: types.EventType(event.EventAgentFinalAnswer), - SessionID: chatManage.SessionID, - Data: event.AgentFinalAnswerData{ - Content: "", - Done: true, - }, - }) - } + closeThinking() pipelineInfo(ctx, "Stream", "channel_close", map[string]interface{}{ "session_id": chatManage.SessionID, }) @@ -167,43 +161,26 @@ func (p *PluginChatCompletionStream) OnEvent(ctx context.Context, } if response.ResponseType == types.ResponseTypeThinking { - content := response.Content - if !thinkingStarted { - content = "" + content - thinkingStarted = true - } - if response.Done && !thinkingEnded { - content = content + "" - thinkingEnded = true - } - finalContent += content - eventBus.Emit(ctx, types.Event{ - ID: answerID, - Type: types.EventType(event.EventAgentFinalAnswer), - SessionID: chatManage.SessionID, - Data: event.AgentFinalAnswerData{ - Content: content, - Done: false, - }, - }) - continue - } - - if response.ResponseType == types.ResponseTypeAnswer { - if thinkingStarted && !thinkingEnded { - thinkingEnded = true - finalContent += "" + if response.Content != "" { + thinkingOpen = true eventBus.Emit(ctx, types.Event{ - ID: answerID, - Type: types.EventType(event.EventAgentFinalAnswer), + ID: thinkingID, + Type: types.EventType(event.EventAgentThought), SessionID: chatManage.SessionID, - Data: event.AgentFinalAnswerData{ - Content: "", + Data: event.AgentThoughtData{ + Content: response.Content, Done: false, }, }) } - finalContent += response.Content + if response.Done { + closeThinking() + } + continue + } + + if response.ResponseType == types.ResponseTypeAnswer { + closeThinking() eventBus.Emit(ctx, types.Event{ ID: answerID, Type: types.EventType(event.EventAgentFinalAnswer), diff --git a/internal/handler/session/qa.go b/internal/handler/session/qa.go index ca6ff896..be216c55 100644 --- a/internal/handler/session/qa.go +++ b/internal/handler/session/qa.go @@ -700,6 +700,20 @@ func (h *Handler) executeQA(reqCtx *qaRequestContext, mode qaMode, generateTitle // (Agent mode handles completion in the defer block instead) if mode == qaModeNormal { var completionHandled bool + + // Persist reasoning_content into agent_steps so historical reload can + // reconstruct the thinking card (same shape as Agent-mode steps). + // Accumulate on assistantMessage directly so user-initiated stop also + // keeps whatever reasoning had streamed before the cancel. + streamCtx.eventBus.On(event.EventAgentThought, func(ctx context.Context, evt event.Event) error { + data, ok := evt.Data.(event.AgentThoughtData) + if !ok || data.Content == "" { + return nil + } + appendQuickAnswerReasoning(streamCtx.assistantMessage, data.Content) + return nil + }) + streamCtx.eventBus.On(event.EventAgentFinalAnswer, func(ctx context.Context, evt event.Event) error { data, ok := evt.Data.(event.AgentFinalAnswerData) if !ok { @@ -903,6 +917,22 @@ func (h *Handler) persistLastRequestState(parentCtx context.Context, reqCtx *qaR } } +// appendQuickAnswerReasoning accumulates streamed reasoning_content from +// KnowledgeQA (fast answer) into a single AgentStep for history replay. +func appendQuickAnswerReasoning(msg *types.Message, content string) { + if content == "" { + return + } + if len(msg.AgentSteps) == 0 { + msg.AgentSteps = types.AgentSteps{{ + Iteration: 0, + Timestamp: time.Now(), + ToolCalls: make([]types.ToolCall, 0), + }} + } + msg.AgentSteps[0].ReasoningContent += content +} + // completeAssistantMessage marks an assistant message as complete, updates it, // and asynchronously indexes the Q&A pair into the chat history knowledge base. func (h *Handler) completeAssistantMessage(ctx context.Context, assistantMessage *types.Message, userQuery string) {