@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,178 +1,151 @@
1
- // /skills add|update|remove:从 git 仓库、npm 包或本地目录安装 skill。
2
- //
3
- // 不加 --project 装到 $JADE_HOME/skills/<名字>,加了装到 <工作目录>/.jade/skills/<名字>。
4
- // 来源的写法和下载经核心的 sources@1 服务(和 jade plugins add 同一份):先下到 skills 目录里的 .staging-*
5
- // (以 . 开头,查找 skill 时跳过),找到 SKILL.md 后再换进正式目录。
6
- // 安装记录在 skills 目录的 .installed.json(按名字),update 按它重新下载;记录里有安装时的内容哈希,
7
- // 装好后手动改过的默认不覆盖,要加 --force。skill 只是文字和附带的文件:安装不执行任何东西,也不装依赖。
8
1
  import { randomBytes } from "node:crypto";
9
2
  import { mkdir, readdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
10
3
  import path from "node:path";
11
4
  import { NAME, parseSkill } from "./skills.js";
12
- /** 不加 --project 装到 $JADE_HOME/skills,加了装到 <工作目录>/.jade/skills */
13
- export const skillsRoot = (home, cwd, project) => (project ? path.join(cwd, ".jade", "skills") : path.join(home, "skills"));
5
+ export const skillsRoot = (home, cwd, project) => project ? path.join(cwd, ".jade", "skills") : path.join(home, "skills");
14
6
  const RECORDS = ".installed.json";
15
7
  export async function readRecords(root) {
16
- let text;
17
- try {
18
- text = await readFile(path.join(root, RECORDS), "utf8");
19
- }
20
- catch {
21
- return {};
22
- }
23
- try {
24
- return JSON.parse(text);
25
- }
26
- catch (e) {
27
- throw new Error(`${path.join(root, RECORDS)}: ${e instanceof Error ? e.message : String(e)}`);
28
- }
8
+ let text;
9
+ try {
10
+ text = await readFile(path.join(root, RECORDS), "utf8");
11
+ } catch {
12
+ return {};
13
+ }
14
+ try {
15
+ return JSON.parse(text);
16
+ } catch (e) {
17
+ throw new Error(`${path.join(root, RECORDS)}: ${e instanceof Error ? e.message : String(e)}`);
18
+ }
29
19
  }
30
20
  async function writeRecords(root, records) {
31
- await writeFile(path.join(root, RECORDS), JSON.stringify(records, null, 2) + "\n");
21
+ await writeFile(path.join(root, RECORDS), JSON.stringify(records, null, 2) + `
22
+ `);
32
23
  }
33
24
  const exists = (p) => stat(p).then(() => true, () => false);
34
- /** 把 --path 限制在来源目录里,不能跑到外面去 */
35
25
  function inSubdir(root, subdir) {
36
- const full = path.resolve(root, subdir);
37
- const rel = path.relative(root, full);
38
- if (rel === ".." || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel))
39
- throw new Error(`--path ${subdir} points outside the source`);
40
- return full;
26
+ const full = path.resolve(root, subdir);
27
+ const rel = path.relative(root, full);
28
+ if (rel === ".." || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel))
29
+ throw new Error(`--path ${subdir} points outside the source`);
30
+ return full;
41
31
  }
42
- /** 服务里抛出的错误带着 "plugin jade (sources@1.fetch): " 的前缀;给用户看的只要原来那句 */
43
32
  async function plain(call) {
44
- try {
45
- return await call;
46
- }
47
- catch (e) {
48
- throw e instanceof Error && e.name === "PluginError" && e.cause instanceof Error ? e.cause : e;
49
- }
33
+ try {
34
+ return await call;
35
+ } catch (e) {
36
+ throw e instanceof Error && e.name === "PluginError" && e.cause instanceof Error ? e.cause : e;
37
+ }
50
38
  }
51
- /** 安装一个 skill,返回它的名字和目录 */
52
39
  export async function addSkill(spec, opts) {
53
- const source = await plain(opts.sources.parse(spec, opts.cwd));
54
- const record = await install(source, opts);
55
- return { name: record.name, dir: path.join(path.resolve(opts.root), record.name) };
40
+ const source = await plain(opts.sources.parse(spec, opts.cwd));
41
+ const record = await install(source, opts);
42
+ return { name: record.name, dir: path.join(path.resolve(opts.root), record.name) };
56
43
  }
