run_code
针对一项需要多次调用的任务,运行一段简短脚本来调用你的 SDK。代理会在一段脚本中获取、筛选并合并数据,只返回答案,因此原始响应不会占满其上下文。脚本中的每次 SDK 调用都是真实的 API 调用,并会像其所代表的工具一样接受检查:需要审批的调用会在脚本等待时请求用户批准。
何时会列出
在代码模式下,与 describe_sdk 并列。
参数
codestring必填脚本:函数体,其中 client 在作用域内,且函数会 return 一个 JSON 值。最多 20,000 个字符。
languagestringtypescript,调用会被 await;或者 python,调用会阻塞。Python 以 MicroPython 运行,并提供 json、re、math 和 collections。
返回值
脚本是否运行完成、调用次数、返回值、打印的内容,以及每次调用各占一行的状态信息。调用失败时会抛出脚本可捕获的错误:ApiError、ApprovalDeclined、ApprovalRequired、ApiOutage 或 CallLimitReached。
限制
每次运行都有以下限制:
| 限制 | 值 |
|---|---|
| API 调用 | 每次运行 50 次 |
| 需要请求批准的调用 | 每次运行 10 次 |
| 运行时间 | 30 秒,不包括等待调用的时间 |
| 总耗时 | 5 分钟,包括等待时间 |
| 内存 | 64 MB |
| 打印输出 | 8,000 字节,超出部分将被丢弃 |
| 返回值 | 24,000 字节 |
脚本不能访问 SDK 以外的网络、文件或导入项。在 Cloudflare Worker 上,运行时间限制为 20 秒,内存限制为 24 MB。
提示
客户端读取的注解,用于判断哪些操作可以在不询问用户的情况下运行。
| 提示 | 值 | 为 true 时,表示 |
|---|---|---|
readOnlyHint | false | 不会产生任何更改 |
destructiveHint | true | 可能会更改或删除数据 |
openWorldHint | true | 会通过网络访问服务器以外的资源 |
示例
在脚本中筛选
在 练习模式 下运行,因此 API 的响应为示例数据。
Script
const subs = await client.subscriptions.list();
const trialing = subs.data.filter((s) => s.status === "trialing");
return { total: subs.data.length, trialing: trialing.map((s) => s.id) };Call
{
"name": "run_code",
"arguments": {
"code": "const subs = await client.subscriptions.list();\nconst trialing = subs.data.filter((s) => s.status === \"trialing\");\nreturn { total: subs.data.length, trialing: trialing.map((s) => s.id) };"
}
}Result
PRACTICE MODE: fake response built from the API's documentation. Nothing was sent to the API.
TypeScript script finished. 1 API call.
Result:
{
"total": 1,
"trialing": [
"sub_7Kd02m"
]
}
Calls:
GET listSubscriptions: 200相关内容
- 代码模式工具:
describe_sdk。 - 搜索和代码模式:何时提供代码模式,以及使用哪些语言。
- 练习模式:使用示例数据试运行脚本。