@uncarved/jade 0.3.0 → 0.4.0

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 (207) hide show
  1. package/README.md +1 -0
  2. package/dist/api.d.ts +1 -1
  3. package/dist/api.js +1 -1
  4. package/dist/host.d.ts +2 -0
  5. package/dist/host.js +39 -5
  6. package/dist/host.js.map +1 -1
  7. package/dist/remote.js +5 -0
  8. package/dist/remote.js.map +1 -1
  9. package/dist/session.d.ts +7 -0
  10. package/dist/session.js +482 -27
  11. package/dist/session.js.map +1 -1
  12. package/package.json +4 -4
  13. package/plugins/approval/index.ts +49 -24
  14. package/plugins/approval/policy.ts +48 -25
  15. package/plugins/approval/review.ts +9 -5
  16. package/plugins/checks/checks.ts +10 -7
  17. package/plugins/checks/index.ts +13 -13
  18. package/plugins/checks/plugin.json +1 -1
  19. package/plugins/checks/project.ts +37 -38
  20. package/plugins/commands/index.ts +27 -3
  21. package/plugins/compact/compact.ts +13 -5
  22. package/plugins/compact/index.ts +20 -3
  23. package/plugins/compact/style.ts +13 -14
  24. package/plugins/completions/index.ts +35 -0
  25. package/plugins/completions/plugin.json +7 -0
  26. package/plugins/copy/index.ts +46 -0
  27. package/plugins/copy/plugin.json +7 -0
  28. package/plugins/editor/index.ts +10 -4
  29. package/plugins/fs/diff.ts +7 -6
  30. package/plugins/fs/fs.ts +8 -4
  31. package/plugins/fs/grep.ts +55 -12
  32. package/plugins/fs/index.ts +2 -7
  33. package/plugins/fs/plugin.json +1 -1
  34. package/plugins/fs/renderers.ts +16 -15
  35. package/plugins/fs/style.ts +16 -15
  36. package/plugins/hooks/index.ts +3 -2
  37. package/plugins/keys/index.ts +5 -1
  38. package/plugins/mcp/index.ts +197 -78
  39. package/plugins/mcp/install.ts +5 -3
  40. package/plugins/mcp/mcp.ts +163 -11
  41. package/plugins/mcp/plugin.json +2 -1
  42. package/plugins/mcp/project.ts +47 -50
  43. package/plugins/memory/index.ts +4 -4
  44. package/plugins/memory/style.ts +13 -14
  45. package/plugins/mentions/candidates.ts +67 -0
  46. package/plugins/mentions/index.ts +12 -5
  47. package/plugins/mentions/plugin.json +1 -1
  48. package/plugins/mentions/style.ts +13 -14
  49. package/plugins/models/configure.ts +10 -9
  50. package/plugins/models/index.ts +31 -4
  51. package/plugins/models/provider.ts +12 -1
  52. package/plugins/modes/index.ts +13 -2
  53. package/plugins/print/index.ts +7 -4
  54. package/plugins/prompts/index.ts +0 -0
  55. package/plugins/renderers/index.ts +10 -5
  56. package/plugins/repeats/index.ts +39 -9
  57. package/plugins/schedule/index.ts +2 -0
  58. package/plugins/screenshot/browser.ts +231 -0
  59. package/plugins/screenshot/index.ts +3 -2
  60. package/plugins/screenshot/tool.ts +27 -51
  61. package/plugins/sessions/index.ts +48 -2
  62. package/plugins/shell/index.ts +75 -22
  63. package/plugins/shell/plugin.json +1 -2
  64. package/plugins/shell/renderers.ts +9 -7
  65. package/plugins/shell/shell.ts +93 -34
  66. package/plugins/skills/index.ts +28 -19
  67. package/plugins/skills/project.ts +12 -7
  68. package/plugins/steer/index.ts +5 -4
  69. package/plugins/task/index.ts +81 -81
  70. package/plugins/task/plugin.json +5 -2
  71. package/plugins/task/style.ts +13 -14
  72. package/plugins/task/subagent.ts +67 -184
  73. package/plugins/todo/index.ts +39 -9
  74. package/plugins/tools/index.ts +57 -0
  75. package/plugins/tools/plugin.json +7 -0
  76. package/plugins/tui/frontend.ts +161 -87
  77. package/plugins/tui/index.ts +2 -3
  78. package/plugins/tui/mascot.ts +8 -7
  79. package/plugins/tui/mentions.ts +11 -80
  80. package/plugins/tui/plugin.json +1 -1
  81. package/plugins/tui/screen.ts +55 -5
  82. package/plugins/tui/transcript.ts +17 -7
  83. package/plugins/tui/tui.ts +356 -104
  84. package/plugins/tui/vendor.ts +1 -1
  85. package/plugins/undo/checkpoint.ts +5 -3
  86. package/plugins/undo/index.ts +43 -14
  87. package/plugins/undo/style.ts +13 -14
  88. package/plugins/web/index.ts +7 -18
  89. package/plugins/web/plugin.json +1 -1
  90. package/plugins-dist/approval/index.js +363 -414
  91. package/plugins-dist/approval/policy.js +395 -497
  92. package/plugins-dist/approval/review.js +132 -158
  93. package/plugins-dist/approval/schema.js +50 -55
  94. package/plugins-dist/ask/index.js +115 -129
  95. package/plugins-dist/budget/index.js +27 -31
  96. package/plugins-dist/checks/checks.js +293 -329
  97. package/plugins-dist/checks/index.js +119 -139
  98. package/plugins-dist/checks/plugin.json +2 -1
  99. package/plugins-dist/checks/project.js +57 -65
  100. package/plugins-dist/commands/index.js +47 -29
  101. package/plugins-dist/compact/compact.js +233 -249
  102. package/plugins-dist/compact/index.js +98 -95
  103. package/plugins-dist/compact/style.js +9 -14
  104. package/plugins-dist/completions/index.js +30 -0
  105. package/plugins-dist/completions/plugin.json +9 -0
  106. package/plugins-dist/continue/index.js +40 -52
  107. package/plugins-dist/copy/index.js +40 -0
  108. package/plugins-dist/copy/plugin.json +9 -0
  109. package/plugins-dist/cost/index.js +98 -115
  110. package/plugins-dist/custom-commands/index.js +55 -72
  111. package/plugins-dist/editor/index.js +64 -72
  112. package/plugins-dist/fs/diff.js +111 -124
  113. package/plugins-dist/fs/fs.js +426 -469
  114. package/plugins-dist/fs/grep.js +262 -244
  115. package/plugins-dist/fs/index.js +20 -34
  116. package/plugins-dist/fs/plugin.json +1 -2
  117. package/plugins-dist/fs/renderers.js +100 -112
  118. package/plugins-dist/fs/style.js +11 -15
  119. package/plugins-dist/fs/sys.js +31 -43
  120. package/plugins-dist/hooks/index.js +107 -127
  121. package/plugins-dist/keys/index.js +27 -31
  122. package/plugins-dist/mcp/index.js +271 -199
  123. package/plugins-dist/mcp/install.js +123 -137
  124. package/plugins-dist/mcp/mcp.js +638 -577
  125. package/plugins-dist/mcp/plugin.json +3 -1
  126. package/plugins-dist/mcp/project.js +98 -115
  127. package/plugins-dist/memory/index.js +83 -88
  128. package/plugins-dist/memory/memory.js +111 -128
  129. package/plugins-dist/memory/style.js +9 -14
  130. package/plugins-dist/mentions/attach.js +91 -99
  131. package/plugins-dist/mentions/candidates.js +49 -0
  132. package/plugins-dist/mentions/index.js +71 -81
  133. package/plugins-dist/mentions/plugin.json +2 -1
  134. package/plugins-dist/mentions/style.js +9 -14
  135. package/plugins-dist/models/config.js +29 -43
  136. package/plugins-dist/models/configure.js +392 -473
  137. package/plugins-dist/models/context.js +14 -22
  138. package/plugins-dist/models/index.js +182 -188
  139. package/plugins-dist/models/provider.js +243 -295
  140. package/plugins-dist/models/saved.js +59 -71
  141. package/plugins-dist/modes/index.js +138 -165
  142. package/plugins-dist/plugins/index.js +34 -34
  143. package/plugins-dist/print/index.js +192 -175
  144. package/plugins-dist/prompts/index.js +81 -89
  145. package/plugins-dist/prompts/notes.js +31 -37
  146. package/plugins-dist/prompts/prompts.js +155 -193
  147. package/plugins-dist/prompts/results.js +40 -53
  148. package/plugins-dist/prompts/sys.js +39 -50
  149. package/plugins-dist/renderers/index.js +32 -32
  150. package/plugins-dist/repeats/index.js +134 -151
  151. package/plugins-dist/schedule/cron.js +49 -56
  152. package/plugins-dist/schedule/index.js +229 -247
  153. package/plugins-dist/schedule/store.js +93 -109
  154. package/plugins-dist/scratch/index.js +12 -18
  155. package/plugins-dist/screenshot/browser.js +169 -0
  156. package/plugins-dist/screenshot/index.js +37 -45
  157. package/plugins-dist/screenshot/sys.js +53 -65
  158. package/plugins-dist/screenshot/tool.js +101 -151
  159. package/plugins-dist/sessions/index.js +149 -117
  160. package/plugins-dist/shell/index.js +117 -74
  161. package/plugins-dist/shell/plugin.json +1 -2
  162. package/plugins-dist/shell/renderers.js +28 -33
  163. package/plugins-dist/shell/service.js +17 -23
  164. package/plugins-dist/shell/shell.js +317 -320
  165. package/plugins-dist/shell/sys.js +64 -79
  166. package/plugins-dist/skills/index.js +193 -211
  167. package/plugins-dist/skills/install.js +122 -149
  168. package/plugins-dist/skills/project.js +65 -73
  169. package/plugins-dist/skills/skills.js +135 -171
  170. package/plugins-dist/steer/index.js +108 -125
  171. package/plugins-dist/suggest/index.js +235 -259
  172. package/plugins-dist/task/index.js +130 -148
  173. package/plugins-dist/task/plugin.json +5 -4
  174. package/plugins-dist/task/style.js +9 -14
  175. package/plugins-dist/task/subagent.js +156 -279
  176. package/plugins-dist/todo/index.js +79 -63
  177. package/plugins-dist/tools/index.js +43 -0
  178. package/plugins-dist/tools/plugin.json +9 -0
  179. package/plugins-dist/tui/editor.js +145 -160
  180. package/plugins-dist/tui/frontend.js +641 -655
  181. package/plugins-dist/tui/history.js +34 -37
  182. package/plugins-dist/tui/index.js +16 -18
  183. package/plugins-dist/tui/markdown.js +203 -247
  184. package/plugins-dist/tui/mascot.js +266 -353
  185. package/plugins-dist/tui/mentions.js +67 -144
  186. package/plugins-dist/tui/plugin.json +1 -2
  187. package/plugins-dist/tui/render.js +35 -45
  188. package/plugins-dist/tui/screen.js +197 -180
  189. package/plugins-dist/tui/transcript.js +436 -468
  190. package/plugins-dist/tui/tui.js +1554 -1645
  191. package/plugins-dist/tui/vendor.js +1388 -1088
  192. package/plugins-dist/undo/checkpoint.js +95 -118
  193. package/plugins-dist/undo/index.js +185 -192
  194. package/plugins-dist/undo/snapshot.js +83 -99
  195. package/plugins-dist/undo/style.js +9 -14
  196. package/plugins-dist/update/index.js +26 -28
  197. package/plugins-dist/web/env.js +55 -68
  198. package/plugins-dist/web/index.js +80 -110
  199. package/plugins-dist/web/plugin.json +1 -2
  200. package/plugins-dist/web/renderers.js +4 -5
  201. package/plugins-dist/web/web.js +514 -593
  202. package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
  203. package/sdk/python/jade_plugin.py +9 -2
  204. package/src/api.ts +1 -1
  205. package/src/host.ts +39 -4
  206. package/src/remote.ts +6 -1
  207. package/src/session.ts +489 -34
