@ai-zen/agents-sdk 0.11.0 → 1.0.0-alpha.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 (116) hide show
  1. package/CHANGELOG.md +85 -24
  2. package/README.md +37 -26
  3. package/README.zh.md +37 -26
  4. package/dist/{plugin → agent-plugins}/AutoRefreshToolsPlugin.js +2 -2
  5. package/dist/{plugin → agent-plugins}/UnknownToolHintPlugin.d.ts +7 -5
  6. package/dist/{plugin → agent-plugins}/UnknownToolHintPlugin.js +9 -5
  7. package/dist/config/ConfigManager.d.ts +3 -1
  8. package/dist/config/ConfigManager.js +31 -4
  9. package/dist/config/constants.js +6 -1
  10. package/dist/index.d.ts +37 -29
  11. package/dist/index.js +28 -21
  12. package/dist/runtime/SdkAgent.d.ts +5 -5
  13. package/dist/runtime/SdkAgent.js +4 -4
  14. package/dist/runtime/TaskMigrationService.d.ts +1 -1
  15. package/dist/runtime/TaskMigrationService.js +2 -2
  16. package/dist/runtime/createAgent.d.ts +4 -4
  17. package/dist/runtime/createAgent.js +8 -8
  18. package/dist/runtime/createModel.d.ts +3 -3
  19. package/dist/runtime/createModel.js +3 -3
  20. package/dist/scope/Scope.d.ts +111 -0
  21. package/dist/scope/Scope.js +165 -0
  22. package/dist/scope/ScopePlugin.d.ts +51 -0
  23. package/dist/scope/ScopePlugin.js +1 -0
  24. package/dist/scope/plugins/allInOne.d.ts +18 -0
  25. package/dist/scope/plugins/allInOne.js +23 -0
  26. package/dist/{capabilities/implements → scope/plugins}/builtin/BatchEditTool.d.ts +1 -1
  27. package/dist/{capabilities/implements → scope/plugins}/builtin/BatchEditTool.js +12 -10
  28. package/dist/scope/plugins/builtin/BuiltinToolsScopePlugin.d.ts +20 -0
  29. package/dist/scope/plugins/builtin/BuiltinToolsScopePlugin.js +30 -0
  30. package/dist/{capabilities/implements → scope/plugins}/builtin/CopyTool.d.ts +1 -1
  31. package/dist/{capabilities/implements → scope/plugins}/builtin/CopyTool.js +1 -1
  32. package/dist/{capabilities/implements → scope/plugins}/builtin/CwdTool.d.ts +1 -1
  33. package/dist/{capabilities/implements → scope/plugins}/builtin/CwdTool.js +1 -1
  34. package/dist/{capabilities/implements → scope/plugins}/builtin/DownloadFileTool.d.ts +1 -1
  35. package/dist/{capabilities/implements → scope/plugins}/builtin/DownloadFileTool.js +1 -1
  36. package/dist/{capabilities/implements → scope/plugins}/builtin/EditTool.d.ts +1 -1
  37. package/dist/{capabilities/implements → scope/plugins}/builtin/EditTool.js +1 -1
  38. package/dist/{capabilities/implements → scope/plugins}/builtin/ExecAsyncTool.d.ts +1 -1
  39. package/dist/{capabilities/implements → scope/plugins}/builtin/ExecAsyncTool.js +1 -1
  40. package/dist/{capabilities/implements → scope/plugins}/builtin/ExecTool.d.ts +1 -1
  41. package/dist/{capabilities/implements → scope/plugins}/builtin/ExecTool.js +34 -2
  42. package/dist/{capabilities/implements → scope/plugins}/builtin/ExistTool.d.ts +1 -1
  43. package/dist/{capabilities/implements → scope/plugins}/builtin/ExistTool.js +1 -1
  44. package/dist/{capabilities/implements → scope/plugins}/builtin/FindTextTool.d.ts +6 -1
  45. package/dist/{capabilities/implements → scope/plugins}/builtin/FindTextTool.js +29 -4
  46. package/dist/{capabilities/implements → scope/plugins}/builtin/GenerateImageTool.d.ts +1 -1
  47. package/dist/{capabilities/implements → scope/plugins}/builtin/GenerateImageTool.js +1 -1
  48. package/dist/{capabilities/implements → scope/plugins}/builtin/GlobTool.d.ts +6 -1
  49. package/dist/{capabilities/implements → scope/plugins}/builtin/GlobTool.js +29 -3
  50. package/dist/scope/plugins/builtin/InspectFileTool.d.ts +17 -0
  51. package/dist/scope/plugins/builtin/InspectFileTool.js +150 -0
  52. package/dist/{capabilities/implements → scope/plugins}/builtin/LsTool.d.ts +1 -1
  53. package/dist/scope/plugins/builtin/LsTool.js +52 -0
  54. package/dist/{capabilities/implements → scope/plugins}/builtin/MkdirTool.d.ts +1 -1
  55. package/dist/{capabilities/implements → scope/plugins}/builtin/MkdirTool.js +1 -1
  56. package/dist/{capabilities/implements → scope/plugins}/builtin/ReadFileTool.d.ts +2 -1
  57. package/dist/scope/plugins/builtin/ReadFileTool.js +94 -0
  58. package/dist/{capabilities/implements → scope/plugins}/builtin/RenameTool.d.ts +1 -1
  59. package/dist/{capabilities/implements → scope/plugins}/builtin/RenameTool.js +1 -1
  60. package/dist/{capabilities/implements → scope/plugins}/builtin/RmTool.d.ts +1 -1
  61. package/dist/{capabilities/implements → scope/plugins}/builtin/RmTool.js +1 -1
  62. package/dist/{runtime → scope/plugins/builtin}/SdkCallbackTool.d.ts +1 -1
  63. package/dist/{capabilities/implements → scope/plugins}/builtin/SleepTool.d.ts +1 -1
  64. package/dist/{capabilities/implements → scope/plugins}/builtin/SleepTool.js +1 -1
  65. package/dist/{capabilities/implements → scope/plugins}/builtin/ViewImageTool.d.ts +1 -1
  66. package/dist/{capabilities/implements → scope/plugins}/builtin/ViewImageTool.js +1 -1
  67. package/dist/{capabilities/implements → scope/plugins}/builtin/WriteFileTool.d.ts +1 -1
  68. package/dist/{capabilities/implements → scope/plugins}/builtin/WriteFileTool.js +1 -1
  69. package/dist/{capabilities/discovery/builtin.d.ts → scope/plugins/builtin/discover.d.ts} +1 -1
  70. package/dist/{capabilities/discovery/builtin.js → scope/plugins/builtin/discover.js} +1 -1
  71. package/dist/{capabilities/implements → scope/plugins}/builtin/index.d.ts +5 -4
  72. package/dist/{capabilities/implements → scope/plugins}/builtin/index.js +5 -3
  73. package/dist/scope/plugins/builtin/outputGuard.d.ts +81 -0
  74. package/dist/scope/plugins/builtin/outputGuard.js +73 -0
  75. package/dist/{runtime → scope/plugins/mcp}/McpConnectionManager.d.ts +1 -1
  76. package/dist/{runtime → scope/plugins/mcp}/McpConnectionManager.js +1 -1
  77. package/dist/scope/plugins/mcp/McpScopePlugin.d.ts +29 -0
  78. package/dist/scope/plugins/mcp/McpScopePlugin.js +60 -0
  79. package/dist/{capabilities/discovery/mcp.d.ts → scope/plugins/mcp/discover.d.ts} +1 -1
  80. package/dist/{capabilities/discovery/mcp.js → scope/plugins/mcp/discover.js} +1 -1
  81. package/dist/{capabilities/implements → scope/plugins/mcp}/mcpTools.d.ts +2 -2
  82. package/dist/{capabilities/implements → scope/plugins/mcp}/mcpTools.js +2 -2
  83. package/dist/scope/plugins/skills/SkillsScopePlugin.d.ts +28 -0
  84. package/dist/scope/plugins/skills/SkillsScopePlugin.js +48 -0
  85. package/dist/{capabilities/discovery/skills.js → scope/plugins/skills/discover.js} +1 -1
  86. package/dist/{capabilities/implements → scope/plugins/skills}/skillTools.d.ts +1 -1
  87. package/dist/{capabilities/implements → scope/plugins/skills}/skillTools.js +3 -3
  88. package/dist/scope/plugins/subagents/SubAgentsScopePlugin.d.ts +20 -0
  89. package/dist/scope/plugins/subagents/SubAgentsScopePlugin.js +34 -0
  90. package/dist/{capabilities/discovery/subagents.d.ts → scope/plugins/subagents/discover.d.ts} +1 -1
  91. package/dist/{capabilities/implements → scope/plugins/subagents}/subAgentTools.d.ts +3 -3
  92. package/dist/{capabilities/implements → scope/plugins/subagents}/subAgentTools.js +5 -5
  93. package/dist/scope/plugins/usertools/UserToolsScopePlugin.d.ts +24 -0
  94. package/dist/scope/plugins/usertools/UserToolsScopePlugin.js +28 -0
  95. package/dist/{capabilities/discovery/usertools.js → scope/plugins/usertools/discover.js} +1 -1
  96. package/dist/types/index.d.ts +17 -2
  97. package/package.json +3 -3
  98. package/dist/capabilities/implements/builtin/LsTool.js +0 -31
  99. package/dist/capabilities/implements/builtin/ReadFileTool.js +0 -35
  100. package/dist/runtime/Provider.d.ts +0 -119
  101. package/dist/runtime/Provider.js +0 -218
  102. /package/dist/{plugin → agent-plugins}/AutoMigratePlugin.d.ts +0 -0
  103. /package/dist/{plugin → agent-plugins}/AutoMigratePlugin.js +0 -0
  104. /package/dist/{plugin → agent-plugins}/AutoRefreshToolsPlugin.d.ts +0 -0
  105. /package/dist/{plugin → agent-plugins}/ContextGuardPlugin.d.ts +0 -0
  106. /package/dist/{plugin → agent-plugins}/ContextGuardPlugin.js +0 -0
  107. /package/dist/{capabilities → scope}/PermissionEvaluator.d.ts +0 -0
  108. /package/dist/{capabilities → scope}/PermissionEvaluator.js +0 -0
  109. /package/dist/{capabilities → scope}/disclosure.d.ts +0 -0
  110. /package/dist/{capabilities → scope}/disclosure.js +0 -0
  111. /package/dist/{runtime → scope/plugins/builtin}/SdkCallbackTool.js +0 -0
  112. /package/dist/{capabilities/implements → scope/plugins}/builtin/test-helpers.d.ts +0 -0
  113. /package/dist/{capabilities/implements → scope/plugins}/builtin/test-helpers.js +0 -0
  114. /package/dist/{capabilities/discovery/skills.d.ts → scope/plugins/skills/discover.d.ts} +0 -0
  115. /package/dist/{capabilities/discovery/subagents.js → scope/plugins/subagents/discover.js} +0 -0
  116. /package/dist/{capabilities/discovery/usertools.d.ts → scope/plugins/usertools/discover.d.ts} +0 -0
