@uncarved/jade 0.2.0 → 0.3.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 (224) hide show
  1. package/README.md +277 -0
  2. package/assets/logo.svg +1 -0
  3. package/bin/jade.js +15 -0
  4. package/dist/api.d.ts +5 -250
  5. package/dist/api.js +5 -7
  6. package/dist/api.js.map +1 -1
  7. package/dist/fetch.d.ts +8 -0
  8. package/dist/fetch.js +19 -17
  9. package/dist/fetch.js.map +1 -1
  10. package/dist/host.d.ts +12 -1
  11. package/dist/host.js +49 -1
  12. package/dist/host.js.map +1 -1
  13. package/dist/index.d.ts +1 -1
  14. package/dist/index.js +1 -1
  15. package/dist/index.js.map +1 -1
  16. package/dist/main.js +28 -5
  17. package/dist/main.js.map +1 -1
  18. package/dist/remote.js +4 -0
  19. package/dist/remote.js.map +1 -1
  20. package/dist/session.d.ts +2 -0
  21. package/dist/session.js +133 -13
  22. package/dist/session.js.map +1 -1
  23. package/dist/source.d.ts +2 -10
  24. package/dist/source.js.map +1 -1
  25. package/dist/update.d.ts +8 -20
  26. package/dist/update.js +12 -1
  27. package/dist/update.js.map +1 -1
  28. package/package.json +6 -4
  29. package/plugins/approval/index.ts +246 -84
  30. package/plugins/approval/plugin.json +5 -1
  31. package/plugins/approval/policy.ts +27 -6
  32. package/plugins/approval/review.ts +1 -1
  33. package/plugins/ask/index.ts +2 -2
  34. package/plugins/budget/index.ts +1 -1
  35. package/plugins/checks/checks.ts +3 -2
  36. package/plugins/checks/index.ts +2 -5
  37. package/plugins/commands/index.ts +2 -44
  38. package/plugins/compact/compact.ts +15 -4
  39. package/plugins/compact/index.ts +13 -16
  40. package/plugins/continue/index.ts +1 -1
  41. package/plugins/cost/index.ts +143 -0
  42. package/plugins/cost/plugin.json +13 -0
  43. package/plugins/custom-commands/index.ts +85 -0
  44. package/plugins/custom-commands/plugin.json +8 -0
  45. package/plugins/editor/index.ts +88 -0
  46. package/plugins/editor/plugin.json +17 -0
  47. package/plugins/fs/fs.ts +110 -17
  48. package/plugins/fs/grep.ts +41 -11
  49. package/plugins/fs/index.ts +9 -7
  50. package/plugins/fs/renderers.ts +1 -1
  51. package/plugins/hooks/index.ts +152 -0
  52. package/plugins/hooks/plugin.json +14 -0
  53. package/plugins/keys/index.ts +2 -18
  54. package/plugins/mcp/index.ts +40 -12
  55. package/plugins/mcp/install.ts +139 -0
  56. package/plugins/mcp/mcp.ts +2 -1
  57. package/plugins/mcp/plugin.json +1 -1
  58. package/plugins/mcp/project.ts +37 -1
  59. package/plugins/memory/index.ts +4 -9
  60. package/plugins/memory/plugin.json +1 -1
  61. package/plugins/mentions/index.ts +33 -13
  62. package/plugins/models/configure.ts +258 -150
  63. package/plugins/models/context.ts +2 -2
  64. package/plugins/models/index.ts +50 -42
  65. package/plugins/models/plugin.json +1 -1
  66. package/plugins/models/provider.ts +22 -4
  67. package/plugins/models/saved.ts +8 -4
  68. package/plugins/modes/index.ts +2 -17
  69. package/plugins/plugins/index.ts +2 -2
  70. package/plugins/print/index.ts +60 -12
  71. package/plugins/print/plugin.json +1 -0
  72. package/plugins/prompts/index.ts +50 -14
  73. package/plugins/{memory → prompts}/notes.ts +21 -9
  74. package/plugins/prompts/plugin.json +8 -2
  75. package/plugins/prompts/prompts.ts +14 -8
  76. package/plugins/prompts/results.ts +63 -0
  77. package/plugins/prompts/sys.ts +16 -4
  78. package/plugins/renderers/index.ts +2 -43
  79. package/plugins/repeats/index.ts +1 -1
  80. package/plugins/schedule/index.ts +2 -2
  81. package/plugins/scratch/index.ts +2 -3
  82. package/plugins/screenshot/index.ts +26 -13
  83. package/plugins/screenshot/plugin.json +1 -1
  84. package/plugins/sessions/index.ts +2 -17
  85. package/plugins/shell/index.ts +56 -6
  86. package/plugins/shell/plugin.json +3 -1
  87. package/plugins/shell/renderers.ts +1 -1
  88. package/plugins/shell/service.ts +2 -13
  89. package/plugins/shell/shell.ts +31 -16
  90. package/plugins/skills/index.ts +172 -37
  91. package/plugins/skills/install.ts +212 -0
  92. package/plugins/skills/plugin.json +3 -2
  93. package/plugins/skills/project.ts +38 -1
  94. package/plugins/skills/skills.ts +28 -9
  95. package/plugins/steer/index.ts +142 -0
  96. package/plugins/steer/plugin.json +8 -0
  97. package/plugins/suggest/index.ts +16 -11
  98. package/plugins/suggest/plugin.json +1 -1
  99. package/plugins/task/index.ts +8 -16
  100. package/plugins/task/plugin.json +9 -2
  101. package/plugins/task/subagent.ts +119 -15
  102. package/plugins/todo/index.ts +4 -4
  103. package/plugins/tui/editor.ts +10 -2
  104. package/plugins/tui/frontend.ts +130 -25
  105. package/plugins/tui/index.ts +3 -8
  106. package/plugins/tui/markdown.ts +51 -6
  107. package/plugins/tui/mascot.ts +505 -0
  108. package/plugins/tui/mentions.ts +48 -8
  109. package/plugins/tui/plugin.json +2 -2
  110. package/plugins/tui/screen.ts +6 -0
  111. package/plugins/tui/transcript.ts +22 -3
  112. package/plugins/tui/tui.ts +323 -95
  113. package/plugins/tui/vendor.ts +16 -0
  114. package/plugins/undo/checkpoint.ts +26 -2
  115. package/plugins/undo/index.ts +86 -24
  116. package/plugins/undo/plugin.json +8 -2
  117. package/plugins/undo/snapshot.ts +129 -0
  118. package/plugins/update/index.ts +11 -15
  119. package/plugins/update/plugin.json +2 -1
  120. package/plugins/web/index.ts +2 -5
  121. package/plugins/web/renderers.ts +1 -1
  122. package/plugins/web/web.ts +6 -5
  123. package/plugins-dist/approval/index.js +232 -50
  124. package/plugins-dist/approval/plugin.json +5 -1
  125. package/plugins-dist/approval/policy.js +27 -5
  126. package/plugins-dist/ask/index.js +1 -1
  127. package/plugins-dist/budget/index.js +1 -1
  128. package/plugins-dist/checks/checks.js +2 -1
  129. package/plugins-dist/checks/index.js +1 -1
  130. package/plugins-dist/commands/index.js +1 -1
  131. package/plugins-dist/compact/compact.js +13 -2
  132. package/plugins-dist/compact/index.js +12 -8
  133. package/plugins-dist/continue/index.js +1 -1
  134. package/plugins-dist/cost/index.js +135 -0
  135. package/plugins-dist/cost/plugin.json +16 -0
  136. package/plugins-dist/custom-commands/index.js +86 -0
  137. package/plugins-dist/custom-commands/plugin.json +12 -0
  138. package/plugins-dist/editor/index.js +89 -0
  139. package/plugins-dist/editor/plugin.json +21 -0
  140. package/plugins-dist/fs/fs.js +106 -17
  141. package/plugins-dist/fs/grep.js +40 -12
  142. package/plugins-dist/fs/index.js +8 -5
  143. package/plugins-dist/hooks/index.js +145 -0
  144. package/plugins-dist/hooks/plugin.json +19 -0
  145. package/plugins-dist/keys/index.js +1 -1
  146. package/plugins-dist/mcp/index.js +41 -9
  147. package/plugins-dist/mcp/install.js +160 -0
  148. package/plugins-dist/mcp/mcp.js +2 -1
  149. package/plugins-dist/mcp/plugin.json +2 -1
  150. package/plugins-dist/mcp/project.js +42 -1
  151. package/plugins-dist/memory/index.js +3 -5
  152. package/plugins-dist/memory/plugin.json +1 -1
  153. package/plugins-dist/mentions/index.js +29 -11
  154. package/plugins-dist/models/configure.js +252 -151
  155. package/plugins-dist/models/context.js +2 -2
  156. package/plugins-dist/models/index.js +53 -16
  157. package/plugins-dist/models/plugin.json +1 -1
  158. package/plugins-dist/models/provider.js +4 -2
  159. package/plugins-dist/models/saved.js +9 -4
  160. package/plugins-dist/modes/index.js +1 -1
  161. package/plugins-dist/plugins/index.js +1 -1
  162. package/plugins-dist/print/index.js +62 -9
  163. package/plugins-dist/print/plugin.json +3 -0
  164. package/plugins-dist/prompts/index.js +49 -2
  165. package/plugins-dist/{memory → prompts}/notes.js +10 -7
  166. package/plugins-dist/prompts/plugin.json +8 -2
  167. package/plugins-dist/prompts/prompts.js +13 -8
  168. package/plugins-dist/prompts/results.js +60 -0
  169. package/plugins-dist/prompts/sys.js +15 -3
  170. package/plugins-dist/renderers/index.js +1 -1
  171. package/plugins-dist/repeats/index.js +1 -1
  172. package/plugins-dist/schedule/index.js +1 -1
  173. package/plugins-dist/scratch/index.js +1 -1
  174. package/plugins-dist/screenshot/index.js +25 -11
  175. package/plugins-dist/screenshot/plugin.json +1 -1
  176. package/plugins-dist/sessions/index.js +1 -1
  177. package/plugins-dist/shell/index.js +55 -3
  178. package/plugins-dist/shell/plugin.json +3 -1
  179. package/plugins-dist/shell/shell.js +26 -8
  180. package/plugins-dist/skills/index.js +184 -35
  181. package/plugins-dist/skills/install.js +178 -0
  182. package/plugins-dist/skills/plugin.json +6 -2
  183. package/plugins-dist/skills/project.js +43 -1
  184. package/plugins-dist/skills/skills.js +27 -10
  185. package/plugins-dist/steer/index.js +135 -0
  186. package/plugins-dist/steer/plugin.json +12 -0
  187. package/plugins-dist/suggest/index.js +15 -9
  188. package/plugins-dist/suggest/plugin.json +1 -1
  189. package/plugins-dist/task/index.js +7 -4
  190. package/plugins-dist/task/plugin.json +10 -2
  191. package/plugins-dist/task/subagent.js +106 -15
  192. package/plugins-dist/todo/index.js +3 -3
  193. package/plugins-dist/tui/editor.js +7 -2
  194. package/plugins-dist/tui/frontend.js +135 -18
  195. package/plugins-dist/tui/index.js +2 -1
  196. package/plugins-dist/tui/markdown.js +48 -6
  197. package/plugins-dist/tui/mascot.js +439 -0
  198. package/plugins-dist/tui/mentions.js +44 -6
  199. package/plugins-dist/tui/plugin.json +3 -2
  200. package/plugins-dist/tui/screen.js +2 -0
  201. package/plugins-dist/tui/transcript.js +19 -2
  202. package/plugins-dist/tui/tui.js +303 -83
  203. package/plugins-dist/tui/vendor.js +13822 -0
  204. package/plugins-dist/undo/checkpoint.js +28 -1
  205. package/plugins-dist/undo/index.js +92 -22
  206. package/plugins-dist/undo/plugin.json +10 -3
  207. package/plugins-dist/undo/snapshot.js +111 -0
  208. package/plugins-dist/update/index.js +9 -10
  209. package/plugins-dist/update/plugin.json +4 -1
  210. package/plugins-dist/web/index.js +1 -1
  211. package/plugins-dist/web/web.js +6 -5
  212. package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
  213. package/sdk/python/jade_plugin.py +8 -0
  214. package/src/api.ts +7 -262
  215. package/src/fetch.ts +18 -16
  216. package/src/host.ts +49 -1
  217. package/src/index.ts +1 -1
  218. package/src/main.ts +25 -6
  219. package/src/remote.ts +2 -0
  220. package/src/session.ts +134 -15
  221. package/src/source.ts +2 -11
  222. package/src/update.ts +16 -10
  223. package/plugins/undo/notes.ts +0 -43
  224. package/plugins-dist/undo/notes.js +0 -36