@@ -1,4 +1,4 @@
1
- // ink 和 react 只经这里导入。构建时 scripts/build-plugins.mjs 把编译出的 vendor.js 连同依赖打成一个文件:
1
+ // ink 和 react 只经这里导入。构建时 scripts/build-plugins.ts 把编译出的 vendor.js 连同依赖打成一个文件:
2
2
  // 逐个解析 ink 和它拉进来的整个 es-toolkit/compat 要约 470ms,打包后约 120ms(docs/review.md #28)。
3
3
  // 两者必须打在同一个文件里,否则 ink 和界面各用一份 react,hooks 会失效。
4
4
  // 只用到类型的从 "ink"/"react" 直接 import type,不经这里。
@@ -1,6 +1,6 @@
1
1
  // 文件快照:write_file / edit_file / multi_edit 执行前保存原内容,/undo 用它恢复。从旧 jade 的 tools/checkpoint.ts 移植。
2
2
  //
3
- // 快照放在 <dir>/<sessionId>/<toolUseId>.json,不进日志:文件可能很大,而撤销是用户的操作,不影响插件决策。
3
+ // 快照放在 <dir>/<sessionId>/<toolUseId>.json(子 Agent 写的放在根会话的目录里,via 是根会话里打开它的调用),不进日志:文件可能很大,而撤销是用户的操作,不影响插件决策。
4
4
  // 哪些修改已经撤销记在同一个目录的 undone.json 里(命令拿不到会话日志),恢复会话后仍然有效。
