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:
@@ -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':
|
||||
|
||||
Reference in New Issue
Block a user