@ai-zen/agents-sdk 2.0.0-alpha.0 → 2.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 (131) hide show
  1. package/CHANGELOG.md +73 -16
  2. package/README.md +397 -113
  3. package/README.zh.md +314 -112
  4. package/dist/agent-plugins/AssetTransportPlugin.d.ts +51 -0
  5. package/dist/agent-plugins/AssetTransportPlugin.js +145 -0
  6. package/dist/agent-plugins/AutoMigratePlugin.d.ts +35 -14
  7. package/dist/agent-plugins/AutoMigratePlugin.js +36 -36
  8. package/dist/agent-plugins/AutoRefreshToolsPlugin.d.ts +6 -6
  9. package/dist/agent-plugins/AutoRefreshToolsPlugin.js +2 -2
  10. package/dist/agent-plugins/ContextGuardPlugin.d.ts +20 -24
  11. package/dist/agent-plugins/ContextGuardPlugin.js +25 -24
  12. package/dist/agent-plugins/SubAgentGuardInstallerPlugin.d.ts +25 -35
  13. package/dist/agent-plugins/SubAgentGuardInstallerPlugin.js +26 -41
  14. package/dist/agent-plugins/UnknownToolHintPlugin.d.ts +14 -9
  15. package/dist/agent-plugins/UnknownToolHintPlugin.js +15 -13
  16. package/dist/config/ConfigManager.d.ts +13 -5
  17. package/dist/config/ConfigManager.js +58 -39
  18. package/dist/config/constants.d.ts +1 -1
  19. package/dist/config/constants.js +2 -2
  20. package/dist/config/factory-skills.js +2 -2
  21. package/dist/config/normalize.d.ts +93 -0
  22. package/dist/config/normalize.js +158 -0
  23. package/dist/crud/AgentRepository.d.ts +3 -1
  24. package/dist/crud/AgentRepository.js +4 -2
  25. package/dist/index.d.ts +19 -9
  26. package/dist/index.js +14 -7
  27. package/dist/runtime/AgentRuntime.d.ts +293 -0
  28. package/dist/runtime/AgentRuntime.js +457 -0
  29. package/dist/runtime/HandoffGuardPlugin.d.ts +82 -15
  30. package/dist/runtime/HandoffGuardPlugin.js +136 -17
  31. package/dist/runtime/HandoffService.d.ts +244 -119
  32. package/dist/runtime/HandoffService.js +373 -251
  33. package/dist/runtime/RuntimePeerSessions.d.ts +34 -0
  34. package/dist/runtime/RuntimePeerSessions.js +50 -0
  35. package/dist/runtime/Scopes.d.ts +123 -0
  36. package/dist/runtime/Scopes.js +169 -0
  37. package/dist/runtime/SdkAgent.d.ts +19 -8
  38. package/dist/runtime/SdkAgent.js +23 -6
  39. package/dist/runtime/completeOnce.d.ts +3 -3
  40. package/dist/runtime/completeOnce.js +3 -3
  41. package/dist/runtime/createAgent.d.ts +29 -7
  42. package/dist/runtime/createAgent.js +57 -12
  43. package/dist/runtime/createModel.d.ts +4 -4
  44. package/dist/runtime/createModel.js +6 -6
  45. package/dist/runtime/lastPromptTokens.d.ts +18 -0
  46. package/dist/runtime/lastPromptTokens.js +26 -0
  47. package/dist/runtime/resolveModel.d.ts +9 -9
  48. package/dist/runtime/resolveModel.js +8 -8
  49. package/dist/scope/Scope.d.ts +10 -9
  50. package/dist/scope/Scope.js +6 -6
  51. package/dist/scope/ScopePlugin.d.ts +2 -8
  52. package/dist/scope/disclosure.d.ts +1 -1
  53. package/dist/scope/disclosure.js +1 -1
  54. package/dist/scope/plugins/allInOne.d.ts +13 -1
  55. package/dist/scope/plugins/allInOne.js +9 -5
  56. package/dist/scope/plugins/builtin/AsyncExecTool.d.ts +12 -0
  57. package/dist/scope/plugins/builtin/AsyncExecTool.js +103 -0
  58. package/dist/scope/plugins/builtin/AsyncTimeoutTool.d.ts +10 -0
  59. package/dist/scope/plugins/builtin/AsyncTimeoutTool.js +90 -0
  60. package/dist/scope/plugins/builtin/BuiltinToolsScopePlugin.d.ts +1 -1
  61. package/dist/scope/plugins/builtin/BuiltinToolsScopePlugin.js +1 -1
  62. package/dist/scope/plugins/builtin/DownloadFileTool.js +1 -1
  63. package/dist/scope/plugins/builtin/ExecTool.js +10 -199
  64. package/dist/scope/plugins/builtin/FindTextTool.d.ts +1 -1
  65. package/dist/scope/plugins/builtin/FindTextTool.js +1 -1
  66. package/dist/scope/plugins/builtin/GenerateImageTool.d.ts +1 -4
  67. package/dist/scope/plugins/builtin/GenerateImageTool.js +2 -8
  68. package/dist/scope/plugins/builtin/InspectFileTool.js +1 -1
  69. package/dist/scope/plugins/builtin/SdkCallbackTool.d.ts +4 -4
  70. package/dist/scope/plugins/builtin/SdkCallbackTool.js +3 -3
  71. package/dist/scope/plugins/builtin/SleepTool.js +2 -3
  72. package/dist/scope/plugins/builtin/ViewImageTool.d.ts +2 -4
  73. package/dist/scope/plugins/builtin/ViewImageTool.js +4 -6
  74. package/dist/scope/plugins/builtin/WriteTool.d.ts +7 -0
  75. package/dist/scope/plugins/builtin/WriteTool.js +8 -1
  76. package/dist/scope/plugins/builtin/asyncTasks.d.ts +41 -0
  77. package/dist/scope/plugins/builtin/asyncTasks.js +97 -0
  78. package/dist/scope/plugins/builtin/commandOutput.d.ts +22 -0
  79. package/dist/scope/plugins/builtin/commandOutput.js +55 -0
  80. package/dist/scope/plugins/builtin/commandRun.d.ts +48 -0
  81. package/dist/scope/plugins/builtin/commandRun.js +169 -0
  82. package/dist/scope/plugins/builtin/discover.d.ts +1 -3
  83. package/dist/scope/plugins/builtin/discover.js +1 -3
  84. package/dist/scope/plugins/builtin/index.d.ts +5 -7
  85. package/dist/scope/plugins/builtin/index.js +7 -7
  86. package/dist/scope/plugins/builtin/outputGuard.d.ts +1 -5
  87. package/dist/scope/plugins/builtin/outputGuard.js +1 -5
  88. package/dist/scope/plugins/mcp/McpConnectionManager.d.ts +56 -11
  89. package/dist/scope/plugins/mcp/McpConnectionManager.js +169 -74
  90. package/dist/scope/plugins/mcp/McpScopePlugin.d.ts +17 -7
  91. package/dist/scope/plugins/mcp/McpScopePlugin.js +36 -12
  92. package/dist/scope/plugins/mcp/discover.d.ts +9 -2
  93. package/dist/scope/plugins/mcp/discover.js +12 -14
  94. package/dist/scope/plugins/mcp/mcpTaskNotice.d.ts +52 -0
  95. package/dist/scope/plugins/mcp/mcpTaskNotice.js +8 -0
  96. package/dist/scope/plugins/mcp/mcpTools.d.ts +20 -2
  97. package/dist/scope/plugins/mcp/mcpTools.js +220 -39
  98. package/dist/scope/plugins/peers/PeerSessionsScopePlugin.d.ts +4 -7
  99. package/dist/scope/plugins/peers/PeerSessionsScopePlugin.js +4 -7
  100. package/dist/scope/plugins/peers/peerSessionTools.d.ts +97 -46
  101. package/dist/scope/plugins/peers/peerSessionTools.js +147 -121
  102. package/dist/scope/plugins/skills/SkillsScopePlugin.d.ts +4 -0
  103. package/dist/scope/plugins/skills/SkillsScopePlugin.js +10 -4
  104. package/dist/scope/plugins/skills/discover.d.ts +15 -8
  105. package/dist/scope/plugins/skills/discover.js +15 -26
  106. package/dist/scope/plugins/skills/skillTools.d.ts +8 -7
  107. package/dist/scope/plugins/skills/skillTools.js +18 -14
  108. package/dist/scope/plugins/subagents/SubAgentsScopePlugin.d.ts +4 -0
  109. package/dist/scope/plugins/subagents/SubAgentsScopePlugin.js +4 -1
  110. package/dist/scope/plugins/subagents/discover.d.ts +7 -1
  111. package/dist/scope/plugins/subagents/discover.js +7 -3
  112. package/dist/scope/plugins/subagents/subAgentTools.d.ts +0 -1
  113. package/dist/scope/plugins/subagents/subAgentTools.js +11 -1
  114. package/dist/scope/plugins/usertools/UserToolsScopePlugin.d.ts +4 -0
  115. package/dist/scope/plugins/usertools/UserToolsScopePlugin.js +4 -1
  116. package/dist/scope/plugins/usertools/discover.d.ts +7 -8
  117. package/dist/scope/plugins/usertools/discover.js +17 -27
  118. package/dist/shared/EntityRepository.d.ts +22 -5
  119. package/dist/shared/EntityRepository.js +45 -29
  120. package/dist/shared/content-blocks.d.ts +9 -3
  121. package/dist/shared/content-blocks.js +12 -19
  122. package/dist/shared/errors.d.ts +5 -7
  123. package/dist/shared/errors.js +6 -8
  124. package/dist/shared/logger.d.ts +21 -10
  125. package/dist/shared/logger.js +9 -15
  126. package/dist/types/index.d.ts +36 -13
  127. package/package.json +2 -2
  128. package/dist/config/migrate.d.ts +0 -115
  129. package/dist/config/migrate.js +0 -200
  130. package/dist/scope/plugins/builtin/test-helpers.d.ts +0 -3
  131. package/dist/scope/plugins/builtin/test-helpers.js +0 -4
