@uncarved/jade 0.3.1 → 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 (199) hide show
  1. package/dist/api.d.ts +1 -1
  2. package/dist/api.js +1 -1
  3. package/dist/host.d.ts +2 -0
  4. package/dist/host.js +39 -5
  5. package/dist/host.js.map +1 -1
  6. package/dist/remote.js +5 -0
  7. package/dist/remote.js.map +1 -1
  8. package/dist/session.d.ts +7 -0
  9. package/dist/session.js +482 -27
  10. package/dist/session.js.map +1 -1
  11. package/package.json +4 -4
  12. package/plugins/approval/index.ts +36 -14
  13. package/plugins/approval/policy.ts +15 -10
  14. package/plugins/approval/review.ts +9 -5
  15. package/plugins/checks/checks.ts +10 -7
  16. package/plugins/checks/index.ts +13 -13
  17. package/plugins/checks/plugin.json +1 -1
  18. package/plugins/checks/project.ts +37 -38
  19. package/plugins/commands/index.ts +27 -3
  20. package/plugins/compact/compact.ts +13 -5
  21. package/plugins/compact/index.ts +20 -3
  22. package/plugins/compact/style.ts +13 -14
  23. package/plugins/completions/index.ts +35 -0
  24. package/plugins/completions/plugin.json +7 -0
  25. package/plugins/copy/index.ts +46 -0
  26. package/plugins/copy/plugin.json +7 -0
  27. package/plugins/editor/index.ts +10 -4
  28. package/plugins/fs/diff.ts +7 -6
  29. package/plugins/fs/fs.ts +8 -4
  30. package/plugins/fs/grep.ts +42 -9
  31. package/plugins/fs/index.ts +2 -7
  32. package/plugins/fs/plugin.json +1 -1
  33. package/plugins/fs/renderers.ts +16 -15
  34. package/plugins/fs/style.ts +16 -15
  35. package/plugins/hooks/index.ts +3 -2
  36. package/plugins/keys/index.ts +5 -1
  37. package/plugins/mcp/index.ts +197 -78
  38. package/plugins/mcp/install.ts +5 -3
  39. package/plugins/mcp/mcp.ts +163 -11
  40. package/plugins/mcp/plugin.json +2 -1
  41. package/plugins/mcp/project.ts +47 -50
  42. package/plugins/memory/index.ts +4 -4
  43. package/plugins/memory/style.ts +13 -14
  44. package/plugins/mentions/candidates.ts +67 -0
  45. package/plugins/mentions/index.ts +12 -5
  46. package/plugins/mentions/plugin.json +1 -1
  47. package/plugins/mentions/style.ts +13 -14
  48. package/plugins/models/configure.ts +10 -9
  49. package/plugins/models/index.ts +27 -1
  50. package/plugins/modes/index.ts +13 -2
  51. package/plugins/print/index.ts +7 -4
  52. package/plugins/prompts/index.ts +0 -0
  53. package/plugins/renderers/index.ts +10 -5
  54. package/plugins/repeats/index.ts +8 -8
  55. package/plugins/schedule/index.ts +2 -0
  56. package/plugins/screenshot/browser.ts +10 -0
  57. package/plugins/sessions/index.ts +48 -2
  58. package/plugins/shell/index.ts +20 -25
  59. package/plugins/shell/plugin.json +1 -2
  60. package/plugins/shell/renderers.ts +9 -7
  61. package/plugins/shell/shell.ts +3 -1
  62. package/plugins/skills/index.ts +28 -19
  63. package/plugins/skills/project.ts +12 -7
  64. package/plugins/steer/index.ts +5 -4
  65. package/plugins/task/index.ts +80 -50
  66. package/plugins/task/plugin.json +6 -2
  67. package/plugins/task/style.ts +13 -14
  68. package/plugins/task/subagent.ts +67 -184
  69. package/plugins/todo/index.ts +39 -9
  70. package/plugins/tools/index.ts +9 -2
  71. package/plugins/tui/frontend.ts +139 -89
  72. package/plugins/tui/index.ts +2 -3
  73. package/plugins/tui/mentions.ts +11 -80
  74. package/plugins/tui/plugin.json +1 -1
  75. package/plugins/tui/screen.ts +55 -5
  76. package/plugins/tui/transcript.ts +17 -7
  77. package/plugins/tui/tui.ts +337 -101
  78. package/plugins/tui/vendor.ts +1 -1
  79. package/plugins/undo/checkpoint.ts +5 -3
  80. package/plugins/undo/index.ts +43 -14
  81. package/plugins/undo/style.ts +13 -14
  82. package/plugins/web/index.ts +7 -18
  83. package/plugins/web/plugin.json +1 -1
  84. package/plugins-dist/approval/index.js +363 -418
  85. package/plugins-dist/approval/policy.js +391 -508
  86. package/plugins-dist/approval/review.js +132 -158
  87. package/plugins-dist/approval/schema.js +50 -55
  88. package/plugins-dist/ask/index.js +115 -129
  89. package/plugins-dist/budget/index.js +27 -31
  90. package/plugins-dist/checks/checks.js +293 -329
  91. package/plugins-dist/checks/index.js +119 -139
  92. package/plugins-dist/checks/plugin.json +2 -1
  93. package/plugins-dist/checks/project.js +57 -65
  94. package/plugins-dist/commands/index.js +47 -29
  95. package/plugins-dist/compact/compact.js +233 -249
  96. package/plugins-dist/compact/index.js +98 -95
  97. package/plugins-dist/compact/style.js +9 -14
  98. package/plugins-dist/completions/index.js +30 -0
  99. package/plugins-dist/completions/plugin.json +9 -0
  100. package/plugins-dist/continue/index.js +40 -52
  101. package/plugins-dist/copy/index.js +40 -0
  102. package/plugins-dist/copy/plugin.json +9 -0
  103. package/plugins-dist/cost/index.js +98 -115
  104. package/plugins-dist/custom-commands/index.js +55 -72
  105. package/plugins-dist/editor/index.js +64 -72
  106. package/plugins-dist/fs/diff.js +111 -124
  107. package/plugins-dist/fs/fs.js +426 -469
  108. package/plugins-dist/fs/grep.js +262 -253
  109. package/plugins-dist/fs/index.js +20 -34
  110. package/plugins-dist/fs/plugin.json +1 -2
  111. package/plugins-dist/fs/renderers.js +100 -112
  112. package/plugins-dist/fs/style.js +11 -15
  113. package/plugins-dist/fs/sys.js +31 -43
  114. package/plugins-dist/hooks/index.js +107 -127
  115. package/plugins-dist/keys/index.js +27 -31
  116. package/plugins-dist/mcp/index.js +271 -199
  117. package/plugins-dist/mcp/install.js +123 -137
  118. package/plugins-dist/mcp/mcp.js +638 -577
  119. package/plugins-dist/mcp/plugin.json +3 -1
  120. package/plugins-dist/mcp/project.js +98 -115
  121. package/plugins-dist/memory/index.js +83 -88
  122. package/plugins-dist/memory/memory.js +111 -128
  123. package/plugins-dist/memory/style.js +9 -14
  124. package/plugins-dist/mentions/attach.js +91 -99
  125. package/plugins-dist/mentions/candidates.js +49 -0
  126. package/plugins-dist/mentions/index.js +71 -81
  127. package/plugins-dist/mentions/plugin.json +2 -1
  128. package/plugins-dist/mentions/style.js +9 -14
  129. package/plugins-dist/models/config.js +29 -43
  130. package/plugins-dist/models/configure.js +392 -473
  131. package/plugins-dist/models/context.js +14 -22
  132. package/plugins-dist/models/index.js +182 -189
  133. package/plugins-dist/models/provider.js +243 -307
  134. package/plugins-dist/models/saved.js +59 -71
  135. package/plugins-dist/modes/index.js +138 -165
  136. package/plugins-dist/plugins/index.js +34 -34
  137. package/plugins-dist/print/index.js +192 -175
  138. package/plugins-dist/prompts/index.js +81 -89
  139. package/plugins-dist/prompts/notes.js +31 -37
  140. package/plugins-dist/prompts/prompts.js +155 -193
  141. package/plugins-dist/prompts/results.js +40 -53
  142. package/plugins-dist/prompts/sys.js +39 -50
  143. package/plugins-dist/renderers/index.js +32 -32
  144. package/plugins-dist/repeats/index.js +131 -174
  145. package/plugins-dist/schedule/cron.js +49 -56
  146. package/plugins-dist/schedule/index.js +229 -247
  147. package/plugins-dist/schedule/store.js +93 -109
  148. package/plugins-dist/scratch/index.js +12 -18
  149. package/plugins-dist/screenshot/browser.js +136 -154
  150. package/plugins-dist/screenshot/index.js +37 -46
  151. package/plugins-dist/screenshot/sys.js +53 -65
  152. package/plugins-dist/screenshot/tool.js +98 -126
  153. package/plugins-dist/sessions/index.js +149 -117
  154. package/plugins-dist/shell/index.js +115 -135
  155. package/plugins-dist/shell/plugin.json +1 -2
  156. package/plugins-dist/shell/renderers.js +28 -33
  157. package/plugins-dist/shell/service.js +17 -23
  158. package/plugins-dist/shell/shell.js +307 -362
  159. package/plugins-dist/shell/sys.js +64 -79
  160. package/plugins-dist/skills/index.js +193 -211
  161. package/plugins-dist/skills/install.js +122 -149
  162. package/plugins-dist/skills/project.js +65 -73
  163. package/plugins-dist/skills/skills.js +135 -171
  164. package/plugins-dist/steer/index.js +108 -125
  165. package/plugins-dist/suggest/index.js +235 -259
  166. package/plugins-dist/task/index.js +130 -117
  167. package/plugins-dist/task/plugin.json +5 -2
  168. package/plugins-dist/task/style.js +9 -14
  169. package/plugins-dist/task/subagent.js +156 -279
  170. package/plugins-dist/todo/index.js +79 -63
  171. package/plugins-dist/tools/index.js +36 -42
  172. package/plugins-dist/tui/editor.js +145 -160
  173. package/plugins-dist/tui/frontend.js +639 -678
  174. package/plugins-dist/tui/history.js +34 -37
  175. package/plugins-dist/tui/index.js +16 -18
  176. package/plugins-dist/tui/markdown.js +203 -247
  177. package/plugins-dist/tui/mascot.js +266 -355
  178. package/plugins-dist/tui/mentions.js +67 -144
  179. package/plugins-dist/tui/plugin.json +1 -2
  180. package/plugins-dist/tui/render.js +35 -45
  181. package/plugins-dist/tui/screen.js +197 -180
  182. package/plugins-dist/tui/transcript.js +436 -468
  183. package/plugins-dist/tui/tui.js +1554 -1656
  184. package/plugins-dist/undo/checkpoint.js +95 -118
  185. package/plugins-dist/undo/index.js +185 -192
  186. package/plugins-dist/undo/snapshot.js +83 -99
  187. package/plugins-dist/undo/style.js +9 -14
  188. package/plugins-dist/update/index.js +26 -28
  189. package/plugins-dist/web/env.js +55 -68
  190. package/plugins-dist/web/index.js +80 -110
  191. package/plugins-dist/web/plugin.json +1 -2
  192. package/plugins-dist/web/renderers.js +4 -5
  193. package/plugins-dist/web/web.js +514 -593
  194. package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
  195. package/sdk/python/jade_plugin.py +9 -2
  196. package/src/api.ts +1 -1
  197. package/src/host.ts +39 -4
  198. package/src/remote.ts +6 -1
  199. package/src/session.ts +489 -34
