@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,85 +1,73 @@
1
- // 保存过的模型:$JADE_HOME/models.json。同一个服务(类型、地址、密钥都相同)只存一份,下面是在它上面用过的模型,
2
- // 每个模型带自己的设置(输出上限、思考力度、上下文窗口、openai-compat 的额外请求参数等)。.env 里始终是当前用的那一个,这里记着用过的全部,
3
- // /model 可以切回去。
4
1
  import { mkdir, readFile, writeFile } from "node:fs/promises";
5
2
  import path from "node:path";
6
- /** 每个模型自己的设置:切换模型时一起换掉 */
7
3
  export const MODEL_SETTINGS = ["JADE_MAX_TOKENS", "JADE_EFFORT", "JADE_CONTEXT_WINDOW", "JADE_RPM", "JADE_VISION", "OPENAI_COMPAT_EXTRA_BODY"];
8
4
  const strings = (v) => typeof v === "object" && v !== null ? Object.fromEntries(Object.entries(v).filter(([, x]) => typeof x === "string")) : {};
9
- /** 读不了或格式不对时返回空数组 */
10
5
  export async function readSavedServices(file) {
11
- try {
12
- const data = JSON.parse(await readFile(file, "utf8"));
13
- if (!Array.isArray(data.services))
14
- return [];
15
- return data.services.flatMap((raw) => {
16
- const s = raw;
17
- if (!s || !Array.isArray(s.models))
18
- return [];
19
- // 以前额外请求参数存在服务上:分给它下面没有自己那份的模型
20
- const { OPENAI_COMPAT_EXTRA_BODY: extraBody, ...env } = strings(s.env);
21
- const models = s.models.flatMap((m) => {
22
- const name = m?.name;
23
- const settings = strings(m.settings);
24
- if (extraBody && settings.OPENAI_COMPAT_EXTRA_BODY === undefined)
25
- settings.OPENAI_COMPAT_EXTRA_BODY = extraBody;
26
- return typeof name === "string" && name ? [{ name, settings }] : [];
27
- });
28
- return models.length ? [{ env, models }] : [];
29
- });
30
- }
31
- catch {
6
+ try {
7
+ const data = JSON.parse(await readFile(file, "utf8"));
8
+ if (!Array.isArray(data.services))
9
+ return [];
10
+ return data.services.flatMap((raw) => {
11
+ const s = raw;
12
+ if (!s || !Array.isArray(s.models))
32
13
  return [];
33
- }
14
+ const { OPENAI_COMPAT_EXTRA_BODY: extraBody, ...env } = strings(s.env);
15
+ const models = s.models.flatMap((m) => {
16
+ const name = m?.name;
17
+ const settings = strings(m.settings);
18
+ if (extraBody && settings.OPENAI_COMPAT_EXTRA_BODY === undefined)
19
+ settings.OPENAI_COMPAT_EXTRA_BODY = extraBody;
20
+ return typeof name === "string" && name ? [{ name, settings }] : [];
21
+ });
22
+ return models.length ? [{ env, models }] : [];
23
+ });
24
+ } catch {
25
+ return [];
26
+ }
34
27
  }
35
28
  export async function writeSavedServices(file, services) {
36
- await mkdir(path.dirname(file), { recursive: true });
37
- // 里面有 API 密钥
38
- await writeFile(file, JSON.stringify({ services }, null, 2) + "\n", { mode: 0o600 });
29
+ await mkdir(path.dirname(file), { recursive: true });
30
+ await writeFile(file, JSON.stringify({ services }, null, 2) + `
31
+ `, { mode: 384 });
39
32
  }
