@uncarved/jade 0.2.1 → 0.3.1

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 (227) hide show
  1. package/README.md +65 -2
  2. package/assets/logo.svg +1 -0
  3. package/bin/jade.js +4 -0
  4. package/dist/api.d.ts +5 -250
  5. package/dist/api.js +5 -7
  6. package/dist/api.js.map +1 -1
  7. package/dist/fetch.d.ts +8 -0
  8. package/dist/fetch.js +19 -17
  9. package/dist/fetch.js.map +1 -1
  10. package/dist/host.d.ts +12 -1
  11. package/dist/host.js +49 -1
  12. package/dist/host.js.map +1 -1
  13. package/dist/index.d.ts +1 -1
  14. package/dist/index.js +1 -1
  15. package/dist/index.js.map +1 -1
  16. package/dist/main.js +26 -4
  17. package/dist/main.js.map +1 -1
  18. package/dist/remote.js +4 -0
  19. package/dist/remote.js.map +1 -1
  20. package/dist/session.d.ts +2 -0
  21. package/dist/session.js +133 -13
  22. package/dist/session.js.map +1 -1
  23. package/dist/source.d.ts +2 -10
  24. package/dist/source.js.map +1 -1
  25. package/dist/update.d.ts +8 -20
  26. package/dist/update.js +12 -1
  27. package/dist/update.js.map +1 -1
  28. package/package.json +4 -2
  29. package/plugins/approval/index.ts +236 -85
  30. package/plugins/approval/plugin.json +1 -1
  31. package/plugins/approval/policy.ts +36 -17
  32. package/plugins/approval/review.ts +1 -1
  33. package/plugins/ask/index.ts +2 -2
  34. package/plugins/budget/index.ts +1 -1
  35. package/plugins/checks/checks.ts +3 -2
  36. package/plugins/checks/index.ts +2 -5
  37. package/plugins/commands/index.ts +2 -44
  38. package/plugins/compact/compact.ts +8 -3
  39. package/plugins/compact/index.ts +13 -16
  40. package/plugins/continue/index.ts +1 -1
  41. package/plugins/cost/index.ts +143 -0
  42. package/plugins/cost/plugin.json +13 -0
  43. package/plugins/custom-commands/index.ts +85 -0
  44. package/plugins/custom-commands/plugin.json +8 -0
  45. package/plugins/editor/index.ts +88 -0
  46. package/plugins/editor/plugin.json +17 -0
  47. package/plugins/fs/fs.ts +33 -6
  48. package/plugins/fs/grep.ts +19 -7
  49. package/plugins/fs/index.ts +6 -7
  50. package/plugins/fs/plugin.json +1 -1
  51. package/plugins/fs/renderers.ts +1 -1
  52. package/plugins/hooks/index.ts +152 -0
  53. package/plugins/hooks/plugin.json +14 -0
  54. package/plugins/keys/index.ts +2 -18
  55. package/plugins/mcp/index.ts +43 -15
  56. package/plugins/mcp/install.ts +139 -0
  57. package/plugins/mcp/mcp.ts +2 -1
  58. package/plugins/mcp/plugin.json +1 -1
  59. package/plugins/mcp/project.ts +37 -1
  60. package/plugins/memory/index.ts +4 -9
  61. package/plugins/memory/plugin.json +1 -1
  62. package/plugins/mentions/index.ts +2 -2
  63. package/plugins/models/configure.ts +258 -150
  64. package/plugins/models/context.ts +2 -2
  65. package/plugins/models/index.ts +39 -45
  66. package/plugins/models/plugin.json +1 -1
  67. package/plugins/models/provider.ts +34 -5
  68. package/plugins/models/saved.ts +8 -4
  69. package/plugins/modes/index.ts +2 -17
  70. package/plugins/plugins/index.ts +2 -2
  71. package/plugins/print/index.ts +36 -6
  72. package/plugins/print/plugin.json +1 -0
  73. package/plugins/prompts/index.ts +22 -14
  74. package/plugins/{memory → prompts}/notes.ts +21 -9
  75. package/plugins/prompts/plugin.json +8 -2
  76. package/plugins/prompts/prompts.ts +5 -4
  77. package/plugins/renderers/index.ts +2 -43
  78. package/plugins/repeats/index.ts +32 -2
  79. package/plugins/schedule/index.ts +2 -2
  80. package/plugins/scratch/index.ts +2 -3
  81. package/plugins/screenshot/browser.ts +221 -0
  82. package/plugins/screenshot/index.ts +29 -15
  83. package/plugins/screenshot/plugin.json +1 -1
  84. package/plugins/screenshot/tool.ts +27 -51
  85. package/plugins/sessions/index.ts +2 -17
  86. package/plugins/shell/index.ts +115 -7
  87. package/plugins/shell/plugin.json +3 -1
  88. package/plugins/shell/renderers.ts +1 -1
  89. package/plugins/shell/service.ts +2 -13
  90. package/plugins/shell/shell.ts +119 -47
  91. package/plugins/skills/index.ts +172 -37
  92. package/plugins/skills/install.ts +212 -0
  93. package/plugins/skills/plugin.json +3 -2
  94. package/plugins/skills/project.ts +38 -1
  95. package/plugins/skills/skills.ts +28 -9
  96. package/plugins/steer/index.ts +142 -0
  97. package/plugins/steer/plugin.json +8 -0
  98. package/plugins/suggest/index.ts +16 -11
  99. package/plugins/suggest/plugin.json +1 -1
  100. package/plugins/task/index.ts +18 -57
  101. package/plugins/task/plugin.json +9 -3
  102. package/plugins/task/subagent.ts +25 -7
  103. package/plugins/todo/index.ts +2 -2
  104. package/plugins/tools/index.ts +50 -0
  105. package/plugins/tools/plugin.json +7 -0
  106. package/plugins/tui/editor.ts +10 -2
  107. package/plugins/tui/frontend.ts +129 -23
  108. package/plugins/tui/index.ts +3 -8
  109. package/plugins/tui/markdown.ts +51 -6
  110. package/plugins/tui/mascot.ts +506 -0
  111. package/plugins/tui/plugin.json +2 -2
  112. package/plugins/tui/screen.ts +6 -0
  113. package/plugins/tui/transcript.ts +17 -2
  114. package/plugins/tui/tui.ts +344 -97
  115. package/plugins/undo/checkpoint.ts +2 -2
  116. package/plugins/undo/index.ts +84 -26
  117. package/plugins/undo/plugin.json +8 -2
  118. package/plugins/undo/snapshot.ts +129 -0
  119. package/plugins/update/index.ts +11 -15
  120. package/plugins/update/plugin.json +2 -1
  121. package/plugins/web/index.ts +8 -11
  122. package/plugins/web/plugin.json +1 -1
  123. package/plugins/web/renderers.ts +1 -1
  124. package/plugins-dist/approval/index.js +221 -51
  125. package/plugins-dist/approval/plugin.json +1 -1
  126. package/plugins-dist/approval/policy.js +30 -15
  127. package/plugins-dist/ask/index.js +1 -1
  128. package/plugins-dist/budget/index.js +1 -1
  129. package/plugins-dist/checks/checks.js +2 -1
  130. package/plugins-dist/checks/index.js +1 -1
  131. package/plugins-dist/commands/index.js +1 -1
  132. package/plugins-dist/compact/compact.js +6 -1
  133. package/plugins-dist/compact/index.js +12 -8
  134. package/plugins-dist/continue/index.js +1 -1
  135. package/plugins-dist/cost/index.js +135 -0
  136. package/plugins-dist/cost/plugin.json +16 -0
  137. package/plugins-dist/custom-commands/index.js +86 -0
  138. package/plugins-dist/custom-commands/plugin.json +12 -0
  139. package/plugins-dist/editor/index.js +89 -0
  140. package/plugins-dist/editor/plugin.json +21 -0
  141. package/plugins-dist/fs/fs.js +34 -6
  142. package/plugins-dist/fs/grep.js +17 -6
  143. package/plugins-dist/fs/index.js +6 -6
  144. package/plugins-dist/fs/plugin.json +1 -1
  145. package/plugins-dist/hooks/index.js +145 -0
  146. package/plugins-dist/hooks/plugin.json +19 -0
  147. package/plugins-dist/keys/index.js +1 -1
  148. package/plugins-dist/mcp/index.js +45 -13
  149. package/plugins-dist/mcp/install.js +160 -0
  150. package/plugins-dist/mcp/mcp.js +2 -1
  151. package/plugins-dist/mcp/plugin.json +2 -1
  152. package/plugins-dist/mcp/project.js +42 -1
  153. package/plugins-dist/memory/index.js +3 -5
  154. package/plugins-dist/memory/plugin.json +1 -1
  155. package/plugins-dist/mentions/index.js +1 -1
  156. package/plugins-dist/models/configure.js +252 -151
  157. package/plugins-dist/models/context.js +2 -2
  158. package/plugins-dist/models/index.js +41 -17
  159. package/plugins-dist/models/plugin.json +1 -1
  160. package/plugins-dist/models/provider.js +17 -3
  161. package/plugins-dist/models/saved.js +9 -4
  162. package/plugins-dist/modes/index.js +1 -1
  163. package/plugins-dist/plugins/index.js +1 -1
  164. package/plugins-dist/print/index.js +37 -3
  165. package/plugins-dist/print/plugin.json +3 -0
  166. package/plugins-dist/prompts/index.js +22 -3
  167. package/plugins-dist/{memory → prompts}/notes.js +10 -7
  168. package/plugins-dist/prompts/plugin.json +8 -2
  169. package/plugins-dist/prompts/prompts.js +5 -4
  170. package/plugins-dist/renderers/index.js +1 -1
  171. package/plugins-dist/repeats/index.js +28 -2
  172. package/plugins-dist/schedule/index.js +1 -1
  173. package/plugins-dist/scratch/index.js +1 -1
  174. package/plugins-dist/screenshot/browser.js +187 -0
  175. package/plugins-dist/screenshot/index.js +28 -13
  176. package/plugins-dist/screenshot/plugin.json +1 -1
  177. package/plugins-dist/screenshot/tool.js +27 -49
  178. package/plugins-dist/sessions/index.js +1 -1
  179. package/plugins-dist/shell/index.js +119 -4
  180. package/plugins-dist/shell/plugin.json +3 -1
  181. package/plugins-dist/shell/shell.js +106 -36
  182. package/plugins-dist/skills/index.js +184 -35
  183. package/plugins-dist/skills/install.js +178 -0
  184. package/plugins-dist/skills/plugin.json +6 -2
  185. package/plugins-dist/skills/project.js +43 -1
  186. package/plugins-dist/skills/skills.js +27 -10
  187. package/plugins-dist/steer/index.js +135 -0
  188. package/plugins-dist/steer/plugin.json +12 -0
  189. package/plugins-dist/suggest/index.js +15 -9
  190. package/plugins-dist/suggest/plugin.json +1 -1
  191. package/plugins-dist/task/index.js +17 -46
  192. package/plugins-dist/task/plugin.json +11 -5
  193. package/plugins-dist/task/subagent.js +21 -6
  194. package/plugins-dist/todo/index.js +1 -1
  195. package/plugins-dist/tools/index.js +49 -0
  196. package/plugins-dist/tools/plugin.json +9 -0
  197. package/plugins-dist/tui/editor.js +7 -2
  198. package/plugins-dist/tui/frontend.js +134 -15
  199. package/plugins-dist/tui/index.js +2 -1
  200. package/plugins-dist/tui/markdown.js +48 -6
  201. package/plugins-dist/tui/mascot.js +441 -0
  202. package/plugins-dist/tui/plugin.json +3 -2
  203. package/plugins-dist/tui/screen.js +2 -0
  204. package/plugins-dist/tui/transcript.js +16 -1
  205. package/plugins-dist/tui/tui.js +320 -88
  206. package/plugins-dist/tui/vendor.js +1388 -1088
  207. package/plugins-dist/undo/checkpoint.js +1 -1
  208. package/plugins-dist/undo/index.js +91 -24
  209. package/plugins-dist/undo/plugin.json +10 -3
  210. package/plugins-dist/undo/snapshot.js +111 -0
  211. package/plugins-dist/update/index.js +9 -10
  212. package/plugins-dist/update/plugin.json +4 -1
  213. package/plugins-dist/web/index.js +7 -7
  214. package/plugins-dist/web/plugin.json +1 -1
  215. package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
  216. package/sdk/python/jade_plugin.py +8 -0
  217. package/src/api.ts +7 -262
  218. package/src/fetch.ts +18 -16
  219. package/src/host.ts +49 -1
  220. package/src/index.ts +1 -1
  221. package/src/main.ts +23 -5
  222. package/src/remote.ts +2 -0
  223. package/src/session.ts +134 -15
  224. package/src/source.ts +2 -11
  225. package/src/update.ts +16 -10
  226. package/plugins/undo/notes.ts +0 -43
  227. package/plugins-dist/undo/notes.js +0 -36
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "id": "jade/models",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "api": "^1",
5
5
  "entry": "./index.js",