@@ -1,150 +1,130 @@
1
- // jade/checks:模型改了文件之后,下一次请求模型之前对改过的文件跑项目的检查(类型检查、lint),
2
- // 把发现的问题附在这批修改的最后一个结果后面(见 checks.ts);/checks 查看、开关;对话里显示检查的结果。
3
- //
4
- // 检查在本轮之内、下一次请求模型之前(context.project)运行,不放到 session.afterTurn:模型要在同一轮里看到
5
- // 自己刚引入的错误并改掉;轮次结束后才检查就得再发起一轮(session.send),等于替用户多说一句话。
6
- //
7
- // 检查会执行仓库里的代码(node_modules 里的 tsc、.jade/checks.json 里的命令),每条命令第一次运行前请用户确认,
8
- // 记在 $JADE_HOME/trust.jsonl(和旧 jade 同一个文件、同一种记录,旧 jade 里确认过的不用再确认)。
9
- // yolo 模式不问;不能提问时不运行没确认过的命令。
10
1
  import { createHash } from "node:crypto";
11
2
  import path from "node:path";
12
3
  import { definePlugin } from "@uncarved/jade-api";
13
4
  import { changedFiles, checks } from "./checks.js";
14
- import { bold, dim, display, realPath, repoRoot, TrustFile, yellow } from "./project.js";
15
- /** 确认记录:同一个目录里的同一条命令只问一次。和旧 jade 的算法相同 */
16
- const checkHash = (c) => createHash("sha256").update(`jade-check\0${c.cwd}\0${c.command}`).digest("hex");
17
- /** 展开后最多显示的行数 */
5
+ import { bold, dim, display, lines as joinLines, realPath, repoRoot, warn, withLegacyTrust } from "./project.js";
6
+ const checkHash = (c) => createHash("sha256").update(`jade-check\x00${c.cwd}\x00${c.command}`).digest("hex");
18
7
  const MAX_DETAIL_LINES = 40;
