@uncarved/jade 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (199) hide show
  1. package/dist/api.d.ts +1 -1
  2. package/dist/api.js +1 -1
  3. package/dist/host.d.ts +2 -0
  4. package/dist/host.js +39 -5
  5. package/dist/host.js.map +1 -1
  6. package/dist/remote.js +5 -0
  7. package/dist/remote.js.map +1 -1
  8. package/dist/session.d.ts +7 -0
  9. package/dist/session.js +482 -27
  10. package/dist/session.js.map +1 -1
  11. package/package.json +4 -4
  12. package/plugins/approval/index.ts +36 -14
  13. package/plugins/approval/policy.ts +15 -10
  14. package/plugins/approval/review.ts +9 -5
  15. package/plugins/checks/checks.ts +10 -7
  16. package/plugins/checks/index.ts +13 -13
  17. package/plugins/checks/plugin.json +1 -1
  18. package/plugins/checks/project.ts +37 -38
  19. package/plugins/commands/index.ts +27 -3
  20. package/plugins/compact/compact.ts +13 -5
  21. package/plugins/compact/index.ts +20 -3
  22. package/plugins/compact/style.ts +13 -14
  23. package/plugins/completions/index.ts +35 -0
  24. package/plugins/completions/plugin.json +7 -0
  25. package/plugins/copy/index.ts +46 -0
  26. package/plugins/copy/plugin.json +7 -0
  27. package/plugins/editor/index.ts +10 -4
  28. package/plugins/fs/diff.ts +7 -6
  29. package/plugins/fs/fs.ts +8 -4
  30. package/plugins/fs/grep.ts +42 -9
  31. package/plugins/fs/index.ts +2 -7
  32. package/plugins/fs/plugin.json +1 -1
  33. package/plugins/fs/renderers.ts +16 -15
  34. package/plugins/fs/style.ts +16 -15
  35. package/plugins/hooks/index.ts +3 -2
  36. package/plugins/keys/index.ts +5 -1
  37. package/plugins/mcp/index.ts +197 -78
  38. package/plugins/mcp/install.ts +5 -3
  39. package/plugins/mcp/mcp.ts +163 -11
  40. package/plugins/mcp/plugin.json +2 -1
  41. package/plugins/mcp/project.ts +47 -50
  42. package/plugins/memory/index.ts +4 -4
  43. package/plugins/memory/style.ts +13 -14
  44. package/plugins/mentions/candidates.ts +67 -0
  45. package/plugins/mentions/index.ts +12 -5
  46. package/plugins/mentions/plugin.json +1 -1
  47. package/plugins/mentions/style.ts +13 -14
  48. package/plugins/models/configure.ts +10 -9
  49. package/plugins/models/index.ts +27 -1
  50. package/plugins/modes/index.ts +13 -2
  51. package/plugins/print/index.ts +7 -4
  52. package/plugins/prompts/index.ts +0 -0
  53. package/plugins/renderers/index.ts +10 -5
  54. package/plugins/repeats/index.ts +8 -8
  55. package/plugins/schedule/index.ts +2 -0
  56. package/plugins/screenshot/browser.ts +10 -0
  57. package/plugins/sessions/index.ts +48 -2
  58. package/plugins/shell/index.ts +20 -25
  59. package/plugins/shell/plugin.json +1 -2
  60. package/plugins/shell/renderers.ts +9 -7
  61. package/plugins/shell/shell.ts +3 -1
  62. package/plugins/skills/index.ts +28 -19
  63. package/plugins/skills/project.ts +12 -7
  64. package/plugins/steer/index.ts +5 -4
  65. package/plugins/task/index.ts +80 -50
  66. package/plugins/task/plugin.json +6 -2
  67. package/plugins/task/style.ts +13 -14
  68. package/plugins/task/subagent.ts +67 -184
  69. package/plugins/todo/index.ts +39 -9
  70. package/plugins/tools/index.ts +9 -2
  71. package/plugins/tui/frontend.ts +139 -89
  72. package/plugins/tui/index.ts +2 -3
  73. package/plugins/tui/mentions.ts +11 -80
  74. package/plugins/tui/plugin.json +1 -1
  75. package/plugins/tui/screen.ts +55 -5
  76. package/plugins/tui/transcript.ts +17 -7
  77. package/plugins/tui/tui.ts +337 -101
  78. package/plugins/tui/vendor.ts +1 -1
  79. package/plugins/undo/checkpoint.ts +5 -3
  80. package/plugins/undo/index.ts +43 -14
  81. package/plugins/undo/style.ts +13 -14
  82. package/plugins/web/index.ts +7 -18
  83. package/plugins/web/plugin.json +1 -1
  84. package/plugins-dist/approval/index.js +363 -418
  85. package/plugins-dist/approval/policy.js +391 -508
  86. package/plugins-dist/approval/review.js +132 -158
  87. package/plugins-dist/approval/schema.js +50 -55
  88. package/plugins-dist/ask/index.js +115 -129
  89. package/plugins-dist/budget/index.js +27 -31
  90. package/plugins-dist/checks/checks.js +293 -329
  91. package/plugins-dist/checks/index.js +119 -139
  92. package/plugins-dist/checks/plugin.json +2 -1
  93. package/plugins-dist/checks/project.js +57 -65
  94. package/plugins-dist/commands/index.js +47 -29
  95. package/plugins-dist/compact/compact.js +233 -249
  96. package/plugins-dist/compact/index.js +98 -95
  97. package/plugins-dist/compact/style.js +9 -14
  98. package/plugins-dist/completions/index.js +30 -0
  99. package/plugins-dist/completions/plugin.json +9 -0
  100. package/plugins-dist/continue/index.js +40 -52
  101. package/plugins-dist/copy/index.js +40 -0
  102. package/plugins-dist/copy/plugin.json +9 -0
  103. package/plugins-dist/cost/index.js +98 -115
  104. package/plugins-dist/custom-commands/index.js +55 -72
  105. package/plugins-dist/editor/index.js +64 -72
  106. package/plugins-dist/fs/diff.js +111 -124
  107. package/plugins-dist/fs/fs.js +426 -469
  108. package/plugins-dist/fs/grep.js +262 -253
  109. package/plugins-dist/fs/index.js +20 -34
  110. package/plugins-dist/fs/plugin.json +1 -2
  111. package/plugins-dist/fs/renderers.js +100 -112
  112. package/plugins-dist/fs/style.js +11 -15
  113. package/plugins-dist/fs/sys.js +31 -43
  114. package/plugins-dist/hooks/index.js +107 -127
  115. package/plugins-dist/keys/index.js +27 -31
  116. package/plugins-dist/mcp/index.js +271 -199
  117. package/plugins-dist/mcp/install.js +123 -137
  118. package/plugins-dist/mcp/mcp.js +638 -577
  119. package/plugins-dist/mcp/plugin.json +3 -1
  120. package/plugins-dist/mcp/project.js +98 -115
  121. package/plugins-dist/memory/index.js +83 -88
  122. package/plugins-dist/memory/memory.js +111 -128
  123. package/plugins-dist/memory/style.js +9 -14
  124. package/plugins-dist/mentions/attach.js +91 -99
  125. package/plugins-dist/mentions/candidates.js +49 -0
  126. package/plugins-dist/mentions/index.js +71 -81
  127. package/plugins-dist/mentions/plugin.json +2 -1
  128. package/plugins-dist/mentions/style.js +9 -14
  129. package/plugins-dist/models/config.js +29 -43
  130. package/plugins-dist/models/configure.js +392 -473
  131. package/plugins-dist/models/context.js +14 -22
  132. package/plugins-dist/models/index.js +182 -189
  133. package/plugins-dist/models/provider.js +243 -307
  134. package/plugins-dist/models/saved.js +59 -71
  135. package/plugins-dist/modes/index.js +138 -165
  136. package/plugins-dist/plugins/index.js +34 -34
  137. package/plugins-dist/print/index.js +192 -175
  138. package/plugins-dist/prompts/index.js +81 -89
  139. package/plugins-dist/prompts/notes.js +31 -37
  140. package/plugins-dist/prompts/prompts.js +155 -193
  141. package/plugins-dist/prompts/results.js +40 -53
  142. package/plugins-dist/prompts/sys.js +39 -50
  143. package/plugins-dist/renderers/index.js +32 -32
  144. package/plugins-dist/repeats/index.js +131 -174
  145. package/plugins-dist/schedule/cron.js +49 -56
  146. package/plugins-dist/schedule/index.js +229 -247
  147. package/plugins-dist/schedule/store.js +93 -109
  148. package/plugins-dist/scratch/index.js +12 -18
  149. package/plugins-dist/screenshot/browser.js +136 -154
  150. package/plugins-dist/screenshot/index.js +37 -46
  151. package/plugins-dist/screenshot/sys.js +53 -65
  152. package/plugins-dist/screenshot/tool.js +98 -126
  153. package/plugins-dist/sessions/index.js +149 -117
  154. package/plugins-dist/shell/index.js +115 -135
  155. package/plugins-dist/shell/plugin.json +1 -2
  156. package/plugins-dist/shell/renderers.js +28 -33
  157. package/plugins-dist/shell/service.js +17 -23
  158. package/plugins-dist/shell/shell.js +307 -362
  159. package/plugins-dist/shell/sys.js +64 -79
  160. package/plugins-dist/skills/index.js +193 -211
  161. package/plugins-dist/skills/install.js +122 -149
  162. package/plugins-dist/skills/project.js +65 -73
  163. package/plugins-dist/skills/skills.js +135 -171
  164. package/plugins-dist/steer/index.js +108 -125
  165. package/plugins-dist/suggest/index.js +235 -259
  166. package/plugins-dist/task/index.js +130 -117
  167. package/plugins-dist/task/plugin.json +5 -2
  168. package/plugins-dist/task/style.js +9 -14
  169. package/plugins-dist/task/subagent.js +156 -279
  170. package/plugins-dist/todo/index.js +79 -63
  171. package/plugins-dist/tools/index.js +36 -42
  172. package/plugins-dist/tui/editor.js +145 -160
  173. package/plugins-dist/tui/frontend.js +639 -678
  174. package/plugins-dist/tui/history.js +34 -37
  175. package/plugins-dist/tui/index.js +16 -18
  176. package/plugins-dist/tui/markdown.js +203 -247
  177. package/plugins-dist/tui/mascot.js +266 -355
  178. package/plugins-dist/tui/mentions.js +67 -144
  179. package/plugins-dist/tui/plugin.json +1 -2
  180. package/plugins-dist/tui/render.js +35 -45
  181. package/plugins-dist/tui/screen.js +197 -180
  182. package/plugins-dist/tui/transcript.js +436 -468
  183. package/plugins-dist/tui/tui.js +1554 -1656
  184. package/plugins-dist/undo/checkpoint.js +95 -118
  185. package/plugins-dist/undo/index.js +185 -192
  186. package/plugins-dist/undo/snapshot.js +83 -99
  187. package/plugins-dist/undo/style.js +9 -14
  188. package/plugins-dist/update/index.js +26 -28
  189. package/plugins-dist/web/env.js +55 -68
  190. package/plugins-dist/web/index.js +80 -110
  191. package/plugins-dist/web/plugin.json +1 -2
  192. package/plugins-dist/web/renderers.js +4 -5
  193. package/plugins-dist/web/web.js +514 -593
  194. package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
  195. package/sdk/python/jade_plugin.py +9 -2
  196. package/src/api.ts +1 -1
  197. package/src/host.ts +39 -4
  198. package/src/remote.ts +6 -1
  199. package/src/session.ts +489 -34
