feat: add thinking mode support for agents

This commit is contained in:
wizardchen
2026-01-20 16:20:43 +08:00
committed by lyingbug
parent b1895f7443
commit aa4af5365e
21 changed files with 1118 additions and 481 deletions
@@ -99,9 +99,13 @@ 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
// This ensures consistent display between streaming and history loading
go func() {
answerID := fmt.Sprintf("%s-answer", uuid.New().String()[:8])
var finalContent string
var thinkingStarted bool
var thinkingEnded bool
for response := range responseChan {
// Handle error responses from the stream
@@ -122,8 +126,53 @@ func (p *PluginChatCompletionStream) OnEvent(ctx context.Context,
continue
}
// For non-agent mode: embed thinking content with <think> tags in answer stream
// This ensures the frontend uses deepThink.vue component consistently
if response.ResponseType == types.ResponseTypeThinking {
content := response.Content
// Add <think> tag at the beginning of thinking content
if !thinkingStarted {
content = "<think>" + content
thinkingStarted = true
}
// Add </think> tag at the end of thinking content
if response.Done && !thinkingEnded {
content = content + "</think>"
thinkingEnded = true
}
finalContent += content
if err := eventBus.Emit(ctx, types.Event{
ID: answerID,
Type: types.EventType(event.EventAgentFinalAnswer),
SessionID: chatManage.SessionID,
Data: event.AgentFinalAnswerData{
Content: content,
Done: false, // Thinking is not the final answer
},
}); err != nil {
logger.Errorf(ctx, "Failed to emit thinking as answer event: %v", err)
}
continue
}
// Emit event for each answer chunk
if response.ResponseType == types.ResponseTypeAnswer {
// If we had thinking but it wasn't explicitly ended, close the think tag
if thinkingStarted && !thinkingEnded {
thinkingEnded = true
finalContent += "</think>"
if err := eventBus.Emit(ctx, types.Event{
ID: answerID,
Type: types.EventType(event.EventAgentFinalAnswer),
SessionID: chatManage.SessionID,
Data: event.AgentFinalAnswerData{
Content: "</think>",
Done: false,
},
}); err != nil {
logger.Errorf(ctx, "Failed to emit think close tag: %v", err)
}
}
finalContent += response.Content
if err := eventBus.Emit(ctx, types.Event{
ID: answerID,
+7
View File
@@ -487,6 +487,12 @@ func (s *sessionService) KnowledgeQA(
summaryConfig.MaxCompletionTokens = customAgent.Config.MaxCompletionTokens
logger.Infof(ctx, "Using custom agent's max_completion_tokens: %d", customAgent.Config.MaxCompletionTokens)
}
// Override thinking mode from agent config
// Agent-level thinking setting takes full control (no global fallback)
summaryConfig.Thinking = customAgent.Config.Thinking
if customAgent.Config.Thinking != nil {
logger.Infof(ctx, "Using custom agent's thinking: %v", *customAgent.Config.Thinking)
}
// Override retrieval strategy settings
if customAgent.Config.EmbeddingTopK > 0 {
embeddingTopK = customAgent.Config.EmbeddingTopK
@@ -1104,6 +1110,7 @@ func (s *sessionService) AgentQA(
HistoryTurns: customAgent.Config.HistoryTurns,
MCPSelectionMode: customAgent.Config.MCPSelectionMode,
MCPServices: customAgent.Config.MCPServices,
Thinking: customAgent.Config.Thinking,
}
// Resolve knowledge bases: request-level @ mentions take priority over agent config