19
- /** 会话 id → 这个会话的检查(/checks 用) */
20
- const bySession = new Map();
21
- /** process() 里准备好、session() 里调用:用进程级的确认记录和"一次只问一个问题" */
8
+ const bySession = new Map;
22
9
  let sessionSetup;
23
10
  export const checksRenderers = [
24
- {
25
- event: "checked",
26
- render: (data) => {
27
- const d = data;
28
- const bad = d.summary.filter((s) => s.problems > 0 || s.others || s.failed);
29
- const lines = d.report ? d.report.split("\n").slice(1) : [];
30
- const details = lines.length
31
- ? [...lines.slice(0, MAX_DETAIL_LINES), ...(lines.length > MAX_DETAIL_LINES ? [dim(`… ${lines.length - MAX_DETAIL_LINES} more lines`)] : [])].join("\n")
32
- : undefined;
33
- const withDetails = (line) => (details ? { line, details } : { line });
34
- if (bad.length === 0)
35
- return withDetails(dim(`✓ checks passed: ${d.summary.map((s) => s.name).join(", ")}`));
36
- const what = bad.map((s) => s.failed
37
- ? `${s.name} ${s.failed}`
38
- : `${s.name}: ${[s.problems > 0 ? `${s.problems} problem${s.problems > 1 ? "s" : ""} in changed files` : "", s.others ? `${s.others} new elsewhere` : ""]
39
- .filter(Boolean)
40
- .join(", ")}`);
41
- return withDetails(yellow(`⚠ checks: ${what.join("; ")}`));
42
- },
43
- },
11
+ {
12
+ event: "checked",
13
+ render: (data) => {
14
+ const d = data;
15
+ const bad = d.summary.filter((s) => s.problems > 0 || s.others || s.failed);
16
+ const lines = d.report ? d.report.split(`
17
+ `).slice(1) : [];
18
+ const details = lines.length ? joinLines([...lines.slice(0, MAX_DETAIL_LINES), ...lines.length > MAX_DETAIL_LINES ? [dim(`… ${lines.length - MAX_DETAIL_LINES} more lines`)] : []]) : undefined;
19
+ const withDetails = (line) => details ? { line, details } : { line };
20
+ if (bad.length === 0)
21
+ return withDetails(dim(`✓ checks passed: ${d.summary.map((s) => s.name).join(", ")}`));
22
+ const what = bad.map((s) => s.failed ? `${s.name} ${s.failed}` : `${s.name}: ${[s.problems > 0 ? `${s.problems} problem${s.problems > 1 ? "s" : ""} in changed files` : "", s.others ? `${s.others} new elsewhere` : ""].filter(Boolean).join(", ")}`);
23
+ return withDetails(warn(`⚠ checks: ${what.join("; ")}`));
24
+ }
25
+ }
44
26
  ];
