feat: implement final_answer tool and enhance agent duration tracking
- Introduced the final_answer tool to ensure agents submit their complete responses as the last action, improving response accuracy and consistency. - Updated the agent engine to log detailed information about tool calls and responses, including the total duration of agent execution. - Enhanced the chat message model to include agent_duration_ms, tracking the total execution time from query start to answer delivery. - Implemented JSON field extraction for streaming responses, allowing for more efficient handling of tool call outputs. These changes significantly improve the agent's response handling and ensure that final answers are consistently delivered, enhancing overall user experience.
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
---
|
||||
description:
|
||||
alwaysApply: true
|
||||
enabled: true
|
||||
updatedAt: 2026-03-06T08:11:34.113Z
|
||||
provider:
|
||||
---
|
||||
|
||||
i18n国际化文件修改的时候同时修改i18n/locals下面的所有语言文件
|
||||
@@ -266,6 +266,8 @@ export default {
|
||||
webSearchFound: 'Found <strong>{count}</strong> web search result(s)',
|
||||
argumentsLabel: 'Arguments',
|
||||
toolFallback: 'Tool',
|
||||
stepsCompleted: 'Completed <strong>{steps}</strong> step(s)',
|
||||
stepsCompletedWithDuration: 'Completed <strong>{steps}</strong> step(s) in <strong>{duration}</strong>',
|
||||
title: 'Agents',
|
||||
subtitle: 'Configure and manage your agents to customize conversation behavior and capabilities',
|
||||
createAgent: 'Create Agent',
|
||||
|
||||
@@ -917,6 +917,8 @@ export default {
|
||||
webSearchFound: "<strong>{count}</strong>개의 웹 검색 결과 발견",
|
||||
argumentsLabel: "파라미터",
|
||||
toolFallback: "도구",
|
||||
stepsCompleted: "<strong>{steps}</strong>개 단계 완료",
|
||||
stepsCompletedWithDuration: "<strong>{steps}</strong>개 단계 완료, 소요 시간 <strong>{duration}</strong>",
|
||||
title: "에이전트",
|
||||
subtitle: "에이전트 구성 및 관리, 대화 동작 및 기능 맞춤화",
|
||||
createAgent: "에이전트 만들기",
|
||||
|
||||
@@ -256,6 +256,8 @@ export default {
|
||||
webSearchFound: 'Найдено <strong>{count}</strong> результатов веб‑поиска',
|
||||
argumentsLabel: 'Параметры',
|
||||
toolFallback: 'Инструмент',
|
||||
stepsCompleted: 'Выполнено <strong>{steps}</strong> шаг(ов)',
|
||||
stepsCompletedWithDuration: 'Выполнено <strong>{steps}</strong> шаг(ов) за <strong>{duration}</strong>',
|
||||
editor: {
|
||||
skillsConfig: 'Skills',
|
||||
skillsConfigDesc: 'Настройка предустановленных Skills для агента, предоставляющих специализированные знания и рабочие процессы',
|
||||
|
||||
@@ -910,6 +910,8 @@ export default {
|
||||
webSearchFound: "找到 <strong>{count}</strong> 个网络搜索结果",
|
||||
argumentsLabel: "参数",
|
||||
toolFallback: "工具",
|
||||
stepsCompleted: "已完成 <strong>{steps}</strong> 个步骤",
|
||||
stepsCompletedWithDuration: "已完成 <strong>{steps}</strong> 个步骤,耗时 <strong>{duration}</strong>",
|
||||
title: "智能体",
|
||||
subtitle: "配置和管理您的智能体,自定义对话行为和能力",
|
||||
createAgent: "创建智能体",
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -156,12 +156,15 @@ const getmsgList = (data, isScrollType = false, scrollHeight) => {
|
||||
|
||||
// Reconstruct agentEventStream from agent_steps stored in database
|
||||
// This allows the frontend to restore the exact conversation state including all agent reasoning steps
|
||||
const reconstructEventStreamFromSteps = (agentSteps, messageContent, isCompleted = false, isFallback = false) => {
|
||||
const reconstructEventStreamFromSteps = (agentSteps, messageContent, isCompleted = false, isFallback = false, agentDurationMs = 0) => {
|
||||
const events = [];
|
||||
|
||||
// Process agent steps if they exist
|
||||
if (agentSteps && Array.isArray(agentSteps) && agentSteps.length > 0) {
|
||||
agentSteps.forEach((step) => {
|
||||
// Compute step timestamp (milliseconds) from step.timestamp if available
|
||||
const stepTimestamp = step.timestamp ? new Date(step.timestamp).getTime() : 0;
|
||||
|
||||
// Add thinking event if thought content exists
|
||||
if (step.thought && step.thought.trim()) {
|
||||
events.push({
|
||||
@@ -170,14 +173,16 @@ const reconstructEventStreamFromSteps = (agentSteps, messageContent, isCompleted
|
||||
content: step.thought,
|
||||
done: true,
|
||||
thinking: false,
|
||||
timestamp: stepTimestamp || undefined,
|
||||
// Extract duration from step if available
|
||||
duration_ms: step.duration || undefined,
|
||||
});
|
||||
}
|
||||
|
||||
// Add tool call and result events
|
||||
|
||||
// Add tool call and result events (skip final_answer as its content is in the answer event)
|
||||
if (step.tool_calls && Array.isArray(step.tool_calls)) {
|
||||
step.tool_calls.forEach((toolCall) => {
|
||||
if (toolCall.name === 'final_answer') return; // Skip - shown as answer event
|
||||
events.push({
|
||||
type: 'tool_call',
|
||||
tool_call_id: toolCall.id,
|
||||
@@ -187,6 +192,7 @@ const reconstructEventStreamFromSteps = (agentSteps, messageContent, isCompleted
|
||||
success: toolCall.result?.success !== false,
|
||||
output: toolCall.result?.output || '',
|
||||
error: toolCall.result?.error || undefined,
|
||||
timestamp: stepTimestamp || undefined,
|
||||
// Use both duration and duration_ms for compatibility
|
||||
duration: toolCall.duration,
|
||||
duration_ms: toolCall.duration,
|
||||
@@ -198,6 +204,14 @@ const reconstructEventStreamFromSteps = (agentSteps, messageContent, isCompleted
|
||||
});
|
||||
}
|
||||
|
||||
// Add agent_complete event with duration info (before answer event)
|
||||
if (agentDurationMs > 0) {
|
||||
events.push({
|
||||
type: 'agent_complete',
|
||||
total_duration_ms: agentDurationMs,
|
||||
});
|
||||
}
|
||||
|
||||
// 总是添加 answer 事件如果有内容(无论是否有 agent_steps)
|
||||
// 这样可以确保最终答案始终被渲染
|
||||
if (messageContent && messageContent.trim()) {
|
||||
@@ -236,7 +250,7 @@ const handleMsgList = async (data, isScrollType = false, newScrollHeight) => {
|
||||
if (item.agent_steps && Array.isArray(item.agent_steps) && item.agent_steps.length > 0) {
|
||||
console.log('[Message Load] Reconstructing agent steps for message:', item.id, 'steps:', item.agent_steps.length);
|
||||
item.isAgentMode = true;
|
||||
item.agentEventStream = reconstructEventStreamFromSteps(item.agent_steps, item.content, item.is_completed, item.is_fallback);
|
||||
item.agentEventStream = reconstructEventStreamFromSteps(item.agent_steps, item.content, item.is_completed, item.is_fallback, item.agent_duration_ms || 0);
|
||||
// 隐藏最终答案内容,因为它已经包含在 agentEventStream 的 answer 事件中
|
||||
item.hideContent = true;
|
||||
console.log('[Message Load] Reconstructed', item.agentEventStream.length, 'events from agent steps');
|
||||
@@ -652,6 +666,10 @@ const handleAgentChunk = (data) => {
|
||||
break;
|
||||
|
||||
case 'tool_call':
|
||||
// Skip final_answer tool call from event stream - its content appears as answer events
|
||||
if (data.data && data.data.tool_name === 'final_answer') {
|
||||
break;
|
||||
}
|
||||
// Store or update pending tool call to pair with result later
|
||||
if (data.data && (data.data.tool_name || data.data.tool_call_id)) {
|
||||
const incomingToolName = data.data.tool_name;
|
||||
@@ -851,7 +869,14 @@ const handleAgentChunk = (data) => {
|
||||
console.log('[Agent] Complete event received');
|
||||
loading.value = false;
|
||||
isReplying.value = false;
|
||||
// 不清空 message.content,保持已有内容
|
||||
// 将 total_duration_ms 存入事件流供 AgentStreamDisplay 使用
|
||||
if (data.data?.total_duration_ms && message.agentEventStream) {
|
||||
message.agentEventStream.push({
|
||||
type: 'agent_complete',
|
||||
total_duration_ms: data.data.total_duration_ms,
|
||||
total_steps: data.data.total_steps,
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
case 'stop':
|
||||
|
||||
+128
-17
@@ -8,7 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/agent/skills"
|
||||
"github.com/Tencent/WeKnora/internal/agent/tools"
|
||||
agenttools "github.com/Tencent/WeKnora/internal/agent/tools"
|
||||
"github.com/Tencent/WeKnora/internal/common"
|
||||
"github.com/Tencent/WeKnora/internal/event"
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
@@ -26,7 +26,7 @@ func generateEventID(suffix string) string {
|
||||
// AgentEngine is the core engine for running ReAct agents
|
||||
type AgentEngine struct {
|
||||
config *types.AgentConfig
|
||||
toolRegistry *tools.ToolRegistry
|
||||
toolRegistry *agenttools.ToolRegistry
|
||||
chatModel chat.Chat
|
||||
eventBus *event.EventBus
|
||||
knowledgeBasesInfo []*KnowledgeBaseInfo // Detailed knowledge base information for prompt
|
||||
@@ -50,7 +50,7 @@ func listToolNames(ts []chat.Tool) []string {
|
||||
func NewAgentEngine(
|
||||
config *types.AgentConfig,
|
||||
chatModel chat.Chat,
|
||||
toolRegistry *tools.ToolRegistry,
|
||||
toolRegistry *agenttools.ToolRegistry,
|
||||
eventBus *event.EventBus,
|
||||
knowledgeBasesInfo []*KnowledgeBaseInfo,
|
||||
selectedDocs []*SelectedDocumentInfo,
|
||||
@@ -78,7 +78,7 @@ func NewAgentEngine(
|
||||
func NewAgentEngineWithSkills(
|
||||
config *types.AgentConfig,
|
||||
chatModel chat.Chat,
|
||||
toolRegistry *tools.ToolRegistry,
|
||||
toolRegistry *agenttools.ToolRegistry,
|
||||
eventBus *event.EventBus,
|
||||
knowledgeBasesInfo []*KnowledgeBaseInfo,
|
||||
selectedDocs []*SelectedDocumentInfo,
|
||||
@@ -237,7 +237,27 @@ func (e *AgentEngine) executeLoop(
|
||||
})
|
||||
|
||||
// 1. Think: Call LLM with function calling and stream thinking through EventBus
|
||||
logger.Infof(ctx, "[Agent][Round-%d] Calling LLM with %d tools available...", state.CurrentRound+1, len(tools))
|
||||
logger.Infof(ctx, "[Agent][Round-%d] Calling LLM with %d messages, %d tools", state.CurrentRound+1, len(messages), len(tools))
|
||||
for i, msg := range messages {
|
||||
contentPreview := msg.Content
|
||||
if len(contentPreview) > 200 {
|
||||
contentPreview = contentPreview[:200] + "..."
|
||||
}
|
||||
if msg.Role == "tool" {
|
||||
logger.Infof(ctx, "[Agent][Round-%d] messages[%d]: role=tool, name=%s, tool_call_id=%s, content_len=%d",
|
||||
state.CurrentRound+1, i, msg.Name, msg.ToolCallID, len(msg.Content))
|
||||
} else if len(msg.ToolCalls) > 0 {
|
||||
tcNames := make([]string, len(msg.ToolCalls))
|
||||
for j, tc := range msg.ToolCalls {
|
||||
tcNames[j] = tc.Function.Name
|
||||
}
|
||||
logger.Infof(ctx, "[Agent][Round-%d] messages[%d]: role=%s, content_len=%d, tool_calls=%v",
|
||||
state.CurrentRound+1, i, msg.Role, len(msg.Content), tcNames)
|
||||
} else {
|
||||
logger.Infof(ctx, "[Agent][Round-%d] messages[%d]: role=%s, content_len=%d, content=%s",
|
||||
state.CurrentRound+1, i, msg.Role, len(msg.Content), contentPreview)
|
||||
}
|
||||
}
|
||||
common.PipelineInfo(ctx, "Agent", "think_start", map[string]interface{}{
|
||||
"iteration": state.CurrentRound,
|
||||
"round": state.CurrentRound + 1,
|
||||
@@ -260,17 +280,15 @@ func (e *AgentEngine) executeLoop(
|
||||
"content_len": len(response.Content),
|
||||
})
|
||||
|
||||
// Debug: log finish reason and tool call count from LLM
|
||||
logger.Infof(ctx, "[Agent][Round-%d] LLM response received: finish_reason=%s, tool_calls=%d, content_length=%d",
|
||||
state.CurrentRound+1, response.FinishReason, len(response.ToolCalls), len(response.Content))
|
||||
logger.Debugf(
|
||||
ctx,
|
||||
"[Agent] LLM response finish=%s, toolCalls=%d",
|
||||
response.FinishReason,
|
||||
len(response.ToolCalls),
|
||||
)
|
||||
// Log LLM response summary
|
||||
logger.Infof(ctx, "[Agent][Round-%d] LLM response: finish_reason=%s, content_len=%d, tool_calls=%d",
|
||||
state.CurrentRound+1, response.FinishReason, len(response.Content), len(response.ToolCalls))
|
||||
if response.Content != "" {
|
||||
logger.Debugf(ctx, "[Agent][Round-%d] LLM thought content:\n%s", state.CurrentRound+1, response.Content)
|
||||
logger.Infof(ctx, "[Agent][Round-%d] LLM content:\n%s", state.CurrentRound+1, response.Content)
|
||||
}
|
||||
for i, tc := range response.ToolCalls {
|
||||
logger.Infof(ctx, "[Agent][Round-%d] tool_call[%d]: %s(%s)",
|
||||
state.CurrentRound+1, i, tc.Function.Name, tc.Function.Arguments)
|
||||
}
|
||||
|
||||
// Create agent step
|
||||
@@ -313,7 +331,53 @@ func (e *AgentEngine) executeLoop(
|
||||
break
|
||||
}
|
||||
|
||||
// 3. Act: Execute tool calls if any
|
||||
// 3. Check for final_answer tool call - if present, agent is done
|
||||
hasFinalAnswer := false
|
||||
if len(response.ToolCalls) > 0 {
|
||||
for _, tc := range response.ToolCalls {
|
||||
if tc.Function.Name == agenttools.ToolFinalAnswer {
|
||||
var faArgs struct {
|
||||
Answer string `json:"answer"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(tc.Function.Arguments), &faArgs); err != nil {
|
||||
logger.Warnf(ctx, "[Agent][Round-%d] Failed to parse final_answer args: %v", state.CurrentRound+1, err)
|
||||
} else {
|
||||
logger.Infof(ctx, "[Agent][Round-%d] final_answer tool called, answer length: %d",
|
||||
state.CurrentRound+1, len(faArgs.Answer))
|
||||
state.FinalAnswer = faArgs.Answer
|
||||
state.IsComplete = true
|
||||
hasFinalAnswer = true
|
||||
|
||||
// Emit answer done marker (content was already streamed via processToolCallsDelta)
|
||||
e.eventBus.Emit(ctx, event.Event{
|
||||
ID: generateEventID("answer-done"),
|
||||
Type: event.EventAgentFinalAnswer,
|
||||
SessionID: sessionID,
|
||||
Data: event.AgentFinalAnswerData{
|
||||
Content: "",
|
||||
Done: true,
|
||||
},
|
||||
})
|
||||
|
||||
common.PipelineInfo(ctx, "Agent", "final_answer_tool", map[string]interface{}{
|
||||
"iteration": state.CurrentRound,
|
||||
"round": state.CurrentRound + 1,
|
||||
"answer_len": len(faArgs.Answer),
|
||||
})
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if hasFinalAnswer {
|
||||
state.RoundSteps = append(state.RoundSteps, step)
|
||||
logger.Infof(ctx, "[Agent][Round-%d] Duration: %dms (final_answer tool)",
|
||||
state.CurrentRound+1, time.Since(roundStart).Milliseconds())
|
||||
break
|
||||
}
|
||||
|
||||
// 4. Act: Execute tool calls if any
|
||||
if len(response.ToolCalls) > 0 {
|
||||
logger.Infof(
|
||||
ctx,
|
||||
@@ -664,7 +728,12 @@ func (e *AgentEngine) streamLLMToEventBus(
|
||||
chunkCount++
|
||||
|
||||
if chunk.Content != "" {
|
||||
fullContent += chunk.Content
|
||||
// Only accumulate LLM's own text output, not content extracted from tool arguments
|
||||
// (e.g. final_answer's answer or thinking tool's thought are streamed separately)
|
||||
isExtracted := chunk.Data != nil && chunk.Data["source"] != nil
|
||||
if !isExtracted {
|
||||
fullContent += chunk.Content
|
||||
}
|
||||
}
|
||||
|
||||
// Collect tool calls if present
|
||||
@@ -754,9 +823,12 @@ func (e *AgentEngine) streamThinkingToEventBus(
|
||||
logger.Debug(context.Background(), "[Agent] streamLLM opts tool_choice=auto temperature=", e.config.Temperature)
|
||||
|
||||
pendingToolCalls := make(map[string]bool)
|
||||
thinkingToolIDs := make(map[string]string) // tool_call_id -> event ID for thinking tool streams
|
||||
|
||||
// Generate a single ID for this entire thinking stream
|
||||
thinkingID := generateEventID("thinking")
|
||||
// Generate a single ID for final_answer streaming (if applicable)
|
||||
answerID := generateEventID("answer")
|
||||
logger.Debugf(ctx, "[Agent][Thinking][Iteration-%d] ThinkingID: %s", iteration+1, thinkingID)
|
||||
|
||||
fullContent, toolCalls, err := e.streamLLMToEventBus(
|
||||
@@ -783,6 +855,45 @@ func (e *AgentEngine) streamThinkingToEventBus(
|
||||
}
|
||||
}
|
||||
|
||||
// Handle final_answer tool's streaming answer content
|
||||
if chunk.ResponseType == types.ResponseTypeAnswer {
|
||||
if source, _ := chunk.Data["source"].(string); source == "final_answer_tool" {
|
||||
e.eventBus.Emit(ctx, event.Event{
|
||||
ID: answerID,
|
||||
Type: event.EventAgentFinalAnswer,
|
||||
SessionID: sessionID,
|
||||
Data: event.AgentFinalAnswerData{
|
||||
Content: chunk.Content,
|
||||
Done: false,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Handle thinking tool's streaming thought content
|
||||
if chunk.ResponseType == types.ResponseTypeThinking && chunk.Data != nil {
|
||||
if source, _ := chunk.Data["source"].(string); source == "thinking_tool" {
|
||||
toolCallID, _ := chunk.Data["tool_call_id"].(string)
|
||||
eventID, exists := thinkingToolIDs[toolCallID]
|
||||
if !exists {
|
||||
eventID = generateEventID("thinking-tool")
|
||||
thinkingToolIDs[toolCallID] = eventID
|
||||
}
|
||||
e.eventBus.Emit(ctx, event.Event{
|
||||
ID: eventID,
|
||||
Type: event.EventAgentThought,
|
||||
SessionID: sessionID,
|
||||
Data: event.AgentThoughtData{
|
||||
Content: chunk.Content,
|
||||
Iteration: iteration,
|
||||
Done: false,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if chunk.Content != "" {
|
||||
// logger.Debugf(ctx, "[Agent][Thinking][Iteration-%d] Emitting thought chunk: %d chars",
|
||||
// iteration+1, len(chunk.Content))
|
||||
|
||||
@@ -403,12 +403,13 @@ To help users solve problems by planning, thinking, and using available tools (l
|
||||
1. **Analyze:** Understand the user's request.
|
||||
2. **Plan:** If the task is complex, use todo_write to create a plan.
|
||||
3. **Execute:** Use available tools to gather information or perform actions.
|
||||
4. **Synthesize:** Provide a comprehensive answer.
|
||||
4. **Synthesize:** Call the final_answer tool with your comprehensive answer. You MUST always end by calling final_answer.
|
||||
|
||||
### Tool Guidelines
|
||||
* **web_search / web_fetch:** Use these if enabled to find information from the internet.
|
||||
* **todo_write:** Use for managing multi-step tasks.
|
||||
* **thinking:** Use to plan and reflect.
|
||||
* **final_answer:** MANDATORY as your final action. Always submit your complete answer through this tool. NEVER end your turn without calling it.
|
||||
|
||||
### System Status
|
||||
Current Time: {{current_time}}
|
||||
@@ -460,7 +461,7 @@ If in **Path B**, execute tasks in todo_write sequentially. For **EACH** task:
|
||||
Only when ALL todo_write tasks are "completed":
|
||||
* Synthesize findings from the full text of all retrieved chunks.
|
||||
* Check for consistency.
|
||||
* Generate the final response.
|
||||
* Call the **final_answer** tool with your complete, well-formatted response. You MUST always end by calling final_answer.
|
||||
|
||||
### Core Retrieval Strategy (Strict Sequence)
|
||||
For every retrieval attempt (Phase 1 or Phase 3), follow this exact chain:
|
||||
@@ -478,6 +479,7 @@ For every retrieval attempt (Phase 1 or Phase 3), follow this exact chain:
|
||||
* **web_search / web_fetch:** Use these ONLY when Web Search is Enabled and KB retrieval is insufficient.
|
||||
* **todo_write:** Your "Manager". Tracks multi-step research.
|
||||
* **think:** Your "Conscience". Use to plan and reflect the content returned by list_knowledge_chunks.
|
||||
* **final_answer:** MANDATORY as your final action. Always submit your complete answer through this tool. NEVER end your turn without calling it.
|
||||
|
||||
### Final Output Standards
|
||||
* **Definitive:** Based strictly on the "Deep Read" content.
|
||||
|
||||
@@ -18,6 +18,7 @@ const (
|
||||
ToolDataSchema = "data_schema"
|
||||
ToolWebSearch = "web_search"
|
||||
ToolWebFetch = "web_fetch"
|
||||
ToolFinalAnswer = "final_answer"
|
||||
// Skills-related tools (only available when skills are enabled)
|
||||
ToolExecuteSkillScript = "execute_skill_script"
|
||||
ToolReadSkill = "read_skill"
|
||||
@@ -46,6 +47,7 @@ func AvailableToolDefinitions() []AvailableTool {
|
||||
{Name: ToolDataSchema, Label: "查看数据元信息", Description: "获取表格文件的元信息"},
|
||||
{Name: ToolReadSkill, Label: "读取技能", Description: "按需读取技能内容以学习专业能力"},
|
||||
{Name: ToolExecuteSkillScript, Label: "执行技能脚本", Description: "在沙箱环境中执行技能脚本"},
|
||||
{Name: ToolFinalAnswer, Label: "提交最终回答", Description: "提交最终回答给用户"},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,5 +64,6 @@ func DefaultAllowedTools() []string {
|
||||
ToolDatabaseQuery,
|
||||
ToolDataAnalysis,
|
||||
ToolDataSchema,
|
||||
ToolFinalAnswer,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
var finalAnswerTool = BaseTool{
|
||||
name: ToolFinalAnswer,
|
||||
description: `Submit your final answer to the user's question.
|
||||
|
||||
## When to Use This Tool
|
||||
|
||||
You MUST call this tool as your LAST action when you are ready to deliver your final response to the user.
|
||||
After gathering all necessary information through other tools (search, retrieval, analysis, etc.),
|
||||
synthesize your findings and submit the complete answer through this tool.
|
||||
|
||||
## Important Rules
|
||||
|
||||
1. NEVER end your turn without calling this tool
|
||||
2. The answer parameter must contain your complete, well-formatted response
|
||||
3. Include all citations, structure, and formatting in the answer
|
||||
4. This should always be the last tool you call
|
||||
|
||||
## Parameters
|
||||
|
||||
- **answer**: Your complete final answer in Markdown format, including all citations and formatting`,
|
||||
schema: json.RawMessage(`{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"answer": {
|
||||
"type": "string",
|
||||
"description": "Your complete final answer in Markdown format. Include all citations, structure, images, and formatting."
|
||||
}
|
||||
},
|
||||
"required": ["answer"]
|
||||
}`),
|
||||
}
|
||||
|
||||
// FinalAnswerInput defines the input parameters for the final answer tool
|
||||
type FinalAnswerInput struct {
|
||||
Answer string `json:"answer"`
|
||||
}
|
||||
|
||||
// FinalAnswerTool submits the agent's final answer to the user
|
||||
type FinalAnswerTool struct {
|
||||
BaseTool
|
||||
}
|
||||
|
||||
// NewFinalAnswerTool creates a new final answer tool instance
|
||||
func NewFinalAnswerTool() *FinalAnswerTool {
|
||||
return &FinalAnswerTool{
|
||||
BaseTool: finalAnswerTool,
|
||||
}
|
||||
}
|
||||
|
||||
// Execute executes the final answer tool
|
||||
func (t *FinalAnswerTool) Execute(ctx context.Context, args json.RawMessage) (*types.ToolResult, error) {
|
||||
logger.Infof(ctx, "[Tool][FinalAnswer] Execute started")
|
||||
|
||||
var input FinalAnswerInput
|
||||
if err := json.Unmarshal(args, &input); err != nil {
|
||||
logger.Errorf(ctx, "[Tool][FinalAnswer] Failed to parse args: %v", err)
|
||||
return &types.ToolResult{
|
||||
Success: false,
|
||||
Error: fmt.Sprintf("Failed to parse args: %v", err),
|
||||
}, err
|
||||
}
|
||||
|
||||
if input.Answer == "" {
|
||||
return &types.ToolResult{
|
||||
Success: false,
|
||||
Error: "answer must be a non-empty string",
|
||||
}, fmt.Errorf("answer must be a non-empty string")
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "[Tool][FinalAnswer] Answer length: %d characters", len(input.Answer))
|
||||
|
||||
return &types.ToolResult{
|
||||
Success: true,
|
||||
Output: input.Answer,
|
||||
}, nil
|
||||
}
|
||||
@@ -37,7 +37,7 @@ Each thought can build on, question, or revise previous insights as understandin
|
||||
- Generates a solution hypothesis
|
||||
- Verifies the hypothesis based on the Chain of Thought steps
|
||||
- Repeats the process until satisfied
|
||||
- Provides a correct answer
|
||||
- When thinking is complete, you can call the final_answer tool to deliver your answer if all your thinking is complete. NEVER include the final answer directly in a thought.
|
||||
|
||||
## Parameters Explained
|
||||
|
||||
@@ -78,8 +78,8 @@ Each thought can build on, question, or revise previous insights as understandin
|
||||
7. Generate a solution hypothesis when appropriate
|
||||
8. Verify the hypothesis based on the Chain of Thought steps
|
||||
9. Repeat the process until satisfied with the solution
|
||||
10. Provide a single, ideally correct answer as the final output
|
||||
11. Only set next_thought_needed to false when truly done and a satisfactory answer is reached`,
|
||||
10. Only set next_thought_needed to false when truly done and a satisfactory answer is reached
|
||||
11. NEVER include the final answer in the thought content. When thinking is complete, you can call the final_answer tool to deliver the final answer to the user`,
|
||||
schema: json.RawMessage(`{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
||||
@@ -347,8 +347,9 @@ func (s *agentService) registerTools(
|
||||
allowedTools = append(allowedTools, tools.ToolWebSearch)
|
||||
allowedTools = append(allowedTools, tools.ToolWebFetch)
|
||||
}
|
||||
logger.Infof(ctx, "Registering tools: %v, webSearchEnabled: %v", allowedTools, config.WebSearchEnabled)
|
||||
|
||||
logger.Infof(ctx, "Registering tools: %v, webSearchEnabled: %v", allowedTools, config.WebSearchEnabled)
|
||||
allowedTools = append(allowedTools, tools.ToolFinalAnswer)
|
||||
// Register each allowed tool
|
||||
for _, toolName := range allowedTools {
|
||||
var toolToRegister types.Tool
|
||||
@@ -402,6 +403,9 @@ func (s *agentService) registerTools(
|
||||
toolToRegister = tools.NewDataSchemaTool(s.knowledgeService, s.chunkService.GetRepository())
|
||||
logger.Infof(ctx, "Registered data_schema tool")
|
||||
|
||||
case tools.ToolFinalAnswer:
|
||||
toolToRegister = tools.NewFinalAnswerTool()
|
||||
logger.Infof(ctx, "Registered final_answer tool")
|
||||
default:
|
||||
logger.Warnf(ctx, "Unknown tool: %s", toolName)
|
||||
}
|
||||
|
||||
@@ -422,6 +422,7 @@ func (h *AgentStreamHandler) handleComplete(ctx context.Context, evt event.Event
|
||||
if data.MessageID == h.assistantMessageID {
|
||||
// h.assistantMessage.Content = data.FinalAnswer
|
||||
h.assistantMessage.IsCompleted = true
|
||||
h.assistantMessage.AgentDurationMs = data.TotalDurationMs
|
||||
|
||||
// Update knowledge references if provided
|
||||
if len(data.KnowledgeRefs) > 0 {
|
||||
@@ -434,6 +435,8 @@ func (h *AgentStreamHandler) handleComplete(ctx context.Context, evt event.Event
|
||||
h.assistantMessage.KnowledgeReferences = knowledgeRefs
|
||||
}
|
||||
|
||||
h.assistantMessage.Content += data.FinalAnswer
|
||||
|
||||
// Update agent steps if provided
|
||||
if data.AgentSteps != nil {
|
||||
if steps, ok := data.AgentSteps.([]types.AgentStep); ok {
|
||||
|
||||
@@ -0,0 +1,224 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// jsonFieldExtractor extracts a specific string field value from streaming JSON fragments.
|
||||
// It processes incremental JSON argument chunks from LLM tool calls.
|
||||
//
|
||||
// Example: for fieldName="answer", expected JSON format: {"answer":"...content..."}
|
||||
// The extractor uses a simple state machine to skip the JSON prefix and extract the string value.
|
||||
type jsonFieldExtractor struct {
|
||||
fieldName string // the JSON field name to extract (e.g. "answer", "thought")
|
||||
buffer string // accumulated full arguments string
|
||||
valueStart int // byte offset where the field value starts (-1 if not found yet)
|
||||
lastEmit int // byte offset of the last emitted position within the value
|
||||
done bool // whether we've seen the closing quote
|
||||
}
|
||||
|
||||
// newJSONFieldExtractor creates a new extractor instance for the given field name
|
||||
func newJSONFieldExtractor(fieldName string) *jsonFieldExtractor {
|
||||
return &jsonFieldExtractor{
|
||||
fieldName: fieldName,
|
||||
valueStart: -1,
|
||||
lastEmit: 0,
|
||||
}
|
||||
}
|
||||
|
||||
// Feed processes a new argument delta and returns any new content to emit.
|
||||
// Returns empty string if no new content is available yet.
|
||||
func (e *jsonFieldExtractor) Feed(argsDelta string) string {
|
||||
if e.done {
|
||||
return ""
|
||||
}
|
||||
|
||||
e.buffer += argsDelta
|
||||
|
||||
// If we haven't found the value start yet, try to find it
|
||||
if e.valueStart < 0 {
|
||||
idx := findFieldValueStart(e.buffer, e.fieldName)
|
||||
if idx < 0 {
|
||||
return "" // Haven't seen the value start yet
|
||||
}
|
||||
e.valueStart = idx
|
||||
e.lastEmit = 0
|
||||
}
|
||||
|
||||
// Extract new content from the value portion
|
||||
valueContent := e.buffer[e.valueStart:]
|
||||
|
||||
// Find how far we can safely emit (stop before potential incomplete escape at the end)
|
||||
safeEnd, finished := findSafeEnd(valueContent, e.lastEmit)
|
||||
|
||||
if safeEnd <= e.lastEmit {
|
||||
if finished {
|
||||
e.done = true
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Extract the new chunk and unescape JSON string escapes
|
||||
rawChunk := valueContent[e.lastEmit:safeEnd]
|
||||
unescaped := unescapeJSONString(rawChunk)
|
||||
|
||||
e.lastEmit = safeEnd
|
||||
if finished {
|
||||
e.done = true
|
||||
}
|
||||
|
||||
return unescaped
|
||||
}
|
||||
|
||||
// IsDone returns whether the extractor has finished (closing quote found)
|
||||
func (e *jsonFieldExtractor) IsDone() bool {
|
||||
return e.done
|
||||
}
|
||||
|
||||
// findFieldValueStart finds the byte offset where the field's string value content begins
|
||||
// (after the opening quote of the value). Returns -1 if not found.
|
||||
func findFieldValueStart(buf string, fieldName string) int {
|
||||
// Look for "fieldName" key followed by colon and opening quote
|
||||
key := `"` + fieldName + `"`
|
||||
idx := strings.Index(buf, key)
|
||||
if idx < 0 {
|
||||
return -1
|
||||
}
|
||||
|
||||
// Skip past the key
|
||||
pos := idx + len(key)
|
||||
|
||||
// Skip whitespace and colon
|
||||
for pos < len(buf) {
|
||||
ch := buf[pos]
|
||||
if ch == ':' {
|
||||
pos++
|
||||
continue
|
||||
}
|
||||
if ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' {
|
||||
pos++
|
||||
continue
|
||||
}
|
||||
if ch == '"' {
|
||||
// Found the opening quote of the value
|
||||
return pos + 1
|
||||
}
|
||||
// Unexpected character
|
||||
return -1
|
||||
}
|
||||
|
||||
return -1 // Haven't seen the opening quote yet
|
||||
}
|
||||
|
||||
// findSafeEnd finds the safe end position for emission within the value content.
|
||||
// It scans from lastEmit forward, handling escape sequences.
|
||||
// Returns (safeEnd, finished) where finished=true if the closing quote was found.
|
||||
func findSafeEnd(value string, from int) (int, bool) {
|
||||
i := from
|
||||
for i < len(value) {
|
||||
ch := value[i]
|
||||
if ch == '\\' {
|
||||
// Escape sequence - need at least 2 bytes
|
||||
if i+1 >= len(value) {
|
||||
// Incomplete escape at end, stop before it
|
||||
return i, false
|
||||
}
|
||||
nextCh := value[i+1]
|
||||
if nextCh == 'u' {
|
||||
// Unicode escape \uXXXX - need 6 bytes total
|
||||
if i+5 >= len(value) {
|
||||
return i, false
|
||||
}
|
||||
i += 6
|
||||
} else {
|
||||
// Simple escape: \", \\, \n, \t, \r, \/, \b, \f
|
||||
i += 2
|
||||
}
|
||||
} else if ch == '"' {
|
||||
// Closing quote of the JSON string value
|
||||
return i, true
|
||||
} else {
|
||||
// Regular character - handle multi-byte UTF-8
|
||||
_, size := utf8.DecodeRuneInString(value[i:])
|
||||
if size == 0 {
|
||||
size = 1
|
||||
}
|
||||
i += size
|
||||
}
|
||||
}
|
||||
return i, false
|
||||
}
|
||||
|
||||
// unescapeJSONString converts JSON string escape sequences to their actual characters
|
||||
func unescapeJSONString(s string) string {
|
||||
if !strings.ContainsRune(s, '\\') {
|
||||
return s
|
||||
}
|
||||
|
||||
var b strings.Builder
|
||||
b.Grow(len(s))
|
||||
|
||||
i := 0
|
||||
for i < len(s) {
|
||||
if s[i] == '\\' && i+1 < len(s) {
|
||||
switch s[i+1] {
|
||||
case '"':
|
||||
b.WriteByte('"')
|
||||
i += 2
|
||||
case '\\':
|
||||
b.WriteByte('\\')
|
||||
i += 2
|
||||
case '/':
|
||||
b.WriteByte('/')
|
||||
i += 2
|
||||
case 'n':
|
||||
b.WriteByte('\n')
|
||||
i += 2
|
||||
case 'r':
|
||||
b.WriteByte('\r')
|
||||
i += 2
|
||||
case 't':
|
||||
b.WriteByte('\t')
|
||||
i += 2
|
||||
case 'b':
|
||||
b.WriteByte('\b')
|
||||
i += 2
|
||||
case 'f':
|
||||
b.WriteByte('\f')
|
||||
i += 2
|
||||
case 'u':
|
||||
// Unicode escape \uXXXX
|
||||
if i+5 < len(s) {
|
||||
// Parse hex digits
|
||||
hexStr := s[i+2 : i+6]
|
||||
var codepoint int
|
||||
for _, h := range hexStr {
|
||||
codepoint <<= 4
|
||||
switch {
|
||||
case h >= '0' && h <= '9':
|
||||
codepoint += int(h - '0')
|
||||
case h >= 'a' && h <= 'f':
|
||||
codepoint += int(h-'a') + 10
|
||||
case h >= 'A' && h <= 'F':
|
||||
codepoint += int(h-'A') + 10
|
||||
}
|
||||
}
|
||||
b.WriteRune(rune(codepoint))
|
||||
i += 6
|
||||
} else {
|
||||
b.WriteByte(s[i])
|
||||
i++
|
||||
}
|
||||
default:
|
||||
b.WriteByte(s[i])
|
||||
i++
|
||||
}
|
||||
} else {
|
||||
b.WriteByte(s[i])
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
return b.String()
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestJSONFieldExtractor_Basic(t *testing.T) {
|
||||
e := newJSONFieldExtractor("answer")
|
||||
|
||||
// Simulate streaming JSON: {"answer":"Hello world"}
|
||||
got := ""
|
||||
got += e.Feed(`{"answer":"`)
|
||||
got += e.Feed(`Hello`)
|
||||
got += e.Feed(` world`)
|
||||
got += e.Feed(`"}`)
|
||||
|
||||
if got != "Hello world" {
|
||||
t.Errorf("expected 'Hello world', got %q", got)
|
||||
}
|
||||
if !e.IsDone() {
|
||||
t.Error("expected extractor to be done")
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONFieldExtractor_WithEscapes(t *testing.T) {
|
||||
e := newJSONFieldExtractor("answer")
|
||||
|
||||
// Simulate: {"answer":"line1\nline2 and \"quoted\""}
|
||||
got := ""
|
||||
got += e.Feed(`{"answer":"line1\nline2`)
|
||||
got += e.Feed(` and \"quoted`)
|
||||
got += e.Feed(`\""}`)
|
||||
|
||||
expected := "line1\nline2 and \"quoted\""
|
||||
if got != expected {
|
||||
t.Errorf("expected %q, got %q", expected, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONFieldExtractor_OneChunk(t *testing.T) {
|
||||
e := newJSONFieldExtractor("answer")
|
||||
|
||||
got := e.Feed(`{"answer":"complete answer here"}`)
|
||||
|
||||
if got != "complete answer here" {
|
||||
t.Errorf("expected 'complete answer here', got %q", got)
|
||||
}
|
||||
if !e.IsDone() {
|
||||
t.Error("expected extractor to be done")
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONFieldExtractor_SmallChunks(t *testing.T) {
|
||||
e := newJSONFieldExtractor("answer")
|
||||
|
||||
// Very small chunks
|
||||
got := ""
|
||||
chunks := []string{`{`, `"`, `a`, `n`, `s`, `w`, `e`, `r`, `"`, `:`, `"`, `H`, `i`, `"`, `}`}
|
||||
for _, c := range chunks {
|
||||
got += e.Feed(c)
|
||||
}
|
||||
|
||||
if got != "Hi" {
|
||||
t.Errorf("expected 'Hi', got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONFieldExtractor_Markdown(t *testing.T) {
|
||||
e := newJSONFieldExtractor("answer")
|
||||
|
||||
got := ""
|
||||
got += e.Feed(`{"answer":"# Title\n\n`)
|
||||
got += e.Feed(`This is **bold** and `)
|
||||
got += e.Feed(`*italic* text.\n\n`)
|
||||
got += e.Feed(`- item 1\n- item 2`)
|
||||
got += e.Feed(`"}`)
|
||||
|
||||
expected := "# Title\n\nThis is **bold** and *italic* text.\n\n- item 1\n- item 2"
|
||||
if got != expected {
|
||||
t.Errorf("expected %q, got %q", expected, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONFieldExtractor_UnicodeEscape(t *testing.T) {
|
||||
e := newJSONFieldExtractor("answer")
|
||||
|
||||
got := ""
|
||||
got += e.Feed(`{"answer":"Hello \u4e16\u754c`)
|
||||
got += e.Feed(`"}`)
|
||||
|
||||
expected := "Hello 世界"
|
||||
if got != expected {
|
||||
t.Errorf("expected %q, got %q", expected, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONFieldExtractor_IncompleteEscapeAtBoundary(t *testing.T) {
|
||||
e := newJSONFieldExtractor("answer")
|
||||
|
||||
// Escape sequence split across chunks
|
||||
got := ""
|
||||
got += e.Feed(`{"answer":"before\`)
|
||||
got += e.Feed(`nafter"}`)
|
||||
|
||||
expected := "before\nafter"
|
||||
if got != expected {
|
||||
t.Errorf("expected %q, got %q", expected, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONFieldExtractor_WhitespaceInJSON(t *testing.T) {
|
||||
e := newJSONFieldExtractor("answer")
|
||||
|
||||
// Whitespace around colon
|
||||
got := e.Feed(`{ "answer" : "content here" }`)
|
||||
|
||||
if got != "content here" {
|
||||
t.Errorf("expected 'content here', got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONFieldExtractor_EmptyAnswer(t *testing.T) {
|
||||
e := newJSONFieldExtractor("answer")
|
||||
|
||||
got := e.Feed(`{"answer":""}`)
|
||||
|
||||
if got != "" {
|
||||
t.Errorf("expected empty string, got %q", got)
|
||||
}
|
||||
if !e.IsDone() {
|
||||
t.Error("expected extractor to be done")
|
||||
}
|
||||
}
|
||||
|
||||
// Test extracting "thought" field (for thinking tool)
|
||||
func TestJSONFieldExtractor_ThoughtField(t *testing.T) {
|
||||
e := newJSONFieldExtractor("thought")
|
||||
|
||||
got := ""
|
||||
got += e.Feed(`{"thought":"Let me analyze`)
|
||||
got += e.Feed(` the problem step by step`)
|
||||
got += e.Feed(`","next_thought_needed":true,"thought_number":1,"total_thoughts":3}`)
|
||||
|
||||
expected := "Let me analyze the problem step by step"
|
||||
if got != expected {
|
||||
t.Errorf("expected %q, got %q", expected, got)
|
||||
}
|
||||
if !e.IsDone() {
|
||||
t.Error("expected extractor to be done")
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONFieldExtractor_ThoughtFieldWithEscapes(t *testing.T) {
|
||||
e := newJSONFieldExtractor("thought")
|
||||
|
||||
got := ""
|
||||
got += e.Feed(`{"thought":"Step 1:\n- Analyze the query\n- `)
|
||||
got += e.Feed(`Search for \"relevant\" info`)
|
||||
got += e.Feed(`","thought_number":1}`)
|
||||
|
||||
expected := "Step 1:\n- Analyze the query\n- Search for \"relevant\" info"
|
||||
if got != expected {
|
||||
t.Errorf("expected %q, got %q", expected, got)
|
||||
}
|
||||
}
|
||||
@@ -197,6 +197,35 @@ func (c *OllamaChat) ChatStream(
|
||||
ToolCalls: c.toolCallTo(resp.Message.ToolCalls),
|
||||
Done: false,
|
||||
}
|
||||
|
||||
// Extract and stream content from special tools (complete, not incremental)
|
||||
for _, tc := range resp.Message.ToolCalls {
|
||||
switch tc.Function.Name {
|
||||
case "final_answer":
|
||||
if answer, ok := tc.Function.Arguments["answer"].(string); ok && answer != "" {
|
||||
streamChan <- types.StreamResponse{
|
||||
ResponseType: types.ResponseTypeAnswer,
|
||||
Content: answer,
|
||||
Done: false,
|
||||
Data: map[string]interface{}{
|
||||
"source": "final_answer_tool",
|
||||
},
|
||||
}
|
||||
}
|
||||
case "thinking":
|
||||
if thought, ok := tc.Function.Arguments["thought"].(string); ok && thought != "" {
|
||||
streamChan <- types.StreamResponse{
|
||||
ResponseType: types.ResponseTypeThinking,
|
||||
Content: thought,
|
||||
Done: false,
|
||||
Data: map[string]interface{}{
|
||||
"source": "thinking_tool",
|
||||
"tool_call_id": tooli2s(tc.Function.Index),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if resp.Done {
|
||||
|
||||
@@ -334,7 +334,7 @@ func (c *RemoteAPIChat) chatStreamWithRawHTTP(ctx context.Context, customReq any
|
||||
return nil, fmt.Errorf("marshal request: %w", err)
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "[LLM Stream] model=%s, raw HTTP request:\n%s", c.modelName, string(jsonData))
|
||||
logger.Infof(ctx, "[LLM Stream] model=%s", c.modelName)
|
||||
|
||||
endpoint := c.baseURL + "/chat/completions"
|
||||
httpReq, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewBuffer(jsonData))
|
||||
@@ -456,6 +456,7 @@ type streamState struct {
|
||||
lastFunctionName map[int]string
|
||||
nameNotified map[int]bool
|
||||
hasThinking bool
|
||||
fieldExtractors map[int]*jsonFieldExtractor // per tool-call-index extractors for streaming field extraction
|
||||
}
|
||||
|
||||
func newStreamState() *streamState {
|
||||
@@ -464,6 +465,7 @@ func newStreamState() *streamState {
|
||||
lastFunctionName: make(map[int]string),
|
||||
nameNotified: make(map[int]bool),
|
||||
hasThinking: false,
|
||||
fieldExtractors: make(map[int]*jsonFieldExtractor),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -587,6 +589,47 @@ func (c *RemoteAPIChat) processToolCallsDelta(toolCalls []openai.ToolCall, state
|
||||
}
|
||||
|
||||
state.lastFunctionName[toolCallIndex] = currName
|
||||
|
||||
// Stream final_answer tool arguments as answer-type chunks
|
||||
if toolCallEntry.Function.Name == "final_answer" && argsUpdated {
|
||||
extractor, exists := state.fieldExtractors[toolCallIndex]
|
||||
if !exists {
|
||||
extractor = newJSONFieldExtractor("answer")
|
||||
state.fieldExtractors[toolCallIndex] = extractor
|
||||
}
|
||||
answerChunk := extractor.Feed(tc.Function.Arguments)
|
||||
if answerChunk != "" {
|
||||
streamChan <- types.StreamResponse{
|
||||
ResponseType: types.ResponseTypeAnswer,
|
||||
Content: answerChunk,
|
||||
Done: false,
|
||||
Data: map[string]interface{}{
|
||||
"source": "final_answer_tool",
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Stream thinking tool's thought field as thinking-type chunks
|
||||
if toolCallEntry.Function.Name == "thinking" && argsUpdated {
|
||||
extractor, exists := state.fieldExtractors[toolCallIndex]
|
||||
if !exists {
|
||||
extractor = newJSONFieldExtractor("thought")
|
||||
state.fieldExtractors[toolCallIndex] = extractor
|
||||
}
|
||||
thoughtChunk := extractor.Feed(tc.Function.Arguments)
|
||||
if thoughtChunk != "" {
|
||||
streamChan <- types.StreamResponse{
|
||||
ResponseType: types.ResponseTypeThinking,
|
||||
Content: thoughtChunk,
|
||||
Done: false,
|
||||
Data: map[string]interface{}{
|
||||
"source": "thinking_tool",
|
||||
"tool_call_id": toolCallEntry.ID,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -80,6 +80,8 @@ type Message struct {
|
||||
IsCompleted bool `json:"is_completed"`
|
||||
// Whether this response is a fallback (no knowledge base match found)
|
||||
IsFallback bool `json:"is_fallback,omitempty"`
|
||||
// Agent total execution duration in milliseconds (from query start to answer start)
|
||||
AgentDurationMs int64 `json:"agent_duration_ms,omitempty" gorm:"column:agent_duration_ms;default:0"`
|
||||
// Message creation timestamp
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
// Last update timestamp
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
-- Remove agent_duration_ms column from messages table
|
||||
ALTER TABLE messages DROP COLUMN IF EXISTS agent_duration_ms;
|
||||
@@ -0,0 +1,3 @@
|
||||
-- Add agent_duration_ms column to messages table
|
||||
-- Stores the total agent execution duration in milliseconds (from query start to answer start)
|
||||
ALTER TABLE messages ADD COLUMN IF NOT EXISTS agent_duration_ms BIGINT DEFAULT 0;
|
||||
Reference in New Issue
Block a user