函数调用
默认开启,由用户收窄:注册的工具零配置可用,每一层默认都不限制。工具调用结果只存在运行历史里,不进入后续 prompt 上下文,所以广泛暴露没有上下文成本。循环机制见工具调用。
单一注册表
| 编写 | API wire |
|---|---|
@function | Tool、ToolCall、ToolResultMessage |
Agent 方法,method_options[name]["tool"] = True | tools=[...] |
agent_tools()、get_agent_tool() | tool_calls=[...] |
动作叫“函数调用”,API 里的对象仍叫 tool。AgentTool、AgentToolResult 补上 wire 没有的东西:sidecar gating、同步转异步、字符上限。Provider 序列化每个工具;返回的 tool_calls 按名匹配并运行 AgentTool.execute(...)。受管 Program 包在 PROGRAM_ENTRIES 列出公开入口。自动作用域采集不暴露任何东西。类式编写和生命周期见 Agent 与 Context 契约。
六层 gating
| 层 | 配置 | 默认 | 被拒绝时 |
|---|---|---|---|
| 1 导入 | available_if | 注册 | 不进 _registry |
| 2 暴露 | expose=False,只用于内部辅助 | 暴露,含 plugin 和 MCP。裸 runtime.exec 拿到全部 | Python 可调,绝不进 tools 数组 |
| 2b 预设 | toolset=[...]、TOOLSETS、DEFAULT_TOOLS、命名 profile;toolset="none" 表示不要工具 | 不强加 | 命名子集,不是可见性门 |
| 3 会话与渠道 | agent_profile.toolset、unsafe_in | 不限制。source=None 不过滤 | 集合收窄;渠道(telegram、wechat、plan)隐藏工具 |
| 4 列表构建 | check_fn、requires_env、can_use;agent_tools(only_available=True) | 假定可运行;只在 dispatcher 路径检查 | 移除,LLM 不会看到调用必错的工具 |
| 5 用户与策略 | agent_tools(deny=, allow=)、agent_profile.disabled、tools.disabled、attended 模式 | 不否决 | 按名减去;attended 模式扣下 ask_user_question |
| 6 Prompt | defer=True、MCP 接线 | schema 随请求发送 | 目录里只有名称和一行说明;tool_search 加载 schema |
第 2、2b、6 层做选择;第 3、4、5 层只否决。第 1 到 5 层表示 LLM 用不了该工具。第 6 层让 LLM 自己选择加载什么。
为什么 2b 和 5 分开
2b 选择要用的集合;5 去掉不许用的。否决必须独立于选择:attended 模式在任何文件夹下都要扣下 ask_user_question。所以 Programs 页以 2b(挑选文件夹)为主,5 是单个工具的关闭开关。系统否决(attended,经 allow、deny 的子 agent 和角色上限)只在代码里,用户看不到。
插件与 MCP server
注册即可,没有中央 allowlist。插件工具和内置工具一样走 _build_and_register_tool。MCP 工具在连接时注册为 AgentTool,默认 defer=True。
工具设置
| 设置 | 控制 |
|---|---|
name、description、parameters、label | LLM 看到的内容。@function 从签名和 docstring 推导;Agent 方法复用 _build_agentic_tool_spec |
max_result_weight、stream_capacity_chars | 比模型更小的结果上限,记为 details["cap"],由 agent 循环截一次,完整文字存到对话的 tool_results/<sha256>.txt(工具输出);流式 on_update 的有界尾部 |
timeout、timeout_min、timeout_max | 静态或由 LLM 控制的超时 |
cache、cache_ttl | 按名称和参数记忆 |
describe | 当前的描述和参数,每次解析工具列表时应用(current_tool),工具只说出可用的东西 |
requires_approval | dispatcher 调用前询问 |
register_globally=False | 构建工具和 sidecar,跳过全局注册(测试隔离) |
参考框架没有的四个旋钮
Claude Code、Hermes、OpenClaw 都没有。
| 旋钮 | 行为 |
|---|---|
| 小窗口里的结果占比 | agent 循环存下的结果限制在模型的 tool_output_weight(默认 10,000)以内,且不超过 0.3 × 上下文窗口,按权重计:中日韩字符算 1,其余算 0.25。手动添加、没填窗口的本地模型(4,096 token)得到 1,228,32,768 token 的模型得到 9,830。spill.result_cap 在每次工具调度前按模型设置,周报读取来源时也一样 |
| 这个占比里的 read 分页 | read 按同样的上限分页,页头和续读提示都算在内,单行截到一页放得下的长度。读取碰到的 AGENTS.md 放在结果旁边、单独截断,不占这一页的地方 |
| LLM 可控超时 | 函数声明 timeout 且装饰器设了范围时,值被钳制,同时用作 wait_for 预算和参数 |
| 有界流式尾部 | _TailAccumulator;容量默认 40,000 字符,溢出时淘汰头部 |
can_use() | 会话级“这个用户或角色能否用”,与 check_fn(进程)、unsafe_in(渠道)并列 |
Agent 方法注册
from openprogram import Agent
class ResearchAgent(Agent):
method_options = {
"research": {"tool": True, "name": "research", "toolset": ["research"], "expose": "io"},
}
def research(self, topic: str) -> str:
"""Research a topic."""
return self(topic)
researcher = ResearchAgent()
PROGRAM_ENTRIES = [researcher.research]
- 方法处理负责取消钩子、runtime 注入、Context 派生、DAG 入口与终态更新、错误恢复。选项保留 schema、缓存、超时、可用性、审批和显式步骤恢复。
- 构造时只注册要求暴露为工具的方法,用同一注册表和 sidecar。
available_if只门控注册,不影响方法作用域。 - 受管加载器记录包归属,不增加公开装饰器,也不扩大权限。
工具 profile
- 目录
- 全部已注册、已暴露的工具,在
/programs只读展示 - Profile
- 命名的工具集。新建时是全部工具的副本,再删掉该场景不需要的
FULL、BUILT-IN- 始终存在,不可修改或删除。
FULL是默认激活项 - 使用
- Agents → Capabilities → Programs 选择全部工具、选定范围、命名 profile(Access preset)或无。输入框没有工具选择器。profile 名可用于所有接受
toolset=的地方,以及agent.json的tools.toolset - 全局禁用
tools.disabled在 profile 之后应用,从所有地方移除该工具
// functions_meta.json,与 programs_meta.json 同目录
{
"profiles": {
"FULL": ["bash", "read", "write", ...],
"BUILT-IN": [...],
"research": ["web_search", "web_fetch", "read", "write", "bash"]
},
"active": "FULL"
}
| 入口 | 控制 | 层 | 存储 |
|---|---|---|---|
Programs 页、/api/tool-profiles | 创建、编辑、删除 profile | 2b | functions_meta.json |
| 输入框“+”菜单 | Web Search 开关,叠加在 Agent 的工具集上 | 2b、5 | 会话元数据中的分支配置 |
Agent profile tools | 全部、选定、Access preset、无;禁用名称 | 2b、5 | ~/.openprogram/agents/<id>.json |
| Attended 开关(CLI、TUI、Web) | 无人值守时扣下 ask_user_question | 5 | 会话状态 |
| 全局工具禁用 | 单个工具全局关闭;覆盖任何 profile;很少用 | 5 | config.json:tools.disabled |
| 装饰器参数 | expose、available_if、defer、toolset、unsafe_in、check_fn | 1、2、3、6 | 代码 |
解析路径
resolve_tools调用agent_tools(toolset=…, source=req.source, only_available=True),走第 2 到 5 层,不走第 6 层。- 延迟目录作为
turn_context的deferred_catalog段放在用户消息旁:范围内的全部延迟工具,加载与否都列出,第一次发完整列表,之后只发新增和移除,所以加载工具或服务器连上都不会改动提示(延迟工具)。agent_loop仍收到完整列表。 - 每次 provider 调用前,
split_tools_for_dispatch保留非延迟工具,以及本次请求自己的消息加载过的工具(loaded_tools(messages):工具结果上的加载记录、压缩摘要携带的名字),作为tools=[]。 - 收到
ToolCall时,agent_loop在context.tools或_registry查找,校验参数,awaitexecute(call_id, args, cancel, on_update)。 tool_search(select="cron")加载cron:下一次请求带上其 schema,因为这次搜索的结果记下每个加载工具的定义(details.loads、record_loads),历史里保留模型第一次收到的版本。原生发送加载器的 Responses provider 把它放在这次搜索的结果里,而不是tools=[](Codex 传输)。
各部分所在位置
| 文件 | 内容 |
|---|---|
openprogram/programs/_host/runtime.py | _registry(第 2 层来源)、_toolset_membership、_unsafe_in_channel、filter_for、_build_and_register_tool、function、_parse_docstring、_build_parameters_schema、_evaluate_approval、tool_requires_approval、_TailAccumulator、loaded_tools、record_loads、split_tools_for_dispatch、deferred_catalog_text、tool_search |
openprogram/programs/_host/execution.py | _normalize_result(从不截断)、with_cap、_persist_full_result |
openprogram/programs/_authoring/helpers.py | is_available_agent_tool:第 4 层 |
openprogram/programs/__init__.py | DEFAULT_TOOLS、TOOLSETS、agent_tools、apply_tool_policy、get_agent_tool、list_registered_agent_tools、list_available;导入所有子包使工具注册 |
openprogram/programs/tools/<group>/<name>/ | 每个叶子工具一个 @function |
openprogram/programs/applications/*/__init__.py | Agent 方法模块 |
openprogram/runtime/agent/agent_class.py | Agent、方法作用域、method_options、注册 |
openprogram/runtime/agent/agent_method.py | MethodOptions、执行、schema、注册 |
openprogram/runtime/agent/call_state.py | 任务本地的调用身份、runtime 注入、取消钩子 |
openprogram/runtime/agent/call_scope.py | 作用域进出、Context 和 DAG 状态 |
openprogram/runtime/conversation/turn_runtime/ | turn_context.py 安装延迟状态;loop_runner.py 解析工具;第 2 到 5 层、目录块 |
openprogram/runtime/agent/loop/streaming.py | 每次 provider 调用的 split_tools_for_dispatch:第 6 层执行 |
测试不变量
tests/unit/programs/runtime/test_tools_runtime.py、tests/component/agent/turns/test_dispatcher_tools.py:
- docstring 和签名转参数 schema;同步、异步分发;异常转
AgentToolResult(is_error=True) - 记下声明的上限,循环只截一次并保存全文;
on_update投递和尾部累积;取消传播;asyncio.wait_for超时;requires_approval - 按 toolset、source、names 过滤;所有内置工具在导入时注册;
@function覆盖项 - 第 1 层在 False 或异常时跳过注册
- 第 6 层 sidecar、
tool_search提升、未知名称、目录文本 - Agent 方法只有显式
tool=True或as_tool=True才注册;tool=False跳过注册表;register_globally=False跳过但附加_agent_tool;available_if为 False 跳过注册,保留作用域处理
稳定边界
| 不触及边界 | 会触及,非必要不做 |
|---|---|
新增 @function 工具;带 tool=True 的 Agent 方法;expose=False;TOOLSETS 或 Functions 页文件夹里的命名子集;defer、available_if;接入 MCP server 和插件 | 新增 gating 层;改 AgentTool.execute 签名;拆分或合并注册表;替换延迟加载 |
实现状态
已完成:注册表、六层、@function 和 Agent 方法注册、四个旋钮、Agent profile 选择(Access preset)、profile 存储和 /api/tool-profiles 端点。未完成:Programs 页的 profile 管理界面。