""" 上下文工具 - 获取当前执行上下文 提供 get_current_context 工具,让 Agent 可以主动获取: - 当前计划(GoalTree) - 焦点提醒 - 协作者状态 框架也会在特定轮次自动调用此工具进行周期性上下文刷新。 """ from agent.tools import tool, ToolResult, ToolContext @tool( description="获取当前执行上下文,包括计划状态、焦点提醒、协作者信息等。当你感到困惑或需要回顾当前任务状态时调用。", hidden_params=["context"] ) async def get_current_context( context: ToolContext, ) -> ToolResult: """ 获取当前执行上下文 Returns: ToolResult: 包含 GoalTree、焦点提醒、协作者状态等信息 """ runner = context.get("runner") goal_tree = context.get("goal_tree") trace_id = context.get("trace_id") if not runner: return ToolResult( title="❌ 无法获取上下文", output="Runner 未初始化", error="Runner not available" ) # 获取 trace 对象 trace = None if runner.trace_store and trace_id: trace = await runner.trace_store.get_trace(trace_id) # 构建上下文内容(复用 runner 的 _build_context_injection 方法) if hasattr(runner, '_build_context_injection'): context_content = runner._build_context_injection(trace, goal_tree) else: # Fallback:只返回 GoalTree if goal_tree and goal_tree.goals: context_content = f"## Current Plan\n\n{goal_tree.to_prompt()}" else: context_content = "暂无计划信息" if not context_content: context_content = "当前无需要刷新的上下文信息" return ToolResult( title="📋 当前执行上下文", output=context_content, long_term_memory="已刷新执行上下文", )