@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.
- package/CHANGELOG.md +103 -36
- package/README.md +29 -21
- package/README.zh.md +29 -21
- package/dist/{plugin → agent-plugins}/AutoRefreshToolsPlugin.js +2 -2
- package/dist/{plugin → agent-plugins}/UnknownToolHintPlugin.d.ts +7 -5
- package/dist/{plugin → agent-plugins}/UnknownToolHintPlugin.js +9 -5
- package/dist/config/ConfigManager.d.ts +17 -0
- package/dist/config/ConfigManager.js +44 -1
- package/dist/config/constants.js +67 -37
- package/dist/index.d.ts +37 -29
- package/dist/index.js +28 -21
- package/dist/runtime/SdkAgent.d.ts +5 -5
- package/dist/runtime/SdkAgent.js +4 -4
- package/dist/runtime/TaskMigrationService.d.ts +1 -1
- package/dist/runtime/TaskMigrationService.js +2 -2
- package/dist/runtime/createAgent.d.ts +4 -4
- package/dist/runtime/createAgent.js +8 -8
- package/dist/runtime/createModel.d.ts +3 -3
- package/dist/runtime/createModel.js +3 -3
- package/dist/scope/Scope.d.ts +111 -0
- package/dist/scope/Scope.js +165 -0
- package/dist/scope/ScopePlugin.d.ts +51 -0
- package/dist/scope/ScopePlugin.js +1 -0
- package/dist/scope/plugins/allInOne.d.ts +18 -0
- package/dist/scope/plugins/allInOne.js +23 -0
- package/dist/{capabilities/implements → scope/plugins}/builtin/BatchEditTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/BatchEditTool.js +1 -1
- package/dist/scope/plugins/builtin/BuiltinToolsScopePlugin.d.ts +20 -0
- package/dist/scope/plugins/builtin/BuiltinToolsScopePlugin.js +30 -0
- package/dist/{capabilities/implements → scope/plugins}/builtin/CopyTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/CopyTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/CwdTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/CwdTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/DownloadFileTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/DownloadFileTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/EditTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/EditTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/ExecAsyncTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/ExecAsyncTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/ExecTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/ExecTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/ExistTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/ExistTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/FindTextTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/FindTextTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/GenerateImageTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/GenerateImageTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/GlobTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/GlobTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/InspectFileTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/InspectFileTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/LsTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/LsTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/MkdirTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/MkdirTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/ReadFileTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/ReadFileTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/RenameTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/RenameTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/RmTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/RmTool.js +1 -1
- package/dist/{runtime → scope/plugins/builtin}/SdkCallbackTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/SleepTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/SleepTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/ViewImageTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/ViewImageTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/WriteFileTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/WriteFileTool.js +1 -1
- package/dist/{capabilities/discovery/builtin.d.ts → scope/plugins/builtin/discover.d.ts} +1 -1
- package/dist/{capabilities/discovery/builtin.js → scope/plugins/builtin/discover.js} +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/index.d.ts +2 -2
- package/dist/{capabilities/implements → scope/plugins}/builtin/index.js +1 -1
- package/dist/{runtime → scope/plugins/mcp}/McpConnectionManager.d.ts +1 -1
- package/dist/{runtime → scope/plugins/mcp}/McpConnectionManager.js +1 -1
- package/dist/scope/plugins/mcp/McpScopePlugin.d.ts +29 -0
- package/dist/scope/plugins/mcp/McpScopePlugin.js +60 -0
- package/dist/{capabilities/discovery/mcp.d.ts → scope/plugins/mcp/discover.d.ts} +1 -1
- package/dist/{capabilities/discovery/mcp.js → scope/plugins/mcp/discover.js} +1 -1
- package/dist/{capabilities/implements → scope/plugins/mcp}/mcpTools.d.ts +2 -2
- package/dist/{capabilities/implements → scope/plugins/mcp}/mcpTools.js +2 -2
- package/dist/scope/plugins/skills/SkillsScopePlugin.d.ts +28 -0
- package/dist/scope/plugins/skills/SkillsScopePlugin.js +48 -0
- package/dist/{capabilities/discovery/skills.js → scope/plugins/skills/discover.js} +1 -1
- package/dist/{capabilities/implements → scope/plugins/skills}/skillTools.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins/skills}/skillTools.js +3 -3
- package/dist/scope/plugins/subagents/SubAgentsScopePlugin.d.ts +20 -0
- package/dist/scope/plugins/subagents/SubAgentsScopePlugin.js +34 -0
- package/dist/{capabilities/discovery/subagents.d.ts → scope/plugins/subagents/discover.d.ts} +1 -1
- package/dist/{capabilities/implements → scope/plugins/subagents}/subAgentTools.d.ts +3 -3
- package/dist/{capabilities/implements → scope/plugins/subagents}/subAgentTools.js +5 -5
- package/dist/scope/plugins/usertools/UserToolsScopePlugin.d.ts +24 -0
- package/dist/scope/plugins/usertools/UserToolsScopePlugin.js +28 -0
- package/dist/{capabilities/discovery/usertools.js → scope/plugins/usertools/discover.js} +1 -1
- package/dist/types/index.d.ts +23 -2
- package/package.json +3 -3
- package/dist/runtime/Provider.d.ts +0 -119
- package/dist/runtime/Provider.js +0 -218
- /package/dist/{plugin → agent-plugins}/AutoMigratePlugin.d.ts +0 -0
- /package/dist/{plugin → agent-plugins}/AutoMigratePlugin.js +0 -0
- /package/dist/{plugin → agent-plugins}/AutoRefreshToolsPlugin.d.ts +0 -0
- /package/dist/{plugin → agent-plugins}/ContextGuardPlugin.d.ts +0 -0
- /package/dist/{plugin → agent-plugins}/ContextGuardPlugin.js +0 -0
- /package/dist/{capabilities → scope}/PermissionEvaluator.d.ts +0 -0
- /package/dist/{capabilities → scope}/PermissionEvaluator.js +0 -0
- /package/dist/{capabilities → scope}/disclosure.d.ts +0 -0
- /package/dist/{capabilities → scope}/disclosure.js +0 -0
- /package/dist/{runtime → scope/plugins/builtin}/SdkCallbackTool.js +0 -0
- /package/dist/{capabilities/implements → scope/plugins}/builtin/outputGuard.d.ts +0 -0
- /package/dist/{capabilities/implements → scope/plugins}/builtin/outputGuard.js +0 -0
- /package/dist/{capabilities/implements → scope/plugins}/builtin/test-helpers.d.ts +0 -0
- /package/dist/{capabilities/implements → scope/plugins}/builtin/test-helpers.js +0 -0
- /package/dist/{capabilities/discovery/skills.d.ts → scope/plugins/skills/discover.d.ts} +0 -0
- /package/dist/{capabilities/discovery/subagents.js → scope/plugins/subagents/discover.js} +0 -0
- /package/dist/{capabilities/discovery/usertools.d.ts → scope/plugins/usertools/discover.d.ts} +0 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { Tool } from "@ai-zen/agents-core";
|
|
2
|
+
import type { AppConfig, AgentDefinition, ToolEnv } from "../types/index.js";
|
|
3
|
+
import type { ScopePlugin } from "./ScopePlugin.js";
|
|
4
|
+
/**
|
|
5
|
+
* 能力过滤系统的公共类型。
|
|
6
|
+
* FilterOutput 由 filter() 产出,由 instantiate() 消费。
|
|
7
|
+
*/
|
|
8
|
+
export interface FilterOutput {
|
|
9
|
+
tools: string[];
|
|
10
|
+
subagents: string[];
|
|
11
|
+
skills: string[];
|
|
12
|
+
mcps: string[];
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* 排除项黑名单(优先级高于 permissions)。
|
|
16
|
+
* 与 permissions 四维对称,用于安全预过滤。
|
|
17
|
+
*/
|
|
18
|
+
export interface ExcludeOptions {
|
|
19
|
+
tools?: string[];
|
|
20
|
+
skills?: string[];
|
|
21
|
+
mcps?: string[];
|
|
22
|
+
subagents?: string[];
|
|
23
|
+
}
|
|
24
|
+
/** filter / buildTools 的过滤选项 */
|
|
25
|
+
export interface FilterOptions {
|
|
26
|
+
/** 排除项黑名单(优先级高于 permissions) */
|
|
27
|
+
exclude?: ExcludeOptions;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Scope — 全局上下文 + 能力装配编排容器(v1)。
|
|
31
|
+
*
|
|
32
|
+
* Scope 是 SDK 的唯一入口对象,持有:
|
|
33
|
+
* - 应用配置(端点、模型等)
|
|
34
|
+
* - 当前工作目录 cwd(相对路径解析基准,ToolEnv.cwd 的来源)
|
|
35
|
+
* - Agent 定义目录 agentsDir(createAgent 用)
|
|
36
|
+
* - 一组 ScopePlugin(能力插件)
|
|
37
|
+
*
|
|
38
|
+
* 装配方式(插件必须显式注入,Scope 不装任何默认插件):
|
|
39
|
+
* ```ts
|
|
40
|
+
* const scope = new Scope({ config, cwd, agentsDir })
|
|
41
|
+
* .use(...allInOne({ skillsPaths, toolsPaths, subAgentsPaths, mcpPaths }));
|
|
42
|
+
* await scope.init();
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* 设计原则:
|
|
46
|
+
* - Scope 只做编排:三阶段管线骨架(发现 → 过滤 → 实例化)+ 生命周期
|
|
47
|
+
* - 能力来源(内置/用户工具、Skill、MCP、SubAgent)全部疏散到 ScopePlugin,各自自持
|
|
48
|
+
* - Scope 实例一旦创建,config/cwd/agentsDir/env 不变;refresh() 只让插件重新发现
|
|
49
|
+
*/
|
|
50
|
+
export declare class Scope {
|
|
51
|
+
/** 应用配置(端点、模型等) */
|
|
52
|
+
readonly config: AppConfig;
|
|
53
|
+
/** 当前工作目录 — 相对路径解析基准,也是 ToolEnv.cwd 的来源 */
|
|
54
|
+
readonly cwd: string;
|
|
55
|
+
/** 工具环境 — 插件实例化内置工具时注入 */
|
|
56
|
+
readonly env: ToolEnv;
|
|
57
|
+
/** Agent 定义目录(createAgent 用) */
|
|
58
|
+
readonly agentsDir: string;
|
|
59
|
+
/** 已注册的插件 */
|
|
60
|
+
private _plugins;
|
|
61
|
+
/** 是否已 init(init 之后禁止再 use) */
|
|
62
|
+
private _initialized;
|
|
63
|
+
constructor(options: {
|
|
64
|
+
config: AppConfig;
|
|
65
|
+
agentsDir: string;
|
|
66
|
+
cwd?: string;
|
|
67
|
+
});
|
|
68
|
+
/** 已注册插件(只读视图) */
|
|
69
|
+
get plugins(): readonly ScopePlugin[];
|
|
70
|
+
/**
|
|
71
|
+
* 注册能力插件(链式,可变参数)。
|
|
72
|
+
*
|
|
73
|
+
* 支持两种用法:
|
|
74
|
+
* scope.use(plugin)
|
|
75
|
+
* scope.use(...allInOne({ ... })) // 展开批量注册
|
|
76
|
+
*
|
|
77
|
+
* 必须在 init() 之前调用;init() 之后再 use() 会抛错。
|
|
78
|
+
*/
|
|
79
|
+
use(...plugins: ScopePlugin[]): this;
|
|
80
|
+
/** 按 id 查找插件(供外部代码/其它插件查询插件状态) */
|
|
81
|
+
getPluginById<T extends ScopePlugin>(id: string): T | undefined;
|
|
82
|
+
/**
|
|
83
|
+
* 初始化:执行首次全局能力发现(遍历插件 discover()),随后锁定插件注册。
|
|
84
|
+
*/
|
|
85
|
+
init(): Promise<void>;
|
|
86
|
+
/**
|
|
87
|
+
* 重新执行全局发现(遍历插件 discover())。
|
|
88
|
+
*/
|
|
89
|
+
refresh(options?: {
|
|
90
|
+
silent?: boolean;
|
|
91
|
+
}): Promise<void>;
|
|
92
|
+
/**
|
|
93
|
+
* 释放:逆序调用各插件 dispose()(如 MCP 断开所有连接)。与 init() 对称。
|
|
94
|
+
*/
|
|
95
|
+
dispose(): Promise<void>;
|
|
96
|
+
/**
|
|
97
|
+
* 按权限过滤候选集,返回过滤后的名称列表。
|
|
98
|
+
* 汇总各插件 candidates() → 应用 exclude 安全预过滤 → 四维权限过滤。
|
|
99
|
+
* 纯名称操作,不涉及 Tool 实例化。
|
|
100
|
+
*/
|
|
101
|
+
filter(definition: AgentDefinition, options?: FilterOptions): FilterOutput;
|
|
102
|
+
/**
|
|
103
|
+
* 快捷方法:filter + instantiate 一步完成。
|
|
104
|
+
*/
|
|
105
|
+
buildTools(definition: AgentDefinition, options?: FilterOptions): Tool[];
|
|
106
|
+
/**
|
|
107
|
+
* 将过滤后的名称列表实例化为 Tool 数组。
|
|
108
|
+
* 遍历各插件 instantiate(),收集后去重(后注册覆盖先注册)。
|
|
109
|
+
*/
|
|
110
|
+
instantiate(filtered: FilterOutput): Tool[];
|
|
111
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { PermissionEvaluator } from "./PermissionEvaluator.js";
|
|
2
|
+
/**
|
|
3
|
+
* Scope — 全局上下文 + 能力装配编排容器(v1)。
|
|
4
|
+
*
|
|
5
|
+
* Scope 是 SDK 的唯一入口对象,持有:
|
|
6
|
+
* - 应用配置(端点、模型等)
|
|
7
|
+
* - 当前工作目录 cwd(相对路径解析基准,ToolEnv.cwd 的来源)
|
|
8
|
+
* - Agent 定义目录 agentsDir(createAgent 用)
|
|
9
|
+
* - 一组 ScopePlugin(能力插件)
|
|
10
|
+
*
|
|
11
|
+
* 装配方式(插件必须显式注入,Scope 不装任何默认插件):
|
|
12
|
+
* ```ts
|
|
13
|
+
* const scope = new Scope({ config, cwd, agentsDir })
|
|
14
|
+
* .use(...allInOne({ skillsPaths, toolsPaths, subAgentsPaths, mcpPaths }));
|
|
15
|
+
* await scope.init();
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* 设计原则:
|
|
19
|
+
* - Scope 只做编排:三阶段管线骨架(发现 → 过滤 → 实例化)+ 生命周期
|
|
20
|
+
* - 能力来源(内置/用户工具、Skill、MCP、SubAgent)全部疏散到 ScopePlugin,各自自持
|
|
21
|
+
* - Scope 实例一旦创建,config/cwd/agentsDir/env 不变;refresh() 只让插件重新发现
|
|
22
|
+
*/
|
|
23
|
+
export class Scope {
|
|
24
|
+
/** 应用配置(端点、模型等) */
|
|
25
|
+
config;
|
|
26
|
+
/** 当前工作目录 — 相对路径解析基准,也是 ToolEnv.cwd 的来源 */
|
|
27
|
+
cwd;
|
|
28
|
+
/** 工具环境 — 插件实例化内置工具时注入 */
|
|
29
|
+
env;
|
|
30
|
+
/** Agent 定义目录(createAgent 用) */
|
|
31
|
+
agentsDir;
|
|
32
|
+
/** 已注册的插件 */
|
|
33
|
+
_plugins = [];
|
|
34
|
+
/** 是否已 init(init 之后禁止再 use) */
|
|
35
|
+
_initialized = false;
|
|
36
|
+
constructor(options) {
|
|
37
|
+
this.config = options.config;
|
|
38
|
+
this.cwd = options.cwd ?? process.cwd();
|
|
39
|
+
this.env = { cwd: this.cwd, config: this.config };
|
|
40
|
+
this.agentsDir = options.agentsDir;
|
|
41
|
+
}
|
|
42
|
+
// ==================================================================
|
|
43
|
+
// 插件注册与生命周期
|
|
44
|
+
// ==================================================================
|
|
45
|
+
/** 已注册插件(只读视图) */
|
|
46
|
+
get plugins() {
|
|
47
|
+
return this._plugins;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* 注册能力插件(链式,可变参数)。
|
|
51
|
+
*
|
|
52
|
+
* 支持两种用法:
|
|
53
|
+
* scope.use(plugin)
|
|
54
|
+
* scope.use(...allInOne({ ... })) // 展开批量注册
|
|
55
|
+
*
|
|
56
|
+
* 必须在 init() 之前调用;init() 之后再 use() 会抛错。
|
|
57
|
+
*/
|
|
58
|
+
use(...plugins) {
|
|
59
|
+
if (this._initialized) {
|
|
60
|
+
throw new Error("Scope.use() 必须在 init() 之前调用");
|
|
61
|
+
}
|
|
62
|
+
for (const plugin of plugins) {
|
|
63
|
+
this._plugins.push(plugin);
|
|
64
|
+
}
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
/** 按 id 查找插件(供外部代码/其它插件查询插件状态) */
|
|
68
|
+
getPluginById(id) {
|
|
69
|
+
return this._plugins.find((p) => p.id === id);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* 初始化:执行首次全局能力发现(遍历插件 discover()),随后锁定插件注册。
|
|
73
|
+
*/
|
|
74
|
+
async init() {
|
|
75
|
+
await this.refresh();
|
|
76
|
+
this._initialized = true;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* 重新执行全局发现(遍历插件 discover())。
|
|
80
|
+
*/
|
|
81
|
+
async refresh(options) {
|
|
82
|
+
const opts = { silent: options?.silent ?? false };
|
|
83
|
+
for (const plugin of this._plugins) {
|
|
84
|
+
await plugin.discover?.(this, opts);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* 释放:逆序调用各插件 dispose()(如 MCP 断开所有连接)。与 init() 对称。
|
|
89
|
+
*/
|
|
90
|
+
async dispose() {
|
|
91
|
+
for (let i = this._plugins.length - 1; i >= 0; i--) {
|
|
92
|
+
await this._plugins[i].dispose?.();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// ==================================================================
|
|
96
|
+
// 能力过滤与实例化
|
|
97
|
+
// ==================================================================
|
|
98
|
+
/**
|
|
99
|
+
* 按权限过滤候选集,返回过滤后的名称列表。
|
|
100
|
+
* 汇总各插件 candidates() → 应用 exclude 安全预过滤 → 四维权限过滤。
|
|
101
|
+
* 纯名称操作,不涉及 Tool 实例化。
|
|
102
|
+
*/
|
|
103
|
+
filter(definition, options) {
|
|
104
|
+
const exclude = options?.exclude ?? {};
|
|
105
|
+
const excludeTools = new Set(exclude.tools ?? []);
|
|
106
|
+
const excludeSkills = new Set(exclude.skills ?? []);
|
|
107
|
+
const excludeMcps = new Set(exclude.mcps ?? []);
|
|
108
|
+
const excludeSubAgents = new Set(exclude.subagents ?? []);
|
|
109
|
+
// 1. 汇总各插件候选(插件已在 candidates 内完成自身可用性判断)
|
|
110
|
+
const tools = new Set();
|
|
111
|
+
const skills = new Set();
|
|
112
|
+
const mcps = new Set();
|
|
113
|
+
const subagents = new Set();
|
|
114
|
+
for (const plugin of this._plugins) {
|
|
115
|
+
const c = plugin.candidates(this, definition);
|
|
116
|
+
c.tools?.forEach((n) => tools.add(n));
|
|
117
|
+
c.skills?.forEach((n) => skills.add(n));
|
|
118
|
+
c.mcps?.forEach((n) => mcps.add(n));
|
|
119
|
+
c.subagents?.forEach((n) => subagents.add(n));
|
|
120
|
+
}
|
|
121
|
+
// 2 + 3. exclude 预过滤 + 四维权限过滤
|
|
122
|
+
const evaluator = new PermissionEvaluator(definition.permissions);
|
|
123
|
+
const filtered = evaluator.filter({
|
|
124
|
+
tools: [...tools].filter((n) => !excludeTools.has(n)),
|
|
125
|
+
skills: [...skills].filter((n) => !excludeSkills.has(n)),
|
|
126
|
+
mcps: [...mcps].filter((n) => !excludeMcps.has(n)),
|
|
127
|
+
subagents: [...subagents].filter((n) => !excludeSubAgents.has(n)),
|
|
128
|
+
});
|
|
129
|
+
return {
|
|
130
|
+
tools: filtered.tools,
|
|
131
|
+
subagents: filtered.subagents,
|
|
132
|
+
skills: filtered.skills,
|
|
133
|
+
mcps: filtered.mcps,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* 快捷方法:filter + instantiate 一步完成。
|
|
138
|
+
*/
|
|
139
|
+
buildTools(definition, options) {
|
|
140
|
+
return this.instantiate(this.filter(definition, options));
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* 将过滤后的名称列表实例化为 Tool 数组。
|
|
144
|
+
* 遍历各插件 instantiate(),收集后去重(后注册覆盖先注册)。
|
|
145
|
+
*/
|
|
146
|
+
instantiate(filtered) {
|
|
147
|
+
const result = [];
|
|
148
|
+
for (const plugin of this._plugins) {
|
|
149
|
+
result.push(...plugin.instantiate(this, filtered));
|
|
150
|
+
}
|
|
151
|
+
return dedupTools(result);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
function dedupTools(tools) {
|
|
155
|
+
const seen = new Set();
|
|
156
|
+
const result = [];
|
|
157
|
+
for (let i = tools.length - 1; i >= 0; i--) {
|
|
158
|
+
const name = tools[i].function.name;
|
|
159
|
+
if (!seen.has(name)) {
|
|
160
|
+
seen.add(name);
|
|
161
|
+
result.unshift(tools[i]);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return result;
|
|
165
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
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
|
+
/**
|
|
5
|
+
* 插件向四维权限体系贡献的候选名称(任意子集)。
|
|
6
|
+
*/
|
|
7
|
+
export interface ScopeCandidates {
|
|
8
|
+
tools?: string[];
|
|
9
|
+
skills?: string[];
|
|
10
|
+
mcps?: string[];
|
|
11
|
+
subagents?: string[];
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* ScopePlugin — 能力插件契约(v1 核心扩展点)。
|
|
15
|
+
*
|
|
16
|
+
* 一个插件**自持一类能力**的完整生命周期:自己发现、自己持有发现结果、
|
|
17
|
+
* 自己声明候选名、自己按过滤结果实例化工具、自己负责释放。
|
|
18
|
+
* `Scope` 只做编排,不感知任何具体能力的发现状态。
|
|
19
|
+
*
|
|
20
|
+
* 与 `AgentPlugin`(core,挂在 Agent 运行时 send/loop/toolcall)不同,
|
|
21
|
+
* `ScopePlugin` 挂在**能力装配管线**(discover/filter/instantiate)。二者互不相关。
|
|
22
|
+
*
|
|
23
|
+
* 生命周期与三阶段管线的对应:
|
|
24
|
+
* discover —— 阶段 1 · 发现
|
|
25
|
+
* candidates —— 阶段 2 · 候选(供权限过滤)
|
|
26
|
+
* instantiate —— 阶段 3 · 实例化
|
|
27
|
+
* dispose —— 释放
|
|
28
|
+
*/
|
|
29
|
+
export interface ScopePlugin {
|
|
30
|
+
/** 唯一标识,如 "builtin" / "user-tools" / "skills" / "mcp" / "subagents" */
|
|
31
|
+
readonly id: string;
|
|
32
|
+
/**
|
|
33
|
+
* 阶段 1 · 发现:扫描来源、刷新自身状态(每次内部重新扫描,不跨 refresh 累积)。
|
|
34
|
+
* 相对路径应在此基于 `scope.cwd` 解析。
|
|
35
|
+
*/
|
|
36
|
+
discover?(scope: Scope, options: {
|
|
37
|
+
silent?: boolean;
|
|
38
|
+
}): Promise<void> | void;
|
|
39
|
+
/**
|
|
40
|
+
* 阶段 2 · 候选:返回本插件向四维贡献的候选名。
|
|
41
|
+
* 可结合 `definition` 做可用性判断(如内置工具的 isAvailable)。同步返回。
|
|
42
|
+
*/
|
|
43
|
+
candidates(scope: Scope, definition: AgentDefinition): ScopeCandidates;
|
|
44
|
+
/**
|
|
45
|
+
* 阶段 3 · 实例化:按过滤后的允许名产出 Tool 实例。
|
|
46
|
+
* `filtered` 为 `Scope.filter()` 的产物(四维允许名)。
|
|
47
|
+
*/
|
|
48
|
+
instantiate(scope: Scope, filtered: FilterOutput): Tool[];
|
|
49
|
+
/** 可选 · 释放:逆序于注册顺序调用。如 MCP 断开所有连接。 */
|
|
50
|
+
dispose?(): Promise<void> | void;
|
|
51
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ScopePlugin } from "../ScopePlugin.js";
|
|
2
|
+
/** allInOne 的装配选项:各类能力来源的路径(绝对或相对 scope.cwd) */
|
|
3
|
+
export interface AllInOneOptions {
|
|
4
|
+
skillsPaths?: string[];
|
|
5
|
+
toolsPaths?: string[];
|
|
6
|
+
subAgentsPaths?: string[];
|
|
7
|
+
mcpPaths?: string[];
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* 标准能力插件工厂 —— 恒返回 5 个标准插件("全家桶")。
|
|
11
|
+
*
|
|
12
|
+
* 顺序即优先级(去重时后注册覆盖先注册):
|
|
13
|
+
* builtin → user-tools → skills → mcp → subagents
|
|
14
|
+
* 因此用户工具可覆盖同名内置工具(与 v0 一致)。
|
|
15
|
+
*
|
|
16
|
+
* 想裁剪/替换某一类,改用手工逐个 `new XxxScopePlugin(...)` 后 `.use()`。
|
|
17
|
+
*/
|
|
18
|
+
export declare function allInOne(options?: AllInOneOptions): ScopePlugin[];
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { BuiltinToolsScopePlugin } from "./builtin/BuiltinToolsScopePlugin.js";
|
|
2
|
+
import { UserToolsScopePlugin } from "./usertools/UserToolsScopePlugin.js";
|
|
3
|
+
import { SkillsScopePlugin } from "./skills/SkillsScopePlugin.js";
|
|
4
|
+
import { McpScopePlugin } from "./mcp/McpScopePlugin.js";
|
|
5
|
+
import { SubAgentsScopePlugin } from "./subagents/SubAgentsScopePlugin.js";
|
|
6
|
+
/**
|
|
7
|
+
* 标准能力插件工厂 —— 恒返回 5 个标准插件("全家桶")。
|
|
8
|
+
*
|
|
9
|
+
* 顺序即优先级(去重时后注册覆盖先注册):
|
|
10
|
+
* builtin → user-tools → skills → mcp → subagents
|
|
11
|
+
* 因此用户工具可覆盖同名内置工具(与 v0 一致)。
|
|
12
|
+
*
|
|
13
|
+
* 想裁剪/替换某一类,改用手工逐个 `new XxxScopePlugin(...)` 后 `.use()`。
|
|
14
|
+
*/
|
|
15
|
+
export function allInOne(options) {
|
|
16
|
+
return [
|
|
17
|
+
new BuiltinToolsScopePlugin(),
|
|
18
|
+
new UserToolsScopePlugin({ paths: options?.toolsPaths }),
|
|
19
|
+
new SkillsScopePlugin({ paths: options?.skillsPaths }),
|
|
20
|
+
new McpScopePlugin({ paths: options?.mcpPaths }),
|
|
21
|
+
new SubAgentsScopePlugin({ paths: options?.subAgentsPaths }),
|
|
22
|
+
];
|
|
23
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
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
|
+
* 内置工具插件 —— 20 个内置工具类(BUILTIN_TOOL_CLASSES)。
|
|
7
|
+
*
|
|
8
|
+
* 发现层零过滤;可用性由各工具 `isAvailable(config, definition)` 自陈,
|
|
9
|
+
* 在本插件 `candidates()` 阶段按声明剔除(如 GenerateImageTool 依赖 defaultImageModel、
|
|
10
|
+
* ViewImageTool 仅视觉模型可用)。
|
|
11
|
+
*/
|
|
12
|
+
export declare class BuiltinToolsScopePlugin implements ScopePlugin {
|
|
13
|
+
static readonly ID = "builtin";
|
|
14
|
+
readonly id = "builtin";
|
|
15
|
+
/** 发现结果自持(每次 discover 重新实例化) */
|
|
16
|
+
private tools;
|
|
17
|
+
discover(scope: Scope): void;
|
|
18
|
+
candidates(scope: Scope, definition: AgentDefinition): ScopeCandidates;
|
|
19
|
+
instantiate(_scope: Scope, filtered: FilterOutput): Tool[];
|
|
20
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { discoverBuiltinTools } from "./discover.js";
|
|
2
|
+
/**
|
|
3
|
+
* 内置工具插件 —— 20 个内置工具类(BUILTIN_TOOL_CLASSES)。
|
|
4
|
+
*
|
|
5
|
+
* 发现层零过滤;可用性由各工具 `isAvailable(config, definition)` 自陈,
|
|
6
|
+
* 在本插件 `candidates()` 阶段按声明剔除(如 GenerateImageTool 依赖 defaultImageModel、
|
|
7
|
+
* ViewImageTool 仅视觉模型可用)。
|
|
8
|
+
*/
|
|
9
|
+
export class BuiltinToolsScopePlugin {
|
|
10
|
+
static ID = "builtin";
|
|
11
|
+
id = BuiltinToolsScopePlugin.ID;
|
|
12
|
+
/** 发现结果自持(每次 discover 重新实例化) */
|
|
13
|
+
tools = [];
|
|
14
|
+
discover(scope) {
|
|
15
|
+
this.tools = discoverBuiltinTools(scope.env);
|
|
16
|
+
}
|
|
17
|
+
candidates(scope, definition) {
|
|
18
|
+
const tools = this.tools
|
|
19
|
+
.filter((t) => {
|
|
20
|
+
const isAvailable = t?.isAvailable;
|
|
21
|
+
return isAvailable ? isAvailable(scope.config, definition) : true;
|
|
22
|
+
})
|
|
23
|
+
.map((t) => t.function.name);
|
|
24
|
+
return { tools };
|
|
25
|
+
}
|
|
26
|
+
instantiate(_scope, filtered) {
|
|
27
|
+
const allow = new Set(filtered.tools);
|
|
28
|
+
return this.tools.filter((t) => allow.has(t.function.name));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SdkCallbackTool } from "
|
|
1
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
3
|
import type { AgentNS } from "@ai-zen/agents-core";
|
|
4
4
|
export declare class CopyTool extends SdkCallbackTool {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SdkCallbackTool } from "
|
|
1
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
3
|
import type { AgentNS } from "@ai-zen/agents-core";
|
|
4
4
|
export declare class CwdTool extends SdkCallbackTool {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SdkCallbackTool } from "
|
|
1
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
3
|
import type { AgentNS, ToolCallContext } from "@ai-zen/agents-core";
|
|
4
4
|
export declare class DownloadFileTool extends SdkCallbackTool {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as fsp from "fs/promises";
|
|
2
2
|
import * as path from "path";
|
|
3
|
-
import { SdkCallbackTool } from "
|
|
3
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
4
4
|
export class DownloadFileTool extends SdkCallbackTool {
|
|
5
5
|
function = {
|
|
6
6
|
name: "downloadFile",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SdkCallbackTool } from "
|
|
1
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
3
|
import type { AgentNS } from "@ai-zen/agents-core";
|
|
4
4
|
export declare class EditTool extends SdkCallbackTool {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SdkCallbackTool } from "
|
|
1
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
3
|
import type { AgentNS } from "@ai-zen/agents-core";
|
|
4
4
|
export declare class ExecAsyncTool extends SdkCallbackTool {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SdkCallbackTool } from "
|
|
1
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
3
|
import type { AgentNS, ToolCallContext } from "@ai-zen/agents-core";
|
|
4
4
|
export declare class ExecTool extends SdkCallbackTool {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { exec } from "child_process";
|
|
2
|
-
import { SdkCallbackTool } from "
|
|
2
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
3
3
|
import { DEFAULT_MAX_TOOL_OUTPUT, guardOutput, headTailPreview } from "./outputGuard.js";
|
|
4
4
|
export class ExecTool extends SdkCallbackTool {
|
|
5
5
|
function = {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SdkCallbackTool } from "
|
|
1
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
3
|
import type { AgentNS } from "@ai-zen/agents-core";
|
|
4
4
|
export declare class ExistTool extends SdkCallbackTool {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SdkCallbackTool } from "
|
|
1
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
3
|
import type { AgentNS, ToolCallContext } from "@ai-zen/agents-core";
|
|
4
4
|
export declare class FindTextTool extends SdkCallbackTool {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as fsp from "fs/promises";
|
|
2
2
|
import * as path from "path";
|
|
3
|
-
import { SdkCallbackTool } from "
|
|
3
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
4
4
|
import { DEFAULT_MAX_TOOL_OUTPUT, guardOutput, headPreview } from "./outputGuard.js";
|
|
5
5
|
/** 默认总匹配数上限 */
|
|
6
6
|
const DEFAULT_MAX_MATCHES = 200;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { AgentNS, ToolCallContext } from "@ai-zen/agents-core";
|
|
2
|
-
import { SdkCallbackTool } from "
|
|
2
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
3
3
|
import type { AgentDefinition, AppConfig, ToolEnv } from "../../../types/index.js";
|
|
4
4
|
/**
|
|
5
5
|
* 根据文字描述生成图片。**统一返回字符串**(JSON,含图片 URL 列表):
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SdkCallbackTool } from "
|
|
1
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
3
|
import type { AgentNS, ToolCallContext } from "@ai-zen/agents-core";
|
|
4
4
|
export declare class GlobTool extends SdkCallbackTool {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as fsp from "fs/promises";
|
|
2
|
-
import { SdkCallbackTool } from "
|
|
2
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
3
3
|
import { DEFAULT_MAX_TOOL_OUTPUT, guardOutput, headPreview } from "./outputGuard.js";
|
|
4
4
|
export class GlobTool extends SdkCallbackTool {
|
|
5
5
|
function = {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as fsp from "fs/promises";
|
|
2
2
|
import { createReadStream } from "fs";
|
|
3
3
|
import { StringDecoder } from "string_decoder";
|
|
4
|
-
import { SdkCallbackTool } from "
|
|
4
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
5
5
|
import { DEFAULT_MAX_TOOL_OUTPUT } from "./outputGuard.js";
|
|
6
6
|
/**
|
|
7
7
|
* 勘察文件结构概况(行数、每行列数等),不做内容读取。
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SdkCallbackTool } from "
|
|
1
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
3
|
import type { AgentNS } from "@ai-zen/agents-core";
|
|
4
4
|
export declare class LsTool extends SdkCallbackTool {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as fsp from "fs/promises";
|
|
2
|
-
import { SdkCallbackTool } from "
|
|
2
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
3
3
|
import { DEFAULT_MAX_TOOL_OUTPUT, guardOutput, headPreview } from "./outputGuard.js";
|
|
4
4
|
export class LsTool extends SdkCallbackTool {
|
|
5
5
|
function = {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SdkCallbackTool } from "
|
|
1
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
3
|
import type { AgentNS } from "@ai-zen/agents-core";
|
|
4
4
|
export declare class MkdirTool extends SdkCallbackTool {
|