@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,92 +1,77 @@
1
- // 跨运行时的进程和可执行文件查找:只用 Node 的标准库,Bun 和 Node ≥22.18 下走同一套代码。
2
1
  import { spawn as nodeSpawn, spawnSync as nodeSpawnSync } from "node:child_process";
3
2
  import { accessSync, constants, statSync } from "node:fs";
4
3
  import path from "node:path";
5
- const EMPTY = { async *[Symbol.asyncIterator]() { } };
6
- /**
7
- * 启动进程,等到确实启动了才返回。程序不存在(ENOENT)等启动错误会 reject,
8
- * 调用方用 try/catch 判断"没装",和 Bun.spawn 同步抛错的用法一致
9
- */
4
+ const EMPTY = { async* [Symbol.asyncIterator]() {} };
10
5
  export function spawn(cmd, opts = {}) {
11
- const child = nodeSpawn(cmd[0], cmd.slice(1), {
12
- cwd: opts.cwd,
13
- stdio: ["ignore", opts.stdout ?? "pipe", opts.stderr ?? "pipe"],
14
- detached: opts.detached ?? false,
15
- env: opts.env,
16
- // Windows 上不弹出控制台窗口
17
- windowsHide: true,
18
- });
19
- const exited = new Promise((resolve) => child.once("exit", (code) => resolve(code)));
20
- return new Promise((resolve, reject) => {
21
- child.once("error", reject);
22
- child.once("spawn", () => {
23
- child.off("error", reject);
24
- // 启动之后的错误(例如 kill 一个已经退出的进程)不应该变成未处理的异常
25
- child.on("error", () => { });
26
- resolve({
27
- pid: child.pid,
28
- stdout: child.stdout ?? EMPTY,
29
- stderr: child.stderr ?? EMPTY,
30
- exited,
31
- kill: () => void child.kill(),
32
- });
33
- });
6
+ const child = nodeSpawn(cmd[0], cmd.slice(1), {
7
+ cwd: opts.cwd,
8
+ stdio: ["ignore", opts.stdout ?? "pipe", opts.stderr ?? "pipe"],
9
+ detached: opts.detached ?? false,
10
+ env: opts.env,
11
+ windowsHide: true
12
+ });
13
+ const exited = new Promise((resolve) => child.once("exit", (code) => resolve(code)));
14
+ return new Promise((resolve, reject) => {
15
+ child.once("error", reject);
16
+ child.once("spawn", () => {
17
+ child.off("error", reject);
18
+ child.on("error", () => {});
19
+ resolve({
20
+ pid: child.pid,
21
+ stdout: child.stdout ?? EMPTY,
22
+ stderr: child.stderr ?? EMPTY,
23
+ exited,
24
+ kill: () => void child.kill()
25
+ });
34
26
  });
27
+ });
35
28
  }
36
- /** 同步运行,忽略输出和错误;用于退出前的清理 */
37
29
  export function runSync(cmd, opts = {}) {
38
- nodeSpawnSync(cmd[0], cmd.slice(1), { stdio: "ignore", windowsHide: true, env: opts.env });
30
+ nodeSpawnSync(cmd[0], cmd.slice(1), { stdio: "ignore", windowsHide: true, env: opts.env });
39
31
  }
40
- /** which 的结果,按 PATH 和 PATHEXT 记;找不到的过一会儿再查(会话中途装上的工具能用上) */
41
- const found = new Map();
42
- const MISS_TTL_MS = 30_000;
43
- /**
44
- * 在 PATH 里找可执行文件,找不到返回 undefined。Windows 上按 PATHEXT 补扩展名;带路径分隔符时只检查那个文件。
45
- * 结果会记住:编辑后检查每批修改都要找 ruff、go,每次同步扫一遍 PATH 要几毫秒,期间事件循环停住
46
- */
32
+ const found = new Map;
33
+ const MISS_TTL_MS = 30000;
47
34
  export function which(bin) {
48
- if (bin.includes("/") || (process.platform === "win32" && bin.includes("\\")))
49
- return lookup(bin);
50
- const key = `${bin}\0${process.env.PATH ?? process.env.Path ?? ""}\0${process.env.PATHEXT ?? ""}`;
51
- const hit = found.get(key);
52
- if (hit && (hit.file !== undefined || Date.now() - hit.at < MISS_TTL_MS))
53
- return hit.file;
54
- const file = lookup(bin);
55
- found.set(key, { file, at: Date.now() });
56
- return file;
35
+ if (bin.includes("/") || process.platform === "win32" && bin.includes("\\"))
36
+ return lookup(bin);
37
+ const key = `${bin}\x00${process.env.PATH ?? process.env.Path ?? ""}\x00${process.env.PATHEXT ?? ""}`;
38
+ const hit = found.get(key);
39
+ if (hit && (hit.file !== undefined || Date.now() - hit.at < MISS_TTL_MS))
40
+ return hit.file;
41
+ const file = lookup(bin);
42
+ found.set(key, { file, at: Date.now() });
43
+ return file;
57
44
  }
58
45
  function lookup(bin) {
59
- const win = process.platform === "win32";
60
- const exts = win ? ["", ...(process.env.PATHEXT ?? ".COM;.EXE;.BAT;.CMD").split(";").filter(Boolean)] : [""];
61
- const ok = (file) => {
62
- try {
63
- if (!statSync(file).isFile())
64
- return false;
65
- if (!win)
66
- accessSync(file, constants.X_OK);
67
- return true;
68
- }
69
- catch {
70
- return false;
71
- }
72
- };
73
- const tryAll = (base) => {
74
- for (const ext of exts) {
75
- // Windows 上没有扩展名的文件不能直接执行(例如 npm 旁边那个给 Git Bash 用的 sh 脚本)
76
- if (win && ext === "" && !path.extname(base))
77
- continue;
78
- if (ok(base + ext))
79
- return base + ext;
80
- }
81
- return undefined;
82
- };
83
- if (bin.includes("/") || (win && bin.includes("\\")))
84
- return tryAll(path.resolve(bin));
85
- const dirs = (process.env.PATH ?? process.env.Path ?? "").split(path.delimiter).filter(Boolean);
86
- for (const dir of dirs) {
87
- const found = tryAll(path.join(dir.replace(/^"(.*)"$/, "$1"), bin));
88
- if (found)
89
- return found;
46
+ const win = process.platform === "win32";
47
+ const exts = win ? ["", ...(process.env.PATHEXT ?? ".COM;.EXE;.BAT;.CMD").split(";").filter(Boolean)] : [""];
48
+ const ok = (file) => {
49
+ try {
50
+ if (!statSync(file).isFile())
51
+ return false;
52
+ if (!win)
53
+ accessSync(file, constants.X_OK);
54
+ return true;
55
+ } catch {
56
+ return false;
57
+ }
58
+ };
59
+ const tryAll = (base) => {
60
+ for (const ext of exts) {
61
+ if (win && ext === "" && !path.extname(base))
62
+ continue;
63
+ if (ok(base + ext))
64
+ return base + ext;
90
65
  }
91
- return undefined;
66
+ return;
67
+ };
68
+ if (bin.includes("/") || win && bin.includes("\\"))
69
+ return tryAll(path.resolve(bin));
70
+ const dirs = (process.env.PATH ?? process.env.Path ?? "").split(path.delimiter).filter(Boolean);
71
+ for (const dir of dirs) {
72
+ const found = tryAll(path.join(dir.replace(/^"(.*)"$/, "$1"), bin));
73
+ if (found)
74
+ return found;
75
+ }
76
+ return;
92
77
  }
@@ -1,20 +1,13 @@
1
- // jade/skills:skill 工具、系统提示词里的 skill 列表、/skills(列出,以及 add / update / remove,见 install.ts;
2
- // 终端里是 jade skills …),以及每个 skill 一条 /<skill 名> 命令。
3
- //
4
- // 每次调用模型之前重新查找 skill:会话中途装的马上能用,不用 /clear 或重启。随仓库分发的 skill(项目层)不用确认:
5
- // 它只是给模型看的文字,不执行代码;和旧 jade 一样,列表里有项目层的 skill 时整段标为不可信。
6
1
  import path from "node:path";
7
2
  import { definePlugin } from "@uncarved/jade-api";
8
3
  import { addSkill, removeSkill, skillsRoot, updateSkills } from "./install.js";
9
- import { bold, clip, dim, display, estimateTokens, realPath, red, repoRoot, splitArgs } from "./project.js";
4
+ import { bold, clip, dim, display, error, estimateTokens, lines as joinLines, plain, realPath, repoRoot, splitArgs } from "./project.js";
10
5
  import { loadSkills, skillLine, skillsPrompt, skillTool } from "./skills.js";
11
6
  const ID = "jade/skills";
12
- /** /<skill> 发给模型的消息:让它先加载这个 skill */
13
7
  export function skillMessage(name, args) {
14
- return `Use the "${name}" skill${args.trim() ? ` for this: ${args.trim()}` : "."}`;
8
+ return `Use the "${name}" skill${args.trim() ? ` for this: ${args.trim()}` : "."}`;
15
9
  }
16
- const errorText = (e) => (e instanceof Error ? e.message : String(e));
17
- /** cmd 是 "/skills" 或 "jade skills" */
10
+ const errorText = (e) => e instanceof Error ? e.message : String(e);
18
11
  export const manageUsage = (cmd) => `usage: ${cmd} [list]
19
12
  ${cmd} add <source> [--project] [--path <dir>] [--force]
20
13
  ${cmd} update [name…] [--project] [--force]
@@ -25,215 +18,204 @@ export const manageUsage = (cmd) => `usage: ${cmd} [list]
25
18
  --project use <cwd>/.jade/skills instead of $JADE_HOME/skills
26
19
  --path the skill's directory inside the source (for repositories with several skills)
27
20
  --force replace a skill with the same name, or overwrite local changes on update`;
28
- /** list / add / update / remove,参数已经切好;返回给用户看的结果,用法错误或失败时抛错。cmd 用在提示里 */
29
21
  export async function manageSkills(argv, env) {
30
- const usage = manageUsage(env.cmd);
31
- const [sub, ...tail] = argv;
32
- if (sub === "-h" || sub === "--help" || tail.includes("-h") || tail.includes("--help"))
33
- return usage;
34
- if (sub === undefined || sub === "list") {
35
- if (tail.length)
36
- throw new Error(usage);
37
- const skills = env.skills ?? (await find(env.home, env.cwd)).skills;
38
- // 每次请求都带着的:skill 工具的定义(总是注册),和提示词里的 skill 列表。skill 的正文用到时才读入,不算
39
- const tool = skillTool(() => skills, async () => { }, { user: skillsRoot(env.home, env.cwd, false), project: skillsRoot(env.home, env.cwd, true) });
40
- const toolTokens = estimateTokens(JSON.stringify({ name: tool.name, description: tool.description, input_schema: tool.inputSchema }));
41
- if (skills.length === 0) {
42
- return dim(`(no skills) install one with ${env.cmd} add <source>, or put it in ${display(env.cwd, path.join(env.home, "skills"))}/<name>/SKILL.md or .jade/skills/<name>/SKILL.md\nthe skill tool itself takes ≈${toolTokens} tokens in every request`);
43
- }
44
- const total = toolTokens + estimateTokens(skillsPrompt(skills));
45
- return [
46
- ...skills.map((sk) => `${bold(sk.name)} ${dim(`${sk.layer} · ${display(env.cwd, sk.dir)} · ≈${estimateTokens(skillLine(sk))} tokens`)}\n ${clip(sk.description.replace(/\s+/g, " "), 300)}`),
47
- dim(`≈${total} tokens in every request (the skill list and the skill tool); a skill's instructions are read only when it is used`),
48
- ].join("\n");
22
+ const usage = manageUsage(env.cmd);
23
+ const [sub, ...tail] = argv;
24
+ if (sub === "-h" || sub === "--help" || tail.includes("-h") || tail.includes("--help"))
25
+ return usage;
26
+ if (sub === undefined || sub === "list") {
27
+ if (tail.length)
28
+ throw new Error(usage);
29
+ const skills = env.skills ?? (await find(env.home, env.cwd)).skills;
30
+ const tool = skillTool(() => skills, async () => {}, { user: skillsRoot(env.home, env.cwd, false), project: skillsRoot(env.home, env.cwd, true) });
31
+ const toolTokens = estimateTokens(JSON.stringify({ name: tool.name, description: tool.description, input_schema: tool.inputSchema }));
32
+ if (skills.length === 0) {
33
+ return dim(`(no skills) install one with ${env.cmd} add <source>, or put it in ${display(env.cwd, path.join(env.home, "skills"))}/<name>/SKILL.md or .jade/skills/<name>/SKILL.md
34
+ the skill tool itself takes ≈${toolTokens} tokens in every request`);
49
35
  }
50
- const positionals = [];
51
- let project = false;
52
- let force = false;
53
- let subdir;
54
- for (let i = 0; i < tail.length; i++) {
55
- const a = tail[i];
56
- if (a === "--project")
57
- project = true;
58
- else if (a === "--force" || a === "-f")
59
- force = true;
60
- else if (a === "--path") {
61
- subdir = tail[++i];
62
- if (subdir === undefined)
63
- throw new Error(`--path needs a value\n${usage}`);
64
- }
65
- else if (a.startsWith("--path="))
66
- subdir = a.slice("--path=".length);
67
- else if (a.startsWith("-"))
68
- throw new Error(`unknown option ${a}\n${usage}`);
69
- else
70
- positionals.push(a);
36
+ const total = toolTokens + estimateTokens(skillsPrompt(skills));
37
+ return joinLines([
38
+ ...skills.map((sk) => [bold(sk.name), " ", dim(`${sk.layer} · ${display(env.cwd, sk.dir)} · ≈${estimateTokens(skillLine(sk))} tokens`), `
39
+ ${clip(sk.description.replace(/\s+/g, " "), 300)}`]),
40
+ dim(`≈${total} tokens in every request (the skill list and the skill tool); a skill's instructions are read only when it is used`)
41
+ ]);
42
+ }
43
+ const positionals = [];
44
+ let project = false;
45
+ let force = false;
46
+ let subdir;
47
+ for (let i = 0;i < tail.length; i++) {
48
+ const a = tail[i];
49
+ if (a === "--project")
50
+ project = true;
51
+ else if (a === "--force" || a === "-f")
52
+ force = true;
53
+ else if (a === "--path") {
54
+ subdir = tail[++i];
55
+ if (subdir === undefined)
56
+ throw new Error(`--path needs a value
57
+ ${usage}`);
58
+ } else if (a.startsWith("--path="))
59
+ subdir = a.slice("--path=".length);
60
+ else if (a.startsWith("-"))
61
+ throw new Error(`unknown option ${a}
62
+ ${usage}`);
63
+ else
64
+ positionals.push(a);
65
+ }
66
+ const root = skillsRoot(env.home, env.cwd, project);
67
+ const later = "a running jade can use it from its next step";
68
+ switch (sub) {
69
+ case "add":
70
+ case "install": {
71
+ if (positionals.length !== 1)
72
+ throw new Error(usage);
73
+ const { name, dir } = await addSkill(positionals[0], { sources: env.sources, root, cwd: env.cwd, force, ...subdir ? { subdir } : {}, ...env.log ? { log: env.log } : {} });
74
+ return `installed skill ${name} into ${display(env.cwd, dir)}; ${later}`;
71
75
  }
72
- const root = skillsRoot(env.home, env.cwd, project);
73
- const later = "a running jade can use it from its next step";
74
- switch (sub) {
75
- case "add":
76
- case "install": {
77
- if (positionals.length !== 1)
78
- throw new Error(usage);
79
- const { name, dir } = await addSkill(positionals[0], { sources: env.sources, root, cwd: env.cwd, force, ...(subdir ? { subdir } : {}), ...(env.log ? { log: env.log } : {}) });
80
- return `installed skill ${name} into ${display(env.cwd, dir)}; ${later}`;
81
- }
82
- case "update": {
83
- if (subdir)
84
- throw new Error(usage);
85
- const lines = await updateSkills(positionals, { sources: env.sources, root, cwd: env.cwd, force, ...(env.log ? { log: env.log } : {}) });
86
- if (lines.length === 0)
87
- return `no skills in ${display(env.cwd, root)} were installed with "${env.cmd} add"`;
88
- if (lines.some((l) => l.startsWith("updated ")))
89
- lines.push("a running jade uses the updated skills from its next step");
90
- return lines.join("\n");
91
- }
92
- case "remove":
93
- case "rm": {
94
- if (positionals.length !== 1 || subdir || force)
95
- throw new Error(usage);
96
- const dir = await removeSkill(positionals[0], root);
97
- return `removed skill ${positionals[0]} (${display(env.cwd, dir)})`;
98
- }
99
- default:
100
- throw new Error(usage);
76
+ case "update": {
77
+ if (subdir)
78
+ throw new Error(usage);
79
+ const lines = await updateSkills(positionals, { sources: env.sources, root, cwd: env.cwd, force, ...env.log ? { log: env.log } : {} });
80
+ if (lines.length === 0)
81
+ return `no skills in ${display(env.cwd, root)} were installed with "${env.cmd} add"`;
82
+ if (lines.some((l) => l.startsWith("updated ")))
83
+ lines.push("a running jade uses the updated skills from its next step");
84
+ return lines.join(`
85
+ `);
86
+ }
87
+ case "remove":
88
+ case "rm": {
89
+ if (positionals.length !== 1 || subdir || force)
90
+ throw new Error(usage);
91
+ const dir = await removeSkill(positionals[0], root);
92
+ return `removed skill ${positionals[0]} (${display(env.cwd, dir)})`;
101
93
  }
94
+ default:
95
+ throw new Error(usage);
96
+ }
102
97
  }
103
98
  async function find(home, cwd) {
104
- const here = realPath(cwd);
105
- return loadSkills({ home, root: realPath(repoRoot(here)), cwd: here });
99
+ const here = realPath(cwd);
100
+ return loadSkills({ home, root: realPath(repoRoot(here)), cwd: here });
106
101
  }
107
- /** 最近一个会话找到的 skill,/skills 显示它 */
108
102
  let latest;
109
- /** 最近一个会话注册的 /<skill> 命令 → 撤销函数 */
110
- const skillCommands = new Map();
111
- /** 已经告诉过用户的警告:每个会话都会重新查找,同样的警告不重复说 */
112
- const warned = new Set();
103
+ const skillCommands = new Map;
104
+ const warned = new Set;
113
105
  export default definePlugin({
114
- process(ctx) {
115
- const sources = ctx.require("sources@1");
116
- latest = undefined;
117
- skillCommands.clear();
118
- ctx.service("commands@1")?.register({
119
- name: "skills",
120
- args: "[add <source> | update [name…] | remove <name>] [--project]",
121
- description: "skills the agent can use; install them from git, npm or a directory",
122
- instant: true,
123
- async run(args, cctx) {
124
- let argv;
125
- try {
126
- argv = splitArgs(args);
127
- }
128
- catch (e) {
129
- return red(errorText(e));
130
- }
131
- try {
132
- return await manageSkills(argv, { home: ctx.home, cwd: ctx.cwd, cmd: "/skills", sources, log: (t) => cctx.print(dim(t)), ...(latest ? { skills: latest } : {}) });
133
- }
134
- catch (e) {
135
- return red(errorText(e));
136
- }
137
- },
138
- });
139
- // 同样的事也能在终端里做:jade skills add <source> [--project](cli@1,核心提供)
140
- ctx.service("cli@1")?.register({
141
- name: "skills",
142
- run: (argv, cli) => manageSkills(argv, { home: ctx.home, cwd: ctx.cwd, cmd: "jade skills", sources, log: cli.log }),
143
- });
144
- },
145
- async session(ctx) {
146
- const ui = ctx.service("ui@1");
147
- const approval = ctx.service("approval@1");
148
- const prompts = ctx.service("prompts@1");
149
- const commands = ctx.service("commands@1");
150
- /** 这个会话现在的 skill,和据此加上的东西(都挂在会话作用域上,随会话撤销) */
151
- let current = [];
152
- let shown = "";
153
- let removeSection;
154
- const readable = new Map();
155
- /** 这个会话注册过的 /<skill> 命令 */
156
- const mine = new Set();
157
- /** 用新找到的 skill 更新提示词里的列表、可读目录和 /<skill> 命令;没变时什么也不做(不让提示缓存失效) */
158
- const apply = (skills, warnings) => {
159
- for (const w of warnings) {
160
- if (warned.has(w))
161
- continue;
162
- warned.add(w);
163
- ui?.notify(`${ID}: ${w}`, "warning");
164
- }
165
- current = skills;
166
- latest = skills;
167
- const key = JSON.stringify(skills.map((s) => [s.name, s.description, s.dir, s.layer]));
168
- if (key === shown)
169
- return;
170
- shown = key;
171
- // 有随仓库分发的 skill 时整段不可信
172
- removeSection?.();
173
- removeSection = skills.length ? prompts?.add({ text: skillsPrompt(skills), untrusted: !skills.every((s) => s.layer === "user") }) : undefined;
174
- // 读 skill 目录里的文件(脚本、参考资料)不用问
175
- const dirs = new Set(skills.map((s) => s.dir));
176
- for (const [dir, revoke] of readable)
177
- if (!dirs.has(dir))
178
- (revoke(), readable.delete(dir));
179
- for (const dir of dirs)
180
- if (!readable.has(dir) && approval)
181
- readable.set(dir, approval.allowRead(dir));
182
- // skill 也可以当命令用:/<skill 名> [要做的事]。
183
- // 新会话在旧会话关闭之前打开:旧会话注册的同名命令先撤销,换成这个会话的。
184
- // 名字已被别的插件的命令占用时让给它(会话级的注册总在进程级的命令之后)
185
- if (!commands)
186
- return;
187
- const names = new Set(skills.map((s) => s.name.toLowerCase()));
188
- for (const [key, revoke] of skillCommands) {
189
- if (names.has(key))
190
- continue;
191
- revoke();
192
- skillCommands.delete(key);
193
- mine.delete(key);
194
- }
195
- for (const sk of skills) {
196
- const key = sk.name.toLowerCase();
197
- if (mine.has(key))
198
- continue;
199
- const taken = commands.get(sk.name);
200
- if (taken && taken.plugin !== ID)
201
- continue;
202
- if (taken)
203
- skillCommands.get(key)?.();
204
- try {
205
- const revoke = commands.register({
206
- name: sk.name,
207
- args: "[task]",
208
- description: `${clip(sk.description.replace(/\s+/g, " "), 80)} (skill)`,
209
- run: (args) => ({ prompt: skillMessage(sk.name, args) }),
210
- });
211
- skillCommands.set(key, revoke);
212
- mine.add(key);
213
- }
214
- catch {
215
- // 名字不能当命令名(commands@1 的规则更严)或刚被占用:只是少一条快捷命令
216
- }
217
- }
218
- };
219
- const refresh = async () => {
220
- const { skills, warnings } = await find(ctx.home, ctx.cwd);
221
- apply(skills, warnings);
222
- };
106
+ process(ctx) {
107
+ const sources = ctx.require("sources@1");
108
+ latest = undefined;
109
+ skillCommands.clear();
110
+ ctx.service("commands@1")?.register({
111
+ name: "skills",
112
+ args: "[add <source> | update [name…] | remove <name>] [--project]",
113
+ description: "skills the agent can use; install them from git, npm or a directory",
114
+ instant: true,
115
+ async run(args, cctx) {
116
+ let argv;
117
+ try {
118
+ argv = splitArgs(args);
119
+ } catch (e) {
120
+ return error(errorText(e));
121
+ }
122
+ try {
123
+ return await manageSkills(argv, { home: ctx.home, cwd: ctx.cwd, cmd: "/skills", sources, log: (t) => cctx.print(dim(t)), ...latest ? { skills: latest } : {} });
124
+ } catch (e) {
125
+ return error(errorText(e));
126
+ }
127
+ }
128
+ });
129
+ ctx.service("cli@1")?.register({
130
+ name: "skills",
131
+ run: async (argv, cli) => plain(await manageSkills(argv, { home: ctx.home, cwd: ctx.cwd, cmd: "jade skills", sources, log: cli.log }))
132
+ });
133
+ },
134
+ async session(ctx) {
135
+ const ui = ctx.service("ui@1");
136
+ const approval = ctx.service("approval@1");
137
+ const prompts = ctx.service("prompts@1");
138
+ const commands = ctx.service("commands@1");
139
+ let current = [];
140
+ let shown = "";
141
+ let removeSection;
142
+ const readable = new Map;
143
+ const mine = new Set;
144
+ const child = ctx.session.parent !== undefined;
145
+ const applyPrompt = (skills) => {
146
+ const key = JSON.stringify(skills.map((s) => [s.name, s.description, s.dir, s.layer]));
147
+ if (key === shown)
148
+ return false;
149
+ shown = key;
150
+ removeSection?.();
151
+ removeSection = skills.length ? prompts?.add({ text: skillsPrompt(skills), untrusted: !skills.every((s) => s.layer === "user") }) : undefined;
152
+ const dirs = new Set(skills.map((s) => s.dir));
153
+ for (const [dir, revoke] of readable)
154
+ if (!dirs.has(dir))
155
+ revoke(), readable.delete(dir);
156
+ for (const dir of dirs)
157
+ if (!readable.has(dir) && approval)
158
+ readable.set(dir, approval.allowRead(dir));
159
+ return true;
160
+ };
161
+ const apply = (skills, warnings) => {
162
+ for (const w of warnings) {
163
+ if (warned.has(w))
164
+ continue;
165
+ warned.add(w);
166
+ ui?.notify(`${ID}: ${w}`, "warning");
167
+ }
168
+ current = skills;
169
+ if (child)
170
+ return applyPrompt(skills);
171
+ latest = skills;
172
+ if (!applyPrompt(skills))
173
+ return;
174
+ if (!commands)
175
+ return;
176
+ const names = new Set(skills.map((s) => s.name.toLowerCase()));
177
+ for (const [key, revoke] of skillCommands) {
178
+ if (names.has(key))
179
+ continue;
180
+ revoke();
181
+ skillCommands.delete(key);
182
+ mine.delete(key);
183
+ }
184
+ for (const sk of skills) {
185
+ const key = sk.name.toLowerCase();
186
+ if (mine.has(key))
187
+ continue;
188
+ const taken = commands.get(sk.name);
189
+ if (taken && taken.plugin !== ID)
190
+ continue;
191
+ if (taken)
192
+ skillCommands.get(key)?.();
193
+ try {
194
+ const revoke = commands.register({
195
+ name: sk.name,
196
+ args: "[task]",
197
+ description: `${clip(sk.description.replace(/\s+/g, " "), 80)} (skill)`,
198
+ run: (args) => ({ prompt: skillMessage(sk.name, args) })
199
+ });
200
+ skillCommands.set(key, revoke);
201
+ mine.add(key);
202
+ } catch {}
203
+ }
204
+ };
205
+ const refresh = async () => {
206
+ const { skills, warnings } = await find(ctx.home, ctx.cwd);
207
+ apply(skills, warnings);
208
+ };
209
+ await refresh();
210
+ ctx.kernel.tool(skillTool(() => current, refresh, { user: skillsRoot(ctx.home, ctx.cwd, false), project: skillsRoot(ctx.home, ctx.cwd, true) }));
211
+ ctx.kernel.hook({
212
+ kind: "transform",
213
+ point: "context.project",
214
+ optional: true,
215
+ fn: async (messages) => {
223
216
  await refresh();
224
- // 会话中途装的 skill(/skills add、jade skills add,或模型自己装的)马上能用:
225
- // 工具列表在会话创建时就定了,所以 skill 工具总是注册;每次调用模型之前重新查找一次,
226
- // 列表变了才更新提示词。context.project 在 context.build(jade/prompts 拼提示词)之前执行
227
- ctx.kernel.tool(skillTool(() => current, refresh, { user: skillsRoot(ctx.home, ctx.cwd, false), project: skillsRoot(ctx.home, ctx.cwd, true) }));
228
- ctx.kernel.hook({
229
- kind: "transform",
230
- point: "context.project",
231
- // 查找出错时沿用上一次的列表,不让这一轮失败
232
- optional: true,
233
- fn: async (messages) => {
234
- await refresh();
235
- return messages;
236
- },
237
- });
238
- },
217
+ return messages;
218
+ }
219
+ });
220
+ }
239
221
  });