40
- /**
41
- * 改写配置文件里的变量:已有的行原地替换,没有的追加在末尾,值为 undefined 的删除;注释和其他行保持不变。
42
- * 同时更新 env,让改动在本次运行中生效
43
- */
44
33
  export async function updateEnvFile(file, updates, env) {
45
- let text = "";
46
- try {
47
- text = await readFile(file, "utf8");
48
- }
49
- catch {
50
- // 还没有配置文件
51
- }
52
- const lines = text ? text.replace(/\r?\n$/, "").split(/\r?\n/) : [];
53
- const out = [];
54
- const written = new Set();
55
- for (const raw of lines) {
56
- const key = /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=/.exec(raw)?.[1];
57
- if (!key || !(key in updates)) {
58
- out.push(raw);
59
- continue;
60
- }
61
- // 同一个键出现多次时只保留第一处
62
- const value = updates[key];
63
- if (value !== undefined && !written.has(key))
64
- out.push(format(key, value));
65
- written.add(key);
66
- }
67
- for (const [key, value] of Object.entries(updates)) {
68
- if (value !== undefined && !written.has(key))
69
- out.push(format(key, value));
70
- }
71
- await mkdir(path.dirname(file), { recursive: true });
72
- await writeFile(file, out.join("\n") + "\n");
73
- for (const [key, value] of Object.entries(updates)) {
74
- if (value === undefined)
75
- delete env[key];
76
- else
77
- env[key] = value;
34
+ let text = "";
35
+ try {
36
+ text = await readFile(file, "utf8");
37
+ } catch {}
38
+ const lines = text ? text.replace(/\r?\n$/, "").split(/\r?\n/) : [];
39
+ const out = [];
40
+ const written = new Set;
41
+ for (const raw of lines) {
42
+ const key = /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=/.exec(raw)?.[1];
43
+ if (!key || !(key in updates)) {
44
+ out.push(raw);
45
+ continue;
78
46
  }
47
+ const value = updates[key];
48
+ if (value !== undefined && !written.has(key))
49
+ out.push(format(key, value));
50
+ written.add(key);
51
+ }
52
+ for (const [key, value] of Object.entries(updates)) {
53
+ if (value !== undefined && !written.has(key))
54
+ out.push(format(key, value));
55
+ }
56
+ await mkdir(path.dirname(file), { recursive: true });
57
+ await writeFile(file, out.join(`
58
+ `) + `
59
+ `);
60
+ for (const [key, value] of Object.entries(updates)) {
61
+ if (value === undefined)
62
+ delete env[key];
63
+ else
64
+ env[key] = value;
65
+ }
79
66
  }
80
67
  function format(key, value) {
81
- if (!/[\s#"']/.test(value))
82
- return `${key}=${value}`;
83
- // 单引号里的内容原样读回(引号、反斜杠都不转义);只有换行要用双引号里的 \n
84
- return value.includes("\n") ? `${key}="${value.replaceAll("\n", "\\n")}"` : `${key}='${value}'`;
68
+ if (!/[\s#"']/.test(value))
69
+ return `${key}=${value}`;
70
+ return value.includes(`
71
+ `) ? `${key}="${value.replaceAll(`
72
+ `, "\\n")}"` : `${key}='${value}'`;
85
73
  }
@@ -1,190 +1,163 @@
1
- // jade/modes:权限模式的切换。模式本身归 jade/approval(approval@1),这里提供切换方式和给前端显示的状态:
2
- // Shift+Tab 在 ask → auto → plan 之间轮换,/mode 查看或选择,modes@1 让前端显示当前模式、在模式变化时更新。
3
- //
4
- // 计划模式的两件事也在这里:
5
- // - 在 plan 模式时经 prompts@1 往系统提示词里加一段,告诉模型先给计划、不要改东西(切出去时去掉)
6
- // - propose_plan 工具:模型写好要执行的计划后调用它,问用户要不要执行;批准了就换模式,工具结果告诉模型开始做。
7
- // 由模型声明"这是要执行的计划",追问、回答问题、只要计划本身(比如健身计划)的回复不会被当成计划去问。
8
- // 用户的选择在工具结果里,会话日志里看得到
9
1
  import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
10
2
  import path from "node:path";
11
3
  import { definePlugin } from "@uncarved/jade-api";
12
4
  export const DESCRIPTIONS = {
13
- ask: "ask before edits and commands",
14
- auto: "edits in this directory and harmless commands run without asking; others still ask",
15
- plan: "change nothing: research read-only and propose a plan",
16
- readonly: "change nothing",
17
- yolo: "never ask",
5
+ ask: "ask before edits and commands",
6
+ auto: "edits in this directory and harmless commands run without asking; others still ask",
7
+ plan: "change nothing: research read-only and propose a plan",
8
+ readonly: "change nothing",
9
+ yolo: "never ask"
18
10
  };
19
11
  const CYCLE = ["ask", "auto", "plan"];
20
- /** 计划模式时加进系统提示词的段落;不能问用户(-p、非终端)时没有 propose_plan,只让模型给出计划后停下 */
21
12
  export function planNote(canPropose) {
22
- return ("The session is in plan mode. Investigate with read-only tools (and the task tool for broad searches); do not try to modify files " +
23
- "or run commands that change anything, such calls are denied. If the request is unclear in a way that changes the plan, ask the user first. " +
24
- "When the task needs changes made (files edited, commands run), reply with a concise, concrete plan: the steps in order (for code, the files to change and how), " +
25
- "any open questions, and how you will verify the result. " +
26
- (canPropose
27
- ? "Then call propose_plan so the user can approve it. If nothing needs to be executed (a question, or the plan itself is the answer the user wants), " +
28
- "just answer and do not call propose_plan."
29
- : "Then stop and wait for the user's approval."));
13
+ return "The session is in plan mode. Investigate with read-only tools (and the task tool for broad searches); do not try to modify files " + "or run commands that change anything, such calls are denied. If the request is unclear in a way that changes the plan, ask the user first. " + "When the task needs changes made (files edited, commands run), reply with a concise, concrete plan: the steps in order (for code, the files to change and how), " + "any open questions, and how you will verify the result. " + (canPropose ? "Then call propose_plan so the user can approve it. If nothing needs to be executed (a question, or the plan itself is the answer the user wants), " + "just answer and do not call propose_plan." : "Then stop and wait for the user's approval.");
30
14
  }
31
- /** 批准计划之后工具结果里的那句 */
32
15
  export const APPROVED_NOTE = "The user approved the plan. Implement it now.";
33
- /** 没批准时工具结果里的那句 */
34
16
  export const NOT_APPROVED = "The user did not approve the plan yet. End your turn and wait for their feedback.";
35
17
  export const PROPOSE_PLAN = "propose_plan";
36
- /** propose_plan:问用户要不要执行刚写好的计划,批准了就换到用户选的模式 */
37
18
  export function proposePlanTool(approval, ui) {
38
- return {
39
- name: PROPOSE_PLAN,
40
- description: "In plan mode, ask the user to approve the plan you just wrote in your reply, and wait for the answer. Write the full plan in your reply text first; " +
41
- "call this only for a plan of changes to execute, not for questions, clarifications or answers that need nothing executed.",
42
- inputSchema: { type: "object", properties: {}, additionalProperties: false },
43
- // 不碰文件和进程:plan 模式下不用批准
44
- effect: "read",
45
- concurrency: "exclusive",
46
- idempotent: false,
47
- async execute(_input, ctx) {
48
- if (approval.mode() !== "plan")
49
- return { content: "The session is not in plan mode; no approval is needed.", isError: true };
50
- const choice = await ui.select({
51
- title: "Execute this plan?",
52
- options: [
53
- { label: "Yes, in auto mode", value: "auto", hint: "edits and safe commands run without asking" },
54
- { label: "Yes, and ask before each edit", value: "ask", hint: "mode ask" },
55
- { label: "No, keep planning", value: "plan", hint: "type what to change" },
56
- ],
57
- }, ctx.signal);
58
- ctx.signal.throwIfAborted();
59
- if (choice !== "auto" && choice !== "ask")
60
- return { content: NOT_APPROVED };
61
- setMode(approval, choice);
62
- return { content: `${APPROVED_NOTE} (mode: ${choice})` };
63
- },
64
- };
19
+ return {
20
+ name: PROPOSE_PLAN,
21
+ description: "In plan mode, ask the user to approve the plan you just wrote in your reply, and wait for the answer. Write the full plan in your reply text first; " + "call this only for a plan of changes to execute, not for questions, clarifications or answers that need nothing executed.",
22
+ inputSchema: { type: "object", properties: {}, additionalProperties: false },
23
+ effect: "read",
24
+ concurrency: "exclusive",
25
+ idempotent: false,
26
+ async execute(_input, ctx) {
27
+ if (approval.mode() !== "plan")
28
+ return { content: "The session is not in plan mode; no approval is needed.", isError: true };
29
+ const choice = await ui.select({
30
+ title: "Execute this plan?",
31
+ options: [
32
+ { label: "Yes, in auto mode", value: "auto", hint: "edits and safe commands run without asking" },
33
+ { label: "Yes, and ask before each edit", value: "ask", hint: "mode ask" },
34
+ { label: "No, keep planning", value: "plan", hint: "type what to change" }
35
+ ]
36
+ }, ctx.signal);
37
+ ctx.signal.throwIfAborted();
38
+ if (choice !== "auto" && choice !== "ask")
39
+ return { content: NOT_APPROVED };
40
+ setMode(approval, choice);
41
+ return { content: `${APPROVED_NOTE} (mode: ${choice})` };
42
+ }
43
+ };
65
44
  }
66
- /** 模式变化时通知前端和会话里的订阅者(模块每个进程只加载一次,进程级和会话级共用) */
67
- const listeners = new Set();
68
- /**
69
- * 用户选的模式记在 $JADE_HOME/mode.json,下次启动接着用(比配置里的 mode 优先)。
70
- * plan 是一次任务里的,yolo 不该悄悄带到下一次启动:这两个不记
71
- */
45
+ const listeners = new Set;
72
46
  const REMEMBERED = new Set(["ask", "auto", "readonly"]);
73
47
  let modeFile;
74
- /** 记下的模式只在第一个可交互的会话里恢复一次:jade -p 这类不能问用户的运行一直用配置的模式 */
75
48
  let restored = false;
76
49
  function readRemembered(file) {
77
- try {
78
- const mode = JSON.parse(readFileSync(file, "utf8")).mode;
79
- return typeof mode === "string" && REMEMBERED.has(mode) ? mode : undefined;
80
- }
81
- catch {
82
- return undefined;
83
- }
50
+ try {
51
+ const mode = JSON.parse(readFileSync(file, "utf8")).mode;
52
+ return typeof mode === "string" && REMEMBERED.has(mode) ? mode : undefined;
53
+ } catch {
54
+ return;
55
+ }
84
56
  }
85
57
  function remember(mode) {
86
- if (!modeFile || !REMEMBERED.has(mode))
87
- return;
88
- try {
89
- // 同步写:连按几次 Shift+Tab 时,文件里留下的一定是最后一次
90
- mkdirSync(path.dirname(modeFile), { recursive: true });
91
- writeFileSync(modeFile, `${JSON.stringify({ mode })}\n`);
92
- }
93
- catch {
94
- // 记不下只是下次从配置的模式开始
95
- }
58
+ if (!modeFile || !REMEMBERED.has(mode))
59
+ return;
60
+ try {
61
+ mkdirSync(path.dirname(modeFile), { recursive: true });
62
+ writeFileSync(modeFile, `${JSON.stringify({ mode })}
63
+ `);
64
+ } catch {}
96
65
  }
97
66
  function setMode(approval, mode) {
98
- if (mode === approval.mode())
99
- return;
100
- approval.setMode(mode);
101
- remember(mode);
102
- for (const fn of [...listeners]) {
103
- try {
104
- fn(mode);
105
- }
106
- catch {
107
- // 订阅者的错误经过膜归它自己,不影响别的订阅者
108
- }
109
- }
67
+ if (mode === approval.mode())
68
+ return;
69
+ approval.setMode(mode);
70
+ remember(mode);
71
+ for (const fn of [...listeners]) {
72
+ try {
73
+ fn(mode);
74
+ } catch {}
75
+ }
110
76
  }
111
77
  export default definePlugin({
112
- process(ctx) {
113
- const approval = ctx.require("approval@1");
114
- modeFile = path.join(ctx.home, "mode.json");
115
- restored = false;
116
- const set = (mode) => setMode(approval, mode);
117
- const cycle = () => {
118
- const next = CYCLE[(CYCLE.indexOf(approval.mode()) + 1) % CYCLE.length];
119
- set(next);
120
- return next;
121
- };
122
- const list = () => Object.entries(DESCRIPTIONS).map(([name, description]) => ({ name, description }));
123
- ctx.provide("modes@1", (caller) => ({
124
- current: () => approval.mode(),
125
- list,
126
- set,
127
- cycle,
128
- subscribe(fn) {
129
- listeners.add(fn);
130
- const off = () => void listeners.delete(fn);
131
- caller.onDispose(off);
132
- return off;
133
- },
134
- }));
135
- ctx.service("keys@1")?.register({ key: "shift+tab", description: "cycle ask/auto/plan", run: () => void cycle() });
136
- ctx.service("commands@1")?.register({
137
- name: "mode",
138
- args: `[${Object.keys(DESCRIPTIONS).join("|")}]`,
139
- description: "show or change the permission mode",
140
- instant: true,
141
- async run(args, c) {
142
- let want = args.trim() || undefined;
143
- if (!want) {
144
- want = await c.ui.select({ title: "Permission mode", options: list().map((m) => ({ label: m.name, value: m.name, hint: m.name === approval.mode() ? "current" : m.description })) }, c.signal);
145
- if (!want)
146
- return;
147
- }
148
- if (!(want in DESCRIPTIONS))
149
- return `unknown mode ${want}; one of ${Object.keys(DESCRIPTIONS).join(", ")}`;
150
- set(want);
151
- return `mode: ${approval.mode()}`;
152
- },
153
- });
154
- },
155
- session(ctx) {
156
- const approval = ctx.require("approval@1");
157
- const prompts = ctx.service("prompts@1");
158
- const ui = ctx.service("ui@1");
159
- if (!restored && ui?.interactive && modeFile) {
160
- restored = true;
161
- const saved = readRemembered(modeFile);
162
- if (saved)
163
- setMode(approval, saved);
78
+ process(ctx) {
79
+ const approval = ctx.require("approval@1");
80
+ modeFile = path.join(ctx.home, "mode.json");
81
+ restored = false;
82
+ const set = (mode) => setMode(approval, mode);
83
+ const cycle = () => {
84
+ const next = CYCLE[(CYCLE.indexOf(approval.mode()) + 1) % CYCLE.length];
85
+ set(next);
86
+ return next;
87
+ };
88
+ const list = () => Object.entries(DESCRIPTIONS).map(([name, description]) => ({ name, description }));
89
+ ctx.provide("modes@1", (caller) => ({
90
+ current: () => approval.mode(),
91
+ list,
92
+ set,
93
+ cycle,
94
+ subscribe(fn) {
95
+ listeners.add(fn);
96
+ const off = () => void listeners.delete(fn);
97
+ caller.onDispose(off);
98
+ return off;
99
+ }
100
+ }));
101
+ ctx.service("keys@1")?.register({ key: "shift+tab", description: "cycle ask/auto/plan", run: () => void cycle() });
102
+ ctx.service("commands@1")?.register({
103
+ name: "mode",
104
+ args: `[${Object.keys(DESCRIPTIONS).join("|")}]`,
105
+ description: "show or change the permission mode",
106
+ instant: true,
107
+ async run(args, c) {
108
+ let want = args.trim() || undefined;
109
+ if (!want) {
110
+ want = await c.ui.select({ title: "Permission mode", options: list().map((m) => ({ label: m.name, value: m.name, hint: m.name === approval.mode() ? "current" : m.description })) }, c.signal);
111
+ if (!want)
112
+ return;
164
113
  }
165
- // 能问用户时才有 propose_plan;它只在 plan 模式下发给模型
166
- const canPropose = !!ui?.interactive;
167
- if (canPropose)
168
- ctx.kernel.tool(proposePlanTool(approval, ui));
169
- ctx.kernel.hook({
170
- kind: "transform",
171
- point: "context.build",
172
- fn: (req) => (approval.mode() === "plan" ? req : { ...req, tools: req.tools.filter((t) => t.name !== PROPOSE_PLAN) }),
173
- });
174
- // 计划模式的说明跟着模式进出系统提示词
175
- let note;
176
- const sync = () => {
177
- const plan = approval.mode() === "plan";
178
- if (plan && !note && prompts)
179
- note = prompts.add({ text: planNote(canPropose), stable: false });
180
- else if (!plan && note) {
181
- note();
182
- note = undefined;
183
- }
184
- };
185
- sync();
186
- const onChange = () => sync();
187
- listeners.add(onChange);
188
- ctx.effect(() => () => void listeners.delete(onChange));
189
- },
114
+ if (!(want in DESCRIPTIONS))
115
+ return `unknown mode ${want}; one of ${Object.keys(DESCRIPTIONS).join(", ")}`;
116
+ set(want);
117
+ return `mode: ${approval.mode()}`;
118
+ }
119
+ });
120
+ },
121
+ session(ctx) {
122
+ const approval = ctx.require("approval@1");
123
+ const prompts = ctx.service("prompts@1");
124
+ const ui = ctx.service("ui@1");
125
+ if (!restored && ui?.interactive && modeFile) {
126
+ restored = true;
127
+ const saved = readRemembered(modeFile);
128
+ if (saved)
129
+ setMode(approval, saved);
130
+ }
131
+ const canPropose = !!ui?.interactive && ctx.session.parent === undefined;
132
+ if (canPropose)
133
+ ctx.kernel.tool(proposePlanTool(approval, ui));
134
+ ctx.kernel.hook({
135
+ kind: "transform",
136
+ point: "context.build",
137
+ fn: (req) => approval.mode() === "plan" ? req : { ...req, tools: req.tools.filter((t) => t.name !== PROPOSE_PLAN) }
138
+ });
139
+ let note;
140
+ const sync = () => {
141
+ const plan = approval.mode() === "plan";
142
+ if (plan && !note && prompts)
143
+ note = prompts.add({ text: planNote(canPropose), stable: false });
144
+ else if (!plan && note) {
145
+ note();
146
+ note = undefined;
147
+ }
148
+ };
149
+ const show = () => {
150
+ const mode = approval.mode();
151
+ const tone = mode === "yolo" ? "error" : mode === "auto" ? "warn" : mode === "plan" ? "accent" : "ok";
152
+ ctx.status?.({ text: `${mode} (shift+tab)`, short: mode, tone, keep: true });
153
+ };
154
+ sync();
155
+ show();
156
+ const onChange = () => {
157
+ sync();
158
+ show();
159
+ };
160
+ listeners.add(onChange);
161
+ ctx.effect(() => () => void listeners.delete(onChange));
162
+ }
190
163
  });
@@ -1,37 +1,37 @@
1
- // jade/plugins:/plugins 命令。列出这次启动里每个插件的状态和原因(failed、skipped、conflicted……),
2
- // 以及每个服务由谁提供。数据来自核心自己的 jade@1 服务,和救援 CLI `jade plugins list` 是同一份状态。
3
1
  import { definePlugin } from "@uncarved/jade-api";
4
2
  export default definePlugin({
5
- process(ctx) {
6
- const commands = ctx.require("commands@1");
7
- const jade = ctx.require("jade@1");
8
- commands.register({
9
- name: "plugins",
10
- args: "[id]",
11
- description: "every plugin's state and why it is not active, and who provides each service",
12
- // 只读,不碰会话
13
- instant: true,
14
- run(args) {
15
- const all = jade.plugins();
16
- const query = args.trim();
17
- if (query) {
18
- const found = all.filter((p) => p.id === query || p.id?.endsWith(`/${query}`) || p.dir === query);
19
- if (found.length === 0)
20
- return `No plugin matches ${query}. /plugins lists them all.`;
21
- const lines = found.map((p) => `${p.id ?? p.dir}@${p.version ?? "?"} ${p.layer} ${p.state}${p.reason ? ` ${p.reason}` : ""}\n ${p.dir}`);
22
- for (const s of jade.services().filter((s) => found.some((p) => p.id === s.provider)))
23
- lines.push(` provides ${s.service}`);
24
- return lines.join("\n");
25
- }
26
- const lines = all.map((p) => `${p.id ?? p.dir}@${p.version ?? "?"} ${p.layer} ${p.state}${p.reason ? ` ${p.reason}` : ""}`);
27
- const broken = all.filter((p) => p.state !== "active");
28
- if (broken.length > 0)
29
- lines.push("", `${broken.length} of ${all.length} plugins are not active; /plugins <id> shows where one lives.`);
30
- const services = jade.services();
31
- if (services.length > 0)
32
- lines.push("", "services:", ...services.map((s) => ` ${s.service} ${s.provider}`));
33
- return lines.join("\n");
34
- },
35
- });
36
- },
3
+ process(ctx) {
4
+ const commands = ctx.require("commands@1");
5
+ const jade = ctx.require("jade@1");
6
+ commands.register({
7
+ name: "plugins",
8
+ args: "[id]",
9
+ description: "every plugin's state and why it is not active, and who provides each service",
10
+ instant: true,
11
+ run(args) {
12
+ const all = jade.plugins();
13
+ const query = args.trim();
14
+ if (query) {
15
+ const found = all.filter((p) => p.id === query || p.id?.endsWith(`/${query}`) || p.dir === query);
16
+ if (found.length === 0)
17
+ return `No plugin matches ${query}. /plugins lists them all.`;
18
+ const lines = found.map((p) => `${p.id ?? p.dir}@${p.version ?? "?"} ${p.layer} ${p.state}${p.reason ? ` ${p.reason}` : ""}
19
+ ${p.dir}`);
20
+ for (const s of jade.services().filter((s) => found.some((p) => p.id === s.provider)))
21
+ lines.push(` provides ${s.service}`);
22
+ return lines.join(`
23
+ `);
24
+ }
25
+ const lines = all.map((p) => `${p.id ?? p.dir}@${p.version ?? "?"} ${p.layer} ${p.state}${p.reason ? ` ${p.reason}` : ""}`);
26
+ const broken = all.filter((p) => p.state !== "active");
27
+ if (broken.length > 0)
28
+ lines.push("", `${broken.length} of ${all.length} plugins are not active; /plugins <id> shows where one lives.`);
29
+ const services = jade.services();
30
+ if (services.length > 0)
31
+ lines.push("", "services:", ...services.map((s) => ` ${s.service} ${s.provider}`));
32
+ return lines.join(`
33
+ `);
34
+ }
35
+ });
36
+ }
37
37
  });