@ai-zen/agents-sdk 0.12.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 (111) hide show
  1. package/CHANGELOG.md +77 -36
  2. package/README.md +29 -21
  3. package/README.zh.md +29 -21
  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/index.d.ts +37 -29
  8. package/dist/index.js +28 -21
  9. package/dist/runtime/SdkAgent.d.ts +5 -5
  10. package/dist/runtime/SdkAgent.js +4 -4
  11. package/dist/runtime/TaskMigrationService.d.ts +1 -1
  12. package/dist/runtime/TaskMigrationService.js +2 -2
  13. package/dist/runtime/createAgent.d.ts +4 -4
  14. package/dist/runtime/createAgent.js +8 -8
  15. package/dist/runtime/createModel.d.ts +3 -3
  16. package/dist/runtime/createModel.js +3 -3
  17. package/dist/scope/Scope.d.ts +111 -0
  18. package/dist/scope/Scope.js +165 -0
  19. package/dist/scope/ScopePlugin.d.ts +51 -0
  20. package/dist/scope/ScopePlugin.js +1 -0
  21. package/dist/scope/plugins/allInOne.d.ts +18 -0
  22. package/dist/scope/plugins/allInOne.js +23 -0
  23. package/dist/{capabilities/implements → scope/plugins}/builtin/BatchEditTool.d.ts +1 -1
  24. package/dist/{capabilities/implements → scope/plugins}/builtin/BatchEditTool.js +1 -1
  25. package/dist/scope/plugins/builtin/BuiltinToolsScopePlugin.d.ts +20 -0
  26. package/dist/scope/plugins/builtin/BuiltinToolsScopePlugin.js +30 -0
  27. package/dist/{capabilities/implements → scope/plugins}/builtin/CopyTool.d.ts +1 -1
  28. package/dist/{capabilities/implements → scope/plugins}/builtin/CopyTool.js +1 -1
  29. package/dist/{capabilities/implements → scope/plugins}/builtin/CwdTool.d.ts +1 -1
  30. package/dist/{capabilities/implements → scope/plugins}/builtin/CwdTool.js +1 -1
  31. package/dist/{capabilities/implements → scope/plugins}/builtin/DownloadFileTool.d.ts +1 -1
  32. package/dist/{capabilities/implements → scope/plugins}/builtin/DownloadFileTool.js +1 -1
  33. package/dist/{capabilities/implements → scope/plugins}/builtin/EditTool.d.ts +1 -1
  34. package/dist/{capabilities/implements → scope/plugins}/builtin/EditTool.js +1 -1
  35. package/dist/{capabilities/implements → scope/plugins}/builtin/ExecAsyncTool.d.ts +1 -1
  36. package/dist/{capabilities/implements → scope/plugins}/builtin/ExecAsyncTool.js +1 -1
  37. package/dist/{capabilities/implements → scope/plugins}/builtin/ExecTool.d.ts +1 -1
  38. package/dist/{capabilities/implements → scope/plugins}/builtin/ExecTool.js +1 -1
  39. package/dist/{capabilities/implements → scope/plugins}/builtin/ExistTool.d.ts +1 -1
  40. package/dist/{capabilities/implements → scope/plugins}/builtin/ExistTool.js +1 -1
  41. package/dist/{capabilities/implements → scope/plugins}/builtin/FindTextTool.d.ts +1 -1
  42. package/dist/{capabilities/implements → scope/plugins}/builtin/FindTextTool.js +1 -1
  43. package/dist/{capabilities/implements → scope/plugins}/builtin/GenerateImageTool.d.ts +1 -1
  44. package/dist/{capabilities/implements → scope/plugins}/builtin/GenerateImageTool.js +1 -1
  45. package/dist/{capabilities/implements → scope/plugins}/builtin/GlobTool.d.ts +1 -1
  46. package/dist/{capabilities/implements → scope/plugins}/builtin/GlobTool.js +1 -1
  47. package/dist/{capabilities/implements → scope/plugins}/builtin/InspectFileTool.d.ts +1 -1
  48. package/dist/{capabilities/implements → scope/plugins}/builtin/InspectFileTool.js +1 -1
  49. package/dist/{capabilities/implements → scope/plugins}/builtin/LsTool.d.ts +1 -1
  50. package/dist/{capabilities/implements → scope/plugins}/builtin/LsTool.js +1 -1
  51. package/dist/{capabilities/implements → scope/plugins}/builtin/MkdirTool.d.ts +1 -1
  52. package/dist/{capabilities/implements → scope/plugins}/builtin/MkdirTool.js +1 -1
  53. package/dist/{capabilities/implements → scope/plugins}/builtin/ReadFileTool.d.ts +1 -1
  54. package/dist/{capabilities/implements → scope/plugins}/builtin/ReadFileTool.js +1 -1
  55. package/dist/{capabilities/implements → scope/plugins}/builtin/RenameTool.d.ts +1 -1
  56. package/dist/{capabilities/implements → scope/plugins}/builtin/RenameTool.js +1 -1
  57. package/dist/{capabilities/implements → scope/plugins}/builtin/RmTool.d.ts +1 -1
  58. package/dist/{capabilities/implements → scope/plugins}/builtin/RmTool.js +1 -1
  59. package/dist/{runtime → scope/plugins/builtin}/SdkCallbackTool.d.ts +1 -1
  60. package/dist/{capabilities/implements → scope/plugins}/builtin/SleepTool.d.ts +1 -1
  61. package/dist/{capabilities/implements → scope/plugins}/builtin/SleepTool.js +1 -1
  62. package/dist/{capabilities/implements → scope/plugins}/builtin/ViewImageTool.d.ts +1 -1
  63. package/dist/{capabilities/implements → scope/plugins}/builtin/ViewImageTool.js +1 -1
  64. package/dist/{capabilities/implements → scope/plugins}/builtin/WriteFileTool.d.ts +1 -1
  65. package/dist/{capabilities/implements → scope/plugins}/builtin/WriteFileTool.js +1 -1
  66. package/dist/{capabilities/discovery/builtin.d.ts → scope/plugins/builtin/discover.d.ts} +1 -1
  67. package/dist/{capabilities/discovery/builtin.js → scope/plugins/builtin/discover.js} +1 -1
  68. package/dist/{capabilities/implements → scope/plugins}/builtin/index.d.ts +2 -2
  69. package/dist/{capabilities/implements → scope/plugins}/builtin/index.js +1 -1
  70. package/dist/{runtime → scope/plugins/mcp}/McpConnectionManager.d.ts +1 -1
  71. package/dist/{runtime → scope/plugins/mcp}/McpConnectionManager.js +1 -1
  72. package/dist/scope/plugins/mcp/McpScopePlugin.d.ts +29 -0
  73. package/dist/scope/plugins/mcp/McpScopePlugin.js +60 -0
  74. package/dist/{capabilities/discovery/mcp.d.ts → scope/plugins/mcp/discover.d.ts} +1 -1
  75. package/dist/{capabilities/discovery/mcp.js → scope/plugins/mcp/discover.js} +1 -1
  76. package/dist/{capabilities/implements → scope/plugins/mcp}/mcpTools.d.ts +2 -2
  77. package/dist/{capabilities/implements → scope/plugins/mcp}/mcpTools.js +2 -2
  78. package/dist/scope/plugins/skills/SkillsScopePlugin.d.ts +28 -0
  79. package/dist/scope/plugins/skills/SkillsScopePlugin.js +48 -0
  80. package/dist/{capabilities/discovery/skills.js → scope/plugins/skills/discover.js} +1 -1
  81. package/dist/{capabilities/implements → scope/plugins/skills}/skillTools.d.ts +1 -1
  82. package/dist/{capabilities/implements → scope/plugins/skills}/skillTools.js +3 -3
  83. package/dist/scope/plugins/subagents/SubAgentsScopePlugin.d.ts +20 -0
  84. package/dist/scope/plugins/subagents/SubAgentsScopePlugin.js +34 -0
  85. package/dist/{capabilities/discovery/subagents.d.ts → scope/plugins/subagents/discover.d.ts} +1 -1
  86. package/dist/{capabilities/implements → scope/plugins/subagents}/subAgentTools.d.ts +3 -3
  87. package/dist/{capabilities/implements → scope/plugins/subagents}/subAgentTools.js +5 -5
  88. package/dist/scope/plugins/usertools/UserToolsScopePlugin.d.ts +24 -0
  89. package/dist/scope/plugins/usertools/UserToolsScopePlugin.js +28 -0
  90. package/dist/{capabilities/discovery/usertools.js → scope/plugins/usertools/discover.js} +1 -1
  91. package/dist/types/index.d.ts +2 -2
  92. package/package.json +3 -3
  93. package/dist/runtime/Provider.d.ts +0 -119
  94. package/dist/runtime/Provider.js +0 -218
  95. /package/dist/{plugin → agent-plugins}/AutoMigratePlugin.d.ts +0 -0
  96. /package/dist/{plugin → agent-plugins}/AutoMigratePlugin.js +0 -0
  97. /package/dist/{plugin → agent-plugins}/AutoRefreshToolsPlugin.d.ts +0 -0
  98. /package/dist/{plugin → agent-plugins}/ContextGuardPlugin.d.ts +0 -0
  99. /package/dist/{plugin → agent-plugins}/ContextGuardPlugin.js +0 -0
  100. /package/dist/{capabilities → scope}/PermissionEvaluator.d.ts +0 -0
  101. /package/dist/{capabilities → scope}/PermissionEvaluator.js +0 -0
  102. /package/dist/{capabilities → scope}/disclosure.d.ts +0 -0
  103. /package/dist/{capabilities → scope}/disclosure.js +0 -0
  104. /package/dist/{runtime → scope/plugins/builtin}/SdkCallbackTool.js +0 -0
  105. /package/dist/{capabilities/implements → scope/plugins}/builtin/outputGuard.d.ts +0 -0
  106. /package/dist/{capabilities/implements → scope/plugins}/builtin/outputGuard.js +0 -0
  107. /package/dist/{capabilities/implements → scope/plugins}/builtin/test-helpers.d.ts +0 -0
  108. /package/dist/{capabilities/implements → scope/plugins}/builtin/test-helpers.js +0 -0
  109. /package/dist/{capabilities/discovery/skills.d.ts → scope/plugins/skills/discover.d.ts} +0 -0
  110. /package/dist/{capabilities/discovery/subagents.js → scope/plugins/subagents/discover.js} +0 -0
  111. /package/dist/{capabilities/discovery/usertools.d.ts → scope/plugins/usertools/discover.d.ts} +0 -0