6
6
  "provides": [
@@ -19,6 +19,7 @@ export function withRetry(inner, opts = {}) {
19
19
  return {
20
20
  id: `retry(${inner.id})`,
21
21
  async complete(request, options) {
22
+ const failures = [];
22
23
  for (let attempt = 1;; attempt++) {
23
24
  let streamed = false;
24
25
  await opts.gate?.acquire(options.signal);
@@ -32,11 +33,23 @@ export function withRetry(inner, opts = {}) {
32
33
  },
33
34
  });
34
35
  // 实际回复的是被包装者(4.7 第 3 款)
35
- return { ...res, provider: res.provider ?? inner.id };
36
+ return { ...res, provider: res.provider ?? inner.id, ...(failures.length > 0 ? { retries: failures } : {}) };
36
37
  }
37
38
  catch (err) {
38
- if (!(err instanceof ProviderError) || !err.retryable || attempt > retries || options.signal.aborted)
39
+ if (!(err instanceof ProviderError) || options.signal.aborted)
39
40
  throw err;
41
+ if (!err.retryable || attempt > retries) {
42
+ if (failures.length === 0)
43
+ throw err;
44
+ // 失败时没有回复可以带上重试记录:写进错误信息里,会话日志(turn.ended)和界面上都看得到重试过、等了多久
45
+ const waited = Math.round(failures.reduce((ms, f) => ms + f.delayMs, 0) / 1000);
46
+ throw new ProviderError(`${err.message} (after ${failures.length} ${failures.length === 1 ? "retry" : "retries"}, ${waited}s of waiting)`, {
47
+ retryable: err.retryable,
48
+ status: err.status,
49
+ retryAfterMs: err.retryAfterMs,
50
+ cause: err,
51
+ });
52
+ }
40
53
  // 在计划的时间上随机缩短最多 20%,并行的子 Agent 同时遇到 429 时不会同时重试。
41
54
  // 上游给的 retryAfterMs 只当下限:有的服务(尤其是并发数限流)每次都回 Retry-After: 1,
42
55
  // 拿它代替退避计划会每秒撞一次,十几秒就把重试次数用完
@@ -48,6 +61,7 @@ export function withRetry(inner, opts = {}) {
48
61
  // 失败的这次已经推送过增量的,先让订阅者作废它们(4.7)
49
62
  if (streamed)
50
63
  options.onDelta?.({ kind: "reset" });
64
+ failures.push({ attempt, delayMs, error: err.message, ...(err.status !== undefined ? { status: err.status } : {}), ...(streamed ? { discarded: true } : {}) });
51
65
  opts.onRetry?.({ attempt, retries, delayMs, error: err });
52
66
  await sleep(delayMs, options.signal);
53
67
  }
@@ -319,7 +333,7 @@ export async function listModels(env = process.env, fetchImpl = fetch) {
319
333
  return [];
320
334
  }
321
335
  }
322
- function jsonObject(name, text) {
336
+ export function jsonObject(name, text) {
323
337
  let value;
324
338
  try {
325
339
  value = JSON.parse(text);
@@ -1,10 +1,10 @@
1
1
  // 保存过的模型:$JADE_HOME/models.json。同一个服务(类型、地址、密钥都相同)只存一份,下面是在它上面用过的模型,
2
- // 每个模型带自己的设置(输出上限、思考力度、上下文窗口)。.env 里始终是当前用的那一个,这里记着用过的全部,
2
+ // 每个模型带自己的设置(输出上限、思考力度、上下文窗口、openai-compat 的额外请求参数等)。.env 里始终是当前用的那一个,这里记着用过的全部,
3
3
  // /model 可以切回去。
4
4
  import { mkdir, readFile, writeFile } from "node:fs/promises";
5
5
  import path from "node:path";
6
6
  /** 每个模型自己的设置:切换模型时一起换掉 */
7
- export const MODEL_SETTINGS = ["JADE_MAX_TOKENS", "JADE_EFFORT", "JADE_CONTEXT_WINDOW", "JADE_RPM", "JADE_VISION"];
7
+ export const MODEL_SETTINGS = ["JADE_MAX_TOKENS", "JADE_EFFORT", "JADE_CONTEXT_WINDOW", "JADE_RPM", "JADE_VISION", "OPENAI_COMPAT_EXTRA_BODY"];
8
8
  const strings = (v) => typeof v === "object" && v !== null ? Object.fromEntries(Object.entries(v).filter(([, x]) => typeof x === "string")) : {};
9
9
  /** 读不了或格式不对时返回空数组 */
10
10
  export async function readSavedServices(file) {
@@ -16,11 +16,16 @@ export async function readSavedServices(file) {
16
16
  const s = raw;
17
17
  if (!s || !Array.isArray(s.models))
18
18
  return [];
19
+ // 以前额外请求参数存在服务上:分给它下面没有自己那份的模型
20
+ const { OPENAI_COMPAT_EXTRA_BODY: extraBody, ...env } = strings(s.env);
19
21
  const models = s.models.flatMap((m) => {
20
22
  const name = m?.name;
21
- return typeof name === "string" && name ? [{ name, settings: strings(m.settings) }] : [];
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 }] : [];
22
27
  });
23
- return models.length ? [{ env: strings(s.env), models }] : [];
28
+ return models.length ? [{ env, models }] : [];
24
29
  });
25
30
  }
26
31
  catch {
@@ -8,7 +8,7 @@
8
8
  // 用户的选择在工具结果里,会话日志里看得到
9
9
  import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
10
10
  import path from "node:path";
11
- import { definePlugin } from "../../dist/api.js";
11
+ import { definePlugin } from "@uncarved/jade-api";
12
12
  export const DESCRIPTIONS = {
13
13
  ask: "ask before edits and commands",
14
14
  auto: "edits in this directory and harmless commands run without asking; others still ask",
@@ -1,6 +1,6 @@
1
1
  // jade/plugins:/plugins 命令。列出这次启动里每个插件的状态和原因(failed、skipped、conflicted……),
2
2
  // 以及每个服务由谁提供。数据来自核心自己的 jade@1 服务,和救援 CLI `jade plugins list` 是同一份状态。
3
- import { definePlugin } from "../../dist/api.js";
3
+ import { definePlugin } from "@uncarved/jade-api";
4
4
  export default definePlugin({
5
5
  process(ctx) {
6
6
  const commands = ctx.require("commands@1");
@@ -1,13 +1,16 @@
1
1
  // jade/print:jade -p "<prompt>" 跑一轮就退出。模型的回复写到 stdout,工具调用和提示写到 stderr。
2
2
  // 不能向用户提问:ui@1 的 select 总是返回 undefined,要审批的调用由审批插件拒绝。
3
- import { definePlugin } from "../../dist/api.js";
4
- const USAGE = `usage: jade -p <prompt> [--resume <session id>]
3
+ import { definePlugin } from "@uncarved/jade-api";
4
+ const USAGE = `usage: jade -p <prompt> [--resume <session id> | --continue] [--json]
5
5
  echo <prompt> | jade -p
6
6
  cat <file> | jade -p - <prompt>
7
7
 
8
8
  With "-", what comes in on stdin is added after the prompt (without a prompt, stdin is the prompt).
9
9
 
10
10
  Runs one prompt and exits. The reply goes to stdout; tool calls and notices go to stderr.
11
+ -c, --continue continues the most recent session in this directory.
12
+ --json prints one JSON object on stdout instead of the reply as it is written:
13
+ {"session", "result" (the final reply), "reason" (why the turn ended), "error"?, "usage"}
11
14
  Exit code: 0 when the turn ends normally, 1 on an error, 130 when interrupted.
12
15
  jade's own commands (plugins, update, --safe): jade help`;
13
16
  const ui = {
@@ -18,6 +21,8 @@ const ui = {
18
21
  select: async () => undefined,
19
22
  input: async () => undefined,
20
23
  };
24
+ /** --continue 用:process() 里记下的工作目录和会话索引(jade/sessions 的 sessions@1,用到的时候取) */
25
+ let here;
21
26
  const frontend = {
22
27
  async run({ argv, sessions }) {
23
28
  const args = parse(argv);
@@ -36,6 +41,14 @@ const frontend = {
36
41
  process.stderr.write(`no prompt given\n\n${USAGE}\n`);
37
42
  return 2;
38
43
  }
44
+ if (args.cont) {
45
+ const latest = (await here?.index()?.list(here.cwd))?.[0]?.id;
46
+ if (!latest) {
47
+ process.stderr.write("jade: there is no earlier session in this directory to continue\n");
48
+ return 2;
49
+ }
50
+ args.resume = latest;
51
+ }
39
52
  let session;
40
53
  try {
41
54
  session = await sessions.open(args.resume ? { resume: args.resume } : {});
@@ -47,7 +60,12 @@ const frontend = {
47
60
  }
48
61
  let ended;
49
62
  let midLine = false;
63
+ /** --json:回复不边写边打印,最后一次回复的正文和用量合计在结束时一起输出 */
64
+ let result = "";
65
+ const used = { inputTokens: 0, outputTokens: 0, cacheReadTokens: 0, cacheWriteTokens: 0 };
50
66
  const out = (text) => {
67
+ if (args.json)
68
+ return;
51
69
  process.stdout.write(text);
52
70
  midLine = !text.endsWith("\n");
53
71
  };
@@ -62,7 +80,12 @@ const frontend = {
62
80
  return;
63
81
  const e = n.event;
64
82
  if (e.type === "model.responded") {
65
- const content = e.data.content;
83
+ const { content, usage } = e.data;
84
+ const text = content.flatMap((b) => (b.kind === "text" ? [b.text] : [])).join("\n");
85
+ if (text)
86
+ result = text;
87
+ for (const k of Object.keys(used))
88
+ used[k] += usage?.[k] ?? 0;
66
89
  if (!streamed)
67
90
  for (const b of content)
68
91
  if (b.kind === "text")
@@ -107,6 +130,10 @@ const frontend = {
107
130
  out("\n");
108
131
  process.stderr.write(`session ${session.id}\n`);
109
132
  const end = ended;
133
+ if (args.json) {
134
+ const error = failure !== undefined ? errorText(failure) : end?.error;
135
+ process.stdout.write(`${JSON.stringify({ session: session.id, result, reason: end?.reason ?? "error", ...(error ? { error } : {}), usage: used })}\n`);
136
+ }
110
137
  if (end?.reason === "aborted")
111
138
  return 130;
112
139
  if (failure !== undefined || end?.reason === "error") {
@@ -118,6 +145,7 @@ const frontend = {
118
145
  };
119
146
  export default definePlugin({
120
147
  process(ctx) {
148
+ here = { cwd: ctx.cwd, index: () => ctx.service("sessions@1") };
121
149
  ctx.provide("ui@1", ui);
122
150
  ctx.provide("frontend@1", frontend);
123
151
  },
@@ -133,6 +161,10 @@ function parse(argv) {
133
161
  out.help = true;
134
162
  else if (a === "-")
135
163
  out.stdin = true;
164
+ else if (a === "-c" || a === "--continue")
165
+ out.cont = true;
166
+ else if (a === "--json")
167
+ out.json = true;
136
168
  else if (a === "--resume") {
137
169
  const id = argv[++i];
138
170
  if (!id)
@@ -144,6 +176,8 @@ function parse(argv) {
144
176
  else
145
177
  words.push(a);
146
178
  }
179
+ if (out.cont && out.resume)
180
+ return "--continue and --resume both pick the session; use one of them";
147
181
  if (words.length > 0)
148
182
  out.prompt = words.join(" ");
149
183
  return out;
@@ -7,6 +7,9 @@
7
7
  "frontend@1",
8
8
  "ui@1"
9
9
  ],
10
+ "optional": [
11
+ "sessions@1"
12
+ ],
10
13
  "frontend": {
11
14
  "name": "print",
12
15
  "flag": "-p"
@@ -1,12 +1,19 @@
1
1
  // jade/prompts:在 context.build 里生成系统提示词。其他插件经 prompts@1 往里加段落。
2
- // 也负责内核不写的、给模型看的说明:内容为空的错误工具结果在投影里补一句原因(results.ts)。
2
+ // 也负责内核不写的、给模型看的说明:内容为空的错误工具结果在投影里补一句原因(results.ts);
3
+ // 其他插件经 prompts@1 的 note 给模型留一句说明(notes.ts)。
4
+ import os from "node:os";
3
5
  import path from "node:path";
4
- import { definePlugin } from "../../dist/api.js";
6
+ import { definePlugin } from "@uncarved/jade-api";
7
+ import { Notes } from "./notes.js";
5
8
  import { allInstructions, environment, mainPrompt, realPath } from "./prompts.js";
6
9
  import { explainEmptyResults, whyEmpty } from "./results.js";
7
10
  const ID = "jade/prompts";
8
11
  /** 其他插件加的段落。模块每个进程只加载一次,进程级和会话级共用 */
9
12
  const sections = [];
13
+ /** 打开着的会话 → 它读到的指令文件(prompts@1.instructions) */
14
+ const loaded = new Map();
15
+ /** 其他插件留给模型的说明 */
16
+ const notes = new Notes();
10
17
  function segment(text, trust = "trusted", stability = "stable", by = ID, source) {
11
18
  return { text, stability, origin: { by: "plugin", id: by, ...(source ? { source } : {}) }, trust };
12
19
  }
@@ -32,11 +39,21 @@ export default definePlugin({
32
39
  caller.onDispose(remove);
33
40
  return remove;
34
41
  },
42
+ note(session, text) {
43
+ if (typeof session !== "string" || !session)
44
+ throw new Error("prompts.note: session must be a session id");
45
+ if (typeof text !== "string" || !text)
46
+ throw new Error("prompts.note: text must be a non-empty string");
47
+ notes.add(session, { plugin: caller.plugin, text, trusted: caller.layer !== "project" });
48
+ },
49
+ instructions: (session) => (loaded.get(session) ?? []).map((i) => ({ ...i, source: { ...i.source } })),
35
50
  }));
36
51
  },
37
52
  async session(ctx) {
38
53
  const models = ctx.service("models@1");
39
- const [env, instructions] = await Promise.all([environment(ctx.cwd), allInstructions(ctx.home, ctx.cwd)]);
54
+ const [env, instructions] = await Promise.all([environment(ctx.cwd), allInstructions(ctx.home, ctx.cwd, ctx.config.claudeInstructions === false ? null : os.homedir())]);
55
+ loaded.set(ctx.session.id, instructions.map(({ text, trust, source }) => ({ text, trust, source })));
56
+ await ctx.effect(() => () => void loaded.delete(ctx.session.id));
40
57
  const fixed = [segment(env), ...instructions.map((i) => segment(i.text, i.trust, "stable", ID, i.source))];
41
58
  // 告诉用户读了哪些指令文件,否则看不出它们生效了
42
59
  if (instructions.length > 0)
@@ -67,6 +84,8 @@ export default definePlugin({
67
84
  optional: true,
68
85
  fn: (messages, hook) => explainEmptyResults(messages, { ...hook.derived[reasonsKey], ...reasons }, ID),
69
86
  });
87
+ ctx.kernel.hook(notes.hook(ctx.session.id, `${ID}/notes`));
88
+ await ctx.effect(() => () => notes.drop(ctx.session.id));
70
89
  ctx.kernel.hook({
71
90
  kind: "transform",
72
91
  point: "context.build",
@@ -1,15 +1,18 @@
1
- // 给模型的说明([Note from jade]):命令里做的事(例如用户删掉了一条记忆)要让模型知道,
2
- // 但命令拿不到会话日志,也不该替用户发一条消息。所以先记下来,下一次请求模型时在 context.project 里
1
+ // 给模型的说明([Note from jade],prompts@1 的 note):命令里做的事(例如用户删掉了一条记忆、撤销了模型改的文件)
2
+ // 要让模型知道,但命令拿不到会话日志,也不该替用户发一条消息。所以先记下来,下一次请求模型时在 context.project 里
3
3
  // 附在最后一条消息后面,并用 ctx.record 写进日志(derived):之后的投影、恢复的会话里它都在同一个位置。
4
- // 插件之间不共享代码,jade/undo 里也有一份。
5
4
  export class Notes {
6
5
  /** 会话 → 还没交给模型的说明 */
7
6
  #pending = new Map();
8
- add(sessionId, text) {
9
- this.#pending.set(sessionId, [...(this.#pending.get(sessionId) ?? []), text]);
7
+ add(sessionId, note) {
8
+ this.#pending.set(sessionId, [...(this.#pending.get(sessionId) ?? []), note]);
9
+ }
10
+ /** 会话关闭:还没交给模型的说明不再留着 */
11
+ drop(sessionId) {
12
+ this.#pending.delete(sessionId);
10
13
  }
11
14
  /** 会话里的 context.project hook;key 是 derived 里的键 */
12
- hook(sessionId, plugin, key) {
15
+ hook(sessionId, key) {
13
16
  return {
14
17
  kind: "transform",
15
18
  point: "context.project",
@@ -27,7 +30,7 @@ export class Notes {
27
30
  const notes = state[i];
28
31
  if (!notes?.length || m.role !== "user")
29
32
  return m;
30
- const blocks = notes.map((text) => ({ kind: "text", text: `[Note from jade] ${text}`, origin: { by: "plugin", id: plugin }, trust: "trusted" }));
33
+ const blocks = notes.map((n) => ({ kind: "text", text: `[Note from jade] ${n.text}`, origin: { by: "plugin", id: n.plugin }, trust: n.trusted ? "trusted" : "untrusted" }));
31
34
  return { ...m, content: [...m.content, ...blocks] };
32
35
  });
33
36
  },
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "id": "jade/prompts",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "api": "^1",
5
5
  "entry": "./index.js",
6
6
  "provides": [
@@ -9,5 +9,11 @@
9
9
  "optional": [
10
10
  "models@1",
11
11
  "ui@1"
12
- ]
12
+ ],
13
+ "config": {
14
+ "claudeInstructions": {
15
+ "default": true,
16
+ "description": "when $JADE_HOME has no AGENTS.md or CLAUDE.md, use ~/.claude/CLAUDE.md as the user instructions; false turns this off"
17
+ }
18
+ }
13
19
  }
@@ -3,7 +3,8 @@
3
3
  // 主体和环境信息都是 stable:环境信息在会话开始时取一次,不在每次请求时更新,
4
4
  // 否则它一变,排在后面的整段历史都无法命中提示缓存。
5
5
  // 指令文件兼容 AGENTS.md 和 CLAUDE.md,同一目录两者都有时只读 AGENTS.md。
6
- // 用户层($JADE_HOME 里)的是 trusted;项目层(随仓库分发)的是 untrusted,并记下出处(宪法 7.2)。
6
+ // 用户层的先找 $JADE_HOME,没有时用 ~/.claude 里的(和 jade/skills 读 ~/.claude/skills 一样,已有的全局指令不用再抄一份)。
7
+ // 用户层的是 trusted;项目层(随仓库分发)的是 untrusted,并记下出处(宪法 7.2)。
7
8
  // 项目层从仓库根目录到工作目录逐级查找,上级的在前,离工作目录近的在后。
8
9
  import { createHash } from "node:crypto";
9
10
  import { realpathSync } from "node:fs";
@@ -84,10 +85,10 @@ export async function environment(cwd, today = localDate()) {
84
85
  }
85
86
  return lines.join("\n");
86
87
  }
87
- /** 用户层(home 里)和项目层(仓库根目录到 cwd 逐级)的指令文件,按顺序 */
88
- export async function allInstructions(home, cwd) {
88
+ /** 用户层(home 里,没有时 userHome/.claude 里;userHome 给 null 时不找)和项目层(仓库根目录到 cwd 逐级)的指令文件,按顺序 */
89
+ export async function allInstructions(home, cwd, userHome = os.homedir()) {
89
90
  const out = [];
90
- const user = await instructions(home, home, "user");
91
+ const user = (await instructions(home, home, "user")) ?? (userHome === null ? undefined : await instructions(userHome, path.join(userHome, ".claude"), "user"));
91
92
  if (user)
92
93
  out.push({ text: `# User instructions (${user.source.path})\n\n${user.text}`, trust: "trusted", source: user.source, file: user.file });
93
94
  const root = realPath(await repoRoot(cwd));
@@ -1,6 +1,6 @@
1
1
  // jade/renderers:工具调用和插件事件在对话里怎么显示的注册表。前端查这里;没有登记的用前端的默认显示。
2
2
  // 显示的文字可以带 ANSI 颜色,前端负责过滤掉颜色之外的控制序列。
3
- import { definePlugin } from "../../dist/api.js";
3
+ import { definePlugin } from "@uncarved/jade-api";
4
4
  const tools = new Map();
5
5
  const events = new Map();
6
6
  function add(map, key, renderer, plugin, what, onDispose) {
@@ -8,12 +8,13 @@
8
8
  // 不拒绝:也可能是在等外面的东西变化,只提醒。压缩插件清理或总结了历史后,之前的结果可能已经不在上下文里,都不算重复。
9
9
  // 打开文件给用户看(start、xdg-open……)会弹出新窗口:这一轮里打开过、之后什么都没改,再打开就直接拒绝。
10
10
  // write_file 之后什么都没改又读回同一个文件,提醒模型内容就是它写的那些。
11
+ // 同一个文件里同一处文字被来回改(A 改成 B、B 改回 A、又改成 B):模型在凭不可靠的依据猜,第三次起提醒它停下来换个办法查,或者问用户。
11
12
  // 这些记录只在本会话的作用域里:恢复会话后从零开始,最多多重试一轮。
12
13
  //
13
14
  // 从旧 jade 的 plugins/repeats.ts 移植。
14
15
  import { createHash } from "node:crypto";
15
16
  import path from "node:path";
16
- import { definePlugin } from "../../dist/api.js";
17
+ import { definePlugin } from "@uncarved/jade-api";
17
18
  const ID = "jade/repeats";
18
19
  /** jade/compact 记录压缩结果用的派生数据键(旧 jade 的约定):记一次就说明历史被压缩过 */
19
20
  export const COMPACT_KEY = "jade/compact";
@@ -38,6 +39,14 @@ function normalize(cwd, p) {
38
39
  }
39
40
  /** 提醒模型的失败次数 */
40
41
  const WARN_AFTER = 2;
42
+ /** 同一处文字来回改到第几次时提醒:改过去、改回来各算一次,改回来一次是正常的撤销 */
43
+ const FLIP_AFTER = 3;
44
+ /** edit_file、multi_edit 里的每一处替换 */
45
+ function replacements(call) {
46
+ const input = call.input;
47
+ const list = call.name === "edit_file" ? [input] : call.name === "multi_edit" && Array.isArray(input?.edits) ? input.edits : [];
48
+ return list.filter((e) => typeof e?.old_string === "string" && typeof e?.new_string === "string");
49
+ }
41
50
  const hash = (content) => createHash("sha1").update(JSON.stringify(content.map((b) => (b.kind === "text" ? b.text : b)))).digest("hex");
42
51
  /** 会话里的 hook:model.before 数回复次数,tool.gate 拒绝重复,tool.after 加提醒,event 在用户发消息、压缩后重新计 */
43
52
  export function repeatsHooks(opts) {
@@ -61,6 +70,8 @@ export function repeatsHooks(opts) {
61
70
  const opened = new Map();
62
71
  /** 本轮用 write_file 写过的文件 → 写完时的 changes;用户发消息时清空 */
63
72
  const written = new Map();
73
+ /** 文件里互相替换的一对文字 → 本轮里换了几次(不分方向);用户发消息时清空 */
74
+ const flips = new Map();
64
75
  const key = (call) => `${call.name}\0${JSON.stringify(call.input)}`;
65
76
  const note = (text) => ({ kind: "text", text: `[Note from jade] ${text}`, origin: { by: "plugin", id }, trust: "trusted" });
66
77
  return [
@@ -117,6 +128,20 @@ export function repeatsHooks(opts) {
117
128
  const readBack = outcome.call.name === "read_file" && typeof file === "string" && written.get(normalize(cwd, file)) === changes
118
129
  ? note("You wrote this file with write_file earlier and nothing has changed since: its content is exactly what you gave. Do not read files back to check them.")
119
130
  : undefined;
131
+ let most = 0;
132
+ if (typeof file === "string") {
133
+ for (const e of replacements(outcome.call)) {
134
+ const pair = `${normalize(cwd, file)}${[e.old_string, e.new_string].sort().join("")}`;
135
+ const n = (flips.get(pair) ?? 0) + 1;
136
+ flips.set(pair, n);
137
+ most = Math.max(most, n);
138
+ }
139
+ }
140
+ const flipped = most >= FLIP_AFTER
141
+ ? note(`You have now switched this same text back and forth ${most} times in this file. Going back and forth means what you are judging by is unreliable or incomplete. ` +
142
+ "Do not flip it again: check your assumption another way (measure it, read the code involved), or tell the user what you see and ask.")
143
+ : undefined;
144
+ const extra = [readBack, flipped].filter((b) => b !== undefined);
120
145
  const before = succeeded.get(k);
121
146
  // 只读之外的调用(包括这一个)可能改了东西,之后的调用都不算"什么都没变"
122
147
  if (!read)
@@ -125,7 +150,7 @@ export function repeatsHooks(opts) {
125
150
  const repeated = before !== undefined && before.version === (read ? version : version - 1) && before.result === result;
126
151
  succeeded.set(k, { version, result });
127
152
  if (!repeated)
128
- return readBack ? { ...outcome, content: [...outcome.content, readBack] } : undefined;
153
+ return extra.length > 0 ? { ...outcome, content: [...outcome.content, ...extra] } : undefined;
129
154
  const text = "This exact call already ran earlier with the same result, and nothing has changed since (no edits, no other commands, no new message from the user). " +
130
155
  "Do not repeat it: use what you already have, or end your turn and tell the user where things stand.";
131
156
  return { ...outcome, content: [...outcome.content, note(text)] };
@@ -152,6 +177,7 @@ export function repeatsHooks(opts) {
152
177
  version++;
153
178
  opened.clear();
154
179
  written.clear();
180
+ flips.clear();
155
181
  }
156
182
  },
157
183
  },
@@ -3,7 +3,7 @@
3
3
  // 下次打开时错过的几次只补一次。不能提问的前端(jade -p)不投递。
4
4
  // 管理入口:/schedule 命令和给模型的 schedule_create、schedule_list、schedule_edit 工具,共用同一份存储。
5
5
  import path from "node:path";
6
- import { definePlugin } from "../../dist/api.js";
6
+ import { definePlugin } from "@uncarved/jade-api";
7
7
  import { parseCron } from "./cron.js";
8
8
  import { claimDue, nextAt, sameDir, ScheduleStore } from "./store.js";
9
9
  const ID = "jade/schedule";
@@ -4,7 +4,7 @@ import { realpathSync } from "node:fs";
4
4
  import { mkdir } from "node:fs/promises";
5
5
  import os from "node:os";
6
6
  import path from "node:path";
7
- import { definePlugin } from "../../dist/api.js";
7
+ import { definePlugin } from "@uncarved/jade-api";
8
8
  /** 会话 id 对应的临时目录 */
9
9
  export const scratchDir = (session) => path.join(os.tmpdir(), "jade-scratch", session);
10
10
  export default definePlugin({