@ai-zen/agents-sdk 0.12.0 → 1.0.0-alpha.1

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 (114) hide show
  1. package/CHANGELOG.md +103 -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/config/ConfigManager.d.ts +17 -0
  8. package/dist/config/ConfigManager.js +44 -1
  9. package/dist/config/constants.js +67 -37
  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 +1 -1
  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 +1 -1
  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 +1 -1
  45. package/dist/{capabilities/implements → scope/plugins}/builtin/FindTextTool.js +1 -1
  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 +1 -1
  49. package/dist/{capabilities/implements → scope/plugins}/builtin/GlobTool.js +1 -1
  50. package/dist/{capabilities/implements → scope/plugins}/builtin/InspectFileTool.d.ts +1 -1
  51. package/dist/{capabilities/implements → scope/plugins}/builtin/InspectFileTool.js +1 -1
  52. package/dist/{capabilities/implements → scope/plugins}/builtin/LsTool.d.ts +1 -1
  53. package/dist/{capabilities/implements → scope/plugins}/builtin/LsTool.js +1 -1
  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 +1 -1
  57. package/dist/{capabilities/implements → scope/plugins}/builtin/ReadFileTool.js +1 -1
  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 +2 -2
  72. package/dist/{capabilities/implements → scope/plugins}/builtin/index.js +1 -1
  73. package/dist/{runtime → scope/plugins/mcp}/McpConnectionManager.d.ts +1 -1
  74. package/dist/{runtime → scope/plugins/mcp}/McpConnectionManager.js +1 -1
  75. package/dist/scope/plugins/mcp/McpScopePlugin.d.ts +29 -0
  76. package/dist/scope/plugins/mcp/McpScopePlugin.js +60 -0
  77. package/dist/{capabilities/discovery/mcp.d.ts → scope/plugins/mcp/discover.d.ts} +1 -1
  78. package/dist/{capabilities/discovery/mcp.js → scope/plugins/mcp/discover.js} +1 -1
  79. package/dist/{capabilities/implements → scope/plugins/mcp}/mcpTools.d.ts +2 -2
  80. package/dist/{capabilities/implements → scope/plugins/mcp}/mcpTools.js +2 -2
  81. package/dist/scope/plugins/skills/SkillsScopePlugin.d.ts +28 -0
  82. package/dist/scope/plugins/skills/SkillsScopePlugin.js +48 -0
  83. package/dist/{capabilities/discovery/skills.js → scope/plugins/skills/discover.js} +1 -1
  84. package/dist/{capabilities/implements → scope/plugins/skills}/skillTools.d.ts +1 -1
  85. package/dist/{capabilities/implements → scope/plugins/skills}/skillTools.js +3 -3
  86. package/dist/scope/plugins/subagents/SubAgentsScopePlugin.d.ts +20 -0
  87. package/dist/scope/plugins/subagents/SubAgentsScopePlugin.js +34 -0
  88. package/dist/{capabilities/discovery/subagents.d.ts → scope/plugins/subagents/discover.d.ts} +1 -1
  89. package/dist/{capabilities/implements → scope/plugins/subagents}/subAgentTools.d.ts +3 -3
  90. package/dist/{capabilities/implements → scope/plugins/subagents}/subAgentTools.js +5 -5
  91. package/dist/scope/plugins/usertools/UserToolsScopePlugin.d.ts +24 -0
  92. package/dist/scope/plugins/usertools/UserToolsScopePlugin.js +28 -0
  93. package/dist/{capabilities/discovery/usertools.js → scope/plugins/usertools/discover.js} +1 -1
  94. package/dist/types/index.d.ts +23 -2
  95. package/package.json +3 -3
  96. package/dist/runtime/Provider.d.ts +0 -119
  97. package/dist/runtime/Provider.js +0 -218
  98. /package/dist/{plugin → agent-plugins}/AutoMigratePlugin.d.ts +0 -0
  99. /package/dist/{plugin → agent-plugins}/AutoMigratePlugin.js +0 -0
  100. /package/dist/{plugin → agent-plugins}/AutoRefreshToolsPlugin.d.ts +0 -0
  101. /package/dist/{plugin → agent-plugins}/ContextGuardPlugin.d.ts +0 -0
  102. /package/dist/{plugin → agent-plugins}/ContextGuardPlugin.js +0 -0
  103. /package/dist/{capabilities → scope}/PermissionEvaluator.d.ts +0 -0
  104. /package/dist/{capabilities → scope}/PermissionEvaluator.js +0 -0
  105. /package/dist/{capabilities → scope}/disclosure.d.ts +0 -0
  106. /package/dist/{capabilities → scope}/disclosure.js +0 -0
  107. /package/dist/{runtime → scope/plugins/builtin}/SdkCallbackTool.js +0 -0
  108. /package/dist/{capabilities/implements → scope/plugins}/builtin/outputGuard.d.ts +0 -0
  109. /package/dist/{capabilities/implements → scope/plugins}/builtin/outputGuard.js +0 -0
  110. /package/dist/{capabilities/implements → scope/plugins}/builtin/test-helpers.d.ts +0 -0
  111. /package/dist/{capabilities/implements → scope/plugins}/builtin/test-helpers.js +0 -0
  112. /package/dist/{capabilities/discovery/skills.d.ts → scope/plugins/skills/discover.d.ts} +0 -0
  113. /package/dist/{capabilities/discovery/subagents.js → scope/plugins/subagents/discover.js} +0 -0
  114. /package/dist/{capabilities/discovery/usertools.d.ts → scope/plugins/usertools/discover.d.ts} +0 -0
