@ai-zen/agents-sdk 0.5.7 → 0.6.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.
Files changed (44) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/dist/capabilities/implements/builtin/BatchEditTool.d.ts +2 -0
  3. package/dist/capabilities/implements/builtin/BatchEditTool.js +36 -38
  4. package/dist/capabilities/implements/builtin/CopyTool.d.ts +2 -0
  5. package/dist/capabilities/implements/builtin/CopyTool.js +23 -25
  6. package/dist/capabilities/implements/builtin/CwdTool.d.ts +2 -0
  7. package/dist/capabilities/implements/builtin/CwdTool.js +10 -12
  8. package/dist/capabilities/implements/builtin/DownloadFileTool.d.ts +3 -1
  9. package/dist/capabilities/implements/builtin/DownloadFileTool.js +32 -24
  10. package/dist/capabilities/implements/builtin/EditTool.d.ts +2 -0
  11. package/dist/capabilities/implements/builtin/EditTool.js +28 -30
  12. package/dist/capabilities/implements/builtin/ExecAsyncTool.d.ts +2 -0
  13. package/dist/capabilities/implements/builtin/ExecAsyncTool.js +23 -25
  14. package/dist/capabilities/implements/builtin/ExecTool.d.ts +3 -1
  15. package/dist/capabilities/implements/builtin/ExecTool.js +62 -29
  16. package/dist/capabilities/implements/builtin/ExistTool.d.ts +2 -0
  17. package/dist/capabilities/implements/builtin/ExistTool.js +15 -17
  18. package/dist/capabilities/implements/builtin/FindTextTool.d.ts +3 -1
  19. package/dist/capabilities/implements/builtin/FindTextTool.js +43 -35
  20. package/dist/capabilities/implements/builtin/GenerateImageTool.d.ts +3 -1
  21. package/dist/capabilities/implements/builtin/GenerateImageTool.js +39 -31
  22. package/dist/capabilities/implements/builtin/GlobTool.d.ts +3 -1
  23. package/dist/capabilities/implements/builtin/GlobTool.js +32 -28
  24. package/dist/capabilities/implements/builtin/LsTool.d.ts +2 -0
  25. package/dist/capabilities/implements/builtin/LsTool.js +15 -17
  26. package/dist/capabilities/implements/builtin/MkdirTool.d.ts +2 -0
  27. package/dist/capabilities/implements/builtin/MkdirTool.js +19 -21
  28. package/dist/capabilities/implements/builtin/ReadFileTool.d.ts +2 -0
  29. package/dist/capabilities/implements/builtin/ReadFileTool.js +15 -17
  30. package/dist/capabilities/implements/builtin/RenameTool.d.ts +2 -0
  31. package/dist/capabilities/implements/builtin/RenameTool.js +19 -21
  32. package/dist/capabilities/implements/builtin/RmTool.d.ts +2 -0
  33. package/dist/capabilities/implements/builtin/RmTool.js +19 -21
  34. package/dist/capabilities/implements/builtin/SleepTool.d.ts +3 -1
  35. package/dist/capabilities/implements/builtin/SleepTool.js +39 -19
  36. package/dist/capabilities/implements/builtin/WriteFileTool.d.ts +2 -0
  37. package/dist/capabilities/implements/builtin/WriteFileTool.js +19 -21
  38. package/dist/capabilities/implements/mcpTools.js +4 -6
  39. package/dist/capabilities/implements/skillTools.js +13 -3
  40. package/dist/capabilities/implements/subAgentTools.js +2 -2
  41. package/dist/index.d.ts +1 -0
  42. package/dist/runtime/SdkCallbackTool.d.ts +14 -6
  43. package/dist/runtime/SdkCallbackTool.js +3 -2
  44. package/package.json +2 -2
@@ -1,29 +1,27 @@
1
1
  import * as fsp from "fs/promises";
2
2
  import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
