WeKnora/internal/agent/tools
wizardchen a6d4266c93 fix(agent): terminate ReAct loop on final_answer regardless of arg parse success
The agent loop previously kept running when the LLM emitted final_answer with
malformed JSON arguments: observe.analyzeResponse strictly json.Unmarshal'd the
raw args and returned isDone=false on failure, while act.runToolCall separately
repaired the JSON and executed the tool. The mismatch made final_answer
non-terminal, so the LLM saw its own answer in the next round's tool result
and re-emitted final_answer, surfacing near-identical answers multiple times
to the user (issue #1008).

Fix by unifying parsing in a new tools.ParseFinalAnswerArgs helper with three
fallbacks (strict -> RepairJSON -> regex extraction) and making final_answer
always terminal in analyzeResponse. When no answer can be recovered, emit a
user-visible fallback message and a Done=true event so the UI still resolves.

Also route final_answer.Execute through the same helper so tool execution and
loop termination stay consistent.

Adds regression tests in observe_test.go and final_answer_test.go covering
valid args, RepairJSON recovery, unrecoverable args, and garbage input.

Refs: Tencent/WeKnora#1008
2026-04-23 19:36:28 +08:00
..
capabilities.go feat: Enhance agent tool compatibility and knowledge base filtering 2026-04-23 17:28:00 +08:00
data_analysis.go feat: use language-neutral English prompts for multilingual LLM responses 2026-03-06 21:08:12 +08:00
data_schema.go feat: add organization management features 2026-02-09 11:40:25 +08:00
database_query.go feat(database): enhance DatabaseQueryTool with search scope filtering 2026-03-31 10:52:01 +08:00
definitions.go feat: Introduce Wiki Fixer agent and related functionalities 2026-04-22 21:18:08 +08:00
final_answer.go fix(agent): terminate ReAct loop on final_answer regardless of arg parse success 2026-04-23 19:36:28 +08:00
final_answer_test.go fix(agent): terminate ReAct loop on final_answer regardless of arg parse success 2026-04-23 19:36:28 +08:00
get_document_info.go feat: Update agent type presets and prompt templates for improved knowledge base handling 2026-04-22 21:18:34 +08:00
grep_chunks.go feat: Enhance agent retrieval strategies and regex handling 2026-04-22 21:18:21 +08:00
json_repair.go feat: Enhance agent retrieval strategies and regex handling 2026-04-22 21:18:21 +08:00
json_repair_test.go feat: Enhance agent retrieval strategies and regex handling 2026-04-22 21:18:21 +08:00
knowledge_search.go feat: Update agent type presets and prompt templates for improved knowledge base handling 2026-04-22 21:18:34 +08:00
list_knowledge_chunks.go feat: Update agent type presets and prompt templates for improved knowledge base handling 2026-04-22 21:18:34 +08:00
mcp_tool.go feat(mcp): implement reconnection logic for MCP tool calls and tool listing 2026-03-31 11:57:15 +08:00
mcp_tool_image_test.go feat: auto-describe MCP tool result images via VLM 2026-03-25 21:38:15 +08:00
mcp_tool_test.go feat(tests): add comprehensive tests for MCPTool functionality 2026-03-25 22:08:29 +08:00
normalize_id.go feat(agent): enhance tool execution and message sanitization 2026-03-25 22:08:29 +08:00
normalize_id_test.go feat(agent): enhance tool execution and message sanitization 2026-03-25 22:08:29 +08:00
param_cast.go refactor(prompt): streamline context handling in templates 2026-03-25 22:08:29 +08:00
param_cast_test.go refactor(prompt): streamline context handling in templates 2026-03-25 22:08:29 +08:00
param_validate.go feat(agent): implement memory consolidation and context management 2026-03-25 22:08:29 +08:00
param_validate_test.go feat(agent): implement memory consolidation and context management 2026-03-25 22:08:29 +08:00
query_knowledge_graph.go feat: use language-neutral English prompts for multilingual LLM responses 2026-03-06 21:08:12 +08:00
registry.go feat(agent): implement memory consolidation and context management 2026-03-25 22:08:29 +08:00
sanitize_messages.go feat(agent): enhance tool execution and message sanitization 2026-03-25 22:08:29 +08:00
sanitize_messages_test.go feat(agent): enhance tool execution and message sanitization 2026-03-25 22:08:29 +08:00
sequentialthinking.go refactor(prompt): streamline context handling in templates 2026-03-25 22:08:29 +08:00
skill_execute.go feat: optimize security and deployment of agent skills 2026-02-04 20:08:49 +08:00
skill_read.go feat: support agent skills 2026-02-04 20:08:49 +08:00
strip_think.go refactor(agent): remove query display from DatabaseQuery component and update tool result structure 2026-03-25 22:08:29 +08:00
strip_think_test.go refactor(agent): remove query display from DatabaseQuery component and update tool result structure 2026-03-25 22:08:29 +08:00
todo_write.go feat: use language-neutral English prompts for multilingual LLM responses 2026-03-06 21:08:12 +08:00
tool.go feat: use language-neutral English prompts for multilingual LLM responses 2026-03-06 21:08:12 +08:00
truncate.go feat(agent): implement memory consolidation and context management 2026-03-25 22:08:29 +08:00
truncate_test.go feat(agent): implement memory consolidation and context management 2026-03-25 22:08:29 +08:00
web_fetch.go fix(security): support IPv6 in SSRF validation via whitelist mechanism 2026-03-31 20:45:15 +08:00
web_search.go feat(web-search): refactor web search provider 2026-03-31 20:22:34 +08:00
wiki_delete_page.go feat: Implement wiki editing functionalities and UI components 2026-04-22 21:18:08 +08:00
wiki_flag_issue.go feat: Introduce `wiki_flag_issue` tool for reporting wiki page issues 2026-04-22 21:17:38 +08:00
wiki_read_issue.go feat: Introduce Wiki Fixer agent and related functionalities 2026-04-22 21:18:08 +08:00
wiki_read_source_doc.go feat: Enhance agent retrieval strategies and regex handling 2026-04-22 21:18:21 +08:00
wiki_rename_page.go feat: Implement wiki editing functionalities and UI components 2026-04-22 21:18:08 +08:00
wiki_replace_text.go feat: Implement wiki editing functionalities and UI components 2026-04-22 21:18:08 +08:00
wiki_tools.go feat: Enhance agent retrieval strategies and regex handling 2026-04-22 21:18:21 +08:00
wiki_tools_test.go feat: Add Wiki Researcher agent and system prompt for enhanced knowledge retrieval 2026-04-22 21:17:35 +08:00
wiki_update_issue.go feat: Introduce Wiki Fixer agent and related functionalities 2026-04-22 21:18:08 +08:00
wiki_write_page.go feat: Implement wiki editing functionalities and UI components 2026-04-22 21:18:08 +08:00