@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,18 +1,28 @@
1
1
  import { resolve as resolvePath } from "node:path";
2
+ import { AgentNS } from "@ai-zen/agents-core";
3
+ import { WRITE_TOOL_NAME } from "../scope/plugins/builtin/WriteTool.js";
4
+ /** 交接那条指令的来源标签(`HandoffService` 投递时填的就是它,这里按同一个值认回来) */
5
+ export const HANDOFF_SOURCE_LABEL = "handoff";
2
6
  /**
3
- * 交接回合的工具守卫 —— 保证「原 agent 停下手头一切工作」不只是提示词约定。
7
+ * 交接期间的守卫:门 + 那次写文档的观察点。
4
8
  *
5
- * 交接由**同一个 agent** 在自己的会话里完成,它天然有继续干活的动力。
6
- * 本插件在交接回合期间拦截写入类工具,只放行对交接文档那一份文件的写入:
7
- * - `write`:仅当目标路径(按交接时给定的 cwd 归一后)等于交接文档路径时放行;
8
- * - `edit` / `batch_edit` / `rm` / `rename` / `mkdir` / `copy`:一律拒绝;
9
- * - 读取类工具与其他工具:放行。
9
+ * 交接由**同一个 agent** 在自己的会话里完成,它天然有继续干活的动力,所以两件事都得在这里:
10
10
  *
11
- * 拒绝方式是**软拒绝**(返回字符串):core 的 `onToolStart` 语义会把该字符串作为那条
12
- * tool 消息的正文回给模型并继续下一圈,因此模型能读到引导(「只允许写交接文档」)并转回正题。
13
- * 反例:抛错会穿到 `start()` 外面把整个循环打死(agent 变成僵尸),整轮交接直接停摆。
11
+ * 1. **门**(PRD 3.6):只放行对交接文档那一份文件的写入 ——
12
+ * - `write`:仅当目标路径(按交接时给定的 cwd 归一后)等于交接文档路径时放行;
13
+ * - `edit` / `batch_edit` / `rm` / `rename` / `mkdir` / `copy`:拒绝;
14
+ * - 读取类工具与其他工具:放行。
15
+ * 拒绝方式是**软拒绝**(返回字符串):core 会把该字符串作为那条 tool 消息的正文回给模型并
16
+ * 接着跑,模型读到引导(「只允许写交接文档」)能自己转回正题。反例:抛错会穿到 `start()`
17
+ * 外面把整个循环打死。
18
+ * 2. **那次写文档的结果**:`onToolEnd` 认出「写文件的工具 + 路径等于交接路径」那一次,结果
19
+ * 回来就落定;这一批收口了还没见到它,就是「没写出来」。判据是那次调用的输入与结果,
20
+ * 不是状态,也不是模型的话。
21
+ * 3. **关输出闸**:指令被取走那一拍关掉。**开闸不在 SDK**:开闸的时刻是「记忆换定了」,
22
+ * 而换记忆在宿主那一侧(SDK 这一趟只产出文档),由宿主在交接收口开。
23
+ * 「用户按了停」不靠这道闸认,靠 `onAbort`(`abort()` 有自己的事实,闸只是它的副作用)。
14
24
  *
15
- * 边界(必须如实记录,不要误以为是安全边界):
25
+ * 边界(不是安全边界):
16
26
  * - **挡不住 `exec`**:`echo > file`、`cat > file <<EOF` 永远能绕过。本插件降低的是
17
27
  * 「顺手改代码」的误操作概率,不是沙箱。
18
28
  * - **料不到的参数不算放行**:参数解析失败时 `parsedArgs` 为 `undefined`,`write` 会被
@@ -21,7 +31,7 @@ import { resolve as resolvePath } from "node:path";
21
31
  * 故用「同一实例 + 开关」而非反复注册,避免插件数组无限增长。
22
32
  */