57
- /** 按安装记录重新下载;没给名字时更新这个目录里所有装过的 skill。返回每个 skill 一行结果 */
58
44
  export async function updateSkills(names, opts) {
59
- const root = path.resolve(opts.root);
60
- const records = await readRecords(root);
61
- const lines = [];
62
- for (const name of names.length ? names : Object.keys(records).sort()) {
63
- const rec = records[name];
64
- if (!rec) {
65
- lines.push(`${name}: not installed with "/skills add"${(await exists(path.join(root, name))) ? "; update it by hand" : ""}`);
66
- continue;
67
- }
68
- try {
69
- const dir = path.join(root, name);
70
- if (!opts.force && (await exists(dir)) && (await opts.sources.hash(dir)) !== rec.hash) {
71
- lines.push(`${name}: changed since it was installed; add --force to overwrite the changes`);
72
- continue;
73
- }
74
- const source = { kind: rec.kind, location: rec.location, spec: rec.source, ...(rec.ref ? { ref: rec.ref } : {}) };
75
- const next = await install(source, { ...opts, force: true, ...(rec.subdir ? { subdir: rec.subdir } : {}) }, rec);
76
- lines.push(next.hash === rec.hash ? `current ${name}` : `updated ${name}`);
77
- }
78
- catch (e) {
79
- lines.push(`${name}: ${e instanceof Error ? e.message : String(e)}`);
80
- }
45
+ const root = path.resolve(opts.root);
46
+ const records = await readRecords(root);
47
+ const lines = [];
48
+ for (const name of names.length ? names : Object.keys(records).sort()) {
49
+ const rec = records[name];
50
+ if (!rec) {
51
+ lines.push(`${name}: not installed with "/skills add"${await exists(path.join(root, name)) ? "; update it by hand" : ""}`);
52
+ continue;
81
53
  }
82
- return lines;
54
+ try {
55
+ const dir = path.join(root, name);
56
+ if (!opts.force && await exists(dir) && await opts.sources.hash(dir) !== rec.hash) {
57
+ lines.push(`${name}: changed since it was installed; add --force to overwrite the changes`);
58
+ continue;
59
+ }
60
+ const source = { kind: rec.kind, location: rec.location, spec: rec.source, ...rec.ref ? { ref: rec.ref } : {} };
61
+ const next = await install(source, { ...opts, force: true, ...rec.subdir ? { subdir: rec.subdir } : {} }, rec);
62
+ lines.push(next.hash === rec.hash ? `current ${name}` : `updated ${name}`);
63
+ } catch (e) {
64
+ lines.push(`${name}: ${e instanceof Error ? e.message : String(e)}`);
65
+ }
66
+ }
67
+ return lines;
83
68
  }
84
- /** 删掉一个 skill 目录和它的安装记录;手动放进去的也能删 */
85
69
  export async function removeSkill(name, root) {
86
- if (!NAME.test(name))
87
- throw new Error(`invalid skill name ${JSON.stringify(name)}`);
88
- root = path.resolve(root);
89
- const dir = path.join(root, name);
90
- if (!(await exists(path.join(dir, "SKILL.md"))))
91
- throw new Error(`there is no skill ${name} in ${root}`);
92
- await rm(dir, { recursive: true, force: true });
93
- const records = await readRecords(root);
94
- if (records[name]) {
95
- delete records[name];
96
- await writeRecords(root, records);
97
- }
98
- return dir;
70
+ if (!NAME.test(name))
71
+ throw new Error(`invalid skill name ${JSON.stringify(name)}`);
72
+ root = path.resolve(root);
73
+ const dir = path.join(root, name);
74
+ if (!await exists(path.join(dir, "SKILL.md")))
75
+ throw new Error(`there is no skill ${name} in ${root}`);
76
+ await rm(dir, { recursive: true, force: true });
77
+ const records = await readRecords(root);
78
+ if (records[name]) {
79
+ delete records[name];
80
+ await writeRecords(root, records);
81
+ }
82
+ return dir;
99
83
  }
100
- /** 下载、找到 SKILL.md、换进正式目录、写安装记录。更新时 prev 给出原来的记录(名字不变) */
101
84
  async function install(source, opts, prev) {
102
- const root = path.resolve(opts.root);
103
- // 本地来源包含 skills 目录时,staging 会被复制进去(甚至递归复制自己)
104
- if (source.kind === "local") {
105
- const from = opts.subdir ? inSubdir(source.location, opts.subdir) : source.location;
106
- const rel = path.relative(from, root);
107
- if (rel === "" || (rel !== ".." && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel))) {
108
- throw new Error(`${source.location} contains the skills directory; give the skill's own directory, or use --path`);
109
- }
110
- }
111
- await mkdir(root, { recursive: true });
112
- const records = await readRecords(root);
113
- const staging = path.join(root, `.staging-${randomBytes(4).toString("hex")}`);
114
- try {
115
- const { root: fetched, commit } = await plain(opts.sources.fetch(source, staging, opts.log));
116
- const dir = opts.subdir ? inSubdir(fetched, opts.subdir) : fetched;
117
- if (!(await exists(path.join(dir, "SKILL.md"))))
118
- throw new Error(await noSkillMessage(source.spec, dir, fetched));
119
- const name = prev?.name ?? skillName(parseSkill(await readFile(path.join(dir, "SKILL.md"), "utf8")).meta.name, source, opts.subdir);
120
- const target = path.join(root, name);
121
- if (!opts.force && (await exists(target))) {
122
- throw new Error(`skill ${name} is already installed in ${target}; run "/skills update ${name}", or add --force to replace it`);
123
- }
124
- const hash = await opts.sources.hash(dir);
125
- await rm(target, { recursive: true, force: true });
126
- await rename(dir, target);
127
- const now = new Date().toISOString();
128
- const record = {
129
- name,
130
- source: source.spec,
131
- location: source.location,
132
- kind: source.kind,
133
- ...(source.ref ? { ref: source.ref } : {}),
134
- ...(opts.subdir ? { subdir: opts.subdir } : {}),
135
- ...(commit ? { commit } : {}),
136
- hash,
137
- installedAt: prev?.installedAt ?? now,
138
- ...(prev ? { updatedAt: now } : {}),
139
- };
140
- records[name] = record;
141
- await writeRecords(root, records);
142
- return record;
85
+ const root = path.resolve(opts.root);
86
+ if (source.kind === "local") {
87
+ const from = opts.subdir ? inSubdir(source.location, opts.subdir) : source.location;
88
+ const rel = path.relative(from, root);
89
+ if (rel === "" || rel !== ".." && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel)) {
90
+ throw new Error(`${source.location} contains the skills directory; give the skill's own directory, or use --path`);
143
91
  }
144
- finally {
145
- await rm(staging, { recursive: true, force: true }).catch(() => { });
92
+ }
93
+ await mkdir(root, { recursive: true });
94
+ const records = await readRecords(root);
95
+ const staging = path.join(root, `.staging-${randomBytes(4).toString("hex")}`);
96
+ try {
97
+ const { root: fetched, commit } = await plain(opts.sources.fetch(source, staging, opts.log));
98
+ const dir = opts.subdir ? inSubdir(fetched, opts.subdir) : fetched;
99
+ if (!await exists(path.join(dir, "SKILL.md")))
100
+ throw new Error(await noSkillMessage(source.spec, dir, fetched));
101
+ const name = prev?.name ?? skillName(parseSkill(await readFile(path.join(dir, "SKILL.md"), "utf8")).meta.name, source, opts.subdir);
102
+ const target = path.join(root, name);
103
+ if (!opts.force && await exists(target)) {
104
+ throw new Error(`skill ${name} is already installed in ${target}; run "/skills update ${name}", or add --force to replace it`);
146
105
  }
106
+ const hash = await opts.sources.hash(dir);
107
+ await rm(target, { recursive: true, force: true });
108
+ await rename(dir, target);
109
+ const now = new Date().toISOString();
110
+ const record = {
111
+ name,
112
+ source: source.spec,
113
+ location: source.location,
114
+ kind: source.kind,
115
+ ...source.ref ? { ref: source.ref } : {},
116
+ ...opts.subdir ? { subdir: opts.subdir } : {},
117
+ ...commit ? { commit } : {},
118
+ hash,
119
+ installedAt: prev?.installedAt ?? now,
120
+ ...prev ? { updatedAt: now } : {}
121
+ };
122
+ records[name] = record;
123
+ await writeRecords(root, records);
124
+ return record;
125
+ } finally {
126
+ await rm(staging, { recursive: true, force: true }).catch(() => {});
127
+ }
147
128
  }
148
- /** SKILL.md 里的 name;没有时用 --path 的最后一段,再没有就用仓库名、包名或目录名 */
149
129
  function skillName(declared, source, subdir) {
150
- const fromSource = source.kind === "npm"
151
- ? source.location.replace(/^@[^/]+\//, "").replace(/@.*$/, "").replace(/\.(tgz|tar\.gz)$/i, "")
152
- : source.location.replace(/[\\/]+$/, "").replace(/\.git$/, "");
153
- const candidates = [declared, subdir && path.basename(subdir.replace(/[\\/]+$/, "")), path.basename(fromSource)];
154
- const name = candidates.find((c) => c && NAME.test(c));
155
- if (!name)
156
- throw new Error(`could not tell the skill's name; give it a "name:" in SKILL.md`);
157
- return name;
130
+ const fromSource = source.kind === "npm" ? source.location.replace(/^@[^/]+\//, "").replace(/@.*$/, "").replace(/\.(tgz|tar\.gz)$/i, "") : source.location.replace(/[\\/]+$/, "").replace(/\.git$/, "");
131
+ const candidates = [declared, subdir && path.basename(subdir.replace(/[\\/]+$/, "")), path.basename(fromSource)];
132
+ const name = candidates.find((c) => c && NAME.test(c));
133
+ if (!name)
134
+ throw new Error(`could not tell the skill's name; give it a "name:" in SKILL.md`);
135
+ return name;
158
136
  }
159
- /** 来源里没有 SKILL.md 时:列出它里面的 skill,提示用 --path 选一个 */
160
137
  async function noSkillMessage(spec, dir, fetched) {
161
- let found = [];
162
- try {
163
- found = (await readdir(fetched, { recursive: true, withFileTypes: true }))
164
- .filter((e) => e.isFile() && e.name === "SKILL.md")
165
- .map((e) => path.relative(fetched, e.parentPath).split(path.sep).join("/"))
166
- .filter((d) => d && !d.split("/").some((seg) => seg === "node_modules" || seg.startsWith(".")))
167
- .sort();
168
- }
169
- catch {
170
- // 没有可列的
171
- }
172
- const where = path.relative(fetched, dir).split(path.sep).join("/");
173
- const head = `${spec}${where ? ` (--path ${where})` : ""} has no SKILL.md`;
174
- if (found.length === 0)
175
- return `${head}; it is not a skill`;
176
- const shown = found.slice(0, 20).map((d) => ` ${d}`).join("\n") + (found.length > 20 ? `\n … ${found.length - 20} more` : "");
177
- return `${head}. It contains these skills; pick one with --path <dir>:\n${shown}`;
138
+ let found = [];
139
+ try {
140
+ found = (await readdir(fetched, { recursive: true, withFileTypes: true })).filter((e) => e.isFile() && e.name === "SKILL.md").map((e) => path.relative(fetched, e.parentPath).split(path.sep).join("/")).filter((d) => d && !d.split("/").some((seg) => seg === "node_modules" || seg.startsWith("."))).sort();
141
+ } catch {}
142
+ const where = path.relative(fetched, dir).split(path.sep).join("/");
143
+ const head = `${spec}${where ? ` (--path ${where})` : ""} has no SKILL.md`;
144
+ if (found.length === 0)
145
+ return `${head}; it is not a skill`;
146
+ const shown = found.slice(0, 20).map((d) => ` ${d}`).join(`
147
+ `) + (found.length > 20 ? `
148
+ … ${found.length - 20} more` : "");
149
+ return `${head}. It contains these skills; pick one with --path <dir>:
150
+ ${shown}`;
178
151
  }
@@ -1,89 +1,81 @@
1
- // 仓库目录、显示用的小工具、斜杠命令参数的切分、token 估算。插件之间不共享代码(只能共享类型),jade/mcp、jade/checks 各有一份。
2
1
  import { existsSync, realpathSync } from "node:fs";
3
2
  import path from "node:path";
4
3
  export function realPath(p) {
5
- try {
6
- return realpathSync.native(p);
7
- }
8
- catch {
9
- return path.resolve(p);
10
- }
4
+ try {
5
+ return realpathSync.native(p);
6
+ } catch {
7
+ return path.resolve(p);
8
+ }
11
9
  }
12
- /** git 仓库的根目录(往上找 .git,目录或文件都算);不在仓库里时是 cwd 本身 */
13
10
  export function repoRoot(cwd) {
14
- let d = path.resolve(cwd);
15
- for (;;) {
16
- if (existsSync(path.join(d, ".git")))
17
- return d;
18
- const parent = path.dirname(d);
19
- if (parent === d)
20
- return path.resolve(cwd);
21
- d = parent;
22
- }
11
+ let d = path.resolve(cwd);
12
+ for (;; ) {
13
+ if (existsSync(path.join(d, ".git")))
14
+ return d;
15
+ const parent = path.dirname(d);
16
+ if (parent === d)
17
+ return path.resolve(cwd);
18
+ d = parent;
19
+ }
23
20
  }
24
- /** 从仓库根目录到工作目录的每一级目录,远的在前;工作目录不在仓库里时只有工作目录 */
25
21
  export function projectDirs(root, cwd) {
26
- const rel = path.relative(root, cwd);
27
- const inside = rel === "" || !(rel === ".." || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel));
28
- if (!inside)
29
- return [cwd];
30
- const dirs = [root];
31
- if (rel)
32
- for (const seg of rel.split(path.sep))
33
- dirs.push(path.join(dirs.at(-1), seg));
34
- return dirs;
22
+ const rel = path.relative(root, cwd);
23
+ const inside = rel === "" || !(rel === ".." || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel));
24
+ if (!inside)
25
+ return [cwd];
26
+ const dirs = [root];
27
+ if (rel)
28
+ for (const seg of rel.split(path.sep))
29
+ dirs.push(path.join(dirs.at(-1), seg));
30
+ return dirs;
35
31
  }
36
- /** 工作目录里的显示成相对路径,外面的显示绝对路径 */
37
32
  export function display(cwd, full) {
38
- const rel = path.relative(cwd, full);
39
- const shown = rel && rel !== ".." && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel) ? rel : full;
40
- return shown.split(path.sep).join("/");
33
+ const rel = path.relative(cwd, full);
34
+ const shown = rel && rel !== ".." && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel) ? rel : full;
35
+ return shown.split(path.sep).join("/");
41
36
  }