45
27
  export default definePlugin({
46
- process(ctx) {
47
- const off = ctx.config.enabled === false || process.env.JADE_CHECKS === "off";
48
- const trust = new TrustFile(path.join(ctx.home, "trust.jsonl"));
49
- const renderers = ctx.service("renderers@1");
50
- if (renderers)
51
- for (const r of checksRenderers)
52
- renderers.event(r);
53
- ctx.service("commands@1")?.register({
54
- name: "checks",
55
- args: "[on|off]",
56
- description: "checks jade runs after the agent edits files (type checker, linter)",
57
- instant: true,
58
- async run(args, c) {
59
- const session = c.sessions.current;
60
- const checks = session && bySession.get(session.id);
61
- if (!checks) {
62
- if (off)
63
- return dim(`Edit-time checks are turned off${process.env.JADE_CHECKS === "off" ? " by JADE_CHECKS=off" : ` ({"jade/checks": {"enabled": false}} in ${path.join(ctx.home, "config.json")})`}.`);
64
- return dim(unavailable ?? "Edit-time checks are not available in this session.");
65
- }
66
- const arg = args.trim();
67
- if (arg === "on" || arg === "off") {
68
- checks.enabled = arg === "on";
69
- if (arg === "on")
70
- checks.reset();
71
- return `checks: ${arg}`;
72
- }
73
- if (arg)
74
- return yellow("usage: /checks [on|off]");
75
- const files = changedFiles(session.state.messages, realPath(ctx.cwd));
76
- const found = await checks.resolve(files);
77
- const lines = [`Edit-time checks are ${checks.enabled ? "on" : "off"} in this session.`];
78
- if (found.length > 0) {
79
- lines.push("For the files changed in this session:");
80
- for (const check of found) {
81
- const pending = (await trust.has(checkHash(check))) ? "" : ", not confirmed yet";
82
- lines.push(` ${bold(check.name)} ${dim(`in ${display(ctx.cwd, check.cwd) || "."}${pending}`)}\n ${dim(check.command)}`);
83
- }
84
- }
85
- else
86
- lines.push(dim(files.length ? "No checks apply to the files changed in this session." : "No files changed in this session yet."));
87
- lines.push(dim("Detected per file: TypeScript (nearest tsconfig.json + the project's typescript), Python (ruff), Go (go vet).\n" +
88
- `Add your own in .jade/checks.json or ${path.join(ctx.home, "checks.json")}; JADE_CHECKS=off turns them off.`));
89
- return lines.join("\n");
90
- },
28
+ process(ctx) {
29
+ const off = ctx.config.enabled === false || process.env.JADE_CHECKS === "off";
30
+ const trust = withLegacyTrust(ctx.require("trust@1"), ctx.home);
31
+ const renderers = ctx.service("renderers@1");
32
+ if (renderers)
33
+ for (const r of checksRenderers)
34
+ renderers.event(r);
35
+ ctx.service("commands@1")?.register({
36
+ name: "checks",
37
+ args: "[on|off]",
38
+ description: "checks jade runs after the agent edits files (type checker, linter)",
39
+ instant: true,
40
+ async run(args, c) {
41
+ const session = c.sessions.current;
42
+ const checks = session && bySession.get(session.id);
43
+ if (!checks) {
44
+ if (off)
45
+ return dim(`Edit-time checks are turned off${process.env.JADE_CHECKS === "off" ? " by JADE_CHECKS=off" : ` ({"jade/checks": {"enabled": false}} in ${path.join(ctx.home, "config.json")})`}.`);
46
+ return dim(unavailable ?? "Edit-time checks are not available in this session.");
47
+ }
48
+ const arg = args.trim();
49
+ if (arg === "on" || arg === "off") {
50
+ checks.enabled = arg === "on";
51
+ if (arg === "on")
52
+ checks.reset();
53
+ return `checks: ${arg}`;
54
+ }
55
+ if (arg)
56
+ return warn("usage: /checks [on|off]");
57
+ const files = changedFiles(session.state.messages, realPath(ctx.cwd), (t) => session.tool(t)?.meta?.writes);
58
+ const found = await checks.resolve(files);
59
+ const lines = [`Edit-time checks are ${checks.enabled ? "on" : "off"} in this session.`];
60
+ if (found.length > 0) {
61
+ lines.push("For the files changed in this session:");
62
+ for (const check of found) {
63
+ const pending = await trust.has(checkHash(check)) ? "" : ", not confirmed yet";
64
+ lines.push([" ", bold(check.name), " ", dim(`in ${display(ctx.cwd, check.cwd) || "."}${pending}`), `
65
+ `, dim(check.command)]);
66
+ }
67
+ } else
68
+ lines.push(dim(files.length ? "No checks apply to the files changed in this session." : "No files changed in this session yet."));
69
+ lines.push(dim(`Detected per file: TypeScript (nearest tsconfig.json + the project's typescript), Python (ruff), Go (go vet).
70
+ ` + `Add your own in .jade/checks.json or ${path.join(ctx.home, "checks.json")}; JADE_CHECKS=off turns them off.`));
71
+ return joinLines(lines);
72
+ }
73
+ });
74
+ let unavailable;
75
+ let asking = Promise.resolve();
76
+ sessionSetup = async (s) => {
77
+ if (off)
78
+ return;
79
+ const shell = s.require("shell@1");
80
+ if (!shell.executable) {
81
+ unavailable = `edit-time checks are off: ${shell.unavailable ?? "no shell was found"}`;
82
+ return;
83
+ }
84
+ const ui = s.service("ui@1");
85
+ const approve = async (check, signal) => {
86
+ const hash = checkHash(check);
87
+ if (await trust.has(hash))
88
+ return true;
89
+ if (s.service("approval@1")?.mode() === "yolo")
90
+ return true;
91
+ if (!ui?.interactive)
92
+ return false;
93
+ const turn = asking.then(async () => {
94
+ if (await trust.has(hash))
95
+ return true;
96
+ signal.throwIfAborted();
97
+ const answer = await ui.select({
98
+ title: `Run ${check.name} after the agent edits files, and show it the problems found?`,
99
+ detail: `${check.command}
100
+ in ${display(s.cwd, check.cwd) || "."}${check.source === "config" ? " (from checks.json)" : " (detected)"}`,
101
+ options: [
102
+ { label: "Yes, in this project from now on", value: "yes" },
103
+ { label: "No, not in this session", value: "no" }
104
+ ]
105
+ }, signal);
106
+ if (answer === "yes") {
107
+ await trust.add(hash, check.cwd);
108
+ return true;
109
+ }
110
+ ui.notify(`${check.name} will not run in this session; /checks on to ask again`);
111
+ return false;
91
112
  });
92
- /** 找不到 shell 时的说明,/checks 显示 */
93
- let unavailable;
94
- /** 一次只问一个问题 */
95
- let asking = Promise.resolve();
96
- sessionSetup = async (s) => {
97
- if (off)
98
- return;
99
- // 找 shell、杀进程树都在 jade/shell 里(shell@1),这里不再复制一份
100
- const shell = s.require("shell@1");
101
- if (!shell.executable) {
102
- unavailable = `edit-time checks are off: ${shell.unavailable ?? "no shell was found"}`;
103
- return;
104
- }
105
- const ui = s.service("ui@1");
106
- const approve = async (check, signal) => {
107
- const hash = checkHash(check);
108
- if (await trust.has(hash))
109
- return true;
110
- if (s.service("approval@1")?.mode() === "yolo")
111
- return true;
112
- if (!ui?.interactive)
113
- return false;
114
- const turn = asking.then(async () => {
115
- if (await trust.has(hash))
116
- return true;
117
- signal.throwIfAborted();
118
- const answer = await ui.select({
119
- title: `Run ${check.name} after the agent edits files, and show it the problems found?`,
120
- detail: `${check.command}\nin ${display(s.cwd, check.cwd) || "."}${check.source === "config" ? " (from checks.json)" : " (detected)"}`,
121
- options: [
122
- { label: "Yes, in this project from now on", value: "yes" },
123
- { label: "No, not in this session", value: "no" },
124
- ],
125
- }, signal);
126
- if (answer === "yes") {
127
- await trust.add(hash, check.cwd);
128
- return true;
129
- }
130
- ui.notify(`${check.name} will not run in this session; /checks on to ask again`);
131
- return false;
132
- });
133
- asking = turn.catch(() => { });
134
- return turn;
135
- };
136
- const cwd = realPath(s.cwd);
137
- const c = checks({ cwd, root: realPath(repoRoot(cwd)), home: s.home, run: shell.run, which: (bin) => shell.which(bin) ?? null, approve });
138
- const id = s.session.id;
139
- bySession.set(id, c);
140
- await s.effect(() => () => {
141
- if (bySession.get(id) === c)
142
- bySession.delete(id);
143
- });
144
- s.kernel.hook(c.hook);
145
- };
146
- },
147
- async session(ctx) {
148
- await sessionSetup?.(ctx);
149
- },
113
+ asking = turn.catch(() => {});
114
+ return turn;
115
+ };
116
+ const cwd = realPath(s.cwd);
117
+ const c = checks({ cwd, root: realPath(repoRoot(cwd)), home: s.home, run: shell.run, which: (bin) => shell.which(bin) ?? null, approve, writes: (t) => s.session.tool(t)?.meta?.writes });
118
+ const id = s.session.id;
119
+ bySession.set(id, c);
120
+ await s.effect(() => () => {
121
+ if (bySession.get(id) === c)
122
+ bySession.delete(id);
123
+ });
124
+ s.kernel.hook(c.hook);
125
+ };
126
+ },
127
+ async session(ctx) {
128
+ await sessionSetup?.(ctx);
129
+ }
150
130
  });
@@ -4,7 +4,8 @@
4
4
  "api": "^1",
5
5
  "entry": "./index.js",
6
6
  "requires": [
7
- "shell@1"
7
+ "shell@1",
8
+ "trust@1"
8
9
  ],
9
10
  "optional": [
10
11
  "ui@1",
@@ -1,76 +1,68 @@
1
- // 仓库目录、确认记录、显示用的小工具。插件之间不共享代码(只能共享类型),jade/mcp、jade/skills 各有一份。
2
1
  import { existsSync, realpathSync } from "node:fs";
3
- import { appendFile, mkdir, readFile } from "node:fs/promises";
2
+ import { readFile } from "node:fs/promises";
4
3
  import path from "node:path";
5
4
  export function realPath(p) {
6
- try {
7
- return realpathSync.native(p);
8
- }
9
- catch {
10
- return path.resolve(p);
11
- }
5
+ try {
6
+ return realpathSync.native(p);
7
+ } catch {
8
+ return path.resolve(p);
9
+ }
12
10
  }
13
- /** git 仓库的根目录(往上找 .git,目录或文件都算);不在仓库里时是 cwd 本身 */
14
11
  export function repoRoot(cwd) {
15
- let d = path.resolve(cwd);
16
- for (;;) {
17
- if (existsSync(path.join(d, ".git")))
18
- return d;
19
- const parent = path.dirname(d);
20
- if (parent === d)
21
- return path.resolve(cwd);
22
- d = parent;
23
- }
12
+ let d = path.resolve(cwd);
13
+ for (;; ) {
14
+ if (existsSync(path.join(d, ".git")))
15
+ return d;
16
+ const parent = path.dirname(d);
17
+ if (parent === d)
18
+ return path.resolve(cwd);
19
+ d = parent;
20
+ }
24
21
  }
25
- /** 工作目录里的显示成相对路径,外面的显示绝对路径 */
26
22
  export function display(cwd, full) {
27
- const rel = path.relative(cwd, full);
28
- const shown = rel && rel !== ".." && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel) ? rel : full;
29
- return shown.split(path.sep).join("/");
23
+ const rel = path.relative(cwd, full);
24
+ const shown = rel && rel !== ".." && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel) ? rel : full;
25
+ return shown.split(path.sep).join("/");
30
26
  }
31
- /**
32
- * 用户确认过的东西(检查命令),每行一条 { hash, path, at }。
33
- * 和旧 jade 的 $JADE_HOME/trust.jsonl 同一个文件、同一种格式,旧 jade 里确认过的不用再确认
34
- */
35
- export class TrustFile {
36
- file;
37
- #known = new Set();
38
- constructor(file) {
39
- this.file = file;
40
- }
41
- async has(hash) {
42
- if (this.#known.has(hash))
43
- return true;
44
- let text = "";
45
- try {
46
- text = await readFile(this.file, "utf8");
47
- }
48
- catch {
49
- return false;
50
- }
51
- for (const line of text.split("\n")) {
52
- try {
53
- if (JSON.parse(line).hash === hash) {
54
- this.#known.add(hash);
55
- return true;
56
- }
57
- }
58
- catch {
59
- // 写到一半的行
60
- }
61
- }
62
- return false;
27
+ export const clip = (s, n) => s.length > n ? s.slice(0, n) + "…" : s;
28
+ const styled = (style) => (text) => ({ text, style });
29
+ export const dim = styled("dim");
30
+ export const bold = styled("bold");
31
+ export const ok = styled("ok");
32
+ export const warn = styled("warn");
33
+ export const error = styled("error");
34
+ export const lines = (rows) => rows.flatMap((r, i) => i ? [`
35
+ `, r] : [r]);
36
+ export function withLegacyTrust(trust, home) {
37
+ let legacy;
38
+ const read = async () => {
39
+ const hashes = new Map;
40
+ let text = "";
41
+ try {
42
+ text = await readFile(path.join(home, "trust.jsonl"), "utf8");
43
+ } catch {
44
+ return hashes;
63
45
  }
64
- async add(hash, where) {
65
- await mkdir(path.dirname(this.file), { recursive: true });
66
- await appendFile(this.file, `${JSON.stringify({ hash, path: where, at: new Date().toISOString() })}\n`);
67
- this.#known.add(hash);
46
+ for (const line of text.split(`
47
+ `)) {
48
+ try {
49
+ const r = JSON.parse(line);
50
+ if (typeof r.hash === "string")
51
+ hashes.set(r.hash, typeof r.path === "string" ? r.path : "");
52
+ } catch {}
68
53
  }
54
+ return hashes;
55
+ };
56
+ return {
57
+ has: async (hash) => {
58
+ if (await trust.has(hash))
59
+ return true;
60
+ const where = (await (legacy ??= read())).get(hash);
61
+ if (where === undefined)
62
+ return false;
63
+ await trust.add(hash, where).catch(() => {});
64
+ return true;
65
+ },
66
+ add: (hash, where) => trust.add(hash, where)
67
+ };
69
68
  }
70
- export const clip = (s, n) => (s.length > n ? s.slice(0, n) + "…" : s);
71
- // 设了 NO_COLOR(非空)或标准输出不是终端时不输出颜色,见 https://no-color.org
72
- const colors = Boolean(process.stdout.isTTY) && !process.env.NO_COLOR;
73
- const paint = (open, close) => (s) => colors ? `\x1b[${open}m${s.replaceAll(`\x1b[${close}m`, `\x1b[${close}m\x1b[${open}m`)}\x1b[${close}m` : s;
74
- export const dim = paint("2", "22");
75
- export const yellow = paint("33", "39");
76
- export const bold = paint("1", "22");
@@ -1,33 +1,51 @@
1
- // jade/commands:斜杠命令的注册表。只登记,不执行:前端解析输入、带着自己的上下文调用 run。
2
- // 每个调用方一个实例,调用方关闭(停用、被隔离、换会话时的会话作用域)时它注册的命令自动撤销。
3
1
  import { definePlugin } from "@uncarved/jade-api";
4
2
  const NAME = /^[a-z0-9][a-z0-9_-]*$/i;
5
- const registry = new Map();
3
+ const registry = new Map;
4
+ const prefixes = new Map;
6
5
  export default definePlugin({
7
- process(ctx) {
8
- ctx.provide("commands@1", (caller) => ({
9
- register(command) {
10
- if (typeof command?.name !== "string" || !NAME.test(command.name))
11
- throw new Error(`invalid command name ${JSON.stringify(command?.name)}`);
12
- if (typeof command.run !== "function")
13
- throw new Error(`command /${command.name} has no run function`);
14
- const key = command.name.toLowerCase();
15
- const taken = registry.get(key);
16
- if (taken)
17
- throw new Error(`command /${command.name} is already registered by ${taken.plugin}`);
18
- const entry = { command, plugin: caller.plugin };
19
- registry.set(key, entry);
20
- const revoke = () => {
21
- if (registry.get(key) === entry)
22
- registry.delete(key);
23
- };
24
- caller.onDispose(revoke);
25
- return revoke;
26
- },
27
- get: (name) => registry.get(name.toLowerCase()),
28
- list: () => [...registry.values()]
29
- .map(({ command: c, plugin }) => ({ name: c.name, ...(c.args ? { args: c.args } : {}), ...(c.description ? { description: c.description } : {}), ...(c.instant ? { instant: true } : {}), plugin }))
30
- .sort((a, b) => a.name.localeCompare(b.name)),
31
- }));
32
- },
6
+ process(ctx) {
7
+ ctx.provide("commands@1", (caller) => ({
8
+ register(command) {
9
+ if (typeof command?.name !== "string" || !NAME.test(command.name))
10
+ throw new Error(`invalid command name ${JSON.stringify(command?.name)}`);
11
+ if (typeof command.run !== "function")
12
+ throw new Error(`command /${command.name} has no run function`);
13
+ const key = command.name.toLowerCase();
14
+ const taken = registry.get(key);
15
+ if (taken) {
16
+ ctx.report(`/${command.name} from ${caller.plugin} was skipped: ${taken.plugin} already registered it`);
17
+ return () => {};
18
+ }
19
+ if (command.prefix !== undefined && (typeof command.prefix !== "string" || [...command.prefix].length !== 1 || /[\p{L}\p{N}\s/]/u.test(command.prefix))) {
20
+ throw new Error(`command /${command.name}: prefix must be one character that is not a letter, digit, space or "/"`);
21
+ }
22
+ let entry = { command, plugin: caller.plugin };
23
+ let prefix = command.prefix;
24
+ const owner = prefix === undefined ? undefined : prefixes.get(prefix);
25
+ if (prefix !== undefined && owner !== undefined) {
26
+ ctx.report(`the ${prefix} shortcut for /${command.name} from ${caller.plugin} was skipped: /${owner} already uses it`);
27
+ const { prefix: _, prefixHint: __, ...rest } = command;
28
+ entry = { command: rest, plugin: caller.plugin };
29
+ prefix = undefined;
30
+ }
31
+ registry.set(key, entry);
32
+ if (prefix !== undefined)
33
+ prefixes.set(prefix, key);
34
+ const revoke = () => {
35
+ if (registry.get(key) === entry)
36
+ registry.delete(key);
37
+ if (prefix !== undefined && prefixes.get(prefix) === key)
38
+ prefixes.delete(prefix);
39
+ };
40
+ caller.onDispose(revoke);
41
+ return revoke;
42
+ },
43
+ get: (name) => registry.get(name.toLowerCase()),
44
+ byPrefix: (prefix) => {
45
+ const name = prefixes.get(prefix);
46
+ return name === undefined ? undefined : registry.get(name);
47
+ },
48
+ list: () => [...registry.values()].map(({ command: c, plugin }) => ({ name: c.name, ...c.args ? { args: c.args } : {}, ...c.description ? { description: c.description } : {}, ...c.instant ? { instant: true } : {}, ...c.prefix ? { prefix: c.prefix } : {}, ...c.prefixHint ? { prefixHint: c.prefixHint } : {}, plugin })).sort((a, b) => a.name.localeCompare(b.name))
49
+ }));
50
+ }
33
51
  });