23
33
  export class HandoffGuardPlugin {
24
- /** 写入类工具(除白名单的 write 外一律拒绝) */
34
+ /** 写入类工具(门会拒掉这些;`write` 单独判路径) */
25
35
  static WRITE_TOOLS = new Set([
26
36
  "edit",
27
37
  "batch_edit",
@@ -34,29 +44,67 @@ export class HandoffGuardPlugin {
34
44
  handoffPath;
35
45
  /** 相对路径解析基准(交接时 agent 的 cwd) */
36
46
  cwd = process.cwd();
37
- /** 进入交接回合:仅允许写入 handoffPath(相对路径按 cwd 归一) */
47
+ /** 结果落定的那一手(已经有结论之后再来的不算) */
48
+ conclude;
49
+ /** 这一批收口的那一手 */
50
+ concludeRound;
51
+ /** 那条指令进了这一批没有:没认出它,这一批的收口就与我无关 */
52
+ carrying = false;
53
+ /**
54
+ * 进入交接:只允许写 handoffPath,并交出这一趟的两个观察点。
55
+ *
56
+ * 每次交接都重造一份承诺:上一个交接的结论不留给下一个看。
57
+ */
38
58
  arm(handoffPath, cwd) {
39
59
  this.cwd = cwd ? resolvePath(cwd) : process.cwd();
40
60
  this.handoffPath = resolvePath(this.cwd, handoffPath);
61
+ this.carrying = false;
62
+ let settled = false;
63
+ let ended = false;
64
+ const outcome = new Promise((resolve) => {
65
+ this.conclude = (value) => {
66
+ if (settled)
67
+ return;
68
+ settled = true;
69
+ resolve(value);
70
+ };
71
+ });
72
+ const over = new Promise((resolve) => {
73
+ this.concludeRound = () => {
74
+ if (ended)
75
+ return;
76
+ ended = true;
77
+ resolve();
78
+ };
79
+ });
80
+ return { outcome, over };
41
81
  }
42
- /** 离开交接回合:恢复为完全让行 */
82
+ /**
83
+ * 离开交接:完全让行。
84
+ *
85
+ * 还没落定的两件承诺在这里一并给个答案 —— 谁在等它们,都不该因为收摊而永远等下去。
86
+ */
43
87
  disarm() {
88
+ this.conclude?.("missing");
89
+ this.concludeRound?.();
44
90
  this.handoffPath = undefined;
91
+ this.carrying = false;
92
+ this.conclude = undefined;
93
+ this.concludeRound = undefined;
45
94
  }
46
95
  /** 是否处于交接回合(供上层/测试观察) */
47
96
  get armed() {
48
97
  return this.handoffPath !== undefined;
49
98
  }
99
+ /** 门:交接期间只放行写这份文档的那一次工具调用 */
50
100
  onToolStart({ toolCall }) {
51
101
  const handoffPath = this.handoffPath;
52
102
  if (handoffPath == null)
53
103
  return;
54
104
  const name = toolCall.tool_call?.function?.name ?? "";
55
- if (name === "write") {
56
- const raw = toolCall.parsedArgs?.path;
57
- if (typeof raw === "string" && resolvePath(this.cwd, raw) === handoffPath) {
105
+ if (name === WRITE_TOOL_NAME) {
106
+ if (this.documentPathOf(toolCall) === handoffPath)
58
107
  return;
59
- }
60
108
  return `During the handoff round the only allowed write is the handoff document: ${handoffPath} (pass that path to write exactly as given), start writing now.`;
61
109
  }
62
110
  if (HandoffGuardPlugin.WRITE_TOOLS.has(name)) {
@@ -64,4 +112,75 @@ export class HandoffGuardPlugin {
64
112
  }
65
113
  return;
66
114
  }
115
+ /**
116
+ * 那次写文档的结果回来了:跑完就是「写完了」,标成 Aborted 就是「被掐」。
117
+ *
118
+ * 只看「工具名 + 路径」对得上的那一次:别的写操作要么被门拒了、要么与这份文档无关。
119
+ */
120
+ onToolEnd({ toolCall }) {
121
+ if (this.handoffPath == null)
122
+ return;
123
+ if (this.documentPathOf(toolCall) !== this.handoffPath)
124
+ return;
125
+ const status = toolCall.resultMessage?.status;
126
+ this.conclude?.(status === AgentNS.MessageStatus.Aborted ? "interrupted" : "written");
127
+ }
128
+ /**
129
+ * 这一批从队里取走了:里面带着交接那条指令 —— 这一批的收口才算我的,并且**从这一拍起关掉输出闸**。
130
+ *
131
+ * 为什么关:交接要换的是整份记忆,换之前落进来的话会被一起挪进存档,等于从新记忆里消失。
132
+ * 时机只能在这一拍:指令已经取出来了(`onOuterLoopStart` 在落地之后、内循环之前),
133
+ * 而下一批还没开始取;投递那一刻关会把自己这条指令一起关在外面。
134
+ */
135
+ onOuterLoopStart({ agent, entries }) {
136
+ if (this.handoffPath == null)
137
+ return;
138
+ if (!entries.some((entry) => isHandoffEntry(entry)))
139
+ return;
140
+ this.carrying = true;
141
+ agent.pauseOutput();
142
+ }
143
+ /**
144
+ * 这一批收口:还没见到那次写就是「没写出来」,顺带把「这一批完了」告诉等着的人。
145
+ *
146
+ * 收口只能由 core 的钩子报:那一拍之后这一批的消息不再增长,谁要接着动这份记忆都得等它。
147
+ */
148
+ onOuterLoopEnd() {
149
+ if (this.handoffPath == null || !this.carrying)
150
+ return;
151
+ this.carrying = false;
152
+ this.conclude?.("missing");
153
+ this.concludeRound?.();
154
+ }
155
+ /** 有人按了停:这一趟不写了,也不算写失败(`onAbort` 是意图本身,不是闸的副作用) */
156
+ onAbort() {
157
+ if (this.handoffPath == null)
158
+ return;
159
+ this.conclude?.("interrupted");
160
+ }
161
+ /** 关门 / 硬杀:这一段已经不在了 */
162
+ onStop() {
163
+ if (this.handoffPath == null)
164
+ return;
165
+ this.conclude?.("gone");
166
+ }
167
+ onKill() {
168
+ if (this.handoffPath == null)
169
+ return;
170
+ this.conclude?.("gone");
171
+ }
172
+ /** 这一次工具调用写的是哪个路径(不是 `write`、或参数里没有路径 → undefined) */
173
+ documentPathOf(toolCall) {
174
+ if (toolCall.tool_call?.function?.name !== WRITE_TOOL_NAME)
175
+ return undefined;
176
+ const raw = toolCall.parsedArgs?.path;
177
+ if (typeof raw !== "string" || raw.trim() === "")
178
+ return undefined;
179
+ return resolvePath(this.cwd, raw);
180
+ }
181
+ }
182
+ /** 这一条入队的东西是不是交接那条指令(按来源标签认,不看正文) */
183
+ function isHandoffEntry(entry) {
184
+ const source = entry.source;
185
+ return source?.kind === "system" && source.label === HANDOFF_SOURCE_LABEL;
67
186
  }