@@ -1,6 +1,6 @@
1
1
  import { Client } from "@modelcontextprotocol/sdk/client/index.js";
2
2
  import type { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
3
- import type { McpServerConfig, McpServerManifest, McpConnectionState } from "../types/index.js";
3
+ import type { McpServerConfig, McpServerManifest, McpConnectionState } from "../../../types/index.js";
4
4
  export interface McpConnectOptions {
5
5
  /** 空闲超时(毫秒),默认 stdio: 30min, http: 5min */
6
6
  idleTimeoutMs?: number;
@@ -2,7 +2,7 @@ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
2
2
  import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
3
3
  import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
4
4
  import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
5
- import { getLogger } from "../shared/logger.js";
5
+ import { getLogger } from "../../../shared/logger.js";
6
6
  const DEFAULT_IDLE_TIMEOUT = {
7
7
  stdio: 30 * 60 * 1000,
8
8
  http: 5 * 60 * 1000,
@@ -0,0 +1,29 @@
1
+ import type { Tool } from "@ai-zen/agents-core";
2
+ import type { Scope, FilterOutput } from "../../Scope.js";
3
+ import type { ScopeCandidates, ScopePlugin } from "../../ScopePlugin.js";
4
+ /**
5
+ * MCP 插件 —— 发现 mcp.json + 自持连接管理器 + 装配渐进式披露元工具。
6
+ *
7
+ * 渐进式披露设计原样保留:
8
+ * - `load_mcp` 枚举所有允许的 server(含 include_manifest 开关)
9
+ * - `call_mcp_tool` 调用已连接 server 的工具
10
+ * - `read_mcp_resource` 读取已连接 server 的资源
11
+ * 三个元工具名恒上报 `tools` 维度;`mcpManager` 由本插件私有持有,
12
+ * 有配置路径时在构造时创建,`dispose()` 时统一断开。
13
+ */
14
+ export declare class McpScopePlugin implements ScopePlugin {
15
+ static readonly ID = "mcp";
16
+ readonly id = "mcp";
17
+ private readonly paths;
18
+ private readonly mcpManager;
19
+ private servers;
20
+ constructor(options?: {
21
+ paths?: string[];
22
+ });
23
+ discover(scope: Scope): Promise<void>;
24
+ /** 是否配置了 MCP(供外部代码判定,如 UnknownToolHintPlugin) */
25
+ hasConfig(): boolean;
26
+ candidates(): ScopeCandidates;
27
+ instantiate(_scope: Scope, filtered: FilterOutput): Tool[];
28
+ dispose(): Promise<void>;
29
+ }
@@ -0,0 +1,60 @@
1
+ import { isAbsolute, join } from "node:path";
2
+ import { discoverMcpServers } from "./discover.js";
3
+ import { createCallMcpTool, createLoadMcpTool, createReadMcpResourceTool, } from "./mcpTools.js";
4
+ import { McpConnectionManager } from "./McpConnectionManager.js";
5
+ /**
6
+ * MCP 插件 —— 发现 mcp.json + 自持连接管理器 + 装配渐进式披露元工具。
7
+ *
8
+ * 渐进式披露设计原样保留:
9
+ * - `load_mcp` 枚举所有允许的 server(含 include_manifest 开关)
10
+ * - `call_mcp_tool` 调用已连接 server 的工具
11
+ * - `read_mcp_resource` 读取已连接 server 的资源
12
+ * 三个元工具名恒上报 `tools` 维度;`mcpManager` 由本插件私有持有,
13
+ * 有配置路径时在构造时创建,`dispose()` 时统一断开。
14
+ */
15
+ export class McpScopePlugin {
16
+ static ID = "mcp";
17
+ id = McpScopePlugin.ID;
18
+ paths;
19
+ mcpManager;
20
+ servers = [];
21
+ constructor(options) {
22
+ this.paths = options?.paths ?? [];
23
+ this.mcpManager =
24
+ this.paths.length > 0 ? new McpConnectionManager() : undefined;
25
+ }
26
+ async discover(scope) {
27
+ const files = this.paths.map((p) => (isAbsolute(p) ? p : join(scope.cwd, p)));
28
+ this.servers = await discoverMcpServers(files);
29
+ }
30
+ /** 是否配置了 MCP(供外部代码判定,如 UnknownToolHintPlugin) */
31
+ hasConfig() {
32
+ return this.paths.length > 0;
33
+ }
34
+ candidates() {
35
+ return {
36
+ mcps: this.servers.map((s) => s.id),
37
+ tools: ["load_mcp", "call_mcp_tool", "read_mcp_resource"],
38
+ };
39
+ }
40
+ instantiate(_scope, filtered) {
41
+ const allow = new Set(filtered.tools);
42
+ const allowedMcps = new Set(filtered.mcps);
43
+ const filteredMcps = this.servers.filter((s) => allowedMcps.has(s.id));
44
+ const m = this.mcpManager;
45
+ const result = [];
46
+ if (allow.has("load_mcp") && m && filteredMcps.length > 0) {
47
+ result.push(createLoadMcpTool(m, filteredMcps));
48
+ }
49
+ if (allow.has("call_mcp_tool") && m) {
50
+ result.push(createCallMcpTool(m));
51
+ }
52
+ if (allow.has("read_mcp_resource") && m) {
53
+ result.push(createReadMcpResourceTool(m));
54
+ }
55
+ return result;
56
+ }
57
+ async dispose() {
58
+ await this.mcpManager?.disconnectAll();
59
+ }
60
+ }
@@ -1,4 +1,4 @@
1
- import type { McpServerConfig } from "../../types/index.js";
1
+ import type { McpServerConfig } from "../../../types/index.js";
2
2
  /**
3
3
  * 从 mcp.json 文件列表中发现所有 MCP server 配置。
4
4
  *
@@ -1,5 +1,5 @@
1
1
  import { promises as fs } from "node:fs";
2
- import { getLogger } from "../../shared/logger.js";
2
+ import { getLogger } from "../../../shared/logger.js";
3
3
  /**
4
4
  * 归一化原始 MCP server 配置为内部 McpServerConfig。
5
5
  * 处理业界标准中的字段别名差异。
@@ -1,6 +1,6 @@
1
1
  import { CallbackTool } from "@ai-zen/agents-core";
2
- import type { McpConnectionManager } from "../../runtime/McpConnectionManager.js";
3
- import type { McpServerConfig } from "../../types/index.js";
2
+ import type { McpConnectionManager } from "./McpConnectionManager.js";
3
+ import type { McpServerConfig } from "../../../types/index.js";
4
4
  /**
5
5
  * 创建 load_mcp 工具。
6
6
  * server 枚举由 filteredMcps (完整 McpServerConfig[])自动推导,
@@ -1,6 +1,6 @@
1
1
  import { CallbackTool } from "@ai-zen/agents-core";
2
- import { createDisclosureParam } from "../disclosure.js";
3
- import { getLogger } from "../../shared/logger.js";
2
+ import { createDisclosureParam } from "../../disclosure.js";
3
+ import { getLogger } from "../../../shared/logger.js";
4
4
  const EMPTY_HINT = "(当前没有可用的 MCP 服务器,请联系用户添加)";
5
5
  const log = getLogger();
6
6
  /**
@@ -0,0 +1,28 @@
1
+ import type { Tool } from "@ai-zen/agents-core";
2
+ import type { Scope, FilterOutput } from "../../Scope.js";
3
+ import type { ScopeCandidates, ScopePlugin } from "../../ScopePlugin.js";
4
+ /**
5
+ * Skill 插件 —— 发现 SKILL.md + 装配渐进式披露元工具。
6
+ *
7
+ * 渐进式披露设计原样保留:
8
+ * - `load_skill` 枚举所有允许的 skill
9
+ * - `call_skill_sub_agent` 枚举 subAgent: true 的 skill(AgentToolLazy)
10
+ * 两个元工具名恒上报 `tools` 维度(供权限允许/拒绝 → 拒绝即切断披露通道);
11
+ * 是否真正注册由 `instantiate()` 按允许名 + 候选条件决定。
12
+ */
13
+ export declare class SkillsScopePlugin implements ScopePlugin {
14
+ static readonly ID = "skills";
15
+ readonly id = "skills";
16
+ private readonly paths;
17
+ private skills;
18
+ /** 解析后的 skill 目录(供 instantiate 时传给工具读取正文) */
19
+ private dirs;
20
+ constructor(options?: {
21
+ paths?: string[];
22
+ });
23
+ discover(scope: Scope, options?: {
24
+ silent?: boolean;
25
+ }): Promise<void>;
26
+ candidates(): ScopeCandidates;
27
+ instantiate(_scope: Scope, filtered: FilterOutput): Tool[];
28
+ }
@@ -0,0 +1,48 @@
1
+ import { isAbsolute, join } from "node:path";
2
+ import { discoverSkills } from "./discover.js";
3
+ import { createCallSkillSubAgentTool, createLoadSkillTool, } from "./skillTools.js";
4
+ /**
5
+ * Skill 插件 —— 发现 SKILL.md + 装配渐进式披露元工具。
6
+ *
7
+ * 渐进式披露设计原样保留:
8
+ * - `load_skill` 枚举所有允许的 skill
9
+ * - `call_skill_sub_agent` 枚举 subAgent: true 的 skill(AgentToolLazy)
10
+ * 两个元工具名恒上报 `tools` 维度(供权限允许/拒绝 → 拒绝即切断披露通道);
11
+ * 是否真正注册由 `instantiate()` 按允许名 + 候选条件决定。
12
+ */
13
+ export class SkillsScopePlugin {
14
+ static ID = "skills";
15
+ id = SkillsScopePlugin.ID;
16
+ paths;
17
+ skills = [];
18
+ /** 解析后的 skill 目录(供 instantiate 时传给工具读取正文) */
19
+ dirs = [];
20
+ constructor(options) {
21
+ this.paths = options?.paths ?? [];
22
+ }
23
+ async discover(scope, options) {
24
+ this.dirs = this.paths.map((p) => (isAbsolute(p) ? p : join(scope.cwd, p)));
25
+ this.skills = await discoverSkills(this.dirs, options);
26
+ }
27
+ candidates() {
28
+ return {
29
+ skills: this.skills.map((s) => s.id),
30
+ tools: ["load_skill", "call_skill_sub_agent"],
31
+ };
32
+ }
33
+ instantiate(_scope, filtered) {
34
+ const allow = new Set(filtered.tools);
35
+ const allowedSkills = new Set(filtered.skills);
36
+ const filteredSkills = this.skills.filter((s) => allowedSkills.has(s.id));
37
+ const result = [];
38
+ if (allow.has("load_skill") && filteredSkills.length > 0) {
39
+ result.push(createLoadSkillTool(this.dirs, filteredSkills));
40
+ }
41
+ // call_skill_sub_agent:只要有支持子 Agent 的 skill 就注册
42
+ if (allow.has("call_skill_sub_agent") &&
43
+ filteredSkills.some((s) => s.subAgent)) {
44
+ result.push(createCallSkillSubAgentTool(this.dirs, filteredSkills));
45
+ }
46
+ return result;
47
+ }
48
+ }
@@ -1,6 +1,6 @@
1
1
  import { promises as fs } from "node:fs";
2
2
  import { join } from "node:path";
3
- import { getLogger } from "../../shared/logger.js";
3
+ import { getLogger } from "../../../shared/logger.js";
4
4
  const log = getLogger();
5
5
  // ---- 发现(返回完整 SkillInfo,不再丢失 subAgent 等信息)----
6
6
  /**
@@ -1,5 +1,5 @@
1
1
  import { CallbackTool, AgentToolLazy } from "@ai-zen/agents-core";
2
- import type { SkillInfo } from "../discovery/skills.js";
2
+ import type { SkillInfo } from "./discover.js";
3
3
  /**
4
4
  * 创建 load_skill 工具。
5
5
  * 枚举所有允许的 skill(不限是否支持子 Agent),由 filteredSkills 提供完整信息。
@@ -1,7 +1,7 @@
1
1
  import { CallbackTool, Agent, AgentToolLazy, Message } from "@ai-zen/agents-core";
2
- import { createDisclosureParam } from "../disclosure.js";
3
- import { readSkill } from "../discovery/skills.js";
4
- import { getLogger } from "../../shared/logger.js";
2
+ import { createDisclosureParam } from "../../disclosure.js";
3
+ import { readSkill } from "./discover.js";
4
+ import { getLogger } from "../../../shared/logger.js";
5
5
  import { promises as fs } from "node:fs";
6
6
  const log = getLogger();
7
7
  const EMPTY_HINT = "(当前没有可用的 Skill,请联系用户添加)";
@@ -0,0 +1,20 @@
1
+ import type { Tool } from "@ai-zen/agents-core";
2
+ import type { Scope, FilterOutput } from "../../Scope.js";
3
+ import type { ScopeCandidates, ScopePlugin } from "../../ScopePlugin.js";
4
+ /**
5
+ * SubAgent 插件 —— 发现 `sub-agents/*.json`,将含 function 的定义注册为工具。
6
+ *
7
+ * 产物为 `AgentToolLazy`(延迟构建,避免工具列表构建阶段的递归创建)。
8
+ */
9
+ export declare class SubAgentsScopePlugin implements ScopePlugin {
10
+ static readonly ID = "subagents";
11
+ readonly id = "subagents";
12
+ private readonly paths;
13
+ private subagents;
14
+ constructor(options?: {
15
+ paths?: string[];
16
+ });
17
+ discover(scope: Scope): Promise<void>;
18
+ candidates(): ScopeCandidates;
19
+ instantiate(scope: Scope, filtered: FilterOutput): Tool[];
20
+ }
@@ -0,0 +1,34 @@
1
+ import { isAbsolute, join } from "node:path";
2
+ import { discoverSubAgents } from "./discover.js";
3
+ import { createSubAgentTool } from "./subAgentTools.js";
4
+ /**
5
+ * SubAgent 插件 —— 发现 `sub-agents/*.json`,将含 function 的定义注册为工具。
6
+ *
7
+ * 产物为 `AgentToolLazy`(延迟构建,避免工具列表构建阶段的递归创建)。
8
+ */
9
+ export class SubAgentsScopePlugin {
10
+ static ID = "subagents";
11
+ id = SubAgentsScopePlugin.ID;
12
+ paths;
13
+ subagents = [];
14
+ constructor(options) {
15
+ this.paths = options?.paths ?? [];
16
+ }
17
+ async discover(scope) {
18
+ const dirs = this.paths.map((p) => (isAbsolute(p) ? p : join(scope.cwd, p)));
19
+ this.subagents = await discoverSubAgents(dirs);
20
+ }
21
+ candidates() {
22
+ return {
23
+ subagents: this.subagents
24
+ .filter((d) => d.function)
25
+ .map((d) => d.function.name),
26
+ };
27
+ }
28
+ instantiate(scope, filtered) {
29
+ const allow = new Set(filtered.subagents);
30
+ return this.subagents
31
+ .filter((d) => d.function && allow.has(d.function.name))
32
+ .map((d) => createSubAgentTool(d, scope));
33
+ }
34
+ }
@@ -1,4 +1,4 @@
1
- import type { AgentDefinition } from "../../types/index.js";
1
+ import type { AgentDefinition } from "../../../types/index.js";
2
2
  /**
3
3
  * 扫描多个目录中发现的所有 SubAgent 完整定义。
4
4
  * 按优先级从高到低传入路径列表,同名 function.name 靠前的路径优先(先到先得)。
@@ -1,10 +1,10 @@
1
1
  import type { Tool } from "@ai-zen/agents-core";
2
- import type { AgentDefinition } from "../../types/index.js";
3
- import type { Provider } from "../../runtime/Provider.js";
2
+ import type { AgentDefinition } from "../../../types/index.js";
3
+ import type { Scope } from "../../Scope.js";
4
4
  /**
5
5
  * 创建 SubAgent 工具(AgentToolLazy)。
6
6
  *
7
7
  * SubAgent 是有 `function` 字段的 Agent,可被其他 Agent 作为工具调用。
8
8
  * 使用延迟构建(AgentToolLazy)避免工具列表构建阶段的递归创建问题。
9
9
  */
10
- export declare function createSubAgentTool(def: AgentDefinition, provider: Provider): Tool;
10
+ export declare function createSubAgentTool(def: AgentDefinition, scope: Scope): Tool;
@@ -1,12 +1,12 @@
1
1
  import { Agent, AgentToolLazy } from "@ai-zen/agents-core";
2
- import { createModel } from "../../runtime/createModel.js";
2
+ import { createModel } from "../../../runtime/createModel.js";
3
3
  /**
4
4
  * 创建 SubAgent 工具(AgentToolLazy)。
5
5
  *
6
6
  * SubAgent 是有 `function` 字段的 Agent,可被其他 Agent 作为工具调用。
7
7
  * 使用延迟构建(AgentToolLazy)避免工具列表构建阶段的递归创建问题。
8
8
  */
9
- export function createSubAgentTool(def, provider) {
9
+ export function createSubAgentTool(def, scope) {
10
10
  if (!def.function) {
11
11
  throw new Error(`Agent "${def.id}" 没有 function 字段,不能作为 SubAgent 工具`);
12
12
  }
@@ -24,7 +24,7 @@ export function createSubAgentTool(def, provider) {
24
24
  let model;
25
25
  let modelConfig;
26
26
  if (def.modelId) {
27
- const m = createModel(provider, def.modelId);
27
+ const m = createModel(scope, def.modelId);
28
28
  client = m.client;
29
29
  model = m.model;
30
30
  modelConfig = m.modelConfig;
@@ -39,12 +39,12 @@ export function createSubAgentTool(def, provider) {
39
39
  modelConfig = parent.modelConfig;
40
40
  }
41
41
  // SubAgent 独立解析自己的工具(排除自身,防止递归)
42
- const subFiltered = provider.filter(def, {
42
+ const subFiltered = scope.filter(def, {
43
43
  exclude: {
44
44
  subagents: def.function?.name ? [def.function.name] : undefined,
45
45
  },
46
46
  });
47
- const tools = provider.instantiate(subFiltered);
47
+ const tools = scope.instantiate(subFiltered);
48
48
  const subAgent = new Agent({
49
49
  client,
50
50
  model,
@@ -0,0 +1,24 @@
1
+ import type { Tool } from "@ai-zen/agents-core";
2
+ import type { AgentDefinition } from "../../../types/index.js";
3
+ import type { Scope, FilterOutput } from "../../Scope.js";
4
+ import type { ScopeCandidates, ScopePlugin } from "../../ScopePlugin.js";
5
+ /**
6
+ * 用户工具插件 —— 扫描 `tools/*.js` / `*.mjs` 动态加载。
7
+ *
8
+ * 与内置工具插件分开,以便单独替换/停用其中一类。
9
+ * 路径可为绝对路径或相对路径(相对则按 scope.cwd join)。
10
+ */
11
+ export declare class UserToolsScopePlugin implements ScopePlugin {
12
+ static readonly ID = "user-tools";
13
+ readonly id = "user-tools";
14
+ private readonly paths;
15
+ private tools;
16
+ constructor(options?: {
17
+ paths?: string[];
18
+ });
19
+ discover(scope: Scope, options?: {
20
+ silent?: boolean;
21
+ }): Promise<void>;
22
+ candidates(_scope: Scope, _definition: AgentDefinition): ScopeCandidates;
23
+ instantiate(_scope: Scope, filtered: FilterOutput): Tool[];
24
+ }
@@ -0,0 +1,28 @@
1
+ import { isAbsolute, join } from "node:path";
2
+ import { discoverUserTools } from "./discover.js";
3
+ /**
4
+ * 用户工具插件 —— 扫描 `tools/*.js` / `*.mjs` 动态加载。
5
+ *
6
+ * 与内置工具插件分开,以便单独替换/停用其中一类。
7
+ * 路径可为绝对路径或相对路径(相对则按 scope.cwd join)。
8
+ */
9
+ export class UserToolsScopePlugin {
10
+ static ID = "user-tools";
11
+ id = UserToolsScopePlugin.ID;
12
+ paths;
13
+ tools = [];
14
+ constructor(options) {
15
+ this.paths = options?.paths ?? [];
16
+ }
17
+ async discover(scope, options) {
18
+ const dirs = this.paths.map((p) => (isAbsolute(p) ? p : join(scope.cwd, p)));
19
+ this.tools = await discoverUserTools(dirs, options);
20
+ }
21
+ candidates(_scope, _definition) {
22
+ return { tools: this.tools.map((t) => t.function.name) };
23
+ }
24
+ instantiate(_scope, filtered) {
25
+ const allow = new Set(filtered.tools);
26
+ return this.tools.filter((t) => allow.has(t.function.name));
27
+ }
28
+ }
@@ -2,7 +2,7 @@ import { promises as fs } from "node:fs";
2
2
  import { join } from "node:path";
3
3
  import { pathToFileURL } from "node:url";
4
4
  import { Tool, CallbackTool } from "@ai-zen/agents-core";
5
- import { getLogger } from "../../shared/logger.js";
5
+ import { getLogger } from "../../../shared/logger.js";
6
6
  /**
7
7
  * 扫描多个 tools/ 目录,发现所有 .js / .mjs 文件,动态加载为 Tool 实例。
8
8
  * 使用原生 dynamic import() 加载(在 "type": "module" 下 .js 也是 ESM),
@@ -46,6 +46,13 @@ export interface AgentDefinition {
46
46
  createdAt: string;
47
47
  updatedAt: string;
48
48
  version?: number;
49
+ /**
50
+ * 是否用户自定义。
51
+ *
52
+ * 为 true 时 SDK 初始化不会同步/替换该 Agent 的内置内容(如默认提示词);
53
+ * 缺省(或为 false)时,初始化会以出厂定义同步其提示词,其余字段保留。
54
+ */
55
+ custom?: boolean;
49
56
  }
50
57
  /** 图片生成模型配置 */
51
58
  export interface ImageModel {
@@ -71,11 +78,19 @@ export interface AppConfig {
71
78
  defaultAgent?: string;
72
79
  /** 默认迁移模型 ID */
73
80
  defaultMigrationModel?: string;
81
+ /**
82
+ * 工具输出上限(字符数),缺省取 32768。
83
+ *
84
+ * 工具本次输出超过该值时,由工具自身的保护策略处置(落盘或仅警告),
85
+ * 避免超长输出撑爆上下文。仅作用于"输出",与工具的读取阈值无关
86
+ * (如 readFile/findText 对 300KB 文件的读取上限)。
87
+ */
88
+ maxToolOutput?: number;
74
89
  }
75
90
  /**
76
- * 工具环境 — Provider 在实例化内置工具时注入的环境信息。
91
+ * 工具环境 — Scope 的能力插件在实例化内置工具时注入的环境信息。
77
92
  *
78
- * cwd 是相对路径解析的基准(每个 Provider 对应一个工作目录);
93
+ * cwd 是相对路径解析的基准(每个 Scope 对应一个工作目录);
79
94
  * config 供需要模型/端点配置的工具(如 generateImage)使用。
80
95
  */
81
96
  export interface ToolEnv {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-zen/agents-sdk",
3
- "version": "0.11.0",
3
+ "version": "1.0.0-alpha.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",
@@ -16,10 +16,10 @@
16
16
  "author": "LZQCN",
17
17
  "license": "MIT",
18
18
  "dependencies": {
19
- "@ai-zen/agents-core": "^4.3.0",
20
19
  "@modelcontextprotocol/sdk": "^1.29.0",
21
20
  "dayjs": "^1.11.21",
22
- "openai": "^7.5.0"
21
+ "openai": "^7.5.0",
22
+ "@ai-zen/agents-core": "^4.3.0"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@types/node": "^24.12.2",
@@ -1,31 +0,0 @@
1
- import * as fsp from "fs/promises";
2
- import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
3
- export class LsTool extends SdkCallbackTool {
4
- function = {
5
- name: "ls",
6
- description: "列出目录",
7
- parameters: {
8
- type: "object",
9
- properties: {
10
- path: {
11
- type: "string",
12
- description: "目录路径",
13
- },
14
- },
15
- required: ["path"],
16
- additionalProperties: false,
17
- },
18
- };
19
- constructor(env) {
20
- super({ env });
21
- }
22
- async call(input) {
23
- try {
24
- const result = await fsp.readdir(this.resolve(input.path));
25
- return JSON.stringify(result);
26
- }
27
- catch (error) {
28
- return error?.message;
29
- }
30
- }
31
- }
@@ -1,35 +0,0 @@
1
- import * as fsp from "fs/promises";
2
- import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
3
- export class ReadFileTool extends SdkCallbackTool {
4
- function = {
5
- name: "readFile",
6
- description: "读取文件",
7
- parameters: {
8
- type: "object",
9
- properties: {
10
- path: {
11
- type: "string",
12
- description: "文件路径",
13
- },
14
- },
15
- required: ["path"],
16
- additionalProperties: false,
17
- },
18
- };
19
- constructor(env) {
20
- super({ env });
21
- }
22
- async call(input) {
23
- try {
24
- const filePath = this.resolve(input.path);
25
- const stats = await fsp.stat(filePath);
26
- if (stats.size > 300 * 1024) {
27
- throw new Error(`文件过大,无法读取,当前文件大小 ${stats.size} 字节`);
28
- }
29
- return await fsp.readFile(filePath, "utf-8");
30
- }
31
- catch (error) {
32
- return error?.message;
33
- }
34
- }
35
- }