@@ -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),
@@ -88,9 +88,9 @@ export interface AppConfig {
88
88
  maxToolOutput?: number;
89
89
  }
90
90
  /**
91
- * 工具环境 — Provider 在实例化内置工具时注入的环境信息。
91
+ * 工具环境 — Scope 的能力插件在实例化内置工具时注入的环境信息。
92
92
  *
93
- * cwd 是相对路径解析的基准(每个 Provider 对应一个工作目录);
93
+ * cwd 是相对路径解析的基准(每个 Scope 对应一个工作目录);
94
94
  * config 供需要模型/端点配置的工具(如 generateImage)使用。
95
95
  */
96
96
  export interface ToolEnv {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-zen/agents-sdk",
3
- "version": "0.12.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,119 +0,0 @@
1
- import type { Tool } from "@ai-zen/agents-core";
2
- import type { AppConfig, AgentDefinition, McpServerConfig, ToolEnv } from "../types/index.js";
3
- import type { SkillInfo } from "../capabilities/discovery/skills.js";
4
- import { McpConnectionManager } from "./McpConnectionManager.js";
5
- /**
6
- * 能力过滤系统的公共类型。
7
- * FilterOutput 由 filter() 产出,由 instantiate() 消费。
8
- */
9
- export interface FilterOutput {
10
- tools: string[];
11
- subagents: string[];
12
- skills: string[];
13
- mcps: string[];
14
- }
15
- /**
16
- * 排除项黑名单(优先级高于 permissions)。
17
- * 与 permissions 四维对称,用于安全预过滤。
18
- */
19
- export interface ExcludeOptions {
20
- tools?: string[];
21
- skills?: string[];
22
- mcps?: string[];
23
- subagents?: string[];
24
- }
25
- /** filter / buildTools 的过滤选项 */
26
- export interface FilterOptions {
27
- /** 排除项黑名单(优先级高于 permissions) */
28
- exclude?: ExcludeOptions;
29
- }
30
- /**
31
- * Provider — 全局上下文实例 + 能力注册表。
32
- *
33
- * Provider 是 SDK 的全局单例(按需创建),持有:
34
- * - 应用配置(端点、模型等)
35
- * - 文件系统路径(Agent 目录、Skill 目录等)
36
- * - 全局能力注册表(内置工具、用户工具、SubAgent、Skill、MCP)
37
- * - MCP 连接管理器
38
- *
39
- * 创建方式:
40
- * ```ts
41
- * // 推荐:一步完成构造 + 能力发现
42
- * const provider = await Provider.create({ ... });
43
- *
44
- * // 仅构造(能力需后续加载)
45
- * const provider = new Provider({ ... });
46
- * await provider.init();
47
- * ```
48
- *
49
- * 设计原则:
50
- * - Provider 实例一旦创建不可变(路径、配置等不变),refresh 时重新发现能力
51
- * - 各层不直接 import 散装函数,而是通过 Provider 实例获取
52
- */
53
- export declare class Provider {
54
- /** 应用配置(端点、模型等) */
55
- readonly config: AppConfig;
56
- /** 当前工作目录 — 相对路径解析基准,也是 ToolEnv.cwd 的来源 */
57
- readonly cwd: string;
58
- /** 工具环境 — 实例化内置工具时注入 */
59
- readonly env: ToolEnv;
60
- readonly agentsDir: string;
61
- readonly subAgentsPaths: string[];
62
- readonly skillsPaths: string[];
63
- readonly toolsPaths: string[];
64
- readonly mcpPaths: string[];
65
- builtinTools: Tool[];
66
- userTools: Tool[];
67
- subagents: AgentDefinition[];
68
- /** 完整 SkillInfo,含 subAgent 标记,可据此区分枚举 */
69
- skills: SkillInfo[];
70
- /** 完整 MCP 服务器配置 */
71
- mcps: McpServerConfig[];
72
- readonly mcpManager: McpConnectionManager | undefined;
73
- constructor(options: {
74
- config: AppConfig;
75
- agentsDir: string;
76
- subAgentsPaths?: string[];
77
- skillsPaths?: string[];
78
- toolsPaths?: string[];
79
- mcpPaths?: string[];
80
- /** 当前工作目录,默认 process.cwd() */
81
- cwd?: string;
82
- });
83
- /**
84
- * 异步创建 Provider,一步完成构造 + 全局能力发现。
85
- */
86
- static create(options: {
87
- config: AppConfig;
88
- agentsDir: string;
89
- subAgentsPaths?: string[];
90
- skillsPaths?: string[];
91
- toolsPaths?: string[];
92
- mcpPaths?: string[];
93
- /** 当前工作目录,默认 process.cwd() */
94
- cwd?: string;
95
- }): Promise<Provider>;
96
- /**
97
- * 初始化:执行全局能力发现。
98
- */
99
- init(): Promise<void>;
100
- /**
101
- * 按权限过滤候选集,返回过滤后的名称列表。
102
- * 纯名称操作(安全预过滤 + 权限过滤),不涉及 Tool 实例化。
103
- */
104
- filter(definition: AgentDefinition, options?: FilterOptions): FilterOutput;
105
- /**
106
- * 快捷方法:filter + instantiate 一步完成。
107
- */
108
- buildTools(definition: AgentDefinition, options?: FilterOptions): Tool[];
109
- /**
110
- * 将过滤后的名称列表实例化为 Tool 数组。
111
- */
112
- instantiate(filtered: FilterOutput): Tool[];
113
- /**
114
- * 重新执行全局发现(重新扫描文件系统)。
115
- */
116
- refresh(options?: {
117
- silent?: boolean;
118
- }): Promise<void>;
119
- }
@@ -1,218 +0,0 @@
1
- import { McpConnectionManager } from "./McpConnectionManager.js";
2
- import { PermissionEvaluator } from "../capabilities/PermissionEvaluator.js";
3
- import { createLoadSkillTool, createCallSkillSubAgentTool } from "../capabilities/implements/skillTools.js";
4
- import { createLoadMcpTool, createCallMcpTool, createReadMcpResourceTool } from "../capabilities/implements/mcpTools.js";
5
- import { createSubAgentTool } from "../capabilities/implements/subAgentTools.js";
6
- import { discoverBuiltinTools } from "../capabilities/discovery/builtin.js";
7
- import { discoverSubAgents } from "../capabilities/discovery/subagents.js";
8
- import { discoverSkills } from "../capabilities/discovery/skills.js";
9
- import { discoverMcpServers } from "../capabilities/discovery/mcp.js";
10
- import { discoverUserTools } from "../capabilities/discovery/usertools.js";
11
- /**
12
- * Provider — 全局上下文实例 + 能力注册表。
13
- *
14
- * Provider 是 SDK 的全局单例(按需创建),持有:
15
- * - 应用配置(端点、模型等)
16
- * - 文件系统路径(Agent 目录、Skill 目录等)
17
- * - 全局能力注册表(内置工具、用户工具、SubAgent、Skill、MCP)
18
- * - MCP 连接管理器
19
- *
20
- * 创建方式:
21
- * ```ts
22
- * // 推荐:一步完成构造 + 能力发现
23
- * const provider = await Provider.create({ ... });
24
- *
25
- * // 仅构造(能力需后续加载)
26
- * const provider = new Provider({ ... });
27
- * await provider.init();
28
- * ```
29
- *
30
- * 设计原则:
31
- * - Provider 实例一旦创建不可变(路径、配置等不变),refresh 时重新发现能力
32
- * - 各层不直接 import 散装函数,而是通过 Provider 实例获取
33
- */
34
- export class Provider {
35
- /** 应用配置(端点、模型等) */
36
- config;
37
- /** 当前工作目录 — 相对路径解析基准,也是 ToolEnv.cwd 的来源 */
38
- cwd;
39
- /** 工具环境 — 实例化内置工具时注入 */
40
- env;
41
- // ---- 路径 ----
42
- agentsDir;
43
- subAgentsPaths;
44
- skillsPaths;
45
- toolsPaths;
46
- mcpPaths;
47
- // ---- 全局候选集(发现一次,全局复用)----
48
- builtinTools;
49
- userTools;
50
- subagents;
51
- /** 完整 SkillInfo,含 subAgent 标记,可据此区分枚举 */
52
- skills;
53
- /** 完整 MCP 服务器配置 */
54
- mcps;
55
- // ---- MCP ----
56
- mcpManager;
57
- constructor(options) {
58
- this.config = options.config;
59
- this.cwd = options.cwd ?? process.cwd();
60
- this.env = { cwd: this.cwd, config: this.config };
61
- this.agentsDir = options.agentsDir;
62
- this.subAgentsPaths = options.subAgentsPaths ?? [];
63
- this.skillsPaths = options.skillsPaths ?? [];
64
- this.toolsPaths = options.toolsPaths ?? [];
65
- this.mcpPaths = options.mcpPaths ?? [];
66
- this.builtinTools = [];
67
- this.userTools = [];
68
- this.subagents = [];
69
- this.skills = [];
70
- this.mcps = [];
71
- this.mcpManager = this.mcpPaths.length > 0 ? new McpConnectionManager() : undefined;
72
- }
73
- /**
74
- * 异步创建 Provider,一步完成构造 + 全局能力发现。
75
- */
76
- static async create(options) {
77
- const provider = new Provider(options);
78
- await provider.init();
79
- return provider;
80
- }
81
- /**
82
- * 初始化:执行全局能力发现。
83
- */
84
- async init() {
85
- await this.refresh();
86
- }
87
- // ==================================================================
88
- // 能力过滤与实例化
89
- // ==================================================================
90
- /**
91
- * 按权限过滤候选集,返回过滤后的名称列表。
92
- * 纯名称操作(安全预过滤 + 权限过滤),不涉及 Tool 实例化。
93
- */
94
- filter(definition, options) {
95
- const exclude = options?.exclude ?? {};
96
- const excludeTools = new Set(exclude.tools ?? []);
97
- const excludeSkills = new Set(exclude.skills ?? []);
98
- const excludeMcps = new Set(exclude.mcps ?? []);
99
- const excludeSubAgents = new Set(exclude.subagents ?? []);
100
- const safeSubagents = excludeSubAgents.size === 0
101
- ? this.subagents
102
- : this.subagents.filter((def) => !excludeSubAgents.has(def.function?.name ?? ""));
103
- const DYNAMIC_TOOL_NAMES = [
104
- "load_skill",
105
- "call_skill_sub_agent",
106
- "load_mcp",
107
- "call_mcp_tool",
108
- "read_mcp_resource",
109
- ];
110
- const toolNames = [
111
- ...this.builtinTools.map((t) => t.function.name),
112
- ...this.userTools.map((t) => t.function.name),
113
- ...DYNAMIC_TOOL_NAMES,
114
- ].filter((name) => !excludeTools.has(name));
115
- // 工具可用性过滤:调用候选工具的 isAvailable(直接透传完整 app config + agent definition)。
116
- // 工具自行声明可用条件(如依赖图片模型配置、仅视觉模型可用),返回 false 则剔除。
117
- const candidates = [...this.builtinTools, ...this.userTools];
118
- for (let i = toolNames.length - 1; i >= 0; i--) {
119
- const tool = candidates.find((t) => t.function.name === toolNames[i]);
120
- const isAvailable = tool?.isAvailable;
121
- if (isAvailable && !isAvailable(this.config, definition)) {
122
- toolNames.splice(i, 1);
123
- }
124
- }
125
- const evaluator = new PermissionEvaluator(definition.permissions);
126
- const filtered = evaluator.filter({
127
- tools: toolNames,
128
- skills: this.skills
129
- .filter((s) => !excludeSkills.has(s.id))
130
- .map((s) => s.id),
131
- mcps: this.mcps
132
- .filter((m) => !excludeMcps.has(m.id))
133
- .map((m) => m.id),
134
- subagents: safeSubagents.map((d) => d.function.name),
135
- });
136
- return {
137
- tools: filtered.tools,
138
- subagents: filtered.subagents,
139
- skills: filtered.skills,
140
- mcps: filtered.mcps,
141
- };
142
- }
143
- /**
144
- * 快捷方法:filter + instantiate 一步完成。
145
- */
146
- buildTools(definition, options) {
147
- return this.instantiate(this.filter(definition, options));
148
- }
149
- /**
150
- * 将过滤后的名称列表实例化为 Tool 数组。
151
- */
152
- instantiate(filtered) {
153
- const result = [];
154
- const allowedToolNames = new Set(filtered.tools);
155
- // 1. 内置 + 用户工具
156
- for (const t of [...this.builtinTools, ...this.userTools]) {
157
- if (allowedToolNames.has(t.function.name)) {
158
- result.push(t);
159
- }
160
- }
161
- // 2. 筛选候选项(保留完整信息,传给工具函数自行决定如何构建枚举)
162
- const allowedSkillSet = new Set(filtered.skills);
163
- const allowedMcpSet = new Set(filtered.mcps);
164
- const filteredSkills = this.skills.filter((s) => allowedSkillSet.has(s.id));
165
- const filteredMcps = this.mcps.filter((m) => allowedMcpSet.has(m.id));
166
- // 3. 动态工具
167
- if (allowedToolNames.has("load_skill") && filteredSkills.length > 0) {
168
- result.push(createLoadSkillTool(this.skillsPaths, filteredSkills));
169
- }
170
- // call_skill_sub_agent:只要有支持子 Agent 的 skill 就注册
171
- const hasSubAgentSkills = filteredSkills.some((s) => s.subAgent);
172
- if (allowedToolNames.has("call_skill_sub_agent") && hasSubAgentSkills) {
173
- result.push(createCallSkillSubAgentTool(this.skillsPaths, filteredSkills));
174
- }
175
- const mcpManager = this.mcpManager;
176
- if (allowedToolNames.has("load_mcp") && mcpManager && filteredMcps.length > 0) {
177
- result.push(createLoadMcpTool(mcpManager, filteredMcps));
178
- }
179
- if (allowedToolNames.has("call_mcp_tool") && mcpManager) {
180
- result.push(createCallMcpTool(mcpManager));
181
- }
182
- if (allowedToolNames.has("read_mcp_resource") && mcpManager) {
183
- result.push(createReadMcpResourceTool(mcpManager));
184
- }
185
- // 4. SubAgent
186
- const allowedSubagentSet = new Set(filtered.subagents);
187
- for (const def of this.subagents) {
188
- if (!def.function || !allowedSubagentSet.has(def.function.name))
189
- continue;
190
- result.push(createSubAgentTool(def, this));
191
- }
192
- // 5. 去重
193
- return dedupTools(result);
194
- }
195
- /**
196
- * 重新执行全局发现(重新扫描文件系统)。
197
- */
198
- async refresh(options) {
199
- const silent = options?.silent ?? false;
200
- this.builtinTools = discoverBuiltinTools(this.env);
201
- this.userTools = await discoverUserTools(this.toolsPaths, { silent });
202
- this.subagents = await discoverSubAgents(this.subAgentsPaths);
203
- this.skills = await discoverSkills(this.skillsPaths, { silent });
204
- this.mcps = await discoverMcpServers(this.mcpPaths);
205
- }
206
- }
207
- function dedupTools(tools) {
208
- const seen = new Set();
209
- const result = [];
210
- for (let i = tools.length - 1; i >= 0; i--) {
211
- const name = tools[i].function.name;
212
- if (!seen.has(name)) {
213
- seen.add(name);
214
- result.unshift(tools[i]);
215
- }
216
- }
217
- return result;
218
- }
File without changes
File without changes