3
3
  export class RmTool extends SdkCallbackTool {
4
- constructor(env) {
5
- super({
6
- function: {
7
- name: "rm",
8
- description: "删除文件或目录",
9
- parameters: {
10
- type: "object",
11
- properties: {
12
- path: {
13
- type: "string",
14
- description: "文件或目录路径",
15
- },
16
- recursive: {
17
- type: "boolean",
18
- description: "是否递归删除子目录",
19
- },
20
- },
21
- required: ["path"],
22
- additionalProperties: false,
4
+ function = {
5
+ name: "rm",
6
+ description: "删除文件或目录",
7
+ parameters: {
8
+ type: "object",
9
+ properties: {
10
+ path: {
11
+ type: "string",
12
+ description: "文件或目录路径",
13
+ },
14
+ recursive: {
15
+ type: "boolean",
16
+ description: "是否递归删除子目录",
23
17
  },
24
18
  },
25
- env,
26
- });
19
+ required: ["path"],
20
+ additionalProperties: false,
21
+ },
22
+ };
23
+ constructor(env) {
24
+ super({ env });
27
25
  }
28
26
  async call(input) {
29
27
  try {
@@ -1,8 +1,10 @@
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 SleepTool extends SdkCallbackTool {
5
+ function: AgentNS.FunctionDefine;
4
6
  constructor(env: ToolEnv);
5
7
  call(input: {
6
8
  ms: number;
7
- }): Promise<string>;
9
+ }, ctx?: ToolCallContext): Promise<string>;
8
10
  }
@@ -1,26 +1,24 @@
1
1
  import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
2
2
  export class SleepTool extends SdkCallbackTool {
3
- constructor(env) {
4
- super({
5
- function: {
6
- name: "sleep",
7
- description: "等待指定的时间(毫秒)。用于需要延迟执行的场景,例如等待其他操作完成、操作间隔控制等。注意:过长的等待时间会影响响应速度,建议不超过 30000 毫秒(30 秒)。",
8
- parameters: {
9
- type: "object",
10
- properties: {
11
- ms: {
12
- type: "number",
13
- description: "等待的毫秒数(1 秒 = 1000 毫秒)。建议不超过 30000(30 秒)。",
14
- },
15
- },
16
- required: ["ms"],
17
- additionalProperties: false,
3
+ function = {
4
+ name: "sleep",
5
+ description: "等待指定的时间(毫秒)。用于需要延迟执行的场景,例如等待其他操作完成、操作间隔控制等。注意:过长的等待时间会影响响应速度,建议不超过 30000 毫秒(30 秒)。",
6
+ parameters: {
7
+ type: "object",
8
+ properties: {
9
+ ms: {
10
+ type: "number",
11
+ description: "等待的毫秒数(1 秒 = 1000 毫秒)。建议不超过 30000(30 秒)。",
18
12
  },
19
13
  },
20
- env,
21
- });
14
+ required: ["ms"],
15
+ additionalProperties: false,
16
+ },
17
+ };
18
+ constructor(env) {
19
+ super({ env });
22
20
  }
23
- async call(input) {
21
+ async call(input, ctx) {
24
22
  const ms = input.ms;
25
23
  if (typeof ms !== "number" || !Number.isFinite(ms) || ms < 0) {
26
24
  return JSON.stringify({
@@ -35,7 +33,29 @@ export class SleepTool extends SdkCallbackTool {
35
33
  error: `等待时间 ${ms}ms 过长,建议不超过 300000ms(5 分钟)。如需长时间等待请分段执行。`,
36
34
  });
37
35
  }
38
- await new Promise((resolve) => setTimeout(resolve, ms));
36
+ // 若 signal 已处于 aborted,直接返回中断结果,避免不必要的等待
37
+ const signal = ctx?.signal;
38
+ // 等待 ms 毫秒,中断(abort)时提前结束
39
+ await new Promise((resolve) => {
40
+ if (signal?.aborted) {
41
+ resolve();
42
+ return;
43
+ }
44
+ const timer = setTimeout(resolve, ms);
45
+ if (signal) {
46
+ signal.addEventListener("abort", () => {
47
+ clearTimeout(timer);
48
+ resolve();
49
+ }, { once: true });
50
+ }
51
+ });
52
+ if (signal?.aborted) {
53
+ return JSON.stringify({
54
+ success: false,
55
+ aborted: true,
56
+ error: "等待被中断(aborted)",
57
+ });
58
+ }
39
59
  return JSON.stringify({
40
60
  success: true,
41
61
  waitedMs: ms,
@@ -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 WriteFileTool extends SdkCallbackTool {
5
+ function: AgentNS.FunctionDefine;
4
6
  constructor(env: ToolEnv);
5
7
  call(input: {
6
8
  path: string;
@@ -2,29 +2,27 @@ 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 WriteFileTool extends SdkCallbackTool {
5
- constructor(env) {
6
- super({
7
- function: {
8
- name: "writeFile",
9
- description: "写入文件",
10
- parameters: {
11
- type: "object",
12
- properties: {
13
- path: {
14
- type: "string",
15
- description: "文件路径",
16
- },
17
- content: {
18
- type: "string",
19
- description: "文件内容",
20
- },
21
- },
22
- required: ["path", "content"],
23
- additionalProperties: false,
5
+ function = {
6
+ name: "writeFile",
7
+ description: "写入文件",
8
+ parameters: {
9
+ type: "object",
10
+ properties: {
11
+ path: {
12
+ type: "string",
13
+ description: "文件路径",
14
+ },
15
+ content: {
16
+ type: "string",
17
+ description: "文件内容",
24
18
  },
25
19
  },
26
- env,
27
- });
20
+ required: ["path", "content"],
21
+ additionalProperties: false,
22
+ },
23
+ };
24
+ constructor(env) {
25
+ super({ env });
28
26
  }
29
27
  async call(input) {
30
28
  try {
@@ -81,7 +81,7 @@ export function createCallMcpTool(mcpManager) {
81
81
  additionalProperties: false,
82
82
  },
83
83
  },
84
- callback: async (input) => {
84
+ callback: async (input, ctx) => {
85
85
  const serverName = input.server;
86
86
  const state = mcpManager.getState(serverName);
87
87
  if (state !== "connected") {
@@ -96,7 +96,7 @@ export function createCallMcpTool(mcpManager) {
96
96
  const result = await client.callTool({
97
97
  name: input.tool,
98
98
  arguments: input.arguments,
99
- });
99
+ }, undefined, { signal: ctx?.signal });
100
100
  const contents = result.content ?? [];
101
101
  const textParts = contents
102
102
  .filter((c) => c.type === "text")
@@ -131,7 +131,7 @@ export function createReadMcpResourceTool(mcpManager) {
131
131
  additionalProperties: false,
132
132
  },
133
133
  },
134
- callback: async (input) => {
134
+ callback: async (input, ctx) => {
135
135
  const serverName = input.server;
136
136
  const state = mcpManager.getState(serverName);
137
137
  if (state !== "connected") {
@@ -143,9 +143,7 @@ export function createReadMcpResourceTool(mcpManager) {
143
143
  }
144
144
  try {
145
145
  mcpManager.touch(serverName);
146
- const result = await client.readResource({
147
- uri: input.uri,
148
- });
146
+ const result = await client.readResource({ uri: input.uri }, { signal: ctx?.signal });
149
147
  const contents = result?.contents ?? [];
150
148
  const textParts = contents
151
149
  .filter((c) => c.text)
@@ -103,7 +103,7 @@ export function createCallSkillSubAgentTool(skillDirs, filteredSkills, provider)
103
103
  additionalProperties: false,
104
104
  },
105
105
  },
106
- async callback(input) {
106
+ async callback(input, ctx) {
107
107
  const skillId = input.skill_id;
108
108
  const task = input.task;
109
109
  const skill = await readSkill(skillDirs, skillId);
@@ -113,7 +113,7 @@ export function createCallSkillSubAgentTool(skillDirs, filteredSkills, provider)
113
113
  if (!skill.subAgent) {
114
114
  return `Skill "${skillId}" 不支持子 Agent 模式,请使用 load_skill 加载指导后自行处理`;
115
115
  }
116
- const parentAgent = (this).agent;
116
+ const parentAgent = ctx.agent;
117
117
  const parentPermissions = parentAgent instanceof SdkAgent
118
118
  ? parentAgent.permissions
119
119
  : undefined;
@@ -130,7 +130,17 @@ export function createCallSkillSubAgentTool(skillDirs, filteredSkills, provider)
130
130
  ],
131
131
  tools: skillTools,
132
132
  });
133
- await subAgent.run();
133
+ // 子 Agent 中断联动:外层 abort → subAgent.abort()
134
+ const onAbort = () => subAgent.abort();
135
+ if (ctx.signal) {
136
+ ctx.signal.addEventListener("abort", onAbort, { once: true });
137
+ }
138
+ try {
139
+ await subAgent.run();
140
+ }
141
+ finally {
142
+ ctx.signal?.removeEventListener("abort", onAbort);
143
+ }
134
144
  const lastMsg = subAgent.messages.at(-1);
135
145
  return typeof lastMsg?.content === "string" ? lastMsg.content : "";
136
146
  },
@@ -18,14 +18,14 @@ export function createSubAgentTool(def, provider) {
18
18
  parameters: def.function.parameters,
19
19
  },
20
20
  messages: def.messages,
21
- buildAgent: function (_parsedArgs) {
21
+ buildAgent: (_parsed_args, ctx) => {
22
22
  // 模型解析:SubAgent 可指定独立模型,否则复用父 Agent 的模型
23
23
  let subModel;
24
24
  if (def.modelId) {
25
25
  subModel = createModel(provider, def.modelId);
26
26
  }
27
27
  else {
28
- const parentModel = this.agent?.model;
28
+ const parentModel = ctx.agent?.model;
29
29
  if (!parentModel) {
30
30
  throw new Error(`SubAgent "${selfName}" 未指定 modelId,且父 Agent 没有可复用的模型`);
31
31
  }
package/dist/index.d.ts CHANGED
@@ -21,6 +21,7 @@ export { createAgent } from "./runtime/createAgent.js";
21
21
  export { McpConnectionManager } from "./runtime/McpConnectionManager.js";
22
22
  export type { McpConnectOptions } from "./runtime/McpConnectionManager.js";
23
23
  export { SdkCallbackTool } from "./runtime/SdkCallbackTool.js";
24
+ export type { SdkCallbackToolOptions } from "./runtime/SdkCallbackTool.js";
24
25
  export { TaskMigrationService } from "./runtime/TaskMigrationService.js";
25
26
  export type { BuildMigrationAgentOptions } from "./runtime/TaskMigrationService.js";
26
27
  export { AutoMigratePlugin } from "./plugin/AutoMigratePlugin.js";
@@ -1,6 +1,11 @@
1
1
  import { Tool, ToolCallContext } from "@ai-zen/agents-core";
2
2
  import type { AgentNS } from "@ai-zen/agents-core";
3
3
  import type { ToolEnv } from "../types/index.js";
4
+ /** SdkCallbackTool 构造选项。env 为核心字段,未来可在此扩展可选项。 */
5
+ export interface SdkCallbackToolOptions {
6
+ /** 注入的工具环境 */
7
+ env: ToolEnv;
8
+ }
4
9
  /**
5
10
  * SdkCallbackTool — SDK 内置工具基类。
6
11
  *
@@ -11,16 +16,19 @@ import type { ToolEnv } from "../types/index.js";
11
16
  * - 一个工具一个类,文件名 = 类名(PascalCase)
12
17
  * - 依赖环境(cwd、config)通过构造参数显式注入
13
18
  * - 相对路径统一用 resolve() 解析到 env.cwd
19
+ * - 工具定义(function)由子类以类体字段提供(见 SdkCallbackTool.function)
14
20
  */
15
21
  export declare abstract class SdkCallbackTool extends Tool {
16
22
  /** 注入的工具环境 */
17
23
  readonly env: ToolEnv;
18
- constructor(options: {
19
- function: AgentNS.FunctionDefine;
20
- env: ToolEnv;
21
- });
22
- /** 工具核心逻辑(由子类实现,参数为 parsed_args) */
23
- abstract call(input: unknown): unknown | Promise<unknown>;
24
+ /** 工具定义。由子类以类体字段 function = {...} 提供。 */
25
+ function: AgentNS.FunctionDefine;
26
+ constructor(options: SdkCallbackToolOptions);
27
+ /**
28
+ * 工具核心逻辑(由子类实现,参数为 parsed_args)。
29
+ * 可选第二参 ctx:透传完整 ToolCallContext,需中止能力(signal)的工具自行读取。
30
+ */
31
+ abstract call(input: unknown, ctx?: ToolCallContext): unknown | Promise<unknown>;
24
32
  /** 桥接 core 的 Tool.exec:解析参数 → call → 序列化 */
25
33
  exec(ctx: ToolCallContext): Promise<string>;
26
34
  /** 将相对路径解析到 env.cwd,绝对路径原样返回 */
@@ -10,17 +10,18 @@ import { Tool } from "@ai-zen/agents-core";
10
10
  * - 一个工具一个类,文件名 = 类名(PascalCase)
11
11
  * - 依赖环境(cwd、config)通过构造参数显式注入
12
12
  * - 相对路径统一用 resolve() 解析到 env.cwd
13
+ * - 工具定义(function)由子类以类体字段提供(见 SdkCallbackTool.function)
13
14
  */
14
15
  export class SdkCallbackTool extends Tool {
15
16
  /** 注入的工具环境 */
16
17
  env;
17
18
  constructor(options) {
18
- super({ function: options.function });
19
+ super();
19
20
  this.env = options.env;
20
21
  }
21
22
  /** 桥接 core 的 Tool.exec:解析参数 → call → 序列化 */
22
23
  async exec(ctx) {
23
- const result = await this.call(ctx.parsed_args);
24
+ const result = await this.call(ctx.parsed_args, ctx);
24
25
  if (typeof result === "string")
25
26
  return result;
26
27
  // JSON.stringify(undefined) 返回 undefined(非字符串),会破坏 Promise<string> 契约
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-zen/agents-sdk",
3
- "version": "0.5.7",
3
+ "version": "0.6.0",
4
4
  "description": "AI-Zen Agents SDK — shared business logic for CLI and Desktop",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -18,7 +18,7 @@
18
18
  "dependencies": {
19
19
  "@modelcontextprotocol/sdk": "^1.29.0",
20
20
  "dayjs": "^1.11.21",
21
- "@ai-zen/agents-core": "^3.3.1"
21
+ "@ai-zen/agents-core": "^3.4.0"
22
22
  },
23
23
  "devDependencies": {
24
24
  "@types/node": "^24.12.2",