简体中文
工具:属性与上下文
工具就是一个普通的 Python 函数。ToolBox 从函数签名与 docstring 推导 OpenAI 工具 schema:docstring 整体作为描述,参数生成 JSON schema。
python
from xun import ToolBox
def multiply(a: int, b: int) -> int:
"""Multiply two numbers."""
return a * b
toolbox = ToolBox().register(multiply)模型需要填写的参数必须有类型标注;带默认值的参数成为可选参数。参数中的多余字段会被拒绝(extra="forbid");序列参数则比较宽容:模型把 "[1, 2]" 作为字符串传入,仍会得到一个列表。
返回值可以是任何可 JSON 序列化的值。若要以模型能读懂的方式报告失败,返回 Result.Err(ErrorInfo(...)),或者直接抛异常:注册时会包装函数,异常会变成会话记录里的错误结果,而不会终止运行。
ToolBox
| 方法 | 说明 |
|---|---|
register(*funcs) | 包装并注册普通函数。 |
tool | 装饰器写法:@toolbox.tool。 |
register_raw(*functions) | 注册已准备好的 Function 对象。 |
with_defaults(*sets) | 注册内置工具集;不带参数表示全部。可链式调用。 |
pop(name) | 彻底移除一个工具。 |
disable(*names_or_globs) / enable(...) | 保留注册但对模型隐藏;支持 */? 通配。 |
is_disabled(name), list_tools(capabilities=None, include_disabled=False) | 自省接口;能力过滤由执行循环依据 config.model.capabilities 完成。 |
clone() | 浅拷贝 —— Agent.inherit 用的就是它。 |
with_subagent_provider(...), disable_subagent() | 见子智能体。 |
名称冲突会抛出异常,除非其中一方声明了 override=True。
内置工具集
| 工具集 | 工具 |
|---|---|
fs | list_dir, read_file, file_info, write_file, move, copy, mkdir, delete, temp_dir, glob, grep, request_image |
patch | apply_patch(git 风格的统一 diff,用 git apply 应用) |
cmd | bash, bash_spawn, bash_wait, bash_running, bash_kill —— 在 Windows 上不注册 |
search | web_search(浏览器驱动的网页搜索) |
browser | browser_page, browser_resize, browser_snapshot, browser_interact, browser_evaluate, browser_logs, browser_screenshot(Playwright;需要 playwright install) |
system | system_info, datetime, sleep |
framework | ask_preference, extract_compacted_tool_result |
diagnostic | check_syntax, check_lint |
文件类工具把自己限定在智能体的 Workspace(工作目录及其临时目录)内;写入、删除与风险命令都会走确认策略,除非开启 auto_confirm。允许列表由 /policy 管理。
工具属性
@tool_attr 只附加元数据,不改变函数的调用方式:
python
from xun import tool_attr
@tool_attr(name="MultiplyTool", required_capabilities=["vision"], override=False)
def multiply(a: int, b: int) -> int:
"""Multiply two numbers."""| 字段 | 作用 |
|---|---|
name | 模型看到的名称;Python 名称仍由你决定(内置工具会用短名称,例如 fs_grep_files 对外是 grep)。 |
required_capabilities | 只有当模型的 config.model.capabilities 包含这些能力时才提供该工具(目前是 vision,例如截图与图片工具)。 |
override | 直接替换同名的已注册工具而不抛异常。带 override 的注册优先于其后的普通注册,扩展替换内置工具就靠这一点。 |
工具上下文
需要访问智能体、工具名称或单次运行数据的工具,声明一个 ToolCallContext 参数即可;框架会注入它,并将其从 schema 中排除。
python
from xun import ToolCallContext as Context, setup_agent, NullDisplay
import datetime
from pathlib import Path
def workdir_name(context: Context[dict]):
"""Name of the agent's working directory."""
context.value["tool_call_time"] = datetime.datetime.now().strftime("%H:%M:%S")
return context.agent.workspace.workdir.name
state = {"tool_call_time": "?"}
agent = setup_agent(tools=[workdir_name], display=NullDisplay())
agent.instruct("What is your work directory's name?").execute(context=state)
print(state) # 工具写进了这个字典| 成员 | 内容 |
|---|---|
ctx.agent | 正在运行的智能体 —— 工作目录、配置、state、check_cancel()、显示层方法。 |
ctx.tool_name | 被调用工具的注册名称。 |
ctx.value | 即 execute(context=…) 传入的值。同一次运行中的所有工具共享它,因此可用作暂存空间或结果收集袋。 |
标注可以是裸的 ToolCallContext,也可以是泛型的 ToolCallContext[dict] —— 类型参数只是给 ctx.value 的提示,而 execute(context=…) 默认为 None。同一次运行中调用的工具共享同一个上下文对象,子智能体也正是从父智能体的工具调用继承它。