@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
@@ -11,13 +11,32 @@ import { InputHistory } from "./history.js";
11
11
  import { mentionCandidates } from "./mentions.js";
12
12
  import { dim, red, safeScreen, yellow } from "./screen.js";
13
13
  import { transcript } from "./transcript.js";
14
- import { tuiScreen } from "./tui.js";
14
+ /**
15
+ * 界面(ink、React)用到时才加载:--help、参数出错、不在终端里时都不需要它,导入一次要几百毫秒。
16
+ * React 在加载时按 NODE_ENV 选开发版或生产版,开发版做大量检查,流式输出时 CPU 翻倍。
17
+ * 用户没设 NODE_ENV 时只在加载期间设成 production,加载完还原,免得传给之后 shell 工具、MCP 等子进程。
18
+ * ink 依赖的 yoga 有顶层 await,只能异步加载,做不到同步设置再还原:加载期间还没有会话,工具和 MCP 都没启动,
19
+ * 这时可能启动的子进程只有 git(不读它)和后台的 npm 自动升级(全局安装本来就不装 devDependencies)。
20
+ * 编译好的 plugins-dist 里 ink、react 已打包成生产版(vendor.ts),这里设 NODE_ENV 只对直接跑源码(bun)时有用
21
+ */
22
+ async function loadTui() {
23
+ if (process.env.NODE_ENV !== undefined)
24
+ return import("./tui.js");
25
+ process.env.NODE_ENV = "production";
26
+ try {
27
+ return await import("./tui.js");
28
+ }
29
+ finally {
30
+ delete process.env.NODE_ENV;
31
+ }
32
+ }
15
33
  const USAGE = `usage: jade [prompt] [--resume <session id> | --continue]
16
34
 
17
35
  Starts the terminal interface. With a prompt, sends it first.
18
36
  -c, --continue continue the most recent session in this directory
19
37
  -r, --resume resume a session by id (inside jade, /resume lists them)
20
- Run one prompt without the interface: jade -p <prompt>`;
38
+ Run one prompt without the interface: jade -p <prompt> (jade -p --help for more)
39
+ jade's own commands (plugins, update, --safe): jade help`;
21
40
  /** 前端自己的命令:帮助和退出。其余命令都来自 commands@1 */
