@ai-zen/agents-sdk 0.2.8 → 0.3.2
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 +46 -0
- package/README.md +6 -7
- package/dist/capabilities/discovery/mcp.d.ts +1 -1
- package/dist/capabilities/discovery/mcp.js +8 -4
- package/dist/capabilities/discovery/skills.d.ts +2 -2
- package/dist/capabilities/discovery/skills.js +23 -11
- package/dist/capabilities/discovery/subagents.d.ts +1 -1
- package/dist/capabilities/discovery/subagents.js +10 -5
- package/dist/capabilities/discovery/usertools.d.ts +5 -8
- package/dist/capabilities/discovery/usertools.js +42 -68
- package/dist/capabilities/implements/skillTools.d.ts +2 -2
- package/dist/capabilities/implements/skillTools.js +7 -7
- package/dist/capabilities/implements/subAgentTools.d.ts +1 -2
- package/dist/capabilities/implements/subAgentTools.js +4 -8
- package/dist/config/ConfigManager.d.ts +8 -8
- package/dist/config/ConfigManager.js +66 -35
- package/dist/config/constants.js +9 -3
- package/dist/crud/DraftRepository.d.ts +3 -3
- package/dist/crud/DraftRepository.js +22 -11
- package/dist/index.d.ts +1 -2
- package/dist/index.js +0 -1
- package/dist/plugin/AutoDraftPlugin.d.ts +1 -1
- package/dist/plugin/AutoDraftPlugin.js +10 -6
- package/dist/plugin/AutoRefreshToolsPlugin.js +2 -4
- package/dist/runtime/Provider.d.ts +84 -10
- package/dist/runtime/Provider.js +172 -12
- package/dist/runtime/SdkAgent.d.ts +8 -6
- package/dist/runtime/SdkAgent.js +18 -4
- package/dist/runtime/createAgent.d.ts +4 -5
- package/dist/runtime/createAgent.js +7 -24
- package/dist/runtime/createModel.d.ts +4 -7
- package/dist/runtime/createModel.js +4 -6
- package/dist/shared/EntityRepository.d.ts +4 -4
- package/dist/shared/EntityRepository.js +31 -15
- package/package.json +2 -2
- package/dist/capabilities/Capabilities.d.ts +0 -73
- package/dist/capabilities/Capabilities.js +0 -163
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
import { PermissionEvaluator } from "./PermissionEvaluator.js";
|
|
2
|
-
import { createLoadSkillTool, createCallSkillSubAgentTool } from "./implements/skillTools.js";
|
|
3
|
-
import { createLoadMcpTool, createCallMcpTool, createReadMcpResourceTool } from "./implements/mcpTools.js";
|
|
4
|
-
import { createSubAgentTool } from "./implements/subAgentTools.js";
|
|
5
|
-
import { discoverBuiltinTools } from "./discovery/builtin.js";
|
|
6
|
-
import { discoverSubAgents } from "./discovery/subagents.js";
|
|
7
|
-
import { discoverSkills } from "./discovery/skills.js";
|
|
8
|
-
import { discoverMcpServers } from "./discovery/mcp.js";
|
|
9
|
-
import { discoverUserTools } from "./discovery/usertools.js";
|
|
10
|
-
/**
|
|
11
|
-
* Capabilities — 全局能力注册表。
|
|
12
|
-
*
|
|
13
|
-
* 职责:
|
|
14
|
-
* 1. 全局发现一次(扫描文件系统,收集所有完整候选)
|
|
15
|
-
* 2. 提供 filter(permissions) 按权限过滤
|
|
16
|
-
* 3. 提供 instantiate(filterOutput) 将过滤后的名称实例化为 Tool[]
|
|
17
|
-
*
|
|
18
|
-
* 设计原则:
|
|
19
|
-
* - 发现结果全局共享,SubAgent 复用同一份候选集
|
|
20
|
-
* - filter 是纯函数(名称匹配),可被任意 Agent 独立调用
|
|
21
|
-
* - instantiate 是纯映射(名称→实例),不含业务逻辑
|
|
22
|
-
* - 通过持有的 Provider 实例获取配置、路径、模型工厂等全局服务
|
|
23
|
-
*/
|
|
24
|
-
export class Capabilities {
|
|
25
|
-
/** Provider 全局上下文 */
|
|
26
|
-
provider;
|
|
27
|
-
// ---- 全局候选集(发现一次,全局复用)----
|
|
28
|
-
builtinInstances;
|
|
29
|
-
userInstances;
|
|
30
|
-
subagentDefs;
|
|
31
|
-
/** 完整 SkillInfo,含 subAgent 标记,可据此区分枚举 */
|
|
32
|
-
skills;
|
|
33
|
-
/** 完整 MCP 服务器配置 */
|
|
34
|
-
mcps;
|
|
35
|
-
constructor(provider) {
|
|
36
|
-
this.provider = provider;
|
|
37
|
-
this.builtinInstances = discoverBuiltinTools(provider.config);
|
|
38
|
-
this.userInstances = discoverUserTools(provider.toolsPaths);
|
|
39
|
-
this.subagentDefs = discoverSubAgents(provider.subAgentsPaths);
|
|
40
|
-
this.skills = discoverSkills(provider.skillsPaths);
|
|
41
|
-
this.mcps = discoverMcpServers(provider.mcpPaths);
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* 按权限过滤候选集,返回过滤后的名称列表。
|
|
45
|
-
* 纯名称操作(安全预过滤 + 权限过滤),不涉及 Tool 实例化。
|
|
46
|
-
*/
|
|
47
|
-
filter(permissions, options) {
|
|
48
|
-
const exclude = options?.exclude ?? {};
|
|
49
|
-
const excludeTools = new Set(exclude.tools ?? []);
|
|
50
|
-
const excludeSkills = new Set(exclude.skills ?? []);
|
|
51
|
-
const excludeMcps = new Set(exclude.mcps ?? []);
|
|
52
|
-
const excludeSubAgents = new Set(exclude.subagents ?? []);
|
|
53
|
-
const safeSubagents = excludeSubAgents.size === 0
|
|
54
|
-
? this.subagentDefs
|
|
55
|
-
: this.subagentDefs.filter((def) => !excludeSubAgents.has(def.function?.name ?? ""));
|
|
56
|
-
const DYNAMIC_TOOL_NAMES = [
|
|
57
|
-
"load_skill",
|
|
58
|
-
"call_skill_sub_agent",
|
|
59
|
-
"load_mcp",
|
|
60
|
-
"call_mcp_tool",
|
|
61
|
-
"read_mcp_resource",
|
|
62
|
-
];
|
|
63
|
-
const toolNames = [
|
|
64
|
-
...this.builtinInstances.map((t) => t.function.name),
|
|
65
|
-
...this.userInstances.map((t) => t.function.name),
|
|
66
|
-
...DYNAMIC_TOOL_NAMES,
|
|
67
|
-
].filter((name) => !excludeTools.has(name));
|
|
68
|
-
const evaluator = new PermissionEvaluator(permissions);
|
|
69
|
-
const filtered = evaluator.filter({
|
|
70
|
-
tools: toolNames,
|
|
71
|
-
skills: this.skills
|
|
72
|
-
.filter((s) => !excludeSkills.has(s.id))
|
|
73
|
-
.map((s) => s.id),
|
|
74
|
-
mcps: this.mcps
|
|
75
|
-
.filter((m) => !excludeMcps.has(m.id))
|
|
76
|
-
.map((m) => m.id),
|
|
77
|
-
subagents: safeSubagents.map((d) => d.function.name),
|
|
78
|
-
});
|
|
79
|
-
return {
|
|
80
|
-
tools: filtered.tools,
|
|
81
|
-
subagents: filtered.subagents,
|
|
82
|
-
skills: filtered.skills,
|
|
83
|
-
mcps: filtered.mcps,
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* 快捷方法:filter + instantiate 一步完成。
|
|
88
|
-
*/
|
|
89
|
-
buildTools(permissions, options) {
|
|
90
|
-
return this.instantiate(this.filter(permissions, options));
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* 将过滤后的名称列表实例化为 Tool 数组。
|
|
94
|
-
*/
|
|
95
|
-
instantiate(filtered) {
|
|
96
|
-
const result = [];
|
|
97
|
-
const allowedToolNames = new Set(filtered.tools);
|
|
98
|
-
const provider = this.provider;
|
|
99
|
-
// 1. 内置 + 用户工具
|
|
100
|
-
for (const t of [...this.builtinInstances, ...this.userInstances]) {
|
|
101
|
-
if (allowedToolNames.has(t.function.name)) {
|
|
102
|
-
result.push(t);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
// 2. 筛选候选项(保留完整信息,传给工具函数自行决定如何构建枚举)
|
|
106
|
-
const allowedSkillSet = new Set(filtered.skills);
|
|
107
|
-
const allowedMcpSet = new Set(filtered.mcps);
|
|
108
|
-
const filteredSkills = this.skills.filter((s) => allowedSkillSet.has(s.id));
|
|
109
|
-
const filteredMcps = this.mcps.filter((m) => allowedMcpSet.has(m.id));
|
|
110
|
-
// 3. 动态工具
|
|
111
|
-
if (allowedToolNames.has("load_skill") && filteredSkills.length > 0) {
|
|
112
|
-
result.push(createLoadSkillTool(provider.skillsPaths, filteredSkills));
|
|
113
|
-
}
|
|
114
|
-
// call_skill_sub_agent:只要有支持子 Agent 的 skill 就注册
|
|
115
|
-
const hasSubAgentSkills = filteredSkills.some((s) => s.subAgent);
|
|
116
|
-
if (allowedToolNames.has("call_skill_sub_agent") && hasSubAgentSkills) {
|
|
117
|
-
result.push(createCallSkillSubAgentTool(provider.skillsPaths, filteredSkills, this));
|
|
118
|
-
}
|
|
119
|
-
const mcpManager = provider.getMcpManager();
|
|
120
|
-
if (allowedToolNames.has("load_mcp") && mcpManager && filteredMcps.length > 0) {
|
|
121
|
-
result.push(createLoadMcpTool(mcpManager, filteredMcps));
|
|
122
|
-
}
|
|
123
|
-
if (allowedToolNames.has("call_mcp_tool") && mcpManager) {
|
|
124
|
-
result.push(createCallMcpTool(mcpManager));
|
|
125
|
-
}
|
|
126
|
-
if (allowedToolNames.has("read_mcp_resource") && mcpManager) {
|
|
127
|
-
result.push(createReadMcpResourceTool(mcpManager));
|
|
128
|
-
}
|
|
129
|
-
// 4. SubAgent
|
|
130
|
-
const allowedSubagentSet = new Set(filtered.subagents);
|
|
131
|
-
for (const def of this.subagentDefs) {
|
|
132
|
-
if (!def.function || !allowedSubagentSet.has(def.function.name))
|
|
133
|
-
continue;
|
|
134
|
-
result.push(createSubAgentTool(def, this.provider, this));
|
|
135
|
-
}
|
|
136
|
-
// 5. 去重
|
|
137
|
-
return dedupTools(result);
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* 重新执行全局发现(重新扫描文件系统)。
|
|
141
|
-
*/
|
|
142
|
-
refresh(options) {
|
|
143
|
-
const silent = options?.silent ?? false;
|
|
144
|
-
const provider = this.provider;
|
|
145
|
-
this.builtinInstances = discoverBuiltinTools(provider.config);
|
|
146
|
-
this.userInstances = discoverUserTools(provider.toolsPaths, { silent });
|
|
147
|
-
this.subagentDefs = discoverSubAgents(provider.subAgentsPaths);
|
|
148
|
-
this.skills = discoverSkills(provider.skillsPaths, { silent });
|
|
149
|
-
this.mcps = discoverMcpServers(provider.mcpPaths);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
function dedupTools(tools) {
|
|
153
|
-
const seen = new Set();
|
|
154
|
-
const result = [];
|
|
155
|
-
for (let i = tools.length - 1; i >= 0; i--) {
|
|
156
|
-
const name = tools[i].function.name;
|
|
157
|
-
if (!seen.has(name)) {
|
|
158
|
-
seen.add(name);
|
|
159
|
-
result.unshift(tools[i]);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
return result;
|
|
163
|
-
}
|