5
5
 
6
6
  import { createHash } from "node:crypto";
@@ -19,6 +19,8 @@ export interface Checkpoint {
19
19
  afterHash: string;
20
20
  /** 写入完成的时间(毫秒,带小数),决定撤销顺序 */
21
21
  at: number;
22
+ /** 子 Agent(子会话)写的:根会话里打开它的那次工具调用(例如 task)。/undo 按它找到是哪一轮 */
23
+ via?: string;
22
24
  }
23
25
 
24
26
  export const sha256 = (s: string) => createHash("sha256").update(s).digest("hex");
@@ -101,7 +103,7 @@ const UNDONE = "undone.json";
101
103
  * 最近一轮还有没撤销的修改的快照(最新的在前)。一轮从一条用户自己写的消息开始;
102
104
  * 只有工具结果、或者只有插件、宿主加的说明(例如自动续写的提示)的消息不算新的一轮。没有可撤销的返回空数组
103
105
  */
104
- export function lastTurnChanges<T extends { toolUseId: string }>(messages: readonly Message[], checkpoints: readonly T[], undone: ReadonlySet<string>): T[] {
106
+ export function lastTurnChanges<T extends { toolUseId: string; via?: string }>(messages: readonly Message[], checkpoints: readonly T[], undone: ReadonlySet<string>): T[] {
105
107
  const pending = checkpoints.filter((c) => !undone.has(c.toolUseId));
106
108
  if (pending.length === 0) return [];
107
109
  let ids = new Set<string>();
@@ -113,7 +115,7 @@ export function lastTurnChanges<T extends { toolUseId: string }>(messages: reado
113
115
  }
114
116
  const startsTurn = m.content.some((b) => b.kind !== "tool_result" && b.origin.by === "user");
115
117
  if (!startsTurn) continue;
116
- const hits = pending.filter((c) => ids.has(c.toolUseId));
118
+ const hits = pending.filter((c) => ids.has(c.via ?? c.toolUseId));
117
119
  if (hits.length > 0) return hits;
118
120
  ids = new Set();
119
121
  }
@@ -1,9 +1,10 @@
1
1
  // jade/undo:/undo 把模型上一轮改过的文件恢复原样(再执行一次撤销更早的一轮)。
2
2
  //
3
- // jade/fs 不再保存检查点,这里自己观察:tool.before 在 write_file / edit_file / multi_edit 执行前记下文件原来的内容,
3
+ // jade/fs 不再保存检查点,这里自己观察:tool.before 在写文件的工具(meta.writes,例如 write_file)执行前记下文件原来的内容,
4
4
  // tool.after 在执行成功后记下新内容的哈希,存成快照(checkpoint.ts)。tool.before 在审批之前执行,
5
5
  // 所以等用户批准期间别人对文件的改动会算进"原来的内容"。
6
- // shell 命令改的文件:在 git 仓库里,每轮第一次 bash 之前给工作区拍一张快照(snapshot.ts),撤销时把之后变了的文件恢复回去。
6
+ // shell 命令改的文件:在 git 仓库里,每轮第一次执行命令(meta.shell,例如 bash)之前给工作区拍一张快照(snapshot.ts),撤销时把之后变了的文件恢复回去。
7
+ // 认工具靠它注册时给的 meta,不认工具名:换掉提供文件工具的插件照样能撤销。
7
8
 
8
9
  import { readFile } from "node:fs/promises";
9
10
  import { realpathSync } from "node:fs";
@@ -12,10 +13,7 @@ import { definePlugin } from "@uncarved/jade-api";
12
13
  import type { Commands, Prompts } from "@uncarved/jade-api/services";
13
14
  import { Checkpoints, lastTurnChanges, safe, sha256, turnToolNames, type Checkpoint } from "./checkpoint.ts";
14
15
  import { Snapshots, type Change, type Snapshot } from "./snapshot.ts";
15
- import { dim, green } from "./style.ts";
16
-
17
- /** 会改文件、参数里有 path 的工具 */
18
- export const WRITE_TOOLS = new Set(["write_file", "edit_file", "multi_edit"]);
16
+ import { dim, ok } from "./style.ts";
19
17
 
20
18
  /** 没有工作区快照(不在 git 仓库里、配置关掉了)时,shell 命令改了什么无从得知,撤销不了;这一轮跑过时提醒一句 */
21
19
  const SHELL_NOTE = "This turn also ran shell commands; what they changed is not undone.";
@@ -45,6 +43,11 @@ function realPath(p: string): string {
45
43
  }
46
44
  }