@@ -3,11 +3,13 @@
3
3
  "version": "0.2.0",
4
4
  "api": "^1",
5
5
  "entry": "./index.js",
6
+ "requires": [
7
+ "trust@1"
8
+ ],
6
9
  "optional": [
7
10
  "ui@1",
8
11
  "commands@1",
9
12
  "prompts@1",
10
- "tools@1",
11
13
  "cli@1"
12
14
  ]
13
15
  }
@@ -1,131 +1,114 @@
1
- // 仓库目录、确认记录、显示用的小工具、斜杠命令参数的切分、token 估算。插件之间不共享代码(只能共享类型),jade/skills、jade/checks 各有一份。
2
1
  import { existsSync, realpathSync } from "node:fs";
3
- import { appendFile, mkdir, readFile } from "node:fs/promises";
2
+ import { readFile } from "node:fs/promises";
4
3
  import path from "node:path";
5
4
  export function realPath(p) {
6
- try {
7
- return realpathSync.native(p);
8
- }
9
- catch {
10
- return path.resolve(p);
11
- }
5
+ try {
6
+ return realpathSync.native(p);
7
+ } catch {
8
+ return path.resolve(p);
9
+ }
12
10
  }
13
- /** git 仓库的根目录(往上找 .git,目录或文件都算);不在仓库里时是 cwd 本身 */
14
11
  export function repoRoot(cwd) {
15
- let d = path.resolve(cwd);
16
- for (;;) {
17
- if (existsSync(path.join(d, ".git")))
18
- return d;
19
- const parent = path.dirname(d);
20
- if (parent === d)
21
- return path.resolve(cwd);
22
- d = parent;
23
- }
12
+ let d = path.resolve(cwd);
13
+ for (;; ) {
14
+ if (existsSync(path.join(d, ".git")))
15
+ return d;
16
+ const parent = path.dirname(d);
17
+ if (parent === d)
18
+ return path.resolve(cwd);
19
+ d = parent;
20
+ }
24
21
  }
