refactor(chat): improve handling of <think> tags in chat messages
- Updated logic to manage <think> and </think> tags in chat message content, ensuring accurate representation of thinking states. - Introduced a new function to handle the closing of thinking events, enhancing clarity and reducing redundancy in event emissions. - Adjusted event handling for reasoning content to support historical message reconstruction, improving user experience during chat interactions.
This commit is contained in:
@@ -609,18 +609,19 @@ const handleMsgList = async (data, isScrollType = false, newScrollHeight) => {
|
||||
}
|
||||
|
||||
if (item.content) {
|
||||
if (!item.content.includes('<think>') && !item.content.includes('<\/think>')) {
|
||||
const thinkCloseTag = '</think>';
|
||||
if (!item.content.includes('<think>') && !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)) {
|
||||
// 历史消息中包含完整的 <think>...</think> 标签,说明 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('<think>', '').trim();
|
||||
item.content = item.content.trim().substring(index + 8);
|
||||
item.content = item.content.trim().substring(index + thinkCloseTag.length);
|
||||
} else if (item.content.includes('<think>')) {
|
||||
// 内容包含 <think> 但没有 </think>,说明 thinking 还在进行中(不太可能出现在历史消息中)
|
||||
item.showThink = true;
|
||||
@@ -1018,19 +1019,20 @@ onChunk((data) => {
|
||||
obj.is_fallback = true;
|
||||
}
|
||||
|
||||
if (fullContent.value.includes('<think>') && !fullContent.value.includes('<\/think>')) {
|
||||
const thinkCloseTag = '</think>';
|
||||
if (fullContent.value.includes('<think>') && !fullContent.value.includes(thinkCloseTag)) {
|
||||
obj.thinking = true;
|
||||
obj.showThink = true;
|
||||
obj.content = '';
|
||||
obj.thinkContent = fullContent.value.replace('<think>', '').trim();
|
||||
} else if (fullContent.value.includes('<think>') && fullContent.value.includes('<\/think>')) {
|
||||
} else if (fullContent.value.includes('<think>') && fullContent.value.includes(thinkCloseTag)) {
|
||||
obj.thinking = false;
|
||||
obj.showThink = true;
|
||||
// Use lastIndexOf to handle edge cases with multiple </think> 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('<think>', '').trim();
|
||||
obj.content = fullContent.value.substring(index + 8).trim();
|
||||
obj.content = fullContent.value.substring(index + thinkCloseTag.length).trim();
|
||||
} else {
|
||||
obj.content = fullContent.value;
|
||||
}
|
||||
|
||||
@@ -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 <think> 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: "</think>",
|
||||
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 += "</think>"
|
||||
eventBus.Emit(ctx, types.Event{
|
||||
ID: answerID,
|
||||
Type: types.EventType(event.EventAgentFinalAnswer),
|
||||
SessionID: chatManage.SessionID,
|
||||
Data: event.AgentFinalAnswerData{
|
||||
Content: "</think>",
|
||||
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 = "<think>" + content
|
||||
thinkingStarted = true
|
||||
}
|
||||
if response.Done && !thinkingEnded {
|
||||
content = content + "</think>"
|
||||
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 += "</think>"
|
||||
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: "</think>",
|
||||
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),
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user