@uncarved/jade 0.2.0 → 0.2.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 (57) hide show
  1. package/README.md +215 -0
  2. package/bin/jade.js +11 -0
  3. package/dist/main.js +2 -1
  4. package/dist/main.js.map +1 -1
  5. package/package.json +4 -4
  6. package/plugins/approval/index.ts +16 -2
  7. package/plugins/approval/plugin.json +4 -0
  8. package/plugins/approval/policy.ts +25 -5
  9. package/plugins/compact/compact.ts +7 -1
  10. package/plugins/fs/fs.ts +78 -12
  11. package/plugins/fs/grep.ts +35 -7
  12. package/plugins/fs/index.ts +7 -4
  13. package/plugins/mentions/index.ts +31 -11
  14. package/plugins/models/index.ts +17 -2
  15. package/plugins/print/index.ts +26 -8
  16. package/plugins/prompts/index.ts +29 -1
  17. package/plugins/prompts/prompts.ts +9 -4
  18. package/plugins/prompts/results.ts +63 -0
  19. package/plugins/prompts/sys.ts +16 -4
  20. package/plugins/task/index.ts +1 -0
  21. package/plugins/task/subagent.ts +96 -10
  22. package/plugins/todo/index.ts +2 -2
  23. package/plugins/tui/frontend.ts +27 -4
  24. package/plugins/tui/mentions.ts +48 -8
  25. package/plugins/tui/transcript.ts +5 -1
  26. package/plugins/tui/tui.ts +2 -5
  27. package/plugins/tui/vendor.ts +16 -0
  28. package/plugins/undo/checkpoint.ts +24 -0
  29. package/plugins/undo/index.ts +8 -4
  30. package/plugins/web/web.ts +6 -5
  31. package/plugins-dist/approval/index.js +18 -2
  32. package/plugins-dist/approval/plugin.json +4 -0
  33. package/plugins-dist/approval/policy.js +27 -5
  34. package/plugins-dist/compact/compact.js +7 -1
  35. package/plugins-dist/fs/fs.js +73 -12
  36. package/plugins-dist/fs/grep.js +35 -9
  37. package/plugins-dist/fs/index.js +7 -4
  38. package/plugins-dist/mentions/index.js +28 -10
  39. package/plugins-dist/models/index.js +16 -2
  40. package/plugins-dist/print/index.js +25 -6
  41. package/plugins-dist/prompts/index.js +28 -0
  42. package/plugins-dist/prompts/prompts.js +8 -4
  43. package/plugins-dist/prompts/results.js +60 -0
  44. package/plugins-dist/prompts/sys.js +15 -3
  45. package/plugins-dist/task/index.js +1 -0
  46. package/plugins-dist/task/subagent.js +86 -10
  47. package/plugins-dist/todo/index.js +2 -2
  48. package/plugins-dist/tui/frontend.js +28 -5
  49. package/plugins-dist/tui/mentions.js +44 -6
  50. package/plugins-dist/tui/transcript.js +3 -1
  51. package/plugins-dist/tui/tui.js +1 -2
  52. package/plugins-dist/tui/vendor.js +13822 -0
  53. package/plugins-dist/undo/checkpoint.js +27 -0
  54. package/plugins-dist/undo/index.js +7 -4
  55. package/plugins-dist/web/web.js +6 -5
  56. package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
  57. package/src/main.ts +2 -1
@@ -106,5 +106,32 @@ export function lastTurnChanges(messages, checkpoints, undone) {
106
106
  // 找不到对应的消息(不应该发生):退回到只撤销最近的一次写入
107
107
  return pending.slice(0, 1);
108
108
  }