42
- export const clip = (s, n) => (s.length > n ? s.slice(0, n) + "…" : s);
43
- // 设了 NO_COLOR(非空)或标准输出不是终端时不输出颜色,见 https://no-color.org
44
- const colors = Boolean(process.stdout.isTTY) && !process.env.NO_COLOR;
45
- const paint = (open, close) => (s) => colors ? `\x1b[${open}m${s.replaceAll(`\x1b[${close}m`, `\x1b[${close}m\x1b[${open}m`)}\x1b[${close}m` : s;
46
- export const dim = paint("2", "22");
47
- export const red = paint("31", "39");
48
- export const bold = paint("1", "22");
49
- /** 斜杠命令的参数按空白切开;"…" 或 '…' 里的空白不切(不处理反斜杠,Windows 路径照写) */
37
+ export const clip = (s, n) => s.length > n ? s.slice(0, n) + "…" : s;
38
+ const styled = (style) => (text) => ({ text, style });
39
+ export const dim = styled("dim");
40
+ export const bold = styled("bold");
41
+ export const ok = styled("ok");
42
+ export const warn = styled("warn");
43
+ export const error = styled("error");
44
+ export const lines = (rows) => rows.flatMap((r, i) => i ? [`
45
+ `, r] : [r]);
46
+ export const plain = (t) => typeof t === "string" ? t : Array.isArray(t) ? t.map(plain).join("") : t.text;
50
47
  export function splitArgs(text) {
51
- const out = [];
52
- let cur = "";
53
- let started = false;
54
- let quote;
55
- for (const ch of text) {
56
- if (quote) {
57
- if (ch === quote)
58
- quote = undefined;
59
- else
60
- cur += ch;
61
- }
62
- else if (ch === '"' || ch === "'") {
63
- quote = ch;
64
- started = true;
65
- }
66
- else if (/\s/.test(ch)) {
67
- if (started)
68
- out.push(cur);
69
- cur = "";
70
- started = false;
71
- }
72
- else {
73
- cur += ch;
74
- started = true;
75
- }
76
- }
77
- if (quote)
78
- throw new Error(`unclosed ${quote}`);
79
- if (started)
48
+ const out = [];
49
+ let cur = "";
50
+ let started = false;
51
+ let quote;
52
+ for (const ch of text) {
53
+ if (quote) {
54
+ if (ch === quote)
55
+ quote = undefined;
56
+ else
57
+ cur += ch;
58
+ } else if (ch === '"' || ch === "'") {
59
+ quote = ch;
60
+ started = true;
61
+ } else if (/\s/.test(ch)) {
62
+ if (started)
80
63
  out.push(cur);
81
- return out;
64
+ cur = "";
65
+ started = false;
66
+ } else {
67
+ cur += ch;
68
+ started = true;
69
+ }
70
+ }
71
+ if (quote)
72
+ throw new Error(`unclosed ${quote}`);
73
+ if (started)
74
+ out.push(cur);
75
+ return out;
82
76
  }
83
- /** 中日韩文字和全角符号:大约一个字一个 token(和 jade/compact 的估算一致) */
84
77
  const WIDE = /[ -鿿가-힯豈-﫿＀-￯]/g;
85
- /** 粗略估算一段文字发给模型时占多少 token:英文和代码约 4 个字符 1 个,中日韩文字每字约 1 个 */
86
78
  export function estimateTokens(text) {
87
- const wide = text.match(WIDE)?.length ?? 0;
88
- return Math.ceil((text.length - wide) / 4 + wide);
79
+ const wide = text.match(WIDE)?.length ?? 0;
80
+ return Math.ceil((text.length - wide) / 4 + wide);
89
81
  }