@@ -3,7 +3,8 @@
3
3
  // 主体和环境信息都是 stable:环境信息在会话开始时取一次,不在每次请求时更新,
4
4
  // 否则它一变,排在后面的整段历史都无法命中提示缓存。
5
5
  // 指令文件兼容 AGENTS.md 和 CLAUDE.md,同一目录两者都有时只读 AGENTS.md。
6
- // 用户层($JADE_HOME 里)的是 trusted;项目层(随仓库分发)的是 untrusted,并记下出处(宪法 7.2)。
6
+ // 用户层的先找 $JADE_HOME,没有时用 ~/.claude 里的(和 jade/skills 读 ~/.claude/skills 一样,已有的全局指令不用再抄一份)。
7
+ // 用户层的是 trusted;项目层(随仓库分发)的是 untrusted,并记下出处(宪法 7.2)。
7
8
  // 项目层从仓库根目录到工作目录逐级查找,上级的在前,离工作目录近的在后。
8
9
 
9
10
  import { createHash } from "node:crypto";
@@ -89,7 +90,8 @@ export async function environment(cwd: string, today = localDate()): Promise<str
89
90
  const [branch, status, entries] = await gitSnapshot(cwd);
90
91
  if (branch) {
91
92
  lines.push(`- Git branch: ${branch}`);
92
- lines.push(`- Git status at session start: ${status ? `\n${status.split("\n").slice(0, 30).join("\n")}` : "clean"}`);
93
+ const shown = status === undefined ? "unknown (git status did not finish)" : status ? `\n${status.split("\n").slice(0, 30).join("\n")}` : "clean";
94
+ lines.push(`- Git status at session start: ${shown}`);
93
95
  } else {
94
96
  lines.push("- Not a git repository");
95
97
  }
