think_and_plan.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import json
  2. from agent.tools import tool
  3. from utils.log_capture import log, log_fold
  4. def _log_tool_call(tool_name: str, params: dict, result: str) -> None:
  5. """以折叠块结构化输出工具调用的参数与返回内容(经 log() 进入 build_log buffer)。"""
  6. with log_fold(f"🔧 {tool_name}"):
  7. with log_fold("📥 调用参数"):
  8. log(json.dumps(params, ensure_ascii=False, indent=2))
  9. with log_fold("📤 返回内容"):
  10. log(result)
  11. @tool(
  12. description="系统化思考与规划工具。不会获取新信息或更改数据库,只用于记录思考过程。",
  13. )
  14. def think_and_plan(thought: str, thought_number: int, action: str, plan: str) -> str:
  15. """这是用于系统化思考与规划的工具,支持在面对复杂选题构建任务时分阶段梳理思考、规划和行动步骤。该工具不会获取新信息或更改数据库,只会将想法附加到记忆中。
  16. Args:
  17. thought: 当前的思考内容,可以是对问题的分析、假设、洞见、反思或对前一步骤的总结。
  18. thought_number: 当前思考步骤的编号,用于追踪和回溯整个思考与规划过程。
  19. action: 基于当前思考和计划,建议下一步采取的行动步骤。
  20. plan: 针对当前任务拟定的计划或方案。
  21. Returns:
  22. A string describing the thought, plan, and action steps.
  23. """
  24. params = {
  25. "thought": thought,
  26. "thought_number": thought_number,
  27. "action": action,
  28. "plan": plan,
  29. }
  30. result = (
  31. f"[思考 #{thought_number}]\n"
  32. f"思考: {thought}\n"
  33. f"计划: {plan}\n"
  34. f"下一步: {action}\n"
  35. f"(此工具仅用于记录思考过程,不会修改任何数据)"
  36. )
  37. _log_tool_call("think_and_plan", params, result)
  38. return result