@ai-zen/agents-sdk 1.2.0-alpha.2 → 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.
- package/CHANGELOG.md +77 -0
- package/README.md +397 -113
- package/README.zh.md +314 -112
- package/dist/agent-plugins/AssetTransportPlugin.d.ts +51 -0
- package/dist/agent-plugins/AssetTransportPlugin.js +145 -0
- package/dist/agent-plugins/AutoMigratePlugin.d.ts +56 -6
- package/dist/agent-plugins/AutoMigratePlugin.js +64 -15
- package/dist/agent-plugins/AutoRefreshToolsPlugin.d.ts +12 -9
- package/dist/agent-plugins/AutoRefreshToolsPlugin.js +6 -7
- package/dist/agent-plugins/ContextGuardPlugin.d.ts +32 -24
- package/dist/agent-plugins/ContextGuardPlugin.js +37 -22
- package/dist/agent-plugins/SubAgentGuardInstallerPlugin.d.ts +38 -46
- package/dist/agent-plugins/SubAgentGuardInstallerPlugin.js +37 -50
- package/dist/agent-plugins/UnknownToolHintPlugin.d.ts +15 -1
- package/dist/agent-plugins/UnknownToolHintPlugin.js +15 -1
- package/dist/config/ConfigManager.d.ts +13 -5
- package/dist/config/ConfigManager.js +58 -39
- package/dist/config/constants.d.ts +1 -1
- package/dist/config/constants.js +2 -2
- package/dist/config/factory-skills.js +2 -2
- package/dist/config/normalize.d.ts +93 -0
- package/dist/config/normalize.js +158 -0
- package/dist/crud/AgentRepository.d.ts +3 -1
- package/dist/crud/AgentRepository.js +4 -2
- package/dist/index.d.ts +22 -9
- package/dist/index.js +16 -6
- package/dist/runtime/AgentRuntime.d.ts +293 -0
- package/dist/runtime/AgentRuntime.js +457 -0
- package/dist/runtime/HandoffGuardPlugin.d.ts +85 -16
- package/dist/runtime/HandoffGuardPlugin.js +140 -19
- package/dist/runtime/HandoffService.d.ts +255 -91
- package/dist/runtime/HandoffService.js +392 -177
- package/dist/runtime/RuntimePeerSessions.d.ts +34 -0
- package/dist/runtime/RuntimePeerSessions.js +50 -0
- package/dist/runtime/Scopes.d.ts +123 -0
- package/dist/runtime/Scopes.js +169 -0
- package/dist/runtime/SdkAgent.d.ts +21 -11
- package/dist/runtime/SdkAgent.js +24 -8
- package/dist/runtime/completeOnce.d.ts +3 -3
- package/dist/runtime/completeOnce.js +3 -3
- package/dist/runtime/createAgent.d.ts +29 -7
- package/dist/runtime/createAgent.js +57 -12
- package/dist/runtime/createModel.d.ts +4 -4
- package/dist/runtime/createModel.js +6 -6
- package/dist/runtime/lastPromptTokens.d.ts +18 -0
- package/dist/runtime/lastPromptTokens.js +26 -0
- package/dist/runtime/resolveModel.d.ts +9 -9
- package/dist/runtime/resolveModel.js +8 -8
- package/dist/scope/Scope.d.ts +42 -14
- package/dist/scope/Scope.js +55 -16
- package/dist/scope/ScopePlugin.d.ts +2 -8
- package/dist/scope/disclosure.d.ts +1 -1
- package/dist/scope/disclosure.js +1 -1
- package/dist/scope/plugins/allInOne.d.ts +13 -1
- package/dist/scope/plugins/allInOne.js +9 -5
- package/dist/scope/plugins/builtin/AsyncExecTool.d.ts +12 -0
- package/dist/scope/plugins/builtin/AsyncExecTool.js +103 -0
- package/dist/scope/plugins/builtin/AsyncTimeoutTool.d.ts +10 -0
- package/dist/scope/plugins/builtin/AsyncTimeoutTool.js +90 -0
- package/dist/scope/plugins/builtin/BuiltinToolsScopePlugin.d.ts +1 -1
- package/dist/scope/plugins/builtin/BuiltinToolsScopePlugin.js +1 -1
- package/dist/scope/plugins/builtin/DownloadFileTool.js +1 -1
- package/dist/scope/plugins/builtin/ExecTool.js +10 -199
- package/dist/scope/plugins/builtin/FindTextTool.d.ts +1 -1
- package/dist/scope/plugins/builtin/FindTextTool.js +1 -1
- package/dist/scope/plugins/builtin/GenerateImageTool.d.ts +1 -4
- package/dist/scope/plugins/builtin/GenerateImageTool.js +2 -8
- package/dist/scope/plugins/builtin/InspectFileTool.js +1 -1
- package/dist/scope/plugins/builtin/SdkCallbackTool.d.ts +4 -4
- package/dist/scope/plugins/builtin/SdkCallbackTool.js +3 -3
- package/dist/scope/plugins/builtin/SleepTool.js +2 -3
- package/dist/scope/plugins/builtin/ViewImageTool.d.ts +2 -4
- package/dist/scope/plugins/builtin/ViewImageTool.js +4 -6
- package/dist/scope/plugins/builtin/WriteTool.d.ts +7 -0
- package/dist/scope/plugins/builtin/WriteTool.js +8 -1
- package/dist/scope/plugins/builtin/asyncTasks.d.ts +41 -0
- package/dist/scope/plugins/builtin/asyncTasks.js +97 -0
- package/dist/scope/plugins/builtin/commandOutput.d.ts +22 -0
- package/dist/scope/plugins/builtin/commandOutput.js +55 -0
- package/dist/scope/plugins/builtin/commandRun.d.ts +48 -0
- package/dist/scope/plugins/builtin/commandRun.js +169 -0
- package/dist/scope/plugins/builtin/discover.d.ts +1 -3
- package/dist/scope/plugins/builtin/discover.js +1 -3
- package/dist/scope/plugins/builtin/index.d.ts +5 -7
- package/dist/scope/plugins/builtin/index.js +7 -7
- package/dist/scope/plugins/builtin/outputGuard.d.ts +1 -5
- package/dist/scope/plugins/builtin/outputGuard.js +1 -5
- package/dist/scope/plugins/mcp/McpConnectionManager.d.ts +61 -9
- package/dist/scope/plugins/mcp/McpConnectionManager.js +174 -72
- package/dist/scope/plugins/mcp/McpScopePlugin.d.ts +37 -4
- package/dist/scope/plugins/mcp/McpScopePlugin.js +100 -9
- package/dist/scope/plugins/mcp/discover.d.ts +9 -2
- package/dist/scope/plugins/mcp/discover.js +12 -14
- package/dist/scope/plugins/mcp/mcpTaskNotice.d.ts +52 -0
- package/dist/scope/plugins/mcp/mcpTaskNotice.js +8 -0
- package/dist/scope/plugins/mcp/mcpTools.d.ts +20 -2
- package/dist/scope/plugins/mcp/mcpTools.js +220 -39
- package/dist/scope/plugins/peers/PeerSessionsScopePlugin.d.ts +25 -0
- package/dist/scope/plugins/peers/PeerSessionsScopePlugin.js +32 -0
- package/dist/scope/plugins/peers/peerSessionTools.d.ts +173 -0
- package/dist/scope/plugins/peers/peerSessionTools.js +292 -0
- package/dist/scope/plugins/skills/SkillsScopePlugin.d.ts +4 -0
- package/dist/scope/plugins/skills/SkillsScopePlugin.js +10 -4
- package/dist/scope/plugins/skills/discover.d.ts +15 -8
- package/dist/scope/plugins/skills/discover.js +15 -26
- package/dist/scope/plugins/skills/skillTools.d.ts +9 -8
- package/dist/scope/plugins/skills/skillTools.js +21 -13
- package/dist/scope/plugins/subagents/SubAgentsScopePlugin.d.ts +4 -0
- package/dist/scope/plugins/subagents/SubAgentsScopePlugin.js +4 -1
- package/dist/scope/plugins/subagents/discover.d.ts +7 -1
- package/dist/scope/plugins/subagents/discover.js +7 -3
- package/dist/scope/plugins/subagents/subAgentTools.d.ts +0 -1
- package/dist/scope/plugins/subagents/subAgentTools.js +14 -1
- package/dist/scope/plugins/usertools/UserToolsScopePlugin.d.ts +4 -0
- package/dist/scope/plugins/usertools/UserToolsScopePlugin.js +4 -1
- package/dist/scope/plugins/usertools/discover.d.ts +7 -8
- package/dist/scope/plugins/usertools/discover.js +17 -27
- package/dist/shared/EntityRepository.d.ts +22 -5
- package/dist/shared/EntityRepository.js +45 -29
- package/dist/shared/content-blocks.d.ts +9 -3
- package/dist/shared/content-blocks.js +12 -19
- package/dist/shared/errors.d.ts +8 -3
- package/dist/shared/errors.js +9 -4
- package/dist/shared/logger.d.ts +21 -10
- package/dist/shared/logger.js +9 -15
- package/dist/types/index.d.ts +38 -13
- package/package.json +2 -2
- package/dist/config/migrate.d.ts +0 -115
- package/dist/config/migrate.js +0 -200
- package/dist/scope/plugins/builtin/test-helpers.d.ts +0 -3
- package/dist/scope/plugins/builtin/test-helpers.js +0 -4
|
@@ -1,64 +1,56 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* subagent 护栏安装器 — SubAgentGuardInstallerPlugin
|
|
3
3
|
*
|
|
4
|
-
* 为什么在 SDK 而不在 CLI
|
|
4
|
+
* 为什么在 SDK 而不在 CLI:subagent 内循环缺护栏是**所有宿主**共有的问题,不是 CLI 特性。
|
|
5
5
|
* 会话执行搬到服务端之后,服务端装配插件的那一处(`installPlugins`)装不到 CLI 仓里的实现,
|
|
6
|
-
*
|
|
6
|
+
* 远程会话的 subagent 就会裸奔;把它放进 SDK,CLI 与服务端才能装同一份护栏、
|
|
7
7
|
* 共享同一套阈值语义。
|
|
8
8
|
*
|
|
9
|
-
* 背景:SDK
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* `ContextGuardPlugin`
|
|
13
|
-
*
|
|
14
|
-
* 介入点:core 的 `onSubAgentStart`
|
|
15
|
-
*
|
|
16
|
-
* 该列表,故在钩子内安插对**本次
|
|
17
|
-
* init)。
|
|
18
|
-
*
|
|
19
|
-
* 约束口径(与主 Agent 同款,一项不落):把主 Agent 同款的 `ContextGuardPlugin`
|
|
20
|
-
* —— 同一个 `guardTokens`(当前模型的 `tokenThresholds.guard
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
* `
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* 终止用户对话(如需硬停,父 Agent 须设 allowJsonParseError=false)。
|
|
34
|
-
*
|
|
35
|
-
* 覆盖范围:core 只有两条委派路径(`AgentTool` / `AgentToolLazy`),均分发 onSubAgentStart,
|
|
36
|
-
* 故本插件对**全部**子 Agent 生效。SDK 的 `call_skill_sub_agent`(技能子 Agent)同样由
|
|
9
|
+
* 背景:SDK 的 subagent 由 `AgentToolLazy` 在委派时 `new Agent({ lifetime: "one-shot", ... })`
|
|
10
|
+
* 构建(core 的 `buildAgent` 回调),新建实例不携带任何插件(`Agent` 的插件表每实例私有,
|
|
11
|
+
* 不向 subagent 传播),且 subagent 走 `runOnce()`(一次性的那一条路),主 Agent 上的
|
|
12
|
+
* `ContextGuardPlugin` 对它完全无效:subagent 的内循环没有上下文 token 护栏。
|
|
13
|
+
*
|
|
14
|
+
* 介入点:core 的 `onSubAgentStart` 钩子在「subagent 已构建、尚未 `runOnce()`」时由宿主 Agent
|
|
15
|
+
* 分发,载荷携带 subagent 实例。`use()` 只是把插件推入插件表,而钩子分发每次都实时遍历
|
|
16
|
+
* 该列表,故在钩子内安插对**本次 runOnce 立即生效**(无需 init;`AgentToolLazy` 也不会调用
|
|
17
|
+
* subagent 的 init)。
|
|
18
|
+
*
|
|
19
|
+
* 约束口径(与主 Agent 同款,一项不落):把主 Agent 同款的 `ContextGuardPlugin` 装到 subagent 上
|
|
20
|
+
* —— 同一个 `guardTokens`(当前模型的 `tokenThresholds.guard`;会话行上有覆盖时取会话行那一格),
|
|
21
|
+
* 使「请求前用量超过硬线即中断」对 subagent 同样成立。
|
|
22
|
+
* **不设**循环次数上限,**不设**工具调用次数上限。
|
|
23
|
+
*
|
|
24
|
+
* 中断语义(core 重写之后变了,这是必须知道的一条):护栏在 `onInnerLoopStart` 里**返回
|
|
25
|
+
* 拒绝字符串**(`CORE.md` §11「哪些钩子能拒」),于是 subagent 的那一次请求**压根没发出去** ——
|
|
26
|
+
* 历史里一个字都不多,core 报 `denied`,委派工具照常收尾。父 Agent 这边拿到的是 subagent
|
|
27
|
+
* 已装好的历史(委派工具交回 `messages` 的最后一条),**不是**一段「执行工具时出错」的异常
|
|
28
|
+
* 文本 —— 新 core 里钩子抛错会穿到 `start()` 外面把循环打死,反而连收尾都做不干净。
|
|
29
|
+
*
|
|
30
|
+
* 覆盖范围:core 的委派路径是 `AgentToolLazy`,它分发 onSubAgentStart / onSubAgentEnd,
|
|
31
|
+
* 故本插件对**全部**subagent 生效。SDK 的 `call_skill_sub_agent`(技能 subagent)与
|
|
32
|
+
* `createSubAgentTool` 造出来的那些(名称取自定义的 `function.name`)同样由
|
|
37
33
|
* `AgentToolLazy` 承载、走同一委派边界,因此也在覆盖范围内。
|
|
38
34
|
*/
|
|
39
|
-
import type { AgentPlugin,
|
|
35
|
+
import type { AgentPlugin, SubAgentHookContext } from "@ai-zen/agents-core";
|
|
40
36
|
export interface SubAgentGuardOptions {
|
|
41
|
-
/** 强行阻断阈值;与主 Agent 同源(当前模型的 `tokenThresholds.guard`) */
|
|
42
|
-
guardTokens: number;
|
|
43
37
|
/**
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
* 会被自己的硬线打断。
|
|
38
|
+
* 强行阻断阈值;与主 Agent 同源(当前模型的 `tokenThresholds.guard`,会话行上有覆盖时取那一格)。
|
|
39
|
+
*
|
|
40
|
+
* 可以是一个 getter:那样每次委派都现读,改阈值不必重建执行体。
|
|
48
41
|
*/
|
|
49
|
-
|
|
42
|
+
guardTokens: number;
|
|
50
43
|
}
|
|
51
44
|
/**
|
|
52
|
-
*
|
|
45
|
+
* subagent 护栏安装器(装在**宿主 Agent** 上)。
|
|
53
46
|
*
|
|
54
|
-
* 每次委派边界把 `ContextGuardPlugin`
|
|
55
|
-
*
|
|
47
|
+
* 每次委派边界把 `ContextGuardPlugin` 装到 subagent 身上(阈值与主 Agent 相同),
|
|
48
|
+
* 并递归安插自身,使 subagent 再委派(孙级)时同样受约束。始终返回 undefined,永不拒绝委派
|
|
56
49
|
* ——是否中断由 `ContextGuardPlugin` 按阈值决定。
|
|
57
50
|
*/
|
|
58
51
|
export declare class SubAgentGuardInstallerPlugin implements AgentPlugin {
|
|
59
|
-
private readonly
|
|
60
|
-
/**
|
|
61
|
-
private readonly isSuspended?;
|
|
52
|
+
private readonly options;
|
|
53
|
+
/** 选项按**引用**存下:阈值每次委派现读(它可能是一个 getter),不存装配那一刻的快照 */
|
|
62
54
|
constructor(options: SubAgentGuardOptions);
|
|
63
|
-
onSubAgentStart({ subAgent }:
|
|
55
|
+
onSubAgentStart({ subAgent }: SubAgentHookContext): void;
|
|
64
56
|
}
|
|
@@ -1,68 +1,55 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* subagent 护栏安装器 — SubAgentGuardInstallerPlugin
|
|
3
3
|
*
|
|
4
|
-
* 为什么在 SDK 而不在 CLI
|
|
4
|
+
* 为什么在 SDK 而不在 CLI:subagent 内循环缺护栏是**所有宿主**共有的问题,不是 CLI 特性。
|
|
5
5
|
* 会话执行搬到服务端之后,服务端装配插件的那一处(`installPlugins`)装不到 CLI 仓里的实现,
|
|
6
|
-
*
|
|
6
|
+
* 远程会话的 subagent 就会裸奔;把它放进 SDK,CLI 与服务端才能装同一份护栏、
|
|
7
7
|
* 共享同一套阈值语义。
|
|
8
8
|
*
|
|
9
|
-
* 背景:SDK
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* `ContextGuardPlugin`
|
|
13
|
-
*
|
|
14
|
-
* 介入点:core 的 `onSubAgentStart`
|
|
15
|
-
*
|
|
16
|
-
* 该列表,故在钩子内安插对**本次
|
|
17
|
-
* init)。
|
|
18
|
-
*
|
|
19
|
-
* 约束口径(与主 Agent 同款,一项不落):把主 Agent 同款的 `ContextGuardPlugin`
|
|
20
|
-
* —— 同一个 `guardTokens`(当前模型的 `tokenThresholds.guard
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
* `
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* 终止用户对话(如需硬停,父 Agent 须设 allowJsonParseError=false)。
|
|
34
|
-
*
|
|
35
|
-
* 覆盖范围:core 只有两条委派路径(`AgentTool` / `AgentToolLazy`),均分发 onSubAgentStart,
|
|
36
|
-
* 故本插件对**全部**子 Agent 生效。SDK 的 `call_skill_sub_agent`(技能子 Agent)同样由
|
|
9
|
+
* 背景:SDK 的 subagent 由 `AgentToolLazy` 在委派时 `new Agent({ lifetime: "one-shot", ... })`
|
|
10
|
+
* 构建(core 的 `buildAgent` 回调),新建实例不携带任何插件(`Agent` 的插件表每实例私有,
|
|
11
|
+
* 不向 subagent 传播),且 subagent 走 `runOnce()`(一次性的那一条路),主 Agent 上的
|
|
12
|
+
* `ContextGuardPlugin` 对它完全无效:subagent 的内循环没有上下文 token 护栏。
|
|
13
|
+
*
|
|
14
|
+
* 介入点:core 的 `onSubAgentStart` 钩子在「subagent 已构建、尚未 `runOnce()`」时由宿主 Agent
|
|
15
|
+
* 分发,载荷携带 subagent 实例。`use()` 只是把插件推入插件表,而钩子分发每次都实时遍历
|
|
16
|
+
* 该列表,故在钩子内安插对**本次 runOnce 立即生效**(无需 init;`AgentToolLazy` 也不会调用
|
|
17
|
+
* subagent 的 init)。
|
|
18
|
+
*
|
|
19
|
+
* 约束口径(与主 Agent 同款,一项不落):把主 Agent 同款的 `ContextGuardPlugin` 装到 subagent 上
|
|
20
|
+
* —— 同一个 `guardTokens`(当前模型的 `tokenThresholds.guard`;会话行上有覆盖时取会话行那一格),
|
|
21
|
+
* 使「请求前用量超过硬线即中断」对 subagent 同样成立。
|
|
22
|
+
* **不设**循环次数上限,**不设**工具调用次数上限。
|
|
23
|
+
*
|
|
24
|
+
* 中断语义(core 重写之后变了,这是必须知道的一条):护栏在 `onInnerLoopStart` 里**返回
|
|
25
|
+
* 拒绝字符串**(`CORE.md` §11「哪些钩子能拒」),于是 subagent 的那一次请求**压根没发出去** ——
|
|
26
|
+
* 历史里一个字都不多,core 报 `denied`,委派工具照常收尾。父 Agent 这边拿到的是 subagent
|
|
27
|
+
* 已装好的历史(委派工具交回 `messages` 的最后一条),**不是**一段「执行工具时出错」的异常
|
|
28
|
+
* 文本 —— 新 core 里钩子抛错会穿到 `start()` 外面把循环打死,反而连收尾都做不干净。
|
|
29
|
+
*
|
|
30
|
+
* 覆盖范围:core 的委派路径是 `AgentToolLazy`,它分发 onSubAgentStart / onSubAgentEnd,
|
|
31
|
+
* 故本插件对**全部**subagent 生效。SDK 的 `call_skill_sub_agent`(技能 subagent)与
|
|
32
|
+
* `createSubAgentTool` 造出来的那些(名称取自定义的 `function.name`)同样由
|
|
37
33
|
* `AgentToolLazy` 承载、走同一委派边界,因此也在覆盖范围内。
|
|
38
34
|
*/
|
|
39
35
|
import { ContextGuardPlugin } from "./ContextGuardPlugin.js";
|
|
40
36
|
/**
|
|
41
|
-
*
|
|
37
|
+
* subagent 护栏安装器(装在**宿主 Agent** 上)。
|
|
42
38
|
*
|
|
43
|
-
* 每次委派边界把 `ContextGuardPlugin`
|
|
44
|
-
*
|
|
39
|
+
* 每次委派边界把 `ContextGuardPlugin` 装到 subagent 身上(阈值与主 Agent 相同),
|
|
40
|
+
* 并递归安插自身,使 subagent 再委派(孙级)时同样受约束。始终返回 undefined,永不拒绝委派
|
|
45
41
|
* ——是否中断由 `ContextGuardPlugin` 按阈值决定。
|
|
46
42
|
*/
|
|
47
43
|
export class SubAgentGuardInstallerPlugin {
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
isSuspended;
|
|
44
|
+
options;
|
|
45
|
+
/** 选项按**引用**存下:阈值每次委派现读(它可能是一个 getter),不存装配那一刻的快照 */
|
|
51
46
|
constructor(options) {
|
|
52
|
-
this.
|
|
53
|
-
this.isSuspended = options.isSuspended;
|
|
47
|
+
this.options = options;
|
|
54
48
|
}
|
|
55
49
|
onSubAgentStart({ subAgent }) {
|
|
56
|
-
// 与主 Agent 完全同款:同一个 guardTokens 绝对阈值
|
|
57
|
-
subAgent.use(new ContextGuardPlugin({
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}));
|
|
61
|
-
// 传染到孙级:子 Agent 若再委派,其子 Agent 同样被安插。
|
|
62
|
-
// 让行判断也必须跟着递归走,否则只差一层就把「交接中让行」丢了。
|
|
63
|
-
subAgent.use(new SubAgentGuardInstallerPlugin({
|
|
64
|
-
guardTokens: this.guardTokens,
|
|
65
|
-
isSuspended: this.isSuspended,
|
|
66
|
-
}));
|
|
50
|
+
// 与主 Agent 完全同款:同一个 guardTokens 绝对阈值
|
|
51
|
+
subAgent.use(new ContextGuardPlugin({ guardTokens: this.options.guardTokens }));
|
|
52
|
+
// 传染到孙级:subagent 若再委派,其 subagent 同样被安插。
|
|
53
|
+
subAgent.use(new SubAgentGuardInstallerPlugin({ guardTokens: this.options.guardTokens }));
|
|
67
54
|
}
|
|
68
55
|
}
|
|
@@ -9,9 +9,23 @@ import type { Scope } from "../scope/Scope.js";
|
|
|
9
9
|
* - 否则 → 仅提示工具不存在
|
|
10
10
|
*
|
|
11
11
|
* 由调用方显式注册(`agent.use(new UnknownToolHintPlugin({ scope }))`)。
|
|
12
|
-
*
|
|
12
|
+
* 未注册时(或返回 `undefined` 时),core 自己回落到简单文本提示
|
|
13
|
+
* (`未知工具: X,没有找到对应的工具实现。`)。
|
|
13
14
|
*
|
|
14
15
|
* 是否"有 MCP 配置"经 `scope.getPluginById(McpScopePlugin.ID)` 查询 MCP 插件获得。
|
|
16
|
+
*
|
|
17
|
+
* ## 钩子:`onUnknownTool`
|
|
18
|
+
*
|
|
19
|
+
* 「未知工具」与「工具开始」是**两个问题**,core 分别用两个钩子问,一次工具调用只走其中一个:
|
|
20
|
+
* - `onToolStart`:工具**存在**,问「要不要开始跑」;
|
|
21
|
+
* - `onUnknownTool`:工具**不存在**,问「模型调了个不存在的工具,要怎么回应它」。
|
|
22
|
+
*
|
|
23
|
+
* 本插件守的是后一条缝 —— 于是不必再自己判「工具在不在」:来到这里就是不存在。
|
|
24
|
+
* 被调用的工具名与当时全部可用的工具都在入参里(`toolCall.function?.name` /
|
|
25
|
+
* `availableTools`),不必去别处捞。
|
|
26
|
+
*
|
|
27
|
+
* 返回的字符串**就是那条 tool 消息的正文**:模型读到的是引导,而不是默认的
|
|
28
|
+
* 「没有找到对应的工具实现」;返回 `undefined` 则交回 core 走默认文案。
|
|
15
29
|
*/
|
|
16
30
|
export declare class UnknownToolHintPlugin implements AgentPlugin {
|
|
17
31
|
private readonly scope;
|
|
@@ -8,9 +8,23 @@ import { McpScopePlugin } from "../scope/plugins/mcp/McpScopePlugin.js";
|
|
|
8
8
|
* - 否则 → 仅提示工具不存在
|
|
9
9
|
*
|
|
10
10
|
* 由调用方显式注册(`agent.use(new UnknownToolHintPlugin({ scope }))`)。
|
|
11
|
-
*
|
|
11
|
+
* 未注册时(或返回 `undefined` 时),core 自己回落到简单文本提示
|
|
12
|
+
* (`未知工具: X,没有找到对应的工具实现。`)。
|
|
12
13
|
*
|
|
13
14
|
* 是否"有 MCP 配置"经 `scope.getPluginById(McpScopePlugin.ID)` 查询 MCP 插件获得。
|
|
15
|
+
*
|
|
16
|
+
* ## 钩子:`onUnknownTool`
|
|
17
|
+
*
|
|
18
|
+
* 「未知工具」与「工具开始」是**两个问题**,core 分别用两个钩子问,一次工具调用只走其中一个:
|
|
19
|
+
* - `onToolStart`:工具**存在**,问「要不要开始跑」;
|
|
20
|
+
* - `onUnknownTool`:工具**不存在**,问「模型调了个不存在的工具,要怎么回应它」。
|
|
21
|
+
*
|
|
22
|
+
* 本插件守的是后一条缝 —— 于是不必再自己判「工具在不在」:来到这里就是不存在。
|
|
23
|
+
* 被调用的工具名与当时全部可用的工具都在入参里(`toolCall.function?.name` /
|
|
24
|
+
* `availableTools`),不必去别处捞。
|
|
25
|
+
*
|
|
26
|
+
* 返回的字符串**就是那条 tool 消息的正文**:模型读到的是引导,而不是默认的
|
|
27
|
+
* 「没有找到对应的工具实现」;返回 `undefined` 则交回 core 走默认文案。
|
|
14
28
|
*/
|
|
15
29
|
export class UnknownToolHintPlugin {
|
|
16
30
|
scope;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { AppConfig, AgentDefinition, ReasoningPresets } from "../types/index.js";
|
|
2
|
+
import { type LogSink } from "../shared/logger.js";
|
|
2
3
|
/**
|
|
3
4
|
* 配置坏了、已经按出厂恢复之后的回调(参数是坏掉的那个文件路径)。
|
|
4
5
|
*
|
|
@@ -16,18 +17,25 @@ export declare class ConfigManager {
|
|
|
16
17
|
private knownModelIds;
|
|
17
18
|
/** 配置坏了被恢复出厂之后要通知的那一下(不注入就只是恢复,不告诉任何人) */
|
|
18
19
|
private readonly onRecovered?;
|
|
19
|
-
|
|
20
|
+
/** 日志口(不注入就不记):策略表 / 出厂 Skill 读不出来时的那几句话走它 */
|
|
21
|
+
private readonly logger;
|
|
22
|
+
/**
|
|
23
|
+
* @param configPath `config.json` 的路径(`basePath` 取它的同级目录)
|
|
24
|
+
* @param onRecovered 配置被恢复出厂之后的回调(可选)
|
|
25
|
+
* @param logger 日志口(可选):读不出来但已降级的那几处从这里报一句;缺省不记
|
|
26
|
+
*/
|
|
27
|
+
constructor(configPath: string, onRecovered?: ConfigRecoveredHandler, logger?: LogSink);
|
|
20
28
|
/**
|
|
21
29
|
* 读取配置。文件不存在时返回出厂默认配置;文件坏了(解析不了 / 不是对象)时
|
|
22
30
|
* 恢复出厂并写回磁盘,也返回出厂默认配置(见 `readRaw`)。
|
|
23
31
|
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
32
|
+
* 读到即**补位**(`config/normalize.ts`):缺字段或非法值补成出厂值,上层拿到的永远是
|
|
33
|
+
* 合法形状(阈值两条齐、中断策略合法、端点能力位不缺)。
|
|
34
|
+
* 补位结果不在这里落盘——落盘只在 `ensureDefaultConfig()` 做一次。
|
|
27
35
|
*/
|
|
28
36
|
read(): Promise<AppConfig>;
|
|
29
37
|
/**
|
|
30
|
-
*
|
|
38
|
+
* 读磁盘原文(不做任何补位/同步),供 `ensureDefaultConfig()` 对比写盘用。
|
|
31
39
|
*
|
|
32
40
|
* 文件在但读不成配置(JSON 解析不了、或者解析出来不是对象)就是一档「坏」:先把坏文件
|
|
33
41
|
* 改名留档,再用出厂配置写回磁盘,再回调一次,返回出厂配置。写回是为了让下次启动不再
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { promises as fs } from "node:fs";
|
|
2
2
|
import { dirname, join } from "node:path";
|
|
3
|
+
import { SILENT_LOG } from "../shared/logger.js";
|
|
3
4
|
import { DEFAULT_AGENT_ID, DEFAULT_AGENT_DEFINITION, DEFAULT_SUBAGENT_ID, DEFAULT_SUBAGENT_DEFINITION, DEFAULT_APP_CONFIG, DEFAULT_MCP_CONFIG, CONFIG_SUB_DIRS, } from "./constants.js";
|
|
4
5
|
import { FACTORY_SKILLS, isCustomSkill } from "./factory-skills.js";
|
|
5
6
|
import { FACTORY_REASONING_PRESETS, REASONING_PRESETS_FILE } from "./reasoning-presets.js";
|
|
6
|
-
import {
|
|
7
|
+
import { normalizeConfig } from "./normalize.js";
|
|
7
8
|
/**
|
|
8
9
|
* 配置管理器 — 负责配置文件的读写、目录初始化、默认实体创建。
|
|
9
10
|
*/
|
|
@@ -14,10 +15,18 @@ export class ConfigManager {
|
|
|
14
15
|
knownModelIds = new Set();
|
|
15
16
|
/** 配置坏了被恢复出厂之后要通知的那一下(不注入就只是恢复,不告诉任何人) */
|
|
16
17
|
onRecovered;
|
|
17
|
-
|
|
18
|
+
/** 日志口(不注入就不记):策略表 / 出厂 Skill 读不出来时的那几句话走它 */
|
|
19
|
+
logger;
|
|
20
|
+
/**
|
|
21
|
+
* @param configPath `config.json` 的路径(`basePath` 取它的同级目录)
|
|
22
|
+
* @param onRecovered 配置被恢复出厂之后的回调(可选)
|
|
23
|
+
* @param logger 日志口(可选):读不出来但已降级的那几处从这里报一句;缺省不记
|
|
24
|
+
*/
|
|
25
|
+
constructor(configPath, onRecovered, logger = SILENT_LOG) {
|
|
18
26
|
this.configPath = configPath;
|
|
19
27
|
this.basePath = dirname(configPath);
|
|
20
28
|
this.onRecovered = onRecovered;
|
|
29
|
+
this.logger = logger;
|
|
21
30
|
}
|
|
22
31
|
// -----------------------------------------------------------------------
|
|
23
32
|
// config.json
|
|
@@ -26,16 +35,16 @@ export class ConfigManager {
|
|
|
26
35
|
* 读取配置。文件不存在时返回出厂默认配置;文件坏了(解析不了 / 不是对象)时
|
|
27
36
|
* 恢复出厂并写回磁盘,也返回出厂默认配置(见 `readRaw`)。
|
|
28
37
|
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
38
|
+
* 读到即**补位**(`config/normalize.ts`):缺字段或非法值补成出厂值,上层拿到的永远是
|
|
39
|
+
* 合法形状(阈值两条齐、中断策略合法、端点能力位不缺)。
|
|
40
|
+
* 补位结果不在这里落盘——落盘只在 `ensureDefaultConfig()` 做一次。
|
|
32
41
|
*/
|
|
33
42
|
async read() {
|
|
34
43
|
const raw = await this.readRaw();
|
|
35
|
-
return raw ?
|
|
44
|
+
return raw ? normalizeConfig(raw).config : { ...DEFAULT_APP_CONFIG };
|
|
36
45
|
}
|
|
37
46
|
/**
|
|
38
|
-
*
|
|
47
|
+
* 读磁盘原文(不做任何补位/同步),供 `ensureDefaultConfig()` 对比写盘用。
|
|
39
48
|
*
|
|
40
49
|
* 文件在但读不成配置(JSON 解析不了、或者解析出来不是对象)就是一档「坏」:先把坏文件
|
|
41
50
|
* 改名留档,再用出厂配置写回磁盘,再回调一次,返回出厂配置。写回是为了让下次启动不再
|
|
@@ -69,13 +78,8 @@ export class ConfigManager {
|
|
|
69
78
|
return parsed;
|
|
70
79
|
}
|
|
71
80
|
async write(config) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
await fs.access(dir);
|
|
75
|
-
}
|
|
76
|
-
catch {
|
|
77
|
-
await fs.mkdir(dir, { recursive: true });
|
|
78
|
-
}
|
|
81
|
+
// `mkdir(recursive)` 幂等:目录已在时直接返回,不必先用 `access` 探一遍
|
|
82
|
+
await fs.mkdir(dirname(this.configPath), { recursive: true });
|
|
79
83
|
const tmpPath = this.configPath + ".tmp";
|
|
80
84
|
await fs.writeFile(tmpPath, JSON.stringify(config, null, 2), "utf-8");
|
|
81
85
|
await fs.rename(tmpPath, this.configPath);
|
|
@@ -108,9 +112,9 @@ export class ConfigManager {
|
|
|
108
112
|
await this.write(DEFAULT_APP_CONFIG);
|
|
109
113
|
return { ...DEFAULT_APP_CONFIG };
|
|
110
114
|
}
|
|
111
|
-
//
|
|
112
|
-
const
|
|
113
|
-
const synced = syncManagedModels(
|
|
115
|
+
// 先把缺的字段补成出厂值,再同步出厂模型清单;两者结果与磁盘原文不同才写盘
|
|
116
|
+
const normalized = normalizeConfig(onDisk).config;
|
|
117
|
+
const synced = syncManagedModels(normalized);
|
|
114
118
|
if (JSON.stringify(synced) !== JSON.stringify(onDisk)) {
|
|
115
119
|
await this.write(synced);
|
|
116
120
|
}
|
|
@@ -167,8 +171,12 @@ export class ConfigManager {
|
|
|
167
171
|
await fs.writeFile(defaultPath, JSON.stringify(updated, null, 2), "utf-8");
|
|
168
172
|
return updated;
|
|
169
173
|
}
|
|
170
|
-
catch {
|
|
171
|
-
//
|
|
174
|
+
catch (error) {
|
|
175
|
+
// 文件不在 → 正常首次启动,走下面的初始化流程。
|
|
176
|
+
// 读得到却读不出来(不是合法 JSON、权限不对)是另一件事:**不能当成「没有这个文件」** ——
|
|
177
|
+
// 下面的「目录里已有文件就返回 null」会让它静默消失,用户永远不知道自己的定义坏了
|
|
178
|
+
if (error.code !== "ENOENT")
|
|
179
|
+
throw error;
|
|
172
180
|
}
|
|
173
181
|
await fs.mkdir(agentsDir, { recursive: true });
|
|
174
182
|
let existing;
|
|
@@ -201,12 +209,14 @@ export class ConfigManager {
|
|
|
201
209
|
async ensureDefaultSubAgent() {
|
|
202
210
|
const subDir = join(this.basePath, "sub-agents");
|
|
203
211
|
const defaultPath = join(subDir, `${DEFAULT_SUBAGENT_ID}.json`);
|
|
212
|
+
// 不必先 `access`:`readFile` 在文件不在时自己抛 ENOENT
|
|
204
213
|
try {
|
|
205
|
-
await fs.access(defaultPath);
|
|
206
214
|
return JSON.parse(await fs.readFile(defaultPath, "utf-8"));
|
|
207
215
|
}
|
|
208
|
-
catch {
|
|
209
|
-
//
|
|
216
|
+
catch (error) {
|
|
217
|
+
// 只有「本来就没有」才继续往下建;解析不了 / 读不出来要抛,不能当成「没有」
|
|
218
|
+
if (error.code !== "ENOENT")
|
|
219
|
+
throw error;
|
|
210
220
|
}
|
|
211
221
|
await fs.mkdir(subDir, { recursive: true });
|
|
212
222
|
let existing;
|
|
@@ -235,7 +245,7 @@ export class ConfigManager {
|
|
|
235
245
|
*/
|
|
236
246
|
async bootstrap() {
|
|
237
247
|
await this.ensureDirs();
|
|
238
|
-
//
|
|
248
|
+
// 先把配置读到当前形状(内含补位),再从里面取 `defaultModel` 去补默认 Agent 的模型
|
|
239
249
|
const config = await this.ensureDefaultConfig();
|
|
240
250
|
this.knownModelIds = new Set((config.models ?? []).map((m) => m.id));
|
|
241
251
|
const agent = await this.ensureDefaultAgent(config.defaultModel);
|
|
@@ -260,13 +270,18 @@ export class ConfigManager {
|
|
|
260
270
|
*/
|
|
261
271
|
async readReasoningPresets() {
|
|
262
272
|
try {
|
|
263
|
-
const
|
|
264
|
-
|
|
265
|
-
if (parsed && typeof parsed === "object" && !Array.isArray(parsed))
|
|
273
|
+
const parsed = JSON.parse(await fs.readFile(this.reasoningPresetsPath, "utf-8"));
|
|
274
|
+
if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) {
|
|
266
275
|
return parsed;
|
|
276
|
+
}
|
|
277
|
+
this.logger.error("[ConfigManager] 思考策略表不是合法对象,按出厂值提供服务");
|
|
267
278
|
}
|
|
268
|
-
catch {
|
|
269
|
-
//
|
|
279
|
+
catch (error) {
|
|
280
|
+
// 文件还没释放过是首次启动的正常路径,不吵;其余(读不出来、不是 JSON)要说一声 ——
|
|
281
|
+
// 交出厂表是调用方要的(它要一张能用的表),但换了一张表不能装成没换
|
|
282
|
+
if (error.code !== "ENOENT") {
|
|
283
|
+
this.logger.error(`[ConfigManager] 思考策略表读不出来,按出厂值提供服务:${String(error)}`);
|
|
284
|
+
}
|
|
270
285
|
}
|
|
271
286
|
return { ...FACTORY_REASONING_PRESETS };
|
|
272
287
|
}
|
|
@@ -286,8 +301,13 @@ export class ConfigManager {
|
|
|
286
301
|
try {
|
|
287
302
|
onDisk = JSON.parse(await fs.readFile(this.reasoningPresetsPath, "utf-8"));
|
|
288
303
|
}
|
|
289
|
-
catch {
|
|
290
|
-
|
|
304
|
+
catch (error) {
|
|
305
|
+
// 只有「还没有这个文件」可以当成要释放出厂值。文件在却读不出来(不是 JSON、权限不对)
|
|
306
|
+
// 时**不覆盖** —— 那会把用户标了 `custom: true` 的条目一起抹掉,那是「擅自修」
|
|
307
|
+
if (error.code !== "ENOENT") {
|
|
308
|
+
this.logger.error(`[ConfigManager] 思考策略表读不出来,本次不写盘(你的表原样留着):${String(error)}`);
|
|
309
|
+
return [];
|
|
310
|
+
}
|
|
291
311
|
}
|
|
292
312
|
if (!onDisk || typeof onDisk !== "object" || Array.isArray(onDisk)) {
|
|
293
313
|
await fs.mkdir(this.basePath, { recursive: true });
|
|
@@ -328,12 +348,16 @@ export class ConfigManager {
|
|
|
328
348
|
for (const skill of FACTORY_SKILLS) {
|
|
329
349
|
const dir = join(this.basePath, "skills", skill.id);
|
|
330
350
|
const file = join(dir, "SKILL.md");
|
|
351
|
+
// 读不出来 ≠ 不存在:只有 ENOENT 才能当「还没释放过」,否则下面会拿出厂内容把它盖掉
|
|
331
352
|
let existing = null;
|
|
332
353
|
try {
|
|
333
354
|
existing = await fs.readFile(file, "utf-8");
|
|
334
355
|
}
|
|
335
|
-
catch {
|
|
336
|
-
|
|
356
|
+
catch (error) {
|
|
357
|
+
if (error.code !== "ENOENT") {
|
|
358
|
+
this.logger.error(`[ConfigManager] 出厂 Skill「${skill.id}」读不出来,本次不覆盖:${String(error)}`);
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
337
361
|
}
|
|
338
362
|
// 用户声明自有 → 永不覆盖
|
|
339
363
|
if (existing != null && isCustomSkill(existing))
|
|
@@ -418,13 +442,8 @@ export class ConfigManager {
|
|
|
418
442
|
}
|
|
419
443
|
/** 原子写 JSON 文件(先写 .tmp 再 rename,避免半成品)。 */
|
|
420
444
|
async writeJson(path, data) {
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
await fs.access(dir);
|
|
424
|
-
}
|
|
425
|
-
catch {
|
|
426
|
-
await fs.mkdir(dir, { recursive: true });
|
|
427
|
-
}
|
|
445
|
+
// 同上:`mkdir(recursive)` 幂等,不必先探测
|
|
446
|
+
await fs.mkdir(dirname(path), { recursive: true });
|
|
428
447
|
const tmpPath = path + ".tmp";
|
|
429
448
|
await fs.writeFile(tmpPath, JSON.stringify(data, null, 2) + "\n", "utf-8");
|
|
430
449
|
await fs.rename(tmpPath, path);
|
|
@@ -32,7 +32,7 @@ export declare const DEFAULT_MCP_CONFIG: {
|
|
|
32
32
|
/**
|
|
33
33
|
* 标准共享子目录列表(不包含运行时目录)。
|
|
34
34
|
*
|
|
35
|
-
* 各客户端(CLI/Desktop)的运行时数据(config.json、
|
|
35
|
+
* 各客户端(CLI/Desktop)的运行时数据(config.json、last-session.json、preferences.json)
|
|
36
36
|
* 由客户端自行在各自目录下管理:
|
|
37
37
|
* ~/.ai-zen/cli/ ← CLI 运行时
|
|
38
38
|
* ~/.ai-zen/desktop/ ← Desktop 运行时
|
package/dist/config/constants.js
CHANGED
|
@@ -241,7 +241,7 @@ export const DEFAULT_APP_CONFIG = {
|
|
|
241
241
|
defaultImageModel: "cogview-4",
|
|
242
242
|
defaultAgent: "default",
|
|
243
243
|
defaultMigrationModel: "deepseek-flash",
|
|
244
|
-
//
|
|
244
|
+
// 自动标题:新会话第一次回答结束后异步让模型起个名字(缺省开启,纯体验功能,
|
|
245
245
|
// 关掉只省一次几十 token 的请求;缺字段的老配置也按开启算,判定见 `AppConfig.autoTitle`)
|
|
246
246
|
autoTitle: true,
|
|
247
247
|
// 工具输出上限(字符数),与 DEFAULT_MAX_TOOL_OUTPUT 保持一致
|
|
@@ -285,7 +285,7 @@ export const DEFAULT_MCP_CONFIG = {
|
|
|
285
285
|
/**
|
|
286
286
|
* 标准共享子目录列表(不包含运行时目录)。
|
|
287
287
|
*
|
|
288
|
-
* 各客户端(CLI/Desktop)的运行时数据(config.json、
|
|
288
|
+
* 各客户端(CLI/Desktop)的运行时数据(config.json、last-session.json、preferences.json)
|
|
289
289
|
* 由客户端自行在各自目录下管理:
|
|
290
290
|
* ~/.ai-zen/cli/ ← CLI 运行时
|
|
291
291
|
* ~/.ai-zen/desktop/ ← Desktop 运行时
|
|
@@ -39,7 +39,7 @@ export const FALLBACK_HANDOFF_TEMPLATE = [
|
|
|
39
39
|
/** 出厂 Skill:交接(handoff) */
|
|
40
40
|
const HANDOFF_SKILL = `---
|
|
41
41
|
name: handoff
|
|
42
|
-
description: Write a task handoff document — when you stop work and leave the
|
|
42
|
+
description: Write a task handoff document — when you stop work and leave the session, put the task goal, the actual current state, key decisions and next steps into a document that someone else can pick up directly.
|
|
43
43
|
metadata:
|
|
44
44
|
custom: "false"
|
|
45
45
|
---
|
|
@@ -67,7 +67,7 @@ After writing with the write tool, read it back once with the read tool to confi
|
|
|
67
67
|
## Document Structure (write in this order, keep the headings exactly as they are)
|
|
68
68
|
|
|
69
69
|
## 💬 Conversation Breakpoint
|
|
70
|
-
What the user last asked for, and what you last replied with or delivered. One paragraph each; do not restate the whole
|
|
70
|
+
What the user last asked for, and what you last replied with or delivered. One paragraph each; do not restate the whole session.
|
|
71
71
|
|
|
72
72
|
## 🎯 Task Goal
|
|
73
73
|
What the user actually wants (intent and acceptance criteria), not a checklist of the steps you performed.
|