@@ -109,10 +111,10 @@ export interface Instructions {
109
111
  file: string;
110
112
  }
111
113
 
112
- /** 用户层(home 里)和项目层(仓库根目录到 cwd 逐级)的指令文件,按顺序 */
113
- export async function allInstructions(home: string, cwd: string): Promise<Instructions[]> {
114
+ /** 用户层(home 里,没有时 userHome/.claude 里;userHome 给 null 时不找)和项目层(仓库根目录到 cwd 逐级)的指令文件,按顺序 */
115
+ export async function allInstructions(home: string, cwd: string, userHome: string | null = os.homedir()): Promise<Instructions[]> {
114
116
  const out: Instructions[] = [];
115
- const user = await instructions(home, home, "user");
117
+ const user = (await instructions(home, home, "user")) ?? (userHome === null ? undefined : await instructions(userHome, path.join(userHome, ".claude"), "user"));
116
118
  if (user) out.push({ text: `# User instructions (${user.source.path})\n\n${user.text}`, trust: "trusted", source: user.source, file: user.file });
117
119
  const root = realPath(await repoRoot(cwd));
118
120
  for (const dir of ancestry(root, realPath(cwd))) {
@@ -178,7 +180,8 @@ function gitSnapshot(cwd: string) {
178
180
  // 三个 git 命令同时跑:Windows 上每启动一次 git 要几十毫秒,启动时会串起来等
179
181
  p = Promise.all([
180
182
  git(cwd, ["rev-parse", "--abbrev-ref", "HEAD"]),
181
- git(cwd, ["status", "--short"]),
183
+ // 巨型仓库里 git status 可能要很久,会话打开要等它:超时就不给
184
+ git(cwd, ["status", "--short"], STATUS_TIMEOUT_MS),
182
185
  // 让模型一开始就知道仓库的大致样子,不用先盲猜语言、到处 ls
183
186
  topLevel(cwd),
184
187
  ]);
@@ -238,9 +241,12 @@ export function repoRoot(cwd: string): Promise<string> {
238
241
  return p;
239
242
  }
240
243
 
241
- async function git(cwd: string, args: string[]): Promise<string | undefined> {
244
+ /** 会话开始时 git status 最多等这么久 */
245
+ const STATUS_TIMEOUT_MS = 3000;
246
+
247
+ async function git(cwd: string, args: string[], timeoutMs?: number): Promise<string | undefined> {
242
248
  try {
243
- const { code, stdout } = await run(["git", ...args], { cwd });
249
+ const { code, stdout } = await run(["git", ...args], timeoutMs === undefined ? { cwd } : { cwd, timeoutMs });
244
250
  return code === 0 ? stdout.trim() : undefined;
245
251
  } catch {
246
252
  return undefined;
@@ -0,0 +1,63 @@
1
+ // 内容为空的错误工具结果:内核不写说明文字(I-1),模型只看到 is_error,分不清是工具名写错、没执行还是被拒绝,
2
+ // 容易原样重试。这里在投影里按原因补一句说明(I-4:只改投影,不改日志)。
3
+ //
4
+ // 空结果有四种来源,块本身只能分出最后一种(origin 不是 runtime):
5
+ // - 调用了不存在的工具:运行时直接拒绝(tool.decided 没有 reason,也没有 decidedBy)
6
+ // - 崩溃恢复时,执行到一半的非幂等工具记为 tool.interrupted
7
+ // - 这一轮先结束了(用户中断、出错、输出被截断),没有结果的调用由内核补成空的错误结果
8
+ // - 关卡拒绝但没给原因(例如关卡 hook 出错或超时,按拒绝处理;tool.decided 有 decidedBy)
9
+ // tool.decided 的 origin 一律是 runtime,分不出前一种和最后一种,要看 decidedBy(与内核 step.ts 的判断相同)。
10
+ // 前两种只有日志事件分得出来,由 observe 记进 derived,投影时按它说明;恢复会话后同样成立,
11
+ // 投影也只取决于日志,每次算出的结果相同,不破坏提示缓存。
12
+
13
+ import type { Block, LogEvent, Message, Origin } from "uncarved";
14
+
15
+ export type Why = "unknown-tool" | "interrupted";
16
+ export type Reasons = Readonly<Record<string, Why>>;
17
+
18
+ /** 从事件判断空结果的原因;不是这两种返回 undefined */
19
+ export function whyEmpty(e: LogEvent): { toolUseId: string; why: Why } | undefined {
20
+ if (e.type === "tool.decided") {
21
+ const d = e.data as { toolUseId: string; decision: string; reason?: string; decidedBy?: string };
22
+ if (d.decision === "deny" && !d.reason && d.decidedBy === undefined) return { toolUseId: d.toolUseId, why: "unknown-tool" };
23
+ }
24
+ if (e.type === "tool.interrupted") return { toolUseId: (e.data as { toolUseId: string }).toolUseId, why: "interrupted" };
25
+ return undefined;
26
+ }
27
+
28
+ function explanation(why: Why | undefined, origin: Origin, name: string | undefined): string {
29
+ if (why === "unknown-tool") {
30
+ return `There is no tool named ${JSON.stringify(name ?? "")}; the call was not run. Use only the tools you were given.`;
31
+ }
32
+ if (why === "interrupted") {
33
+ return "This call was interrupted while it was running (jade stopped before it finished), so it may have partly taken effect. Check its effects before running it again.";
34
+ }
35
+ if (origin.by === "runtime") {
36
+ return "This call has no result: the turn ended before it finished (the user interrupted, an error occurred, or the response was cut off). It may not have run, or may have stopped partway; check its effects before relying on them or running it again.";
37
+ }
38
+ if (origin.by === "user") return "The user declined this call.";
39
+ return "This call was blocked by a check in jade, which gave no reason. Do not retry it unchanged.";
40
+ }
41
+
42
+ /** 给内容为空的错误工具结果补上说明;没有要补的时候原样返回 */
43
+ export function explainEmptyResults(messages: Message[], reasons: Reasons, plugin: string): Message[] {
44
+ let names: Map<string, string> | undefined;
45
+ const nameOf = (id: string) => {
46
+ names ??= new Map(messages.flatMap((m) => m.content.flatMap((b) => (b.kind === "tool_use" ? [[b.id, b.name] as const] : []))));
47
+ return names.get(id);
48
+ };
49
+ let changed = false;
50
+ const out = messages.map((m) => {
51
+ if (m.role !== "user" || !m.content.some((b) => b.kind === "tool_result" && b.isError && b.content.length === 0)) return m;
52
+ changed = true;
53
+ return {
54
+ ...m,
55
+ content: m.content.map((b): Block => {
56
+ if (b.kind !== "tool_result" || !b.isError || b.content.length > 0) return b;
57
+ const text = explanation(reasons[b.toolUseId], b.origin, nameOf(b.toolUseId));
58
+ return { ...b, content: [{ kind: "text", text, origin: { by: "plugin", id: plugin }, trust: "trusted" }] };
59
+ }),
60
+ };
61
+ });
62
+ return changed ? out : messages;
63
+ }
@@ -57,11 +57,23 @@ export function spawn(cmd: string[], opts: SpawnOptions = {}): Promise<Proc> {
57
57
  });
58
58
  }
59
59
 
60
- /** 运行一条命令,返回 stdout 文本和退出码;启动失败时 reject */
61
- export async function run(cmd: string[], opts: { cwd?: string } = {}): Promise<{ code: number | null; stdout: string }> {
60
+ /** 运行一条命令,返回 stdout 文本和退出码;启动失败时 reject。超过 timeoutMs 时杀掉,退出码为 null */
61
+ export async function run(cmd: string[], opts: { cwd?: string; timeoutMs?: number } = {}): Promise<{ code: number | null; stdout: string }> {
62
62
  const proc = await spawn(cmd, { cwd: opts.cwd, stderr: "ignore" });
63
- const [stdout, code] = await Promise.all([text(proc.stdout), proc.exited]);
64
- return { code, stdout };
63
+ let timedOut = false;
64
+ const timer =
65
+ opts.timeoutMs === undefined
66
+ ? undefined
67
+ : setTimeout(() => {
68
+ timedOut = true;
69
+ proc.kill();
70
+ }, opts.timeoutMs);
71
+ try {
72
+ const [stdout, code] = await Promise.all([text(proc.stdout), proc.exited]);
73
+ return { code: timedOut ? null : code, stdout };
74
+ } finally {
75
+ clearTimeout(timer);
76
+ }
65
77
  }
66
78
 
67
79
  export async function text(stream: AsyncIterable<Uint8Array>): Promise<string> {
@@ -1,49 +1,8 @@
1
1
  // jade/renderers:工具调用和插件事件在对话里怎么显示的注册表。前端查这里;没有登记的用前端的默认显示。
2
2
  // 显示的文字可以带 ANSI 颜色,前端负责过滤掉颜色之外的控制序列。
3
3
 
4
- import { definePlugin } from "../../src/api.ts";
5
-
6
- export interface ToolRenderer {
7
- tool: string;
8
- /** 调用行 "● 工具名(…)" 括号里的摘要,只显示第一行;默认是 path 参数或参数的 JSON */
9
- summary?(input: unknown): string | undefined;
10
- /** 摘要省略了的参数(例如很长的命令),展开调用行后显示在最前面,可以多行 */
11
- details?(input: unknown): string | undefined;
12
- /** 执行时显示的内容,代替默认的 "工具名(摘要)":第一行跟在 "● " 后面,其余的行原样显示在下面,不折叠 */
13
- call?(input: unknown): string | undefined;
14
- /**
15
- * 运行中的进度:工具用 ToolContext.progress 推送的一项换成一行,显示在对话的最后,工具结束时去掉;返回 undefined 不显示。
16
- * 没有这个函数时,{ text } 形式的进度按输出逐行显示,其他的不显示
17
- */
18
- progress?(update: unknown, call: { toolUseId: string; summary(tool: string, input: unknown): string }): string | undefined;
19
- /** 请用户批准时显示的预览,可以多行;默认是参数的 JSON */
20
- preview?(input: unknown): string | undefined | Promise<string | undefined>;
21
- /** 工具执行之前同步调用(例如记下修改前的文件,好在结果里显示 diff),返回值交给 result 的 before */
22
- before?(input: unknown): unknown;
23
- /**
24
- * 结果:line 接在调用行的 "⎿ " 后面(一行),details 是展开后的内容(可以多行);
25
- * 返回 null 时不显示结果;默认显示结果文字的第一行,多出的行折叠起来
26
- */
27
- result?(r: { toolUseId: string; input: unknown; text: string; isError: boolean; before?: unknown }): { line: string; details?: string } | null | undefined;
28
- }
29
-
30
- /** 插件自己的事件(hook 的 ctx.emit 写入的)怎么显示。只能为自己的事件注册:event 不带 "<插件 id>/" 前缀 */
31
- export interface EventRenderer {
32
- event: string;
33
- /** line 显示成一行,details 是展开后的内容;返回 undefined 时不显示 */
34
- render(data: unknown): { line: string; details?: string } | undefined;
35
- }
36
-
37
- export interface Renderers {
38
- /** 为一个工具登记显示方式,返回撤销函数。同一个工具已有登记时抛错 */
39
- tool(renderer: ToolRenderer): () => void;
40
- /** 为自己的一种事件登记显示方式,返回撤销函数 */
41
- event(renderer: EventRenderer): () => void;
42
- /** 前端查找 */
43
- forTool(tool: string): { renderer: ToolRenderer; plugin: string } | undefined;
44
- /** 前端按完整的事件类型("<插件 id>/<event>")查找 */
45
- forEvent(type: string): { renderer: EventRenderer; plugin: string } | undefined;
46
- }
4
+ import { definePlugin } from "@uncarved/jade-api";
5
+ import type { EventRenderer, Renderers, ToolRenderer } from "@uncarved/jade-api/services";
47
6
 
48
7
  const tools = new Map<string, { renderer: ToolRenderer; plugin: string }>();
49
8
  const events = new Map<string, { renderer: EventRenderer; plugin: string }>();
@@ -15,7 +15,7 @@
15
15
  import { createHash } from "node:crypto";
16
16
  import path from "node:path";
17
17
  import type { Block, GateResult, LocalHookSpec, LogEvent } from "uncarved";
18
- import { definePlugin } from "../../src/api.ts";
18
+ import { definePlugin } from "@uncarved/jade-api";
19
19
 
20
20
  const ID = "jade/repeats";
21
21
  /** jade/compact 记录压缩结果用的派生数据键(旧 jade 的约定):记一次就说明历史被压缩过 */
@@ -5,8 +5,8 @@
5
5
 
6
6
  import path from "node:path";
7
7
  import type { Tool, UserInput } from "uncarved";
8
- import { definePlugin, type UI } from "../../src/api.ts";
9
- import type { Commands } from "../commands/index.ts";
8
+ import { definePlugin, type UI } from "@uncarved/jade-api";
9
+ import type { Commands } from "@uncarved/jade-api/services";
10
10
  import { parseCron } from "./cron.ts";
11
11
  import { claimDue, nextAt, sameDir, ScheduleStore, type Task } from "./store.ts";
12
12
 
@@ -5,9 +5,8 @@ import { realpathSync } from "node:fs";
5
5
  import { mkdir } from "node:fs/promises";
6
6
  import os from "node:os";
7
7
  import path from "node:path";
8
- import { definePlugin } from "../../src/api.ts";
9
- import type { Approval } from "../approval/index.ts";
10
- import type { Prompts } from "../prompts/index.ts";
8
+ import { definePlugin } from "@uncarved/jade-api";
9
+ import type { Approval, Prompts } from "@uncarved/jade-api/services";
11
10
 
12
11
  /** 会话 id 对应的临时目录 */
13
12
  export const scratchDir = (session: string): string => path.join(os.tmpdir(), "jade-scratch", session);
@@ -1,11 +1,9 @@
1
1
  // jade/screenshot:截图工具(见 tool.ts)。本机没有能无头截图的浏览器时不注册这个工具。
2
- // 模型能不能看图片由 jade/models 的 models@1 给出(JADE_VISION):不能看时不截图。
2
+ // 模型能不能看图片由 jade/models 的 models@1 给出(JADE_VISION):不能看时请求里没有这个工具和提示,调用了也不截图。
3
3
  // 访问网络的 URL 由 jade/approval 的关卡决定要不要问用户(effect 是 read,路径之外的 URL 会问)。
4
4
 
5
- import { definePlugin } from "../../src/api.ts";
6
- import type { Models } from "../models/index.ts";
7
- import type { Prompts } from "../prompts/index.ts";
8
- import type { Renderers } from "../renderers/index.ts";
5
+ import { definePlugin } from "@uncarved/jade-api";
6
+ import type { Models, Prompts, Renderers } from "@uncarved/jade-api/services";
9
7
  import { findBrowser, screenshotTool } from "./tool.ts";
10
8
 
11
9
  /** 提示词里的一段:有截图工具时才加 */
@@ -26,15 +24,30 @@ export default definePlugin({
26
24
  const browser = findBrowser(process.env);
27
25
  if (!browser) return;
28
26
  const models = ctx.service<Models>("models@1");
29
- const tool = screenshotTool({
30
- cwd: ctx.cwd,
31
- browser,
32
- // 换了模型(/model)之后也按新的配置判断
33
- available: () => models?.current()?.vision !== false,
34
- });
27
+ // 换了模型(/model)之后也按新的配置判断
28
+ const available = () => models?.current()?.vision !== false;
29
+ const tool = screenshotTool({ cwd: ctx.cwd, browser, available });
35
30
  if (!tool) return;
36
31
  ctx.kernel.tool(tool);
37
- const remove = ctx.service<Prompts>("prompts@1")?.add({ text: NOTE, stable: false });
38
- if (remove) ctx.effect(() => () => remove());
32
+
33
+ // 不能看图片的模型不给这个工具,也不加那句提示:给了它多半会试一次,白费一轮。
34
+ // 排在 jade/prompts 的 context.build 之前(order -1),它组装系统提示词时读到的段落和工具已经是这一次的
35
+ const prompts = ctx.service<Prompts>("prompts@1");
36
+ let note: (() => void) | undefined;
37
+ ctx.effect(() => () => note?.());
38
+ ctx.kernel.hook({
39
+ kind: "transform",
40
+ point: "context.build",
41
+ order: -1,
42
+ fn: (req) => {
43
+ const can = available();
44
+ if (can && !note) note = prompts?.add({ text: NOTE, stable: false });
45
+ else if (!can && note) {
46
+ note();
47
+ note = undefined;
48
+ }
49
+ return can ? req : { ...req, tools: req.tools.filter((t) => t.name !== tool.name) };
50
+ },
51
+ });
39
52
  },
40
53
  });
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "id": "jade/screenshot",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "api": "^1",
5
5
  "entry": "./index.ts",
6
6
  "optional": ["renderers@1", "prompts@1", "models@1"]
@@ -5,17 +5,8 @@
5
5
  import { appendFile, mkdir, readFile } from "node:fs/promises";
6
6
  import path from "node:path";
7
7
  import type { UserInput } from "uncarved";
8
- import { definePlugin } from "../../src/api.ts";
9
- import type { Commands } from "../commands/index.ts";
10
-
11
- export interface SessionSummary {
12
- id: string;
13
- cwd: string;
14
- /** 第一条消息的开头 */
15
- title: string;
16
- /** 最近一次使用的时间(ISO) */
17
- at: string;
18
- }
8
+ import { definePlugin } from "@uncarved/jade-api";
9
+ import type { Commands, SessionIndex, SessionSummary } from "@uncarved/jade-api/services";
19
10
 
20
11
  const TITLE_CHARS = 80;
21
12
 
@@ -78,12 +69,6 @@ function titleOf(input: UserInput): string | undefined {
78
69
  return text.length > TITLE_CHARS ? `${text.slice(0, TITLE_CHARS)}…` : text;
79
70
  }
80
71
 
81
- /** sessions@1 */
82
- export interface SessionIndex {
83
- /** cwd 里的会话,最近用过的在前(jade --continue 取第一个) */
84
- list(cwd: string): Promise<SessionSummary[]>;
85
- }
86
-
87
72
  export default definePlugin({
88
73
  process(ctx) {
89
74
  const file = path.join(ctx.home, "state", "sessions.jsonl");
@@ -1,11 +1,18 @@
1
1
  // jade/shell:bash 工具,以及后台命令的 bash_output、kill_shell;shell@1 给其他插件执行命令。
2
+ // 后台命令自己结束时告诉用户,并给模型留一句说明(不用它一直轮询);/shells 给用户看和停掉后台命令。
2
3
 
3
- import { definePlugin, type UI } from "../../src/api.ts";
4
- import type { Prompts } from "../prompts/index.ts";
5
- import type { Renderers } from "../renderers/index.ts";
4
+ import { definePlugin, type UI } from "@uncarved/jade-api";
5
+ import type { Commands, Prompts, Renderers, Task } from "@uncarved/jade-api/services";
6
6
  import { shellRenderers } from "./renderers.ts";
7
7
  import { shellService } from "./service.ts";
8
- import { shellTools } from "./shell.ts";
8
+ import { shellTools, type ShellTools } from "./shell.ts";
9
+
10
+ /** 打开着的会话 → 它的后台命令(/shells 用) */
11
+ const sessions = new Map<string, Pick<ShellTools, "list" | "kill">>();
12
+ const firstLine = (command: string) => {
13
+ const line = command.split("\n")[0]!;
14
+ return line.length > 80 ? `${line.slice(0, 80)}…` : line;
15
+ };
9
16
 
10
17
  export default definePlugin({
11
18
  process(ctx) {
@@ -14,11 +21,51 @@ export default definePlugin({
14
21
  if (renderers) for (const r of shellRenderers) renderers.tool(r);
15
22
  // shell@1:找 shell 和杀进程树只有一份(见 service.ts)
16
23
  ctx.provide("shell@1", shellService(typeof ctx.config.shell === "string" ? { shell: ctx.config.shell } : {}));
24
+ ctx.service<Commands>("commands@1")?.register({
25
+ name: "shells",
26
+ args: "[kill <id>]",
27
+ description: "list the background commands the agent started, or stop one",
28
+ instant: true,
29
+ async run(args, { sessions: manager }) {
30
+ const shells = manager.current && sessions.get(manager.current.id);
31
+ const [verb, id] = args.trim().split(/\s+/);
32
+ if (verb === "kill" && id) return shells && (await shells.kill(id)) ? `Stopped ${id}.` : `There is no background command ${id}.`;
33
+ const all = shells?.list() ?? [];
34
+ if (all.length === 0) return "No background commands.";
35
+ return all.map((b) => `${b.id} ${b.status} ${firstLine(b.command)}`).join("\n");
36
+ },
37
+ });
17
38
  },
18
39
 
19
40
  async session(ctx) {
20
41
  const shell = typeof ctx.config.shell === "string" ? ctx.config.shell : undefined;
21
- const { tools, executable, unavailable, shutdown } = shellTools({ cwd: ctx.cwd, ...(shell ? { shell } : {}) });
42
+ const id = ctx.session.id;
43
+ const { tools, executable, unavailable, shutdown, list, kill } = shellTools({
44
+ cwd: ctx.cwd,
45
+ ...(shell ? { shell } : {}),
46
+ onExit(bg) {
47
+ const what = `Background shell ${bg.id} (${firstLine(bg.command)}) exited with code ${bg.exitCode}`;
48
+ ctx.service<UI>("ui@1")?.notify(what, bg.exitCode === 0 ? "info" : "warning");
49
+ ctx.service<Prompts>("prompts@1")?.note(id, `${what}. Read its remaining output with bash_output.`);
50
+ },
51
+ });
52
+ sessions.set(id, { list, kill });
53
+ // 子 Agent(jade/task)的 bash:只收命令和时限,不能在后台跑;哪些命令能执行由 jade/task 按审批规则把关(只读的才行)
54
+ const bash = tools.find((t) => t.name === "bash");
55
+ try {
56
+ if (bash) {
57
+ ctx.service<Task>("task@1")?.tool({
58
+ ...bash,
59
+ description:
60
+ "Run a read-only shell command in the working directory (for example git log, git diff, git status, ls, rg) and return its exit code and output. " +
61
+ "Commands that could change anything, or that touch paths outside the working directory, are refused.",
62
+ inputSchema: { type: "object", properties: { command: { type: "string" }, timeout_ms: { type: "integer" } }, required: ["command"], additionalProperties: false },
63
+ execute: (input, c) => bash.execute({ command: (input as { command: string }).command, timeout_ms: (input as { timeout_ms?: number }).timeout_ms }, c),
64
+ });
65
+ }
66
+ } catch {
67
+ // jade/task 被隔离时它的服务已失效:子 Agent 没有 bash,主会话的照常
68
+ }
22
69
  if (unavailable) ctx.service<UI>("ui@1")?.notify(unavailable, "warning");
23
70
  for (const tool of tools) ctx.kernel.tool(tool);
24
71
  if (executable) {
@@ -28,6 +75,9 @@ export default definePlugin({
28
75
  ctx.service<Prompts>("prompts@1")?.add({ text: lines.join("\n") });
29
76
  }
30
77
  // 会话关闭(换会话、退出)时杀掉还在运行的后台命令
31
- await ctx.effect(() => shutdown);
78
+ await ctx.effect(() => () => {
79
+ sessions.delete(id);
80
+ shutdown();
81
+ });
32
82
  },
33
83
  });
@@ -7,7 +7,9 @@
7
7
  "optional": [
8
8
  "ui@1",
9
9
  "prompts@1",
10
- "renderers@1"
10
+ "renderers@1",
11
+ "commands@1",
12
+ "task@1"
11
13
  ],
12
14
  "config": {
13
15
  "shell": {
@@ -3,7 +3,7 @@
3
3
  // 运行中的输出不登记 progress:bash 推送的是 { kind, stream, text },前端对 { text } 形式的进度默认逐行显示,
4
4
  // 正是想要的;自己写 progress 反而只能把一次推送(可能是好几行、半行)显示成一行。
5
5
 
6
- import type { ToolRenderer } from "../renderers/index.ts";
6
+ import type { ToolRenderer } from "@uncarved/jade-api/services";
7
7
 
8
8
  // 截断和颜色从旧 jade 的 ui/render.ts、ui/screen.ts 复制:插件之间不共享代码,fs 插件也有一份
9
9
  const clip = (s: string, n: number) => (s.length > n ? s.slice(0, n) + "…" : s);
@@ -4,20 +4,9 @@
4
4
  // 和 bash 工具不同,这里的命令不经过模型和审批:调用方(插件)自己决定要不要问用户。
5
5
  // 没有 shell 时 executable 是 undefined,run 会抛错;调用方据此说明"在这台机器上做不了"。
6
6
 
7
- import { capture, findShell, shellEnv, type RunCommand, type RunResult } from "./shell.ts";
7
+ import { capture, findShell, shellEnv } from "./shell.ts";
8
8
  import { which } from "./sys.ts";
9
-
10
- /** shell@1 */
11
- export interface Shell {
12
- /** 实际执行的 shell 程序;这台机器上没有时为 undefined */
13
- readonly executable: string | undefined;
14
- /** 找不到 shell 时的说明 */
15
- readonly unavailable?: string;
16
- /** 执行一条命令并收集输出;超时或中止时连同子进程一起杀掉 */
17
- run: RunCommand;
18
- /** 在 PATH 里找可执行文件(Windows 上按 PATHEXT 补扩展名),找不到返回 undefined */
19
- which(bin: string): string | undefined;
20
- }
9
+ import type { RunCommand, RunResult, Shell } from "@uncarved/jade-api/services";
21
10
 
22
11
  /** 默认的 shell:JADE_SHELL,否则 bash(Windows 上是 Git Bash) */
23
12
  export const shellName = (configured: unknown): string => (typeof configured === "string" && configured.trim() ? configured : (process.env.JADE_SHELL ?? "bash"));
@@ -7,6 +7,7 @@ import { existsSync } from "node:fs";
7
7
  import path from "node:path";
8
8
  import type { Tool } from "uncarved";
9
9
  import { runSync, spawn as spawnProc, which, type Proc } from "./sys.ts";
10
+ import type { RunCommand, RunResult } from "@uncarved/jade-api/services";
10
11
 
11
12
  const DEFAULT_TIMEOUT_MS = 120_000;
12
13
  const MAX_TIMEOUT_MS = 600_000;
@@ -18,6 +19,8 @@ export interface ShellOptions {
18
19
  cwd: string;
19
20
  /** 默认 JADE_SHELL 环境变量,否则 bash(Windows 上是 Git Bash) */
20
21
  shell?: string;
22
+ /** 后台命令自己结束时调用(被 kill_shell、shutdown 停掉的不算) */
23
+ onExit?(bg: { id: string; command: string; exitCode: number | null }): void;
21
24
  }
22
25
 
23
26
  export interface ShellTools {
@@ -29,20 +32,14 @@ export interface ShellTools {
29
32
  unavailable?: string;
30
33
  /** 杀掉所有还在运行的后台进程 */
31
34
  shutdown(): void;
35
+ /** 本会话启动过的后台命令 */
36
+ list(): { id: string; command: string; status: string }[];
37
+ /** 停掉一个后台命令(连同它启动的进程);没有这个编号时返回 false */
38
+ kill(id: string): Promise<boolean>;
32
39
  /** 宿主和插件自己执行一条命令(不经过模型和审批),找不到 shell 时为 undefined */
33
40
  run?: RunCommand;
34
41
  }
35
42
 
36
- export interface RunResult {
37
- /** 超时被杀掉时为 null */
38
- code: number | null;
39
- /** stdout 和 stderr 合在一起,超长时保留首尾 */
40
- output: string;
41
- timedOut: boolean;
42
- }
43
-
44
- export type RunCommand = (command: string, opts: { cwd?: string; timeoutMs: number; signal?: AbortSignal }) => Promise<RunResult>;
45
-
46
43
  /** 只要 bash 工具时用(测试) */
47
44
  export function shellTool(opts: ShellOptions): Tool {
48
45
  const { tools, unavailable } = shellTools(opts);
@@ -59,6 +56,8 @@ interface Background {
59
56
  /** 因超出缓存而丢弃的字符数,下次读取时告诉模型 */
60
57
  dropped: number;
61
58
  exitCode?: number | null;
59
+ /** 是被停掉的(kill_shell、shutdown),不是自己结束的 */
60
+ killed?: boolean;
62
61
  }
63
62
 
64
63
  export function shellTools(opts: ShellOptions): ShellTools {
@@ -67,7 +66,7 @@ export function shellTools(opts: ShellOptions): ShellTools {
67
66
  // 找不到 shell 时不提供这几个工具:否则模型每次调用都要用户先批准,执行时才失败,模型还会反复重试
68
67
  if (!found) {
69
68
  const hint = process.platform === "win32" ? "install Git for Windows, or set JADE_SHELL to the path of a bash executable" : "set JADE_SHELL to the path of a shell";
70
- return { tools: [], unavailable: `${shell} was not found, so the bash tool is disabled; ${hint}`, shutdown() {} };
69
+ return { tools: [], unavailable: `${shell} was not found, so the bash tool is disabled; ${hint}`, shutdown() {}, list: () => [], kill: async () => false };
71
70
  }
72
71
  const executable = found;
73
72
  const env = shellEnv(executable);
@@ -96,12 +95,20 @@ export function shellTools(opts: ShellOptions): ShellTools {
96
95
  void Promise.all([collect(bg.proc.stdout, append, false), collect(bg.proc.stderr, append, false)]).catch(() => {});
97
96
  void bg.proc.exited.then((code) => {
98
97
  bg.exitCode = code;
98
+ if (!bg.killed) opts.onExit?.({ id, command, exitCode: code });
99
99
  });
100
100
  background.set(id, bg);
101
101
  return id;
102
102
  }
103
103
 
104
104
  const status = (bg: Background) => (bg.exitCode === undefined ? "running" : `exited with code ${bg.exitCode}`);
105
+ /** 停掉一个还在运行的后台命令,等它退出 */
106
+ const stop = async (bg: Background) => {
107
+ if (bg.exitCode !== undefined) return;
108
+ bg.killed = true;
109
+ killTree(executable, env, bg.proc.pid);
110
+ await bg.proc.exited;
111
+ };
105
112
 
106
113
  const bash: Tool = {
107
114
  name: "bash",
@@ -206,10 +213,7 @@ export function shellTools(opts: ShellOptions): ShellTools {
206
213
  const { id } = input as { id: string };
207
214
  const bg = find(id);
208
215
  if (!bg) return unknown(id);
209
- if (bg.exitCode === undefined) {
210
- killTree(executable, env, bg.proc.pid);
211
- await bg.proc.exited;
212
- }
216
+ await stop(bg);
213
217
  return { content: `${id} stopped (${status(bg)})` };
214
218
  },
215
219
  };
@@ -221,7 +225,18 @@ export function shellTools(opts: ShellOptions): ShellTools {
221
225
  executable,
222
226
  run,
223
227
  shutdown() {
224
- for (const bg of background.values()) if (bg.exitCode === undefined) killTree(executable, env, bg.proc.pid);
228
+ for (const bg of background.values()) {
229
+ if (bg.exitCode !== undefined) continue;
230
+ bg.killed = true;
231
+ killTree(executable, env, bg.proc.pid);
232
+ }
233
+ },
234
+ list: () => [...background.values()].map((bg) => ({ id: bg.id, command: bg.command, status: status(bg) })),
235
+ async kill(id) {
236
+ const bg = background.get(id);
237
+ if (!bg) return false;
238
+ await stop(bg);
239
+ return true;
225
240
  },
226
241
  };
227
242
  }