From a4b255a456c745be05b338bcc518429e6567e79d Mon Sep 17 00:00:00 2001 From: wizardchen Date: Fri, 6 Mar 2026 12:21:23 +0800 Subject: [PATCH] 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. --- .codebuddy/rules/国际化语言支持.mdc | 9 + frontend/src/i18n/locales/en-US.ts | 2 + frontend/src/i18n/locales/ko-KR.ts | 2 + frontend/src/i18n/locales/ru-RU.ts | 2 + frontend/src/i18n/locales/zh-CN.ts | 2 + .../chat/components/AgentStreamDisplay.vue | 1310 +++++++---------- frontend/src/views/chat/index.vue | 35 +- internal/agent/engine.go | 145 +- internal/agent/prompts.go | 6 +- internal/agent/tools/definitions.go | 3 + internal/agent/tools/final_answer.go | 87 ++ internal/agent/tools/sequentialthinking.go | 6 +- internal/application/service/agent_service.go | 6 +- .../handler/session/agent_stream_handler.go | 3 + internal/models/chat/json_field_extractor.go | 224 +++ .../models/chat/json_field_extractor_test.go | 165 +++ internal/models/chat/ollama.go | 29 + internal/models/chat/remote_api.go | 45 +- internal/types/message.go | 2 + .../000017_add_agent_duration_ms.down.sql | 2 + .../000017_add_agent_duration_ms.up.sql | 3 + 21 files changed, 1311 insertions(+), 777 deletions(-) create mode 100644 .codebuddy/rules/国际化语言支持.mdc create mode 100644 internal/agent/tools/final_answer.go create mode 100644 internal/models/chat/json_field_extractor.go create mode 100644 internal/models/chat/json_field_extractor_test.go create mode 100644 migrations/versioned/000017_add_agent_duration_ms.down.sql create mode 100644 migrations/versioned/000017_add_agent_duration_ms.up.sql diff --git a/.codebuddy/rules/国际化语言支持.mdc b/.codebuddy/rules/国际化语言支持.mdc new file mode 100644 index 00000000..5db199f3 --- /dev/null +++ b/.codebuddy/rules/国际化语言支持.mdc @@ -0,0 +1,9 @@ +--- +description: +alwaysApply: true +enabled: true +updatedAt: 2026-03-06T08:11:34.113Z +provider: +--- + +i18n国际化文件修改的时候同时修改i18n/locals下面的所有语言文件 \ No newline at end of file diff --git a/frontend/src/i18n/locales/en-US.ts b/frontend/src/i18n/locales/en-US.ts index cecd9cbc..13d2cfea 100755 --- a/frontend/src/i18n/locales/en-US.ts +++ b/frontend/src/i18n/locales/en-US.ts @@ -266,6 +266,8 @@ export default { webSearchFound: 'Found {count} web search result(s)', argumentsLabel: 'Arguments', toolFallback: 'Tool', + stepsCompleted: 'Completed {steps} step(s)', + stepsCompletedWithDuration: 'Completed {steps} step(s) in {duration}', title: 'Agents', subtitle: 'Configure and manage your agents to customize conversation behavior and capabilities', createAgent: 'Create Agent', diff --git a/frontend/src/i18n/locales/ko-KR.ts b/frontend/src/i18n/locales/ko-KR.ts index 81bbe05a..44f7c36f 100755 --- a/frontend/src/i18n/locales/ko-KR.ts +++ b/frontend/src/i18n/locales/ko-KR.ts @@ -917,6 +917,8 @@ export default { webSearchFound: "{count}개의 웹 검색 결과 발견", argumentsLabel: "파라미터", toolFallback: "도구", + stepsCompleted: "{steps}개 단계 완료", + stepsCompletedWithDuration: "{steps}개 단계 완료, 소요 시간 {duration}", title: "에이전트", subtitle: "에이전트 구성 및 관리, 대화 동작 및 기능 맞춤화", createAgent: "에이전트 만들기", diff --git a/frontend/src/i18n/locales/ru-RU.ts b/frontend/src/i18n/locales/ru-RU.ts index 2c589567..b7d8b53d 100755 --- a/frontend/src/i18n/locales/ru-RU.ts +++ b/frontend/src/i18n/locales/ru-RU.ts @@ -256,6 +256,8 @@ export default { webSearchFound: 'Найдено {count} результатов веб‑поиска', argumentsLabel: 'Параметры', toolFallback: 'Инструмент', + stepsCompleted: 'Выполнено {steps} шаг(ов)', + stepsCompletedWithDuration: 'Выполнено {steps} шаг(ов) за {duration}', editor: { skillsConfig: 'Skills', skillsConfigDesc: 'Настройка предустановленных Skills для агента, предоставляющих специализированные знания и рабочие процессы', diff --git a/frontend/src/i18n/locales/zh-CN.ts b/frontend/src/i18n/locales/zh-CN.ts index b0bc35df..6146acc3 100755 --- a/frontend/src/i18n/locales/zh-CN.ts +++ b/frontend/src/i18n/locales/zh-CN.ts @@ -910,6 +910,8 @@ export default { webSearchFound: "找到 {count} 个网络搜索结果", argumentsLabel: "参数", toolFallback: "工具", + stepsCompleted: "已完成 {steps} 个步骤", + stepsCompletedWithDuration: "已完成 {steps} 个步骤,耗时 {duration}", title: "智能体", subtitle: "配置和管理您的智能体,自定义对话行为和能力", createAgent: "创建智能体", diff --git a/frontend/src/views/chat/components/AgentStreamDisplay.vue b/frontend/src/views/chat/components/AgentStreamDisplay.vue index 72d6a039..13fbd2d6 100644 --- a/frontend/src/views/chat/components/AgentStreamDisplay.vue +++ b/frontend/src/views/chat/components/AgentStreamDisplay.vue @@ -1,23 +1,158 @@