109
+ /**
110
+ * 一轮里调用过的工具名:含 toolUseId 的那一轮;不给时是最近一轮。
111
+ * 一轮从用户发的消息开始,到下一条用户消息之前(与 lastTurnChanges 的划分相同)
112
+ */
113
+ export function turnToolNames(messages, toolUseId) {
114
+ let names = new Set();
115
+ let found = toolUseId === undefined;
116
+ for (let i = messages.length - 1; i >= 0; i--) {
117
+ const m = messages[i];
118
+ if (m.role === "assistant") {
119
+ for (const b of m.content) {
120
+ if (b.kind !== "tool_use")
121
+ continue;
122
+ names.add(b.name);
123
+ if (b.id === toolUseId)
124
+ found = true;
125
+ }
126
+ continue;
127
+ }
128
+ if (!m.content.some((b) => b.kind !== "tool_result" && b.origin.by === "user"))
129
+ continue;
130
+ if (found)
131
+ return names;
132
+ names = new Set();
133
+ }
134
+ return found ? names : new Set();
135
+ }
109
136
  /** 会话和工具调用的 id 用作文件名 */
110
137
  const safe = (id) => id.replace(/[^A-Za-z0-9_.-]/g, "_");
@@ -7,11 +7,13 @@ import { readFile } from "node:fs/promises";
7
7
  import { realpathSync } from "node:fs";
8
8
  import path from "node:path";
9
9
  import { definePlugin } from "../../dist/api.js";
10
- import { Checkpoints, lastTurnChanges, sha256 } from "./checkpoint.js";
10
+ import { Checkpoints, lastTurnChanges, sha256, turnToolNames } from "./checkpoint.js";
11
11
  import { Notes } from "./notes.js";
12
12
  import { dim, green } from "./style.js";
13
13
  /** 会改文件、参数里有 path 的工具 */
14
14
  export const WRITE_TOOLS = new Set(["write_file", "edit_file", "multi_edit"]);
15
+ /** shell 命令改了什么无从得知,撤销不了;这一轮跑过时提醒一句 */
16
+ const SHELL_NOTE = "This turn also ran shell commands; what they changed is not undone.";
15
17
  const notes = new Notes();
16
18
  /** 显示用:工作目录内的路径用相对形式,统一为正斜杠 */