@@ -1,200 +0,0 @@
1
- /**
2
- * 配置字段迁移 —— 历史形状升级到当前形状
3
- *
4
- * 三条遗留字段被淘汰:
5
- *
6
- * 1. `Model.maxContextTokens` / `Model.maxContextChars` → **`Model.tokenThresholds`**
7
- * 旧名字一直在误导:它既不是「模型的真实上下文窗口」,也不是单一概念。现在拆成两条线:
8
- * `handoff`(交接阈值,软线)与 `guard`(强行阻断阈值,硬线)。
9
- * `maxContextChars` 是 token 口径之前的历史遗留,按 `floor(chars / 4)` 折成 tokens
10
- * (与旧代码的回退算法一致)后并入 `handoff`。
11
- * 2. **`Model.reasoning` → `Model.reasoningPreset`**
12
- * 老配置用 `reasoning: { efforts, disable? }` 在模型上声明思考能力,现在是
13
- * 「引用策略表里的名字」。未标 `custom` 的模型会被出厂定义整块覆盖,但**标了 `custom`
14
- * 的模型会原地保留**,于是留下一个无人认识的 `reasoning` 死字段,同时因为缺
15
- * `reasoningPreset` 而默默失去调档能力。这里删掉旧字段,并按**同 id 的出厂模型**
16
- * 补上正确的策略名(同一 id 即同一厂商型号,协议能力不会因用户改过参数而变)。
17
- *
18
- * 另有一类「缺省值缺位」的迁移:`Endpoint.capabilities`(见 `normalizeEndpointCapabilities`)。
19
- *
20
- * ## `defaultModel` 是**反例**:它不需要迁移,也绝不能清
21
- *
22
- * 模型解析是三级(`会话级覆盖 > Agent 定义 > 本字段`,唯一实现在
23
- * `runtime/resolveModel.ts`),`defaultModel` 就是**全局兜底层**。字段名从未变过,
24
- * 所以这里**什么都不用搬**:老配置里写着的原样保留、继续生效。绝对不能再清 ——
25
- * 清掉等于用户升级一次就静默丢一次自己配的兜底模型,而这属于「默认值缺位」里最坏的
26
- * 一种:丢的是用户明确填过的值。
27
- *
28
- * 迁移时机:`ConfigManager.read()` 读出即迁(内存里永远是当前形状);
29
- * 落盘由 `ensureDefaultConfig()` 负责 —— 它比较迁移结果与磁盘原文,不同才写回,
30
- * 因此迁移只发生一次,且不会在每次启动时无谓地改写文件。
31
- */
32
- import { DEFAULT_APP_CONFIG } from "./constants.js";
33
- /** 当前字段名 */
34
- export const TOKEN_THRESHOLDS_KEY = "tokenThresholds";
35
- /** 已淘汰的旧字段名(仅迁移代码认识它们) */
36
- export const LEGACY_TOKEN_FIELD = "maxContextTokens";
37
- export const LEGACY_CHARS_FIELD = "maxContextChars";
38
- export const LEGACY_REASONING_FIELD = "reasoning";
39
- /**
40
- * 旧数据缺失阈值时的交接阈值兜底。
41
- *
42
- * 与 CLI 新建模型的默认值一致;宁可给一个保守值,也不要让模型缺阈值——
43
- * 缺阈值意味着既没有交接触发、也没有阻断保护。
44
- */
45
- export const DEFAULT_HANDOFF_THRESHOLD = 128_000;
46
- function toPositiveInt(value) {
47
- if (typeof value !== "number" || !Number.isFinite(value) || value <= 0)
48
- return undefined;
49
- return Math.floor(value);
50
- }
51
- function readThresholds(value) {
52
- if (typeof value !== "object" || value === null)
53
- return {};
54
- const raw = value;
55
- return {
56
- handoff: toPositiveInt(raw.handoff),
57
- guard: toPositiveInt(raw.guard),
58
- };
59
- }
60
- /**
61
- * 迁移单个模型条目。
62
- *
63
- * 优先级:已有 `tokenThresholds` 的成员 > 旧 tokens 字段 > 旧 chars 字段折算 > 兜底默认。
64
- * 旧字段一律删除(连同一份数据里残留的脏值),保证迁移后的形状唯一。
65
- * 已有 `handoff` 但缺 `guard` 时按 1.5 倍补齐。
66
- */
67
- export function migrateModelThresholds(model) {
68
- const source = model;
69
- const current = readThresholds(source[TOKEN_THRESHOLDS_KEY]);
70
- const legacyTokens = toPositiveInt(source[LEGACY_TOKEN_FIELD]);
71
- const legacyChars = toPositiveInt(source[LEGACY_CHARS_FIELD]);
72
- const legacySingle = toPositiveInt(source.tokenThreshold);
73
- const hasLegacy = LEGACY_TOKEN_FIELD in source || LEGACY_CHARS_FIELD in source || "tokenThreshold" in source;
74
- const complete = current.handoff !== undefined && current.guard !== undefined;
75
- if (!hasLegacy && complete)
76
- return { model, changed: false };
77
- const fromChars = legacyChars !== undefined ? Math.max(1, Math.floor(legacyChars / 4)) : undefined;
78
- const handoff = current.handoff ?? legacyTokens ?? fromChars ?? legacySingle ?? DEFAULT_HANDOFF_THRESHOLD;
79
- // 旧数据里没有 guard 这一项:按 1.5 倍(历史上护栏用的默认倍数)折算一次,
80
- // 让迁移后的实际行为与迁移前一致。这是**一次性**换算,落盘后两个值都是整数,
81
- // 运行时不再有任何倍数关系(想调硬线就直接改 guard 的数字)。
82
- const guard = current.guard ?? Math.round(handoff * 1.5);
83
- const next = { ...source };
84
- delete next[LEGACY_TOKEN_FIELD];
85
- delete next[LEGACY_CHARS_FIELD];
86
- delete next.tokenThreshold;
87
- next[TOKEN_THRESHOLDS_KEY] = { handoff, guard };
88
- return { model: next, changed: true };
89
- }
90
- /**
91
- * 迁移单个模型的思考能力声明:`reasoning` → `reasoningPreset`。
92
- *
93
- * 只处理「旧字段存在」这一种情况:字段一旦落盘消失,用户后续主动删掉
94
- * `reasoningPreset`(表示「这个模型我不想调档」)就不会被反复补回来。
95
- */
96
- export function migrateModelReasoning(model) {
97
- const source = model;
98
- if (!(LEGACY_REASONING_FIELD in source))
99
- return { model, changed: false };
100
- const next = { ...source };
101
- delete next[LEGACY_REASONING_FIELD];
102
- // 同 id 的出厂模型能告诉我们「这个型号属于哪套协议」
103
- if (next.reasoningPreset === undefined) {
104
- const factory = (DEFAULT_APP_CONFIG.models ?? []).find((item) => item.id === model.id);
105
- if (factory?.reasoningPreset)
106
- next.reasoningPreset = factory.reasoningPreset;
107
- }
108
- return { model: next, changed: true };
109
- }
110
- /**
111
- * 迁移整个配置。只处理对话模型(`models`)——
112
- * 图片模型(`imageModels`)既没有上下文阈值、也没有思考概念。
113
- *
114
- * 同时做两件字段级收敛:
115
- * - **保留** `defaultModel`(三级解析的全局兜底层,见文件头):它不在这里被删、
116
- * 也不在这里被补 —— 用户没配就没有兜底层,`createAgent` 会给出明确报错;
117
- * - 补齐中断策略(`abortedMessageStrategy` / `abortedToolRoundStrategy`):把缺失或
118
- * 非法值写成出厂默认。这样「配置即事实」——文件里写着的就是运行时用的,不再需要
119
- * 使用方各自 `?? "keep"` 地兵分两路兜底。
120
- */
121
- export function migrateConfig(config) {
122
- let changed = false;
123
- const models = (config.models ?? []).map((model) => {
124
- const thresholds = migrateModelThresholds(model);
125
- const reasoning = migrateModelReasoning(thresholds.model);
126
- if (thresholds.changed || reasoning.changed)
127
- changed = true;
128
- return reasoning.model;
129
- });
130
- const next = {
131
- ...config,
132
- models,
133
- // 端点数组浅拷贝:能力位补齐会改元素,不把副作用浇到调用方手里的旧对象上
134
- endpoints: (config.endpoints ?? []).map((endpoint) => ({ ...endpoint })),
135
- };
136
- if (normalizeAbortStrategies(next))
137
- changed = true;
138
- if (normalizeEndpointCapabilities(next))
139
- changed = true;
140
- if (!changed)
141
- return { config, changed };
142
- return { config: next, changed };
143
- }
144
- /** 合法的中断策略取值(与 core 的枚举一致) */
145
- export const ABORTED_MESSAGE_STRATEGIES = ["keep", "drop"];
146
- export const ABORTED_TOOL_ROUND_STRATEGIES = ["fill", "drop"];
147
- /** 缺失 / 非法 → 出厂默认;返回是否有改动(直接改写传入对象) */
148
- export function normalizeAbortStrategies(config) {
149
- let changed = false;
150
- if (!ABORTED_MESSAGE_STRATEGIES.includes(config.abortedMessageStrategy)) {
151
- config.abortedMessageStrategy = DEFAULT_APP_CONFIG.abortedMessageStrategy;
152
- changed = true;
153
- }
154
- if (!ABORTED_TOOL_ROUND_STRATEGIES.includes(config.abortedToolRoundStrategy)) {
155
- config.abortedToolRoundStrategy = DEFAULT_APP_CONFIG.abortedToolRoundStrategy;
156
- changed = true;
157
- }
158
- return changed;
159
- }
160
- /**
161
- * 补齐端点**缺失**的能力位声明(`Endpoint.capabilities`)。
162
- *
163
- * ## 为什么要补
164
- *
165
- * `capabilities.filesApi` 是后来才引入的能力位 —— 它把 `view_image` 的判定从「端点 id
166
- * 是不是 `deepseek`」改成「看这一位」,而出厂只有 deepseek 打了 `true`。
167
- * 但端点**不参与出厂托管**(`ConfigManager.syncManagedModels` 明写「保持用户配置」),
168
- * 迁移此前也完全不碰 `endpoints` —— 于是在这之前就已写下的配置,其 deepseek
169
- * 端点永远缺 `capabilities`,`supportsFilesApi()` 恒为 `false`:
170
- *
171
- * · 出厂承诺的「deepseek 走 Files API」对这些用户从未生效;
172
- * · 而且**全程静默** —— 「未声明能力位」和「缺 apiKey」两个分支都不发 warning,
173
- * 图片 / `view_image` / MCP 媒体就悄悄退化成 base64 内联,请求体随历史膨胀。
174
- *
175
- * 这是「默认值缺位」,不是「用户选择」,所以由迁移补 —— 与模型清单不同,端点本身仍然
176
- * 归用户所有,我们只往**空位**里放出厂默认。
177
- *
178
- * ## 边界:只补整体缺失,绝不覆盖
179
- *
180
- * - `capabilities` **整体缺失** → 按**同 id 的出厂端点**补上整块;
181
- * - `capabilities` 已存在(哪怕是 `{}`、哪怕写着 `filesApi: false`)→ 一个字段都不动:
182
- * 用户已经表过态,`false` 就是「我不走 Files API」;
183
- * - 端点 id 不在出厂清单(用户自建)→ 不动:我们不知道它是否真有 `/files`,
184
- * 猜错会把上传请求打到不存在的接口上。
185
- *
186
- * @returns 是否有改动(直接改写传入对象)
187
- */
188
- export function normalizeEndpointCapabilities(config) {
189
- let changed = false;
190
- for (const endpoint of config.endpoints ?? []) {
191
- if (endpoint.capabilities != null)
192
- continue;
193
- const factory = (DEFAULT_APP_CONFIG.endpoints ?? []).find((item) => item.id === endpoint.id);
194
- if (!factory?.capabilities)
195
- continue;
196
- endpoint.capabilities = { ...factory.capabilities };
197
- changed = true;
198
- }
199
- return changed;
200
- }
@@ -1,3 +0,0 @@
1
- import type { AppConfig, ToolEnv, ToolEnvDeps } from "../../../types/index.js";
2
- /** 构造测试用 ToolEnv(deps 用于注入出网依赖,避免 mock openai 模块) */
3
- export declare function makeEnv(cwd?: string, config?: AppConfig, deps?: ToolEnvDeps): ToolEnv;
@@ -1,4 +0,0 @@
1
- /** 构造测试用 ToolEnv(deps 用于注入出网依赖,避免 mock openai 模块) */
2
- export function makeEnv(cwd = process.cwd(), config = {}, deps) {
3
- return deps ? { cwd, config, deps } : { cwd, config };
4
- }