22
41
  const OWN = [
23
42
  { name: "help", about: "list the commands and keys" },
@@ -221,7 +240,7 @@ async function run(services, bridge, opts) {
221
240
  return 2;
222
241
  }
223
242
  const history = new InputHistory(path.join(home, "history.jsonl"), cwd);
224
- const [past, branch] = await Promise.all([history.load(), gitBranch(cwd)]);
243
+ const [past, branch, { tuiScreen }] = await Promise.all([history.load(), gitBranch(cwd), loadTui()]);
225
244
  /** 菜单里的命令:前端自己的 + 注册表里的,每次打开菜单时取 */
226
245
  const commandList = () => [
227
246
  ...OWN,
@@ -285,6 +304,7 @@ async function run(services, bridge, opts) {
285
304
  screen,
286
305
  cwd,
287
306
  contextWindow: () => models?.current()?.contextWindow ?? 0,
307
+ maxTokens: () => models?.current()?.maxTokens,
288
308
  ...(compact ? { compactAt: () => compact.threshold() } : {}),
289
309
  canRetry,
290
310
  renderer: (tool) => renderers.forTool(tool),
@@ -563,8 +583,11 @@ async function run(services, bridge, opts) {
563
583
  catch (e) {
564
584
  screen.log(red(`[error] ${errorText(e)}`));
565
585
  }
566
- if (!models?.current())
567
- screen.log(yellow(`No model is configured yet. Put ANTHROPIC_API_KEY=... (or OPENAI_API_KEY=...) in ${path.join(home, ".env")}, then restart jade.`));
586
+ if (!models?.current()) {
587
+ // /model(jade/models 提供)当场就能配好,不用重启;没有这条命令时只能改文件
588
+ const file = `put ANTHROPIC_API_KEY=... (or OPENAI_API_KEY=...) in ${path.join(home, ".env")}, then restart jade`;
589
+ screen.log(yellow(commands.list().some((c) => c.name === "model") ? `No model is configured yet. Type /model to set one up, or ${file}.` : `No model is configured yet. To set one up, ${file}.`));
590
+ }
568
591
  else if (!resumed)
569
592
  screen.log(dim("Type / for commands, esc to interrupt."));
570
593
  ready = true;
@@ -14,6 +14,24 @@ export function mentionAt(value, cursor) {
14
14
  }
15
15
  /** 补全后的文本:@ 引用加一个空格,含空白的路径加引号 */
16
16
  export const mentionText = (p) => (/\s/.test(p) ? `@"${p}"` : `@${p}`);
17
+ const prepared = new WeakMap();
18
+ function prepare(paths) {
19
+ let out = prepared.get(paths);
20
+ if (!out) {
21
+ out = paths.map((p) => {
22
+ const lower = p.toLowerCase();
23
+ const trimmed = lower.endsWith("/") ? lower.slice(0, -1) : lower;
24
+ return { lower, base: trimmed.slice(trimmed.lastIndexOf("/") + 1) };
25
+ });
26
+ prepared.set(paths, out);
27
+ }
28
+ return out;
29
+ }
30
+ /**
31
+ * 上一次排序时命中的全部候选(下标)。query 只是在后面追加了字符时,新的命中一定在这些里面
32
+ * (包含 q 加字符的也包含 q,按顺序出现的同理),只筛它们,不用再扫一遍全部路径
33
+ */
34
+ let last;
17
35
  /**
18
36
  * 按 query 给候选路径排序:文件名以 query 开头的最前,其次文件名包含、路径包含,
19
37
  * 最后是按顺序出现 query 每个字符的模糊匹配;同一档里短的在前
@@ -22,10 +40,11 @@ export function rankPaths(paths, query, limit) {
22
40
  const q = query.toLowerCase().replaceAll("\\", "/");
23
41
  if (!q)
24
42
  return paths.filter((p) => !p.slice(0, -1).includes("/")).slice(0, limit);
43
+ const info = prepare(paths);
44
+ const from = last && last.paths === paths && q.startsWith(last.q) ? last.hits : undefined;
25
45
  const scored = [];
26
- for (const p of paths) {
27
- const lower = p.toLowerCase();
28
- const base = lower.replace(/\/$/, "").split("/").pop();
46
+ const check = (i) => {
47
+ const { lower, base } = info[i];
29
48
  let score;
30
49
  if (lower.startsWith(q))
31
50
  score = 0;
@@ -38,11 +57,30 @@ export function rankPaths(paths, query, limit) {
38
57
  else if (subsequence(lower, q))
39
58
  score = 4;
40
59
  else
60
+ return;
61
+ scored.push({ i, score });
62
+ };
63
+ if (from)
64
+ for (const i of from)
65
+ check(i);
66
+ else
67
+ for (let i = 0; i < paths.length; i++)
68
+ check(i);
69
+ last = { paths, q, hits: scored.map((s) => s.i) };
70
+ const cmp = (a, b) => a.score - b.score || paths[a.i].length - paths[b.i].length || paths[a.i].localeCompare(paths[b.i]);
71
+ // 只要前 limit 条:命中常有上万条,全部排序(localeCompare 很慢)每按一个键要十几毫秒
72
+ const top = [];
73
+ for (const s of scored) {
74
+ if (top.length === limit && cmp(s, top[limit - 1]) >= 0)
41
75
  continue;
42
- scored.push({ p, score });
76
+ let at = top.length;
77
+ while (at > 0 && cmp(s, top[at - 1]) < 0)
78
+ at--;
79
+ top.splice(at, 0, s);
80
+ if (top.length > limit)
81
+ top.pop();
43
82
  }
44
- scored.sort((a, b) => a.score - b.score || a.p.length - b.p.length || a.p.localeCompare(b.p));
45
- return scored.slice(0, limit).map((s) => s.p);
83
+ return top.map((s) => paths[s.i]);
46
84
  }
47
85
  function subsequence(text, q) {
48
86
  let i = 0;
@@ -376,7 +376,9 @@ export function transcript(opts) {
376
376
  else if (d.reason === "aborted")
377
377
  screen.log(yellow("[interrupted]"));
378
378
  else if (d.reason === "max_tokens") {
379
- const limit = process.env.JADE_MAX_TOKENS ? `${process.env.JADE_MAX_TOKENS} tokens` : "the provider's default";
379
+ // 不读 process.env:写在 $JADE_HOME/.env 里的只有 models 插件知道
380
+ const max = opts.maxTokens?.();
381
+ const limit = max ? `${max} tokens` : "the provider's default";
380
382
  screen.log(yellow(`[reply cut off at the output limit (${limit}); raise it with /model if this keeps happening]`));
381
383
  }
382
384
  else if (d.reason !== "end")
@@ -4,8 +4,7 @@
4
4
  //
5
5
  // 不用 JSX:插件以 .ts 源码加载,Node 的类型剥离不处理 .tsx,所以组件用 createElement(h)写。
6
6
  import { writeSync } from "node:fs";
7
- import { Box, render, Text, useBoxMetrics, useCursor, useInput, usePaste, useWindowSize, } from "ink";
8
- import { createContext, createElement as h, useContext, useEffect, useReducer, useRef, useState, useSyncExternalStore, } from "react";
7
+ import { Box, render, Text, useBoxMetrics, useCursor, useInput, usePaste, useWindowSize, createContext, createElement as h, useContext, useEffect, useReducer, useRef, useState, useSyncExternalStore, } from "./vendor.js";
9
8
  import stringWidth from "string-width";
10
9
  import wrapAnsi from "wrap-ansi";
11
10
  import { mentionAt, mentionText, rankPaths } from "./mentions.js";