47
45
 
46
+ /** 打开着的会话:会话 id → 它的 tool.decided 事件 id → toolUseId(子会话的 parent 只给事件 id) */
47
+ const decidedBy = new Map<string, Map<string, string>>();
48
+ /** 打开着的会话:会话 id → 它的改动记在哪个根会话、算作根会话里哪次调用的 */
49
+ const links = new Map<string, () => { root: string; via?: string }>();
50
+
48
51
  /** 文件现在的内容;不存在时 null */
49
52
  async function contentOf(file: string): Promise<string | null> {
50
53
  try {
@@ -84,9 +87,10 @@ export default definePlugin({
84
87
  await checkpoints.markUndone(s.id, [snap.toolUseId]);
85
88
  }
86
89
  // 这个会话里拍到过快照,说明命令的改动撤销得了,不用提醒
87
- if (cps.length === 0 && !snap) return dim(snaps.length === 0 && turnToolNames(s.state.messages).has("bash") ? `Nothing to undo. ${SHELL_NOTE}` : "Nothing to undo.");
90
+ const ranShell = (names: Set<string>) => [...names].some((n) => s.tool(n)?.meta?.shell !== undefined);
91
+ if (cps.length === 0 && !snap) return dim(snaps.length === 0 && ranShell(turnToolNames(s.state.messages)) ? `Nothing to undo. ${SHELL_NOTE}` : "Nothing to undo.");
88
92
  // 没拍到快照(不在 git 仓库里、配置关掉了)的那一轮跑过命令时提醒一句
89
- const shellToo = !snap && turnToolNames(s.state.messages, cps[0]!.toolUseId).has("bash");
93
+ const shellToo = !snap && ranShell(turnToolNames(s.state.messages, cps[0]!.toolUseId));
90
94
  // 工作区快照覆盖到的文件按快照恢复(那是命令动它之前的内容):拍快照之后文件工具对它的修改不再单独恢复
91
95
  const inTree = new Set(tree.map((c) => same(c.path)));
92
96
  const fileCps = cps.filter((cp) => !(snap && cp.at > snap.at && inTree.has(same(cp.path))));
@@ -135,7 +139,7 @@ export default definePlugin({
135
139
  if (plan.size > MAX_LISTED) what.push(`and ${plan.size - MAX_LISTED} more files`);
136
140
  ctx.service<Prompts>("prompts@1")?.note(s.id, `The user undid your changes from your last turn: ${what.join("; ")}. Read these files again before changing them.`);
137
141
  const title = snap ? `Undid the last turn (${plan.size} file${plan.size > 1 ? "s" : ""}):` : `Undid ${cps.length} change${cps.length > 1 ? "s" : ""} from the last turn:`;
138
- return green(title) + "\n" + dim(list) + (shellToo ? `\n${dim(SHELL_NOTE)}` : "");
142
+ return [ok(title), "\n", dim(list), ...(shellToo ? ["\n", dim(SHELL_NOTE)] : [])];
139
143
  },
140
144
  });
141
145
  },
@@ -143,6 +147,25 @@ export default definePlugin({
143
147
  session(ctx) {
144
148
  const checkpoints = new Checkpoints(path.join(ctx.home, "checkpoints"));
145
149
  const sessionId = ctx.session.id;
150
+ const decided = new Map<string, string>();
151
+ decidedBy.set(sessionId, decided);
152
+ /**
153
+ * 子会话(子 Agent)的改动记在根会话里,算作根会话里打开它的那次调用的:父会话 /undo 时和那一轮的改动一起撤销。
154
+ * 父会话的 tool.decided 在工具执行之前就记下了,所以子会话开好时查得到
155
+ */
156
+ const link = (): { root: string; via?: string } => {
157
+ const p = ctx.session.parent;
158
+ if (!p) return { root: sessionId };
159
+ const up = links.get(p.sessionId)?.();
160
+ if (up?.via) return up;
161
+ const via = decidedBy.get(p.sessionId)?.get(p.eventId);
162
+ return { root: up?.root ?? p.sessionId, ...(via ? { via } : {}) };
163
+ };
164
+ links.set(sessionId, link);
165
+ void ctx.effect(() => () => {
166
+ if (decidedBy.get(sessionId) === decided) decidedBy.delete(sessionId);
167
+ if (links.get(sessionId) === link) links.delete(sessionId);
168
+ });
146
169
  /** toolUseId → 执行前的内容,执行成功后存成快照 */
147
170
  const before = new Map<string, { tool: string; path: string; before: string | null }>();
148
171
  const snapshots = new Snapshots(path.join(ctx.home, "checkpoints", safe(sessionId)), ctx.cwd);
@@ -158,15 +181,17 @@ export default definePlugin({
158
181
  // 第一次给大仓库拍快照要把每个文件算一遍哈希
159
182
  timeoutMs: SNAPSHOT_TIMEOUT_MS,
160
183
  fn: async (call) => {
161
- // 每轮第一次跑命令之前给工作区拍一张快照:命令改了什么,/undo 时和它一比就知道
162
- if (call.name === "bash" && ctx.config.shell !== false && !snapped) {
184
+ const meta = ctx.session.tool(call.name)?.meta;
185
+ // 每轮第一次跑命令之前给工作区拍一张快照:命令改了什么,/undo 时和它一比就知道。
186
+ // 子会话不拍:并行的几个子 Agent 不能同时用根会话的影子仓库(子 Agent 的命令改的文件撤销不了)
187
+ if (meta?.shell !== undefined && ctx.config.shell !== false && !snapped && !ctx.session.parent) {
163
188
  snapped = true;
164
189
  const tree = await snapshots.take();
165
190
  if (tree) await snapshots.save({ toolUseId: call.toolUseId, tree, at: performance.timeOrigin + performance.now() });
166
191
  return;
167
192
  }
168
- if (!WRITE_TOOLS.has(call.name)) return;
169
- const p = (call.input as { path?: unknown } | null)?.path;
193
+ if (meta?.writes === undefined) return;
194
+ const p = (call.input as Record<string, unknown> | null)?.[meta.writes];
170
195
  if (typeof p !== "string") return;
171
196
  const full = path.resolve(ctx.cwd, p);
172
197
  before.set(call.toolUseId, { tool: call.name, path: full, before: await contentOf(full) });
@@ -184,7 +209,8 @@ export default definePlugin({
184
209
  const after = await contentOf(snap.path);
185
210
  // 内容没变(例如同样的内容重复写入)不算修改
186
211
  if (after === snap.before) return;
187
- await checkpoints.save(sessionId, { toolUseId: out.call.toolUseId, ...snap, afterHash: sha256(after ?? "") });
212
+ const { root, via } = link();
213
+ await checkpoints.save(root, { toolUseId: out.call.toolUseId, ...snap, afterHash: sha256(after ?? ""), ...(via ? { via } : {}) });
188
214
  },
189
215
  });
190
216
  // 被拒绝、被中断的调用不经过 tool.after:一轮结束时还留着的都不会再用到,不然整个文件的内容一直占着内存
@@ -192,8 +218,11 @@ export default definePlugin({
192
218
  kind: "observe",
193
219
  point: "event",
194
220
  fn: (e) => {
221
+ if (e.type === "tool.decided") decided.set(e.id, (e.data as { toolUseId: string }).toolUseId);
195
222
  if (e.type !== "turn.ended") return;
196
223
  before.clear();
224
+ // 只有这一轮里开的子会话查它(子会话在打开它的那次调用里结束)
225
+ decided.clear();
197
226
  snapped = false;
198
227
  },
199
228
  });
@@ -1,17 +1,16 @@
1
- // 显示用的小工具:截断和 ANSI 颜色。插件之间不共享代码(只能共享类型),所以这里也有一份(同 jade/fs 的 style.ts)。
1
+ // 显示用的小工具:截断,和带语义样式的文字(jade-api 的 Text)。插件只说这段是次要的、成功、警告……,
2
+ // 怎么画由前端决定(终端界面画成颜色,-p 只取文字)。插件之间不共享代码(只能共享类型),所以每个插件有一份。
3
+
4
+ import type { Span, Text, TextStyle } from "@uncarved/jade-api";
2
5
 
3
6
  export const clip = (s: string, n: number) => (s.length > n ? s.slice(0, n) + "…" : s);
4
7
 
5
- // 设了 NO_COLOR(非空)或标准输出不是终端时不输出颜色,见 https://no-color.org
6
- const colors = Boolean(process.stdout.isTTY) && !process.env.NO_COLOR;
7
- /**
8
- * 每种样式用自己的关闭码,不用 \x1b[0m 全部重置:外面套着别的样式时,后面的文字仍保持外层样式。
9
- * 里面出现同一个关闭码(例如 dim 里套 bold,两者都用 22 关闭)时紧接着重新打开外层的样式
10
- */
11
- const paint = (open: string, close: string) => (s: string) =>
12
- colors ? `\x1b[${open}m${s.replaceAll(`\x1b[${close}m`, `\x1b[${close}m\x1b[${open}m`)}\x1b[${close}m` : s;
13
- export const dim = paint("2", "22");
14
- export const red = paint("31", "39");
15
- export const green = paint("32", "39");
16
- export const yellow = paint("33", "39");
17
- export const bold = paint("1", "22");
8
+ const styled = (style: TextStyle) => (text: string): Span => ({ text, style });
9
+ export const dim = styled("dim");
10
+ export const bold = styled("bold");
11
+ export const ok = styled("ok");
12
+ export const warn = styled("warn");
13
+ export const error = styled("error");
14
+
15
+ /** 几行接成一段,行之间换行 */
16
+ export const lines = (rows: readonly Text[]): Text => rows.flatMap((r, i) => (i ? ["\n", r] : [r]));
@@ -5,16 +5,15 @@
5
5
  // 不用重开会话;会话的工具清单在创建时就定了。
6
6
 
7
7
  import path from "node:path";
8
- import { definePlugin, type ScopeContext, type UI } from "@uncarved/jade-api";
9
- import type { Commands, Prompts, Renderers, Task } from "@uncarved/jade-api/services";
8
+ import { definePlugin, type ScopeContext, type Span, type UI } from "@uncarved/jade-api";
9
+ import type { Commands, Prompts, Renderers } from "@uncarved/jade-api/services";
10
10
  import { envWithFile, updateEnvFile, type Env } from "./env.ts";
11
11
  import { webRenderers } from "./renderers.ts";
12
12
  import { parseOrder, parseResults, SEARCH_PROVIDERS, webTools } from "./web.ts";
13
13
 
14
- // 设了 NO_COLOR(非空)或标准输出不是终端时不输出颜色,见 https://no-color.org
15
- const colors = Boolean(process.stdout.isTTY) && !process.env.NO_COLOR;
16
- const dim = (s: string) => (colors ? `\x1b[2m${s}\x1b[22m` : s);
17
- const green = (s: string) => (colors ? `\x1b[32m${s}\x1b[39m` : s);
14
+ // 带语义样式的文字(jade-api 的 Text):怎么画由前端决定
15
+ const dim = (text: string): Span => ({ text, style: "dim" });
16
+ const ok = (text: string): Span => ({ text, style: "ok" });
18
17
 
19
18
  /**
20
19
  * API key 所在的环境:每次开会话时重新读,/search 改了之后原地更新。模块每个进程只加载一次,进程级的命令和会话级的工具共用
@@ -79,7 +78,7 @@ export default definePlugin({
79
78
  await updateEnvFile(file, { [p.env]: key }, env);
80
79
  offNote?.();
81
80
  offNote = undefined;
82
- return green(`Web search is on with ${p.name}: web_search uses it, and web_fetch falls back to it when a page cannot be fetched directly.`);
81
+ return ok(`Web search is on with ${p.name}: web_search uses it, and web_fetch falls back to it when a page cannot be fetched directly.`);
83
82
  },
84
83
  });
85
84
  },
@@ -98,17 +97,7 @@ export default definePlugin({
98
97
  const web = webTools({ ...opts, env: () => keys ?? env });
99
98
  for (const tool of web.tools) ctx.kernel.tool(tool);
100
99
 
101
- // 子 Agent(jade/task)也能查网页:另一套实例;web_search 只在配置了搜索服务时给它。
102
- // web_fetch 带 url 参数,子 Agent 每次都要问用户(URL 能把读到的内容带出去)。
103
- // jade/task 被隔离时它的服务已失效:子 Agent 没有 web 工具,web 工具本身照常
104
- try {
105
- const task = ctx.service<Task>("task@1");
106
- if (task) {
107
- const sub = webTools({ ...opts, env: () => keys ?? env });
108
- for (const tool of sub.tools) task.tool(tool, tool.name === "web_search" ? { when: () => sub.search().length > 0 } : {});
109
- }
110
- } catch {}
111
-
100
+ // 子 Agent 是核心开的子会话,本插件在里面另有一份,web_fetch 经过和主会话同一套审批
112
101
  ctx.kernel.hook({
113
102
  kind: "transform",
114
103
  point: "context.build",
@@ -3,7 +3,7 @@
3
3
  "version": "0.3.0",
4
4
  "api": "^1",
5
5
  "entry": "./index.ts",
6
- "optional": ["ui@1", "commands@1", "prompts@1", "renderers@1", "task@1"],
6
+ "optional": ["ui@1", "commands@1", "prompts@1", "renderers@1"],
7
7
  "config": {
8
8
  "search": {
9
9
  "description": "search services to use, in order: a list or comma-separated names (tinyfish, anysearch, tavily, exa, firecrawl, jina); default: every service with an API key. Keys are read from the environment and $JADE_HOME/.env (/search sets them). $JADE_SEARCH still works"