17
19
  function display(cwd, full) {
@@ -44,7 +46,7 @@ export default definePlugin({
44
46
  const cwd = realPath(ctx.cwd);
45
47
  ctx.service("commands@1")?.register({
46
48
  name: "undo",
47
- description: "restore every file the agent changed in its last turn (run again to go further back)",
49
+ description: "restore the files the agent wrote or edited in its last turn, not changes made by shell commands (run again to go further back)",
48
50
  async run(_args, { sessions, ui, signal }) {
49
51
  const s = sessions.current;
50
52
  if (!s)
@@ -52,7 +54,8 @@ export default definePlugin({
52
54
  const undone = await checkpoints.undone(s.id);
53
55
  const cps = lastTurnChanges(s.state.messages, await checkpoints.list(s.id), undone);
54
56
  if (cps.length === 0)
55
- return dim("Nothing to undo.");
57
+ return dim(turnToolNames(s.state.messages).has("bash") ? `Nothing to undo. ${SHELL_NOTE}` : "Nothing to undo.");
58
+ const shellToo = turnToolNames(s.state.messages, cps[0].toolUseId).has("bash");
56
59
  /** 文件 → 这一轮最早的快照(恢复成它的 before);cps 是最新的在前 */
57
60
  const files = new Map();
58
61
  for (const cp of cps)
@@ -90,7 +93,7 @@ export default definePlugin({
90
93
  await checkpoints.markUndone(s.id, cps.map((c) => c.toolUseId));
91
94
  const what = [...files.values()].map((cp) => cp.before === null ? `deleted ${show(cp.path)} (you had created it)` : `restored ${show(cp.path)} to its content before your changes`);
92
95
  notes.add(s.id, `The user undid your changes from your last turn: ${what.join("; ")}. Read these files again before changing them.`);
93
- return green(`Undid ${cps.length} change${cps.length > 1 ? "s" : ""} from the last turn:`) + "\n" + dim(list);
96
+ return green(`Undid ${cps.length} change${cps.length > 1 ? "s" : ""} from the last turn:`) + "\n" + dim(list) + (shellToo ? `\n${dim(SHELL_NOTE)}` : "");
94
97
  },
95
98
  });
96
99
  },
@@ -13,7 +13,6 @@
13
13
  // 从旧 jade 的 tools/web.ts 移植。服务的 key 每次调用时取(search()),/search 改了 key 当前会话立即用上,不用重开会话。
14
14
  import { lookup as dnsLookup } from "node:dns/promises";
15
15
  import { isIP } from "node:net";
16
- import { Parser } from "htmlparser2";
17
16
  /** 一次返回的最多字符数,更长的用 offset 分页 */
18
17
  const MAX_CHARS = 30_000;
19
18
  /** 下载的最大字节数 */
@@ -197,7 +196,7 @@ async function download(fetchImpl, lookup, target, signal) {
197
196
  if (!res.ok) {
198
197
  const body = await res.text().catch(() => "");
199
198
  return {
200
- content: `HTTP ${res.status} ${res.statusText} from ${url.href}${body ? `\n${clip(htmlToMarkdown(body).text || body, 1000)}` : ""}`,
199
+ content: `HTTP ${res.status} ${res.statusText} from ${url.href}${body ? `\n${clip((await htmlToMarkdown(body)).text || body, 1000)}` : ""}`,
201
200
  isError: true,
202
201
  retry: res.status === 403 || res.status === 429 || res.status >= 500,
203
202
  };
@@ -219,7 +218,7 @@ async function download(fetchImpl, lookup, target, signal) {
219
218
  const charset = /charset=([^;]+)/i.exec(res.headers.get("content-type") ?? "")?.[1]?.trim();
220
219
  const raw = decode(buf, charset);
221
220
  if (type === "text/html" || type === "application/xhtml+xml" || (!type && /^\s*<(!doctype|html)/i.test(raw))) {
222
- const { title, text } = htmlToMarkdown(raw, url.href);
221
+ const { title, text } = await htmlToMarkdown(raw, url.href);
223
222
  return { at: Date.now(), url: url.href, title, text, thin: text.length < THIN_CHARS };
224
223
  }
225
224
  if (type.startsWith("text/") || /json|xml|javascript|yaml|toml/.test(type) || !type) {
@@ -270,9 +269,11 @@ const SKIP = new Set(["script", "style", "noscript", "template", "svg", "canvas"
270
269
  const BLOCKS = new Set(["p", "div", "section", "article", "main", "header", "ul", "ol", "table", "tr", "blockquote", "figure", "figcaption", "dl", "dt", "dd", "hr"]);
271
270
  /**
272
271
  * 用 htmlparser2 把网页转成 markdown 风格的文本:标题、段落、列表、链接、代码块,去掉脚本、样式和导航。
273
- * 不追求还原排版,只要模型能读。Node 和 Bun 都能用(Bun 的 HTMLRewriter 在 Node 里没有)
272
+ * 不追求还原排版,只要模型能读。Node 和 Bun 都能用(Bun 的 HTMLRewriter 在 Node 里没有)。
273
+ * htmlparser2 第一次用到时才加载:导入要几十毫秒,每次启动都付不值得
274
274
  */
275
- export function htmlToMarkdown(html, base) {
275
+ export async function htmlToMarkdown(html, base) {
276
+ const { Parser } = await import("htmlparser2");
276
277
  const out = [];
277
278
  let skip = 0;
278
279
  let pre = 0;
package/src/main.ts CHANGED
@@ -25,7 +25,8 @@ const USAGE = `usage: jade [--safe] [frontend options…]
25
25
  -v, --version print the version
26
26
 
27
27
  Run "jade plugins --help" for the install and update options.
28
- Everything else is passed to the frontend plugin; see its own --help.`;
28
+ Everything else is passed to the frontend plugin: "jade --help" shows the terminal interface's options,
29
+ "jade -p --help" the options for running one prompt.`;
29
30
 
30
31
  /**
31
32
  * jade plugins 的子命令(含别名):单独写在 jade 后面时多半是漏了 plugins。