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:
@@ -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