@@ -26,13 +26,27 @@ export interface Model {
26
26
  name: string;
27
27
  endpointId: string;
28
28
  modelName?: string;
29
+ /**
30
+ * 任务迁移(交接)触发阈值 —— **不是**模型的真实上下文窗口。
31
+ *
32
+ * AutoMigratePlugin 不估算、不学习:只在 API 响应返回后,用 `usage.prompt_tokens`
33
+ * 与该值比较,超过即触发交接迁移。建议取模型真实窗口的约 25%,为 response 留出空间。
34
+ */
29
35
  maxContextTokens: number;
36
+ /** 旧版字符数阈值(兼容迁移) */
30
37
  maxContextChars?: number;
31
38
  defaultParams?: Record<string, unknown>;
32
39
  /** 是否支持图片输入(视觉模型)。看图工具(viewImage)仅对视觉模型启用;generateImage 是否返回图片内容块也据此判断 */
33
40
  vision?: boolean;
34
41
  description?: string;
35
42
  version?: number;
43
+ /**
44
+ * 是否用户自定义。
45
+ *
46
+ * 为 true 时 SDK 初始化不会用出厂定义替换该模型;缺省(或 false)视为出厂托管,
47
+ * 初始化时会与 DEFAULT_APP_CONFIG.models 同步(出厂清单为准)。
48
+ */
49
+ custom?: boolean;
36
50
  }
37
51
  /** Agent 定义,有 function 字段时为 SubAgent */
38
52
  export interface AgentDefinition {
@@ -64,6 +78,13 @@ export interface ImageModel {
64
78
  defaultSize?: string;
65
79
  defaultQuality?: string;
66
80
  version?: number;
81
+ /**
82
+ * 是否用户自定义。
83
+ *
84
+ * 为 true 时 SDK 初始化不会用出厂定义替换该模型;缺省(或 false)视为出厂托管,
85
+ * 初始化时会与 DEFAULT_APP_CONFIG.imageModels 同步(出厂清单为准)。
86
+ */
87
+ custom?: boolean;
67
88
  }
68
89
  /** config.json 结构 */
69
90
  export interface AppConfig {
@@ -88,9 +109,9 @@ export interface AppConfig {
88
109
  maxToolOutput?: number;
89
110
  }
90
111
  /**
91
- * 工具环境 — Provider 在实例化内置工具时注入的环境信息。
112
+ * 工具环境 — Scope 的能力插件在实例化内置工具时注入的环境信息。
92
113
  *
93
- * cwd 是相对路径解析的基准(每个 Provider 对应一个工作目录);
114
+ * cwd 是相对路径解析的基准(每个 Scope 对应一个工作目录);
94
115
  * config 供需要模型/端点配置的工具(如 generateImage)使用。
95
116
  */
96
117
  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.1",
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