@ai-zen/agents-sdk 0.5.7 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +53 -0
- package/README.md +10 -7
- package/README.zh.md +10 -7
- package/dist/capabilities/discovery/builtin.d.ts +3 -3
- package/dist/capabilities/discovery/builtin.js +4 -12
- package/dist/capabilities/implements/builtin/BatchEditTool.d.ts +2 -0
- package/dist/capabilities/implements/builtin/BatchEditTool.js +36 -38
- package/dist/capabilities/implements/builtin/CopyTool.d.ts +2 -0
- package/dist/capabilities/implements/builtin/CopyTool.js +23 -25
- package/dist/capabilities/implements/builtin/CwdTool.d.ts +2 -0
- package/dist/capabilities/implements/builtin/CwdTool.js +10 -12
- package/dist/capabilities/implements/builtin/DownloadFileTool.d.ts +3 -1
- package/dist/capabilities/implements/builtin/DownloadFileTool.js +32 -24
- package/dist/capabilities/implements/builtin/EditTool.d.ts +2 -0
- package/dist/capabilities/implements/builtin/EditTool.js +28 -30
- package/dist/capabilities/implements/builtin/ExecAsyncTool.d.ts +2 -0
- package/dist/capabilities/implements/builtin/ExecAsyncTool.js +23 -25
- package/dist/capabilities/implements/builtin/ExecTool.d.ts +3 -1
- package/dist/capabilities/implements/builtin/ExecTool.js +62 -29
- package/dist/capabilities/implements/builtin/ExistTool.d.ts +2 -0
- package/dist/capabilities/implements/builtin/ExistTool.js +15 -17
- package/dist/capabilities/implements/builtin/FindTextTool.d.ts +3 -1
- package/dist/capabilities/implements/builtin/FindTextTool.js +43 -35
- package/dist/capabilities/implements/builtin/GenerateImageTool.d.ts +11 -9
- package/dist/capabilities/implements/builtin/GenerateImageTool.js +63 -56
- package/dist/capabilities/implements/builtin/GlobTool.d.ts +3 -1
- package/dist/capabilities/implements/builtin/GlobTool.js +32 -28
- package/dist/capabilities/implements/builtin/LsTool.d.ts +2 -0
- package/dist/capabilities/implements/builtin/LsTool.js +15 -17
- package/dist/capabilities/implements/builtin/MkdirTool.d.ts +2 -0
- package/dist/capabilities/implements/builtin/MkdirTool.js +19 -21
- package/dist/capabilities/implements/builtin/ReadFileTool.d.ts +2 -0
- package/dist/capabilities/implements/builtin/ReadFileTool.js +15 -17
- package/dist/capabilities/implements/builtin/RenameTool.d.ts +2 -0
- package/dist/capabilities/implements/builtin/RenameTool.js +19 -21
- package/dist/capabilities/implements/builtin/RmTool.d.ts +2 -0
- package/dist/capabilities/implements/builtin/RmTool.js +19 -21
- package/dist/capabilities/implements/builtin/SleepTool.d.ts +3 -1
- package/dist/capabilities/implements/builtin/SleepTool.js +39 -19
- package/dist/capabilities/implements/builtin/ViewImageTool.d.ts +24 -0
- package/dist/capabilities/implements/builtin/ViewImageTool.js +109 -0
- package/dist/capabilities/implements/builtin/WriteFileTool.d.ts +2 -0
- package/dist/capabilities/implements/builtin/WriteFileTool.js +19 -21
- package/dist/capabilities/implements/builtin/index.d.ts +6 -4
- package/dist/capabilities/implements/builtin/index.js +8 -4
- package/dist/capabilities/implements/mcpTools.js +4 -6
- package/dist/capabilities/implements/skillTools.js +18 -8
- package/dist/capabilities/implements/subAgentTools.js +17 -8
- package/dist/config/constants.js +12 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +2 -0
- package/dist/plugin/AutoMigratePlugin.d.ts +1 -1
- package/dist/plugin/AutoMigratePlugin.js +1 -1
- package/dist/plugin/AutoRefreshToolsPlugin.d.ts +1 -1
- package/dist/plugin/AutoRefreshToolsPlugin.js +2 -2
- package/dist/plugin/ContextGuardPlugin.d.ts +2 -2
- package/dist/plugin/ContextGuardPlugin.js +1 -1
- package/dist/plugin/UnknownToolHintPlugin.d.ts +21 -0
- package/dist/plugin/UnknownToolHintPlugin.js +30 -0
- package/dist/runtime/Provider.d.ts +8 -7
- package/dist/runtime/Provider.js +14 -4
- package/dist/runtime/SdkAgent.d.ts +14 -107
- package/dist/runtime/SdkAgent.js +8 -120
- package/dist/runtime/SdkCallbackTool.d.ts +25 -9
- package/dist/runtime/SdkCallbackTool.js +7 -3
- package/dist/runtime/createAgent.js +4 -3
- package/dist/runtime/createModel.d.ts +17 -4
- package/dist/runtime/createModel.js +15 -11
- package/dist/types/index.d.ts +2 -0
- package/package.json +3 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,58 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.7.0] - 2026-08-14
|
|
4
|
+
|
|
5
|
+
### ⚠️ Breaking
|
|
6
|
+
|
|
7
|
+
- **`@ai-zen/agents-core` upgraded to 4.0.0** — the SDK now runs on the **official OpenAI SDK**: `createModel(provider, modelId)` returns `{ client, model, modelConfig }` (an `openai` client + model name + params spread into the request) instead of a `ChatCompletionModel`; `SdkAgent` / `createAgent` construct with `{ client, model, modelConfig }`; `GenerateImageTool` calls `client.images.generate`
|
|
8
|
+
- **Plugin mechanism promoted to core** — `AgentPlugin` / `SendContext` / `HookResult` now live in `@ai-zen/agents-core` (re-exported by the SDK for compatibility). `SdkAgent` no longer implements its own `use` / `init` / hook iteration, and the `onUnknownTool` property hack is gone — it inherits everything from Core `Agent`
|
|
9
|
+
- **`SdkAgent.defaultUnknownTool` override removed** — the MCP-aware unknown-tool hint moved out of the class into a standalone plugin (see `UnknownToolHintPlugin` below); Core `Agent` now keeps only a simple built-in hint
|
|
10
|
+
- **`SdkAgent.permissions` field removed** — the Agent no longer carries its own `permissions` copy; read `agent.definition.permissions` instead (single source of truth). `AutoRefreshToolsPlugin` and `call_skill_sub_agent` were updated accordingly
|
|
11
|
+
- **`ContextGuardPlugin` default `ratio` changed from 1.2 to 1.5** — the guard now trips at 50% over `maxTokens` (user setting) instead of 20%
|
|
12
|
+
|
|
13
|
+
### 🚀 New Features
|
|
14
|
+
|
|
15
|
+
- **`UnknownToolHintPlugin`** — new standalone plugin providing the MCP-aware unknown-tool hint (replaces the removed `SdkAgent.defaultUnknownTool` override). Register explicitly: `agent.use(new UnknownToolHintPlugin({ provider }))`; without it, unknown tools fall back to Core's simple built-in hint
|
|
16
|
+
- **`Model.vision` field + self-declared tool availability** — models can be marked `vision: true`. `SdkCallbackTool` gains `isAvailable(config, definition)`: tools **self-declare** availability by directly inspecting the full app config and agent definition (e.g. `GenerateImageTool` requires `defaultImageModel`, `ViewImageTool` requires a vision model). All **19 built-in tools** live in `BUILTIN_TOOL_CLASSES` (discovery does no filtering); `Provider.filter`/`buildTools` take the `AgentDefinition` and call `isAvailable` at build time (when the model is already known) to filter the final tool set — no hard-coded tool names, and no runtime duplication (vision checks live only in `ViewImageTool.isAvailable`)
|
|
17
|
+
- **`ViewImageTool`** — let the Agent view/analyze an image (vision models only): a network URL returns an `image_url` content block; a local path is auto-uploaded via the Files API (`client.files.create`, `purpose: "user_data"`) and returned as a `file` content block (no base64). Always registered as a built-in candidate; filtered out by `buildTools` for non-vision models (a runtime check remains as a defensive guard)
|
|
18
|
+
- **`GenerateImageTool` returns a plain string** — it always returns JSON text with the image URLs plus `viewImage` / `downloadFile` hints; how the generated image is consumed (view / download / forward) is left to the model and the caller, instead of forcing image content blocks on vision models
|
|
19
|
+
- **`SdkCallbackTool.exec` passes content-section arrays through** — aligned with Core's `Tool.exec` returning `AgentNS.MessageContent`; other values are still JSON-serialized
|
|
20
|
+
|
|
21
|
+
### 🎯 Optimized
|
|
22
|
+
|
|
23
|
+
- `createAgent` / `subAgentTools` / `skillTools` adapted to the new Agent constructor (`{ client, model, modelConfig }`)
|
|
24
|
+
- The plugins (`AutoMigratePlugin` / `AutoRefreshToolsPlugin` / `ContextGuardPlugin` / `UnknownToolHintPlugin`) now type their hooks from core's `AgentPlugin` / `HookResult`
|
|
25
|
+
|
|
26
|
+
### ✅ Tests
|
|
27
|
+
|
|
28
|
+
- Adapted `createModel` / `SdkAgent` / `Provider` / `createAgent` / `AutoRefreshToolsPlugin` / `ContextGuardPlugin` / `skillTools` / e2e tests to the core 4.0.0 API — all green (incl. real DeepSeek API e2e)
|
|
29
|
+
|
|
30
|
+
## [0.6.0] - 2026-08-14
|
|
31
|
+
|
|
32
|
+
### ⚠️ Breaking
|
|
33
|
+
|
|
34
|
+
- **`SdkCallbackTool` constructor no longer takes `function` — construction is now `super({ env })` only** — the tool definition (`function`) is declared by each tool as a **class-body field**, colocated with its `call` implementation. The constructor now takes an options container (`SdkCallbackToolOptions`, currently containing `env` as its core field and open to future extension). Any custom tool previously writing `super({ function, env })` must move its `function` definition into a class-body field and call `super({ env })`. `SdkCallbackToolOptions` is exported for external consumers
|
|
35
|
+
- **`SdkCallbackTool.call` now forwards the full `ToolCallContext`** — `call(input)` becomes `call(input, ctx?)`. Concrete tools may read the second argument (e.g. `ctx?.signal`) for abort support. This is additive (optional argument), so existing single-argument `call`s keep compiling
|
|
36
|
+
- **`@ai-zen/agents-core` upgraded to 3.4.0** — the `Tool` base class no longer requires passing `function`/`type` via its constructor; `CallbackTool` / `AgentToolLazy.buildAgent` no longer use `this`-injection and now receive `(parsed_args, ctx)`. SDK callbacks that previously read `this.agent` now read `ctx.agent` instead (`call_skill_sub_agent` in skillTools, `buildAgent` in subAgentTools)
|
|
37
|
+
|
|
38
|
+
### 🛠 Optimized
|
|
39
|
+
|
|
40
|
+
- **All 18 built-in tools now declare their `function` definition as a class-body field** — the definition lives next to the `call` implementation (single source of truth), instead of being passed separately through the constructor
|
|
41
|
+
- Aligned SDK tool context access with core's explicit `(parsed_args, ctx)` convention
|
|
42
|
+
- **Per-tool abort signal support** — high-value tools now honor interruption via `ctx?.signal`:
|
|
43
|
+
- **`sleep`** — listening for abort on the wait timer, returning early so a long wait is interrupted instead of blocking
|
|
44
|
+
- **`exec`** — killing the child process on abort (marking `terminated: "aborted"` distinct from `"timeout"`), with a fallback to avoid hanging if the signal kills the shell
|
|
45
|
+
- **`downloadFile`** — passing the signal to `fetch`, and cleaning up any partially-written file on abort
|
|
46
|
+
- **`generateImage`** — passing the signal to the image-model request (the underlying `fetch` already supports it), returning an aborted result on interruption
|
|
47
|
+
- **`glob` / `findText`** — checking `ctx.signal` during directory-tree traversal (and after each file read), stopping the scan early and returning the partial results with an `aborted` flag when interrupted
|
|
48
|
+
- **MCP tool signal forwarding** — `call_mcp_tool` and `read_mcp_resource` now forward `ctx.signal` into the underlying MCP SDK `callTool` / `readResource` request options, so a remote call can be interrupted
|
|
49
|
+
- **Sub-agent abort linkage** — sub-agent tools (`AgentTool`, `AgentToolLazy`; used by `createSubAgentTool` and `call_skill_sub_agent`) now listen for the outer `ctx.signal` and call `agent.abort()` on the child agent, so a long-running sub-agent is stopped when the parent call is aborted
|
|
50
|
+
|
|
51
|
+
### ✅ Tests
|
|
52
|
+
|
|
53
|
+
- Updated `SdkCallbackTool` / `capabilities` tests for the new `function` assignment and `super({ env })` construction; added a test asserting `SdkCallbackTool.call` receives the forwarded `ToolCallContext`
|
|
54
|
+
- **Added abort tests** for `sleep` (early return + immediate abort), `exec` (child killed + `terminated: "aborted"`), `downloadFile` (abort cleanup of the partial file), `generateImage` (aborted result), `glob`/`findText` (early scan stop), plus MCP `callTool`/`readResource` signal forwarding and sub-agent abort linkage. SDK suite now at **435 passed**, **3 skipped**; core at **198 passed**, **17 skipped**
|
|
55
|
+
|
|
3
56
|
## [0.5.7] - 2026-08-13
|
|
4
57
|
|
|
5
58
|
### 🛠 Optimized
|
package/README.md
CHANGED
|
@@ -23,7 +23,7 @@ config ← read/write config.json + migration + in-memory cache + atomic w
|
|
|
23
23
|
crud ← capability-entity CRUD (Agent definitions, etc.; conversations/drafts are persisted by each consumer)
|
|
24
24
|
capabilities ← capability discovery & assembly (built-in + user + MCP + Skill + SubAgent)
|
|
25
25
|
runtime ← Provider + model factory + Agent assembly + MCP connection management + task migration + SdkCallbackTool base
|
|
26
|
-
plugin ← Agent plugins (autoMigrate, autoRefreshTools)
|
|
26
|
+
plugin ← Agent plugins (autoMigrate, autoRefreshTools, contextGuard, unknownToolHint)
|
|
27
27
|
shared ← logging, errors
|
|
28
28
|
```
|
|
29
29
|
|
|
@@ -78,9 +78,9 @@ await agent.send("Hello");
|
|
|
78
78
|
| `crud` | ✅ Implemented — capability-entity CRUD for Agents, etc. (conversations/drafts persisted by each consumer) |
|
|
79
79
|
| `capabilities` | ✅ Implemented — discovery + permission filtering + safe pre-filtering + enumeration disclosure |
|
|
80
80
|
| `runtime` | ✅ Implemented — Provider, Capabilities, createAgent, MCP connection management, task migration |
|
|
81
|
-
| `plugin` | ✅ Implemented — AutoMigratePlugin / AutoRefreshToolsPlugin |
|
|
81
|
+
| `plugin` | ✅ Implemented — AutoMigratePlugin / AutoRefreshToolsPlugin / ContextGuardPlugin / UnknownToolHintPlugin |
|
|
82
82
|
| `shared` | ✅ Implemented — SdkError + injectable Logger |
|
|
83
|
-
| Tests | ✅
|
|
83
|
+
| Tests | ✅ 445 passing, 51 files, all green (incl. real-API chat & viewImage e2e) |
|
|
84
84
|
|
|
85
85
|
## Built-in Tools
|
|
86
86
|
|
|
@@ -106,11 +106,12 @@ All built-in tools are classes (extending `SdkCallbackTool`), instantiated by th
|
|
|
106
106
|
| `edit` | Edit text in a file |
|
|
107
107
|
| `sleep` | Wait for a specified number of milliseconds |
|
|
108
108
|
|
|
109
|
-
Conditionally injected
|
|
109
|
+
Conditionally injected based on the active model / config:
|
|
110
110
|
|
|
111
|
-
| Tool | Description |
|
|
112
|
-
|
|
113
|
-
| `generateImage` | Generate an image from a text description |
|
|
111
|
+
| Tool | Injection Condition | Description |
|
|
112
|
+
|------|---------------------|-------------|
|
|
113
|
+
| `generateImage` | Only when `defaultImageModel` is configured | Generate an image from a text description |
|
|
114
|
+
| `viewImage` | Only for vision models (the agent's `modelId` resolves to a model with `vision: true`) | View / analyze an image: local images are auto-uploaded via the Files API, network URLs are referenced directly |
|
|
114
115
|
|
|
115
116
|
## Built-in Plugins
|
|
116
117
|
|
|
@@ -118,6 +119,8 @@ Conditionally injected (registered only when `defaultImageModel` is configured):
|
|
|
118
119
|
|--------|-------------|
|
|
119
120
|
| `AutoMigratePlugin` | Automatically triggers task migration when the context overflows, generates a handoff document, and transparently replaces the Agent |
|
|
120
121
|
| `AutoRefreshToolsPlugin` | Re-scans the file system before each `send()` to refresh the tool list |
|
|
122
|
+
| `ContextGuardPlugin` | Context safety guard — before each request, throws `ContextOverflowError` (interrupting the conversation) when the previous round's `usage.prompt_tokens` exceeds `maxTokens × ratio` (default 1.5), preventing context runaway from reading oversized files |
|
|
123
|
+
| `UnknownToolHintPlugin` | Smarter unknown-tool hints — when the LLM calls a nonexistent tool, guides it to `call_mcp_tool` / points out permission issues based on MCP config (opt-in via `agent.use`) |
|
|
121
124
|
|
|
122
125
|
## Design Principles
|
|
123
126
|
|
package/README.zh.md
CHANGED
|
@@ -22,7 +22,7 @@ config ← 读写 config.json + 迁移 + 内存缓存 + 原子写入
|
|
|
22
22
|
crud ← 能力实体 CRUD(Agent 定义等;会话/草稿已下放给各端自行持久化)
|
|
23
23
|
capabilities ← 能力发现与装配(内置 + 用户 + MCP + Skill + SubAgent)
|
|
24
24
|
runtime ← Provider + 模型工厂 + Agent 组装 + MCP 连接管理 + 任务迁移 + SdkCallbackTool 工具基类
|
|
25
|
-
plugin ← Agent 插件(autoMigrate、autoRefreshTools)
|
|
25
|
+
plugin ← Agent 插件(autoMigrate、autoRefreshTools、contextGuard、unknownToolHint)
|
|
26
26
|
shared ← 日志、错误
|
|
27
27
|
```
|
|
28
28
|
|
|
@@ -77,9 +77,9 @@ await agent.send("你好");
|
|
|
77
77
|
| `crud` | ✅ 已实现 — Agent 等能力实体 CRUD(会话/草稿由各端自行持久化) |
|
|
78
78
|
| `capabilities` | ✅ 已实现 — 发现 + 权限过滤 + 安全预过滤 + 枚举披露 |
|
|
79
79
|
| `runtime` | ✅ 已实现 — Provider、Capabilities、createAgent、MCP 连接管理、任务迁移 |
|
|
80
|
-
| `plugin` | ✅ 已实现 — AutoMigratePlugin / AutoRefreshToolsPlugin |
|
|
80
|
+
| `plugin` | ✅ 已实现 — AutoMigratePlugin / AutoRefreshToolsPlugin / ContextGuardPlugin / UnknownToolHintPlugin |
|
|
81
81
|
| `shared` | ✅ 已实现 — SdkError + 可注入 Logger |
|
|
82
|
-
| 测试 | ✅
|
|
82
|
+
| 测试 | ✅ 445 通过,51 个文件,全绿(含真实 API 聊天与 viewImage e2e) |
|
|
83
83
|
|
|
84
84
|
## 内置工具
|
|
85
85
|
|
|
@@ -105,11 +105,12 @@ await agent.send("你好");
|
|
|
105
105
|
| `edit` | 编辑文件中的文本 |
|
|
106
106
|
| `sleep` | 等待指定毫秒数后继续 |
|
|
107
107
|
|
|
108
|
-
|
|
108
|
+
条件注入(按当前模型 / 配置决定是否注册):
|
|
109
109
|
|
|
110
|
-
| 工具 | 说明 |
|
|
111
|
-
|
|
112
|
-
| `generateImage` | 根据文字描述生成图片 |
|
|
110
|
+
| 工具 | 注入条件 | 说明 |
|
|
111
|
+
|------|----------|------|
|
|
112
|
+
| `generateImage` | 配置了 `defaultImageModel` 才注册 | 根据文字描述生成图片 |
|
|
113
|
+
| `viewImage` | 仅视觉模型可用(Agent 的 `modelId` 解析为 `vision: true` 的模型) | 查看/分析图片:本地图片自动经 Files API 上传,网络 URL 直接引用 |
|
|
113
114
|
|
|
114
115
|
## 内置插件
|
|
115
116
|
|
|
@@ -117,6 +118,8 @@ await agent.send("你好");
|
|
|
117
118
|
|------|------|
|
|
118
119
|
| `AutoMigratePlugin` | 上下文超限时自动触发任务迁移,生成交接文档,透明替换 Agent |
|
|
119
120
|
| `AutoRefreshToolsPlugin` | 每次 `send()` 前重新扫描文件系统,刷新工具列表 |
|
|
121
|
+
| `ContextGuardPlugin` | 上下文安全护栏 — 每轮发请求前检测上一轮 `usage.prompt_tokens`,超过 `maxTokens × ratio`(默认 1.5,即 +50%)时抛 `ContextOverflowError` 中断对话,防止读入超大文件导致上下文失控 |
|
|
122
|
+
| `UnknownToolHintPlugin` | 未知工具智能提示 — LLM 调用不存在的工具时,根据 MCP 配置引导使用 `call_mcp_tool` / 提示权限问题(调用方显式 `agent.use` 注册) |
|
|
120
123
|
|
|
121
124
|
## 设计原则
|
|
122
125
|
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { Tool } from "@ai-zen/agents-core";
|
|
2
2
|
import type { ToolEnv } from "../../types/index.js";
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* 发现内置工具:按静态注册表实例化全部内置工具(无任何过滤)。
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
6
|
+
* 工具的可用性由各工具自行声明(SdkCallbackTool.isAvailable,入参含完整 config
|
|
7
|
+
* + 模型信息),在 buildTools/filter 阶段(模型已知)按声明过滤。
|
|
8
8
|
*/
|
|
9
9
|
export declare function discoverBuiltinTools(env: ToolEnv): Tool[];
|
|
@@ -1,18 +1,10 @@
|
|
|
1
1
|
import { BUILTIN_TOOL_CLASSES } from "../implements/builtin/index.js";
|
|
2
|
-
import { GenerateImageTool } from "../implements/builtin/GenerateImageTool.js";
|
|
3
2
|
/**
|
|
4
|
-
*
|
|
3
|
+
* 发现内置工具:按静态注册表实例化全部内置工具(无任何过滤)。
|
|
5
4
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* 工具的可用性由各工具自行声明(SdkCallbackTool.isAvailable,入参含完整 config
|
|
6
|
+
* + 模型信息),在 buildTools/filter 阶段(模型已知)按声明过滤。
|
|
8
7
|
*/
|
|
9
8
|
export function discoverBuiltinTools(env) {
|
|
10
|
-
|
|
11
|
-
if (env.config.defaultImageModel) {
|
|
12
|
-
const existingNames = new Set(tools.map((t) => t.function.name));
|
|
13
|
-
if (!existingNames.has("generateImage")) {
|
|
14
|
-
tools.push(new GenerateImageTool(env));
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
return tools;
|
|
9
|
+
return BUILTIN_TOOL_CLASSES.map((Cls) => new Cls(env));
|
|
18
10
|
}
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
|
+
import type { AgentNS } from "@ai-zen/agents-core";
|
|
3
4
|
interface Replacement {
|
|
4
5
|
oldText: string;
|
|
5
6
|
newText: string;
|
|
6
7
|
isReplaceAll?: boolean;
|
|
7
8
|
}
|
|
8
9
|
export declare class BatchEditTool extends SdkCallbackTool {
|
|
10
|
+
function: AgentNS.FunctionDefine;
|
|
9
11
|
constructor(env: ToolEnv);
|
|
10
12
|
call(input: {
|
|
11
13
|
path: string;
|
|
@@ -1,49 +1,47 @@
|
|
|
1
1
|
import * as fsp from "fs/promises";
|
|
2
2
|
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
3
3
|
export class BatchEditTool extends SdkCallbackTool {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
description: "是否替换所有匹配的文本(默认仅替换首次匹配)。使用此功能前应确保你提供的 oldText 足够精确,避免误替换",
|
|
33
|
-
default: false,
|
|
34
|
-
},
|
|
35
|
-
},
|
|
36
|
-
required: ["oldText", "newText"],
|
|
37
|
-
additionalProperties: false,
|
|
4
|
+
function = {
|
|
5
|
+
name: "batchEdit",
|
|
6
|
+
description: "批量编辑文件文本,可以优先使用这个工具对文件进行编辑",
|
|
7
|
+
parameters: {
|
|
8
|
+
type: "object",
|
|
9
|
+
properties: {
|
|
10
|
+
path: {
|
|
11
|
+
type: "string",
|
|
12
|
+
description: "文件路径",
|
|
13
|
+
},
|
|
14
|
+
replacements: {
|
|
15
|
+
type: "array",
|
|
16
|
+
description: "要替换的文本数组",
|
|
17
|
+
items: {
|
|
18
|
+
type: "object",
|
|
19
|
+
properties: {
|
|
20
|
+
oldText: {
|
|
21
|
+
type: "string",
|
|
22
|
+
description: "要替换的文本",
|
|
23
|
+
},
|
|
24
|
+
newText: {
|
|
25
|
+
type: "string",
|
|
26
|
+
description: "替换后的文本",
|
|
27
|
+
},
|
|
28
|
+
isReplaceAll: {
|
|
29
|
+
type: "boolean",
|
|
30
|
+
description: "是否替换所有匹配的文本(默认仅替换首次匹配)。使用此功能前应确保你提供的 oldText 足够精确,避免误替换",
|
|
31
|
+
default: false,
|
|
38
32
|
},
|
|
39
33
|
},
|
|
34
|
+
required: ["oldText", "newText"],
|
|
35
|
+
additionalProperties: false,
|
|
40
36
|
},
|
|
41
|
-
required: ["path", "replacements"],
|
|
42
|
-
additionalProperties: false,
|
|
43
37
|
},
|
|
44
38
|
},
|
|
45
|
-
|
|
46
|
-
|
|
39
|
+
required: ["path", "replacements"],
|
|
40
|
+
additionalProperties: false,
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
constructor(env) {
|
|
44
|
+
super({ env });
|
|
47
45
|
}
|
|
48
46
|
async call(input) {
|
|
49
47
|
try {
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
|
+
import type { AgentNS } from "@ai-zen/agents-core";
|
|
3
4
|
export declare class CopyTool extends SdkCallbackTool {
|
|
5
|
+
function: AgentNS.FunctionDefine;
|
|
4
6
|
constructor(env: ToolEnv);
|
|
5
7
|
call(input: {
|
|
6
8
|
src: string;
|
|
@@ -1,33 +1,31 @@
|
|
|
1
1
|
import * as fsp from "fs/promises";
|
|
2
2
|
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
3
3
|
export class CopyTool extends SdkCallbackTool {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
type: "boolean",
|
|
22
|
-
description: "是否递归复制目录,复制目录时需设为 true",
|
|
23
|
-
},
|
|
24
|
-
},
|
|
25
|
-
required: ["src", "dest"],
|
|
26
|
-
additionalProperties: false,
|
|
4
|
+
function = {
|
|
5
|
+
name: "copy",
|
|
6
|
+
description: "复制文件或目录(相当于 cp 命令)",
|
|
7
|
+
parameters: {
|
|
8
|
+
type: "object",
|
|
9
|
+
properties: {
|
|
10
|
+
src: {
|
|
11
|
+
type: "string",
|
|
12
|
+
description: "源路径",
|
|
13
|
+
},
|
|
14
|
+
dest: {
|
|
15
|
+
type: "string",
|
|
16
|
+
description: "目标路径",
|
|
17
|
+
},
|
|
18
|
+
recursive: {
|
|
19
|
+
type: "boolean",
|
|
20
|
+
description: "是否递归复制目录,复制目录时需设为 true",
|
|
27
21
|
},
|
|
28
22
|
},
|
|
29
|
-
|
|
30
|
-
|
|
23
|
+
required: ["src", "dest"],
|
|
24
|
+
additionalProperties: false,
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
constructor(env) {
|
|
28
|
+
super({ env });
|
|
31
29
|
}
|
|
32
30
|
async call(input) {
|
|
33
31
|
try {
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
|
+
import type { AgentNS } from "@ai-zen/agents-core";
|
|
3
4
|
export declare class CwdTool extends SdkCallbackTool {
|
|
5
|
+
function: AgentNS.FunctionDefine;
|
|
4
6
|
constructor(env: ToolEnv);
|
|
5
7
|
call(): string;
|
|
6
8
|
}
|
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
2
2
|
export class CwdTool extends SdkCallbackTool {
|
|
3
|
+
function = {
|
|
4
|
+
name: "cwd",
|
|
5
|
+
description: "获取当前工作目录 cwd",
|
|
6
|
+
parameters: {
|
|
7
|
+
type: "object",
|
|
8
|
+
properties: {},
|
|
9
|
+
required: [],
|
|
10
|
+
},
|
|
11
|
+
};
|
|
3
12
|
constructor(env) {
|
|
4
|
-
super({
|
|
5
|
-
function: {
|
|
6
|
-
name: "cwd",
|
|
7
|
-
description: "获取当前工作目录 cwd",
|
|
8
|
-
parameters: {
|
|
9
|
-
type: "object",
|
|
10
|
-
properties: {},
|
|
11
|
-
required: [],
|
|
12
|
-
},
|
|
13
|
-
},
|
|
14
|
-
env,
|
|
15
|
-
});
|
|
13
|
+
super({ env });
|
|
16
14
|
}
|
|
17
15
|
call() {
|
|
18
16
|
return this.env.cwd;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
|
+
import type { AgentNS, ToolCallContext } from "@ai-zen/agents-core";
|
|
3
4
|
export declare class DownloadFileTool extends SdkCallbackTool {
|
|
5
|
+
function: AgentNS.FunctionDefine;
|
|
4
6
|
constructor(env: ToolEnv);
|
|
5
7
|
call(input: {
|
|
6
8
|
url: string;
|
|
7
9
|
outputPath?: string;
|
|
8
|
-
}): Promise<string>;
|
|
10
|
+
}, ctx?: ToolCallContext): Promise<string>;
|
|
9
11
|
}
|
|
@@ -2,36 +2,35 @@ import * as fsp from "fs/promises";
|
|
|
2
2
|
import * as path from "path";
|
|
3
3
|
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
4
4
|
export class DownloadFileTool extends SdkCallbackTool {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
type: "string",
|
|
19
|
-
description: "保存路径(可选)。可以是目录或完整文件路径。不指定则下载到当前工作目录,自动从 URL 推断文件名。",
|
|
20
|
-
},
|
|
21
|
-
},
|
|
22
|
-
required: ["url"],
|
|
23
|
-
additionalProperties: false,
|
|
5
|
+
function = {
|
|
6
|
+
name: "downloadFile",
|
|
7
|
+
description: "从 URL 下载文件(图片、PDF 等二进制文件)并保存到本地磁盘。自动从 URL 推断文件名,也可指定保存路径。",
|
|
8
|
+
parameters: {
|
|
9
|
+
type: "object",
|
|
10
|
+
properties: {
|
|
11
|
+
url: {
|
|
12
|
+
type: "string",
|
|
13
|
+
description: "文件下载链接",
|
|
14
|
+
},
|
|
15
|
+
outputPath: {
|
|
16
|
+
type: "string",
|
|
17
|
+
description: "保存路径(可选)。可以是目录或完整文件路径。不指定则下载到当前工作目录,自动从 URL 推断文件名。",
|
|
24
18
|
},
|
|
25
19
|
},
|
|
26
|
-
|
|
27
|
-
|
|
20
|
+
required: ["url"],
|
|
21
|
+
additionalProperties: false,
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
constructor(env) {
|
|
25
|
+
super({ env });
|
|
28
26
|
}
|
|
29
|
-
async call(input) {
|
|
27
|
+
async call(input, ctx) {
|
|
28
|
+
const signal = ctx?.signal;
|
|
29
|
+
let savePath;
|
|
30
30
|
try {
|
|
31
31
|
const url = input.url;
|
|
32
32
|
if (!url)
|
|
33
33
|
throw new Error("url 不能为空");
|
|
34
|
-
let savePath;
|
|
35
34
|
if (input.outputPath) {
|
|
36
35
|
savePath = this.resolve(input.outputPath);
|
|
37
36
|
const stat = await fsp.stat(savePath).catch(() => null);
|
|
@@ -47,7 +46,7 @@ export class DownloadFileTool extends SdkCallbackTool {
|
|
|
47
46
|
savePath = path.join(this.env.cwd, fileName);
|
|
48
47
|
}
|
|
49
48
|
await fsp.mkdir(path.dirname(savePath), { recursive: true });
|
|
50
|
-
const response = await fetch(url);
|
|
49
|
+
const response = await fetch(url, { signal });
|
|
51
50
|
if (!response.ok) {
|
|
52
51
|
throw new Error(`下载失败: HTTP ${response.status} ${response.statusText}`);
|
|
53
52
|
}
|
|
@@ -66,6 +65,15 @@ export class DownloadFileTool extends SdkCallbackTool {
|
|
|
66
65
|
});
|
|
67
66
|
}
|
|
68
67
|
catch (error) {
|
|
68
|
+
// 中断(abort)时清理可能残留的半成品文件
|
|
69
|
+
if (signal?.aborted && savePath) {
|
|
70
|
+
await fsp.rm(savePath, { force: true }).catch(() => { });
|
|
71
|
+
return JSON.stringify({
|
|
72
|
+
success: false,
|
|
73
|
+
aborted: true,
|
|
74
|
+
error: "下载被中断(aborted)",
|
|
75
|
+
});
|
|
76
|
+
}
|
|
69
77
|
return JSON.stringify({
|
|
70
78
|
success: false,
|
|
71
79
|
error: error?.message || "下载失败",
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
|
+
import type { AgentNS } from "@ai-zen/agents-core";
|
|
3
4
|
export declare class EditTool extends SdkCallbackTool {
|
|
5
|
+
function: AgentNS.FunctionDefine;
|
|
4
6
|
constructor(env: ToolEnv);
|
|
5
7
|
call(input: {
|
|
6
8
|
path: string;
|
|
@@ -13,38 +13,36 @@ function countOccurrences(content, text) {
|
|
|
13
13
|
return count;
|
|
14
14
|
}
|
|
15
15
|
export class EditTool extends SdkCallbackTool {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
description: "是否替换所有匹配的文本(默认 false,仅替换首次匹配)。使用此功能前应确保你提供的 oldText 足够精确,避免误替换",
|
|
39
|
-
default: false,
|
|
40
|
-
},
|
|
41
|
-
},
|
|
42
|
-
required: ["path", "oldText", "newText"],
|
|
43
|
-
additionalProperties: false,
|
|
16
|
+
function = {
|
|
17
|
+
name: "edit",
|
|
18
|
+
description: "编辑文件中的文本,支持精确替换或替换所有匹配项",
|
|
19
|
+
parameters: {
|
|
20
|
+
type: "object",
|
|
21
|
+
properties: {
|
|
22
|
+
path: {
|
|
23
|
+
type: "string",
|
|
24
|
+
description: "文件路径",
|
|
25
|
+
},
|
|
26
|
+
oldText: {
|
|
27
|
+
type: "string",
|
|
28
|
+
description: "要替换的文本",
|
|
29
|
+
},
|
|
30
|
+
newText: {
|
|
31
|
+
type: "string",
|
|
32
|
+
description: "替换后的文本",
|
|
33
|
+
},
|
|
34
|
+
isReplaceAll: {
|
|
35
|
+
type: "boolean",
|
|
36
|
+
description: "是否替换所有匹配的文本(默认 false,仅替换首次匹配)。使用此功能前应确保你提供的 oldText 足够精确,避免误替换",
|
|
37
|
+
default: false,
|
|
44
38
|
},
|
|
45
39
|
},
|
|
46
|
-
|
|
47
|
-
|
|
40
|
+
required: ["path", "oldText", "newText"],
|
|
41
|
+
additionalProperties: false,
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
constructor(env) {
|
|
45
|
+
super({ env });
|
|
48
46
|
}
|
|
49
47
|
async call(input) {
|
|
50
48
|
try {
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
|
+
import type { AgentNS } from "@ai-zen/agents-core";
|
|
3
4
|
export declare class ExecAsyncTool extends SdkCallbackTool {
|
|
5
|
+
function: AgentNS.FunctionDefine;
|
|
4
6
|
constructor(env: ToolEnv);
|
|
5
7
|
call(input: {
|
|
6
8
|
command: string;
|