25
- /** 从仓库根目录到工作目录的每一级目录,远的在前;工作目录不在仓库里时只有工作目录 */
26
22
  export function projectDirs(root, cwd) {
27
- const rel = path.relative(root, cwd);
28
- const inside = rel === "" || !(rel === ".." || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel));
29
- if (!inside)
30
- return [cwd];
31
- const dirs = [root];
32
- if (rel)
33
- for (const seg of rel.split(path.sep))
34
- dirs.push(path.join(dirs.at(-1), seg));
35
- return dirs;
23
+ const rel = path.relative(root, cwd);
24
+ const inside = rel === "" || !(rel === ".." || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel));
25
+ if (!inside)
26
+ return [cwd];
27
+ const dirs = [root];
28
+ if (rel)
29
+ for (const seg of rel.split(path.sep))
30
+ dirs.push(path.join(dirs.at(-1), seg));
31
+ return dirs;
36
32
  }
37
- /** 工作目录里的显示成相对路径,外面的显示绝对路径 */
38
33
  export function display(cwd, full) {
39
- const rel = path.relative(cwd, full);
40
- const shown = rel && rel !== ".." && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel) ? rel : full;
41
- return shown.split(path.sep).join("/");
42
- }
43
- /**
44
- * 用户确认过的东西(项目层的 MCP 服务),每行一条 { hash, path, at }。
45
- * 和旧 jade 的 $JADE_HOME/trust.jsonl 同一个文件、同一种格式,旧 jade 里确认过的不用再确认
46
- */
47
- export class TrustFile {
48
- file;
49
- #known = new Set();
50
- constructor(file) {
51
- this.file = file;
52
- }
53
- async has(hash) {
54
- if (this.#known.has(hash))
55
- return true;
56
- let text = "";
57
- try {
58
- text = await readFile(this.file, "utf8");
59
- }
60
- catch {
61
- return false;
62
- }
63
- for (const line of text.split("\n")) {
64
- try {
65
- if (JSON.parse(line).hash === hash) {
66
- this.#known.add(hash);
67
- return true;
68
- }
69
- }
70
- catch {
71
- // 写到一半的行
72
- }
73
- }
74
- return false;
75
- }
76
- async add(hash, where) {
77
- await mkdir(path.dirname(this.file), { recursive: true });
78
- await appendFile(this.file, `${JSON.stringify({ hash, path: where, at: new Date().toISOString() })}\n`);
79
- this.#known.add(hash);
80
- }
34
+ const rel = path.relative(cwd, full);
35
+ const shown = rel && rel !== ".." && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel) ? rel : full;
36
+ return shown.split(path.sep).join("/");
81
37
  }
82
- export const clip = (s, n) => (s.length > n ? s.slice(0, n) + "…" : s);
83
- // 设了 NO_COLOR(非空)或标准输出不是终端时不输出颜色,见 https://no-color.org
84
- const colors = Boolean(process.stdout.isTTY) && !process.env.NO_COLOR;
85
- const paint = (open, close) => (s) => colors ? `\x1b[${open}m${s.replaceAll(`\x1b[${close}m`, `\x1b[${close}m\x1b[${open}m`)}\x1b[${close}m` : s;
86
- export const dim = paint("2", "22");
87
- export const red = paint("31", "39");
88
- export const green = paint("32", "39");
89
- export const yellow = paint("33", "39");
90
- export const bold = paint("1", "22");
91
- /** 斜杠命令的参数按空白切开;"…" 或 '…' 里的空白不切(不处理反斜杠,Windows 路径照写) */
38
+ export const clip = (s, n) => s.length > n ? s.slice(0, n) + "…" : s;
39
+ const styled = (style) => (text) => ({ text, style });
40
+ export const dim = styled("dim");
41
+ export const bold = styled("bold");
42
+ export const ok = styled("ok");
43
+ export const warn = styled("warn");
44
+ export const error = styled("error");
45
+ export const lines = (rows) => rows.flatMap((r, i) => i ? [`
46
+ `, r] : [r]);
92
47
  export function splitArgs(text) {
93
- const out = [];
94
- let cur = "";
95
- let started = false;
96
- let quote;
97
- for (const ch of text) {
98
- if (quote) {
99
- if (ch === quote)
100
- quote = undefined;
101
- else
102
- cur += ch;
103
- }
104
- else if (ch === '"' || ch === "'") {
105
- quote = ch;
106
- started = true;
107
- }
108
- else if (/\s/.test(ch)) {
109
- if (started)
110
- out.push(cur);
111
- cur = "";
112
- started = false;
113
- }
114
- else {
115
- cur += ch;
116
- started = true;
117
- }
118
- }
119
- if (quote)
120
- throw new Error(`unclosed ${quote}`);
121
- 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)
122
63
  out.push(cur);
123
- 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;
124
76
  }
125
- /** 中日韩文字和全角符号:大约一个字一个 token(和 jade/compact 的估算一致) */
126
77
  const WIDE = /[ -鿿가-힯豈-﫿＀-￯]/g;
127
- /** 粗略估算一段文字发给模型时占多少 token:英文和代码约 4 个字符 1 个,中日韩文字每字约 1 个 */
128
78
  export function estimateTokens(text) {
129
- const wide = text.match(WIDE)?.length ?? 0;
130
- 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);
81
+ }
82
+ export function withLegacyTrust(trust, home) {
83
+ let legacy;
84
+ const read = async () => {
85
+ const hashes = new Map;
86
+ let text = "";
87
+ try {
88
+ text = await readFile(path.join(home, "trust.jsonl"), "utf8");
89
+ } catch {
90
+ return hashes;
91
+ }
92
+ for (const line of text.split(`
93
+ `)) {
94
+ try {
95
+ const r = JSON.parse(line);
96
+ if (typeof r.hash === "string")
97
+ hashes.set(r.hash, typeof r.path === "string" ? r.path : "");
98
+ } catch {}
99
+ }
100
+ return hashes;
101
+ };
102
+ return {
103
+ has: async (hash) => {
104
+ if (await trust.has(hash))
105
+ return true;
106
+ const where = (await (legacy ??= read())).get(hash);
107
+ if (where === undefined)
108
+ return false;
109
+ await trust.add(hash, where).catch(() => {});
110
+ return true;
111
+ },
112
+ add: (hash, where) => trust.add(hash, where)
113
+ };
131
114
  }
@@ -1,92 +1,87 @@
1
- // jade/memory:跨会话的记忆(见 memory.ts)。memory 工具给模型增删,/memory 给用户查看和管理,
2
- // 会话开始时经 prompts@1 把记忆放进系统提示词(会话中途的改动下一个会话才进提示词,这样提示缓存不失效;
3
- // 用户在 /memory 里改了,下一次请求时附一条说明告诉模型)。
4
1
  import { definePlugin } from "@uncarved/jade-api";
5
2
  import { Memory, MEMORY_SCOPES, memoryTool, projectRoot } from "./memory.js";
6
- import { clip, dim, green, red } from "./style.js";
7
- const args = (input) => (input ?? {});
3
+ import { clip, dim, error, ok as success } from "./style.js";
4
+ const args = (input) => input ?? {};
8
5
  export default definePlugin({
9
- process(ctx) {
10
- const memory = new Memory(ctx.home, projectRoot(ctx.cwd));
11
- ctx.service("renderers@1")?.tool({
12
- tool: "memory",
13
- summary: (input) => {
14
- const i = args(input);
15
- return `${i.action} ${i.scope}: ${clip(String(i.text ?? ""), 120)}`;
16
- },
17
- preview: (input) => {
18
- const i = args(input);
19
- return `${i.action === "remove" ? red("- ") : green("+ ")}${String(i.text ?? "")}`;
20
- },
21
- });
22
- ctx.service("commands@1")?.register({
23
- name: "memory",
24
- args: "[text]",
25
- description: "see and remove what jade remembers across sessions, or remember text",
26
- instant: true,
27
- async run(input, { ui, sessions, signal }) {
28
- const note = (text) => {
29
- const id = sessions.current?.id;
30
- // 用到的时候再取:可选服务在 process() 开头不一定已经有了
31
- if (id)
32
- ctx.service("prompts@1")?.note(id, text);
33
- };
34
- let text = input.trim();
35
- if (!text) {
36
- const entries = (await Promise.all(MEMORY_SCOPES.map(async (scope) => (await memory.list(scope)).map((t) => ({ scope, text: t }))))).flat();
37
- if (!ui.interactive)
38
- return entries.length ? entries.map((e) => `${e.scope}: ${e.text}`).join("\n") : "jade does not remember anything yet";
39
- const choice = await ui.select({
40
- title: entries.length ? "Memory: choose an entry to remove it, or add one" : "jade does not remember anything yet",
41
- detail: dim(`user (all projects): ${memory.files.user}\nproject: ${memory.files.project}`),
42
- options: [{ label: "Add a memory…", value: "add" }, ...entries.map((e, i) => ({ label: e.text, value: String(i), hint: e.scope }))],
43
- }, signal);
44
- if (!choice)
45
- return;
46
- if (choice !== "add") {
47
- const entry = entries[Number(choice)];
48
- const ok = await ui.select({
49
- title: `Remove this ${entry.scope} memory?`,
50
- detail: entry.text,
51
- options: [
52
- { label: "No", value: "no" },
53
- { label: "Yes, remove it", value: "yes" },
54
- ],
55
- }, signal);
56
- if (ok !== "yes")
57
- return;
58
- await memory.remove(entry.scope, entry.text);
59
- note(`The user removed this ${entry.scope} memory, so stop following it: ${entry.text}`);
60
- return green(`Removed from ${entry.scope} memory: ${entry.text}`);
61
- }
62
- text = (await ui.input({ title: "What should jade remember?", placeholder: "e.g. Reply in Chinese; run tests with bun test" }, signal)) ?? "";
63
- if (!text.trim())
64
- return;
65
- }
66
- const scope = ui.interactive
67
- ? await ui.select({
68
- title: "Remember it for",
69
- options: [
70
- { label: "All projects", value: "user", hint: "user memory" },
71
- { label: "This project only", value: "project", hint: "project memory" },
72
- ],
73
- }, signal)
74
- : "user";
75
- if (!scope)
76
- return;
77
- const added = await memory.add(scope, text);
78
- if (added)
79
- note(`The user saved a ${scope} memory; follow it from now on: ${text.replace(/\s+/g, " ").trim()}`);
80
- return added ? green(`Saved to ${scope} memory.`) : dim(`The ${scope} memory already has this.`);
81
- },
82
- });
83
- },
84
- async session(ctx) {
85
- const memory = new Memory(ctx.home, projectRoot(ctx.cwd));
86
- ctx.kernel.tool(memoryTool(memory));
87
- const text = await memory.prompt();
88
- // 段落随会话作用域撤销
89
- if (text)
90
- ctx.service("prompts@1")?.add({ text });
91
- },
6
+ process(ctx) {
7
+ const memory = new Memory(ctx.home, projectRoot(ctx.cwd));
8
+ ctx.service("renderers@1")?.tool({
9
+ tool: "memory",
10
+ summary: (input) => {
11
+ const i = args(input);
12
+ return `${i.action} ${i.scope}: ${clip(String(i.text ?? ""), 120)}`;
13
+ },
14
+ preview: (input) => {
15
+ const i = args(input);
16
+ return [i.action === "remove" ? error("- ") : success("+ "), String(i.text ?? "")];
17
+ }
18
+ });
19
+ ctx.service("commands@1")?.register({
20
+ name: "memory",
21
+ args: "[text]",
22
+ description: "see and remove what jade remembers across sessions, or remember text",
23
+ instant: true,
24
+ async run(input, { ui, sessions, signal }) {
25
+ const note = (text) => {
26
+ const id = sessions.current?.id;
27
+ if (id)
28
+ ctx.service("prompts@1")?.note(id, text);
29
+ };
30
+ let text = input.trim();
31
+ if (!text) {
32
+ const entries = (await Promise.all(MEMORY_SCOPES.map(async (scope) => (await memory.list(scope)).map((t) => ({ scope, text: t }))))).flat();
33
+ if (!ui.interactive)
34
+ return entries.length ? entries.map((e) => `${e.scope}: ${e.text}`).join(`
35
+ `) : "jade does not remember anything yet";
36
+ const choice = await ui.select({
37
+ title: entries.length ? "Memory: choose an entry to remove it, or add one" : "jade does not remember anything yet",
38
+ detail: dim(`user (all projects): ${memory.files.user}
39
+ project: ${memory.files.project}`),
40
+ options: [{ label: "Add a memory…", value: "add" }, ...entries.map((e, i) => ({ label: e.text, value: String(i), hint: e.scope }))]
41
+ }, signal);
42
+ if (!choice)
43
+ return;
44
+ if (choice !== "add") {
45
+ const entry = entries[Number(choice)];
46
+ const ok = await ui.select({
47
+ title: `Remove this ${entry.scope} memory?`,
48
+ detail: entry.text,
49
+ options: [
50
+ { label: "No", value: "no" },
51
+ { label: "Yes, remove it", value: "yes" }
52
+ ]
53
+ }, signal);
54
+ if (ok !== "yes")
55
+ return;
56
+ await memory.remove(entry.scope, entry.text);
57
+ note(`The user removed this ${entry.scope} memory, so stop following it: ${entry.text}`);
58
+ return success(`Removed from ${entry.scope} memory: ${entry.text}`);
59
+ }
60
+ text = await ui.input({ title: "What should jade remember?", placeholder: "e.g. Reply in Chinese; run tests with bun test" }, signal) ?? "";
61
+ if (!text.trim())
62
+ return;
63
+ }
64
+ const scope = ui.interactive ? await ui.select({
65
+ title: "Remember it for",
66
+ options: [
67
+ { label: "All projects", value: "user", hint: "user memory" },
68
+ { label: "This project only", value: "project", hint: "project memory" }
69
+ ]
70
+ }, signal) : "user";
71
+ if (!scope)
72
+ return;
73
+ const added = await memory.add(scope, text);
74
+ if (added)
75
+ note(`The user saved a ${scope} memory; follow it from now on: ${text.replace(/\s+/g, " ").trim()}`);
76
+ return added ? success(`Saved to ${scope} memory.`) : dim(`The ${scope} memory already has this.`);
77
+ }
78
+ });
79
+ },
80
+ async session(ctx) {
81
+ const memory = new Memory(ctx.home, projectRoot(ctx.cwd));
82
+ ctx.kernel.tool(memoryTool(memory));
83
+ const text = await memory.prompt();
84
+ if (text)
85
+ ctx.service("prompts@1")?.add({ text });
86
+ }
92
87
  });