@uncarved/jade 0.1.1 → 0.1.2

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 +198 -192
  2. package/dist/distro.d.ts +8 -1
  3. package/dist/distro.js +2 -1
  4. package/dist/distro.js.map +1 -1
  5. package/dist/index.d.ts +1 -0
  6. package/dist/index.js +1 -0
  7. package/dist/index.js.map +1 -1
  8. package/dist/main.js +52 -12
  9. package/dist/main.js.map +1 -1
  10. package/dist/mcp.js +1 -1
  11. package/dist/mcp.js.map +1 -1
  12. package/dist/plugins/policy.d.ts +21 -5
  13. package/dist/plugins/policy.js +46 -9
  14. package/dist/plugins/policy.js.map +1 -1
  15. package/dist/plugins/prompts.d.ts +8 -0
  16. package/dist/plugins/prompts.js +16 -2
  17. package/dist/plugins/prompts.js.map +1 -1
  18. package/dist/plugins/review.d.ts +16 -0
  19. package/dist/plugins/review.js +92 -0
  20. package/dist/plugins/review.js.map +1 -0
  21. package/dist/provider.d.ts +1 -1
  22. package/dist/provider.js +7 -3
  23. package/dist/provider.js.map +1 -1
  24. package/dist/tools/task.d.ts +4 -0
  25. package/dist/tools/task.js +4 -1
  26. package/dist/tools/task.js.map +1 -1
  27. package/dist/ui/configure.js +55 -46
  28. package/dist/ui/configure.js.map +1 -1
  29. package/dist/ui/markdown.d.ts +17 -0
  30. package/dist/ui/markdown.js +204 -0
  31. package/dist/ui/markdown.js.map +1 -0
  32. package/dist/ui/render.d.ts +2 -0
  33. package/dist/ui/render.js +10 -0
  34. package/dist/ui/render.js.map +1 -1
  35. package/dist/ui/screen.d.ts +3 -0
  36. package/dist/ui/screen.js +3 -0
  37. package/dist/ui/screen.js.map +1 -1
  38. package/dist/ui/transcript.js +56 -20
  39. package/dist/ui/transcript.js.map +1 -1
  40. package/dist/ui/tui.js +76 -25
  41. package/dist/ui/tui.js.map +1 -1
  42. package/package.json +5 -5
  43. package/src/distro.ts +7 -2
  44. package/src/index.ts +1 -0
  45. package/src/main.ts +53 -15
  46. package/src/mcp.ts +1 -1
  47. package/src/plugins/policy.ts +53 -12
  48. package/src/plugins/prompts.ts +21 -2
  49. package/src/plugins/review.ts +109 -0
  50. package/src/provider.ts +8 -4
  51. package/src/tools/task.ts +5 -1
  52. package/src/ui/configure.ts +54 -43
  53. package/src/ui/markdown.ts +219 -0
  54. package/src/ui/render.ts +9 -0
  55. package/src/ui/screen.ts +3 -0
  56. package/src/ui/transcript.ts +59 -23
  57. package/src/ui/tui.tsx +73 -23
package/src/ui/tui.tsx CHANGED
@@ -26,6 +26,7 @@ import stringWidth from "string-width";
26
26
  import wrapAnsi from "wrap-ansi";
27
27
  import { mentionAt, mentionText, rankPaths } from "../mentions.ts";
28
28
  import { edit, empty, layout, type EditState } from "./editor.ts";
29
+ import { markdownStream, type MdLine } from "./markdown.ts";
29
30
  import {
30
31
  dim,
31
32
  type Choice,
@@ -78,7 +79,7 @@ interface UiState {
78
79
  /** 对话内容变化时递增,让界面重画 */
79
80
  version: number;
80
81
  /** 还没结束的正文:完整的行已经移进对话,这里只有最后不完整的一行(思考是对话里的一项,见 Thinking) */
81
- live?: { text: string };
82
+ live?: { text: string; /** Markdown 里攒着的部分(还没结束的表格),显示在 text 前面 */ pending?: MdLine[] };
82
83
  /** 对话区离底部滚上去了多少行;0 表示跟着最新的输出 */
83
84
  scroll: number;
84
85
  busy?: { label: string; since: number };
@@ -112,8 +113,14 @@ interface Line {
112
113
  open?: boolean;
113
114
  /** 之后用 amend 补充内容时用的名字 */
114
115
  id?: string;
116
+ /** 按宽度现排的内容(Markdown 表格、分隔线),代替 text */
117
+ render?(width: number): string[];
115
118
  }
116
119
 
120
+ /** Markdown 排好的一行折成屏幕上的行 */
121
+ const mdRows = (l: MdLine, width: number) =>
122
+ typeof l === "string" ? wrap(l, width) : l.render(width);
123
+
117
124
  /** 按宽度截成一行,截掉了内容时以 … 结尾;留出两列给 … 和展开标记 */
118
125
  function oneRow(text: string, width: number): { row: string; cut: boolean } {
119
126
  const rows = wrap(text, Math.max(1, width - 3));
@@ -170,6 +177,7 @@ function thinkingRow(t: Thinking, width: number): string {
170
177
  }
171
178
 
172
179
  function wrapLine(l: Line, width: number): string[] {
180
+ if (l.render) return l.render(width);
173
181
  if (l.think) {
174
182
  if (!foldable(l, width)) return [thinkingRow(l.think, width)];
175
183
  if (!l.open) return [`${thinkingRow(l.think, width)}${dim(" ▸")}`];
@@ -355,6 +363,12 @@ export interface TuiOptions {
355
363
  /** 开关鼠标事件上报(SGR 格式),用来接收滚轮和点击;开着时终端里选中文字要按住 Shift */
356
364
  const MOUSE_ON = "\x1b[?1000h\x1b[?1006h";
357
365
  const MOUSE_OFF = "\x1b[?1006l\x1b[?1000l";
366
+ /**
367
+ * 光标样式:闪烁的竖线(DECSCUSR 5,再加上 ATT610 的"开始闪烁"给只认它的终端),一眼能看出输入框有焦点;
368
+ * 退出时还原成终端自己的默认样式。光标什么时候显示仍由 Ink 管(只有输入框或输入对话框有焦点时才显示)
369
+ */
370
+ const CURSOR_BLINK = "\x1b[5 q\x1b[?12h";
371
+ const CURSOR_RESET = "\x1b[0 q";
358
372
 
359
373
  export function tuiScreen(opts: TuiOptions): Screen {
360
374
  const stdout = opts.io?.stdout ?? process.stdout;
@@ -381,7 +395,16 @@ export function tuiScreen(opts: TuiOptions): Screen {
381
395
  thinking = undefined;
382
396
  };
383
397
 
384
- /** 推进正文:已经完整的行移进对话,只把最后不完整的一行留在底部;all 时全部移进去,连同思考一起结束 */
398
+ /** 正文按 Markdown 排版;一段正文结束时换一个新的,清掉代码块、表格的状态 */
399
+ let md = markdownStream();
400
+ const appendMd = (lines: MdLine[]) => {
401
+ for (const l of lines) {
402
+ if (typeof l === "string") store.append([l]);
403
+ else store.append([""], { render: l.render });
404
+ }
405
+ };
406
+
407
+ /** 推进正文:已经完整的行排版后移进对话,只把最后不完整的一行留在底部;all 时全部移进去,连同思考一起结束 */
385
408
  const flushLive = (all = false) => {
386
409
  if (all) endThinking();
387
410
  const live = store.state.live;
@@ -389,8 +412,12 @@ export function tuiScreen(opts: TuiOptions): Screen {
389
412
  const lines = live.text.split("\n");
390
413
  const rest = all ? "" : lines.pop()!;
391
414
  if (all && lines.at(-1) === "") lines.pop();
392
- store.append(lines);
393
- store.update({ live: rest || !all ? { text: rest } : undefined });
415
+ appendMd(lines.flatMap((l) => md.push(l)));
416
+ if (all) {
417
+ appendMd(md.flush());
418
+ md = markdownStream();
419
+ }
420
+ store.update({ live: rest || !all ? { text: rest, pending: md.pending() } : undefined });
394
421
  };
395
422
 
396
423
  const ask = <T,>(dialog: Dialog) =>
@@ -429,11 +456,11 @@ export function tuiScreen(opts: TuiOptions): Screen {
429
456
  : {}),
430
457
  },
431
458
  );
432
- stdout.write(MOUSE_ON);
459
+ stdout.write(MOUSE_ON + CURSOR_BLINK);
433
460
  // 没走 close 就退出(process.exit)时也要把终端还原,否则会留在备用屏幕、鼠标上报也开着
434
461
  const restore = () => {
435
462
  try {
436
- writeSync(1, `${MOUSE_OFF}\x1b[?1049l\x1b[?25h`);
463
+ writeSync(1, `${MOUSE_OFF}${CURSOR_RESET}\x1b[?1049l\x1b[?25h`);
437
464
  } catch {}
438
465
  };
439
466
  if (!opts.io) process.on("exit", restore);
@@ -527,6 +554,7 @@ export function tuiScreen(opts: TuiOptions): Screen {
527
554
  clear() {
528
555
  clearTimeout(thinking?.timer);
529
556
  thinking = undefined;
557
+ md = markdownStream();
530
558
  store.clear();
531
559
  },
532
560
  async close() {
@@ -534,7 +562,7 @@ export function tuiScreen(opts: TuiOptions): Screen {
534
562
  store.update({ live: undefined, busy: undefined, progress: [] }, true);
535
563
  // 等最后一帧画完再卸载;卸载时 Ink 退出备用屏幕
536
564
  await new Promise((r) => setTimeout(r, 50));
537
- stdout.write(MOUSE_OFF);
565
+ stdout.write(MOUSE_OFF + CURSOR_RESET);
538
566
  instance.unmount();
539
567
  await instance.waitUntilExit();
540
568
  process.off("exit", restore);
@@ -575,6 +603,15 @@ function App({
575
603
  }) {
576
604
  const state = useSyncExternalStore(store.subscribe, store.get);
577
605
  const dialog = state.dialogs[0];
606
+ // 运行中每 100ms 整体重画一次,推动转圈动画。不能让 Spinner 自己重画:
607
+ // Ink 只在输入框也跟着提交的那一帧保留光标,否则把光标藏起来,运行中光标就不见了
608
+ const [, tick] = useReducer((n: number) => n + 1, 0);
609
+ const busy = state.busy !== undefined;
610
+ useEffect(() => {
611
+ if (!busy) return;
612
+ const t = setInterval(tick, 100);
613
+ return () => clearInterval(t);
614
+ }, [busy]);
578
615
  const { columns, rows } = useScreenSize();
579
616
 
580
617
  // 对话区占掉标题和底部之外的全部高度,只画能看见的那几行
@@ -584,6 +621,7 @@ function App({
584
621
  const done = store.lines(width);
585
622
  // 还没结束的流式块和正在运行的工具的进度:思考只显示最新的一行,进度每个工具一行
586
623
  const live = [
624
+ ...(state.live?.pending ?? []).flatMap((l) => mdRows(l, width)),
587
625
  ...(state.live?.text ? wrap(state.live.text, width) : []),
588
626
  ...state.progress.map((p) => oneRow(p.line, width).row),
589
627
  ];
@@ -801,12 +839,9 @@ function dialogKey(d: Dialog): number {
801
839
 
802
840
  const FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
803
841
 
842
+ /** 转圈动画由 App 每 100ms 整体重画一次推动(见 App 里的 tick),这里只按当前时间画 */
804
843
  function Spinner({ label, since }: { label: string; since: number }) {
805
- const [now, setNow] = useState(Date.now());
806
- useEffect(() => {
807
- const t = setInterval(() => setNow(Date.now()), 100);
808
- return () => clearInterval(t);
809
- }, []);
844
+ const now = Date.now();
810
845
  const seconds = Math.floor((now - since) / 1000);
811
846
  return (
812
847
  <Box marginTop={1}>
@@ -861,13 +896,11 @@ function StatusBar({ status, width }: { status: StatusInfo; width: number }) {
861
896
  const modeColor =
862
897
  status.mode === "yolo"
863
898
  ? "red"
864
- : status.mode === "auto-edit"
899
+ : status.mode === "auto"
865
900
  ? "yellow"
866
- : status.mode === "readonly"
867
- ? "blue"
868
- : status.mode === "plan"
869
- ? "magenta"
870
- : "green";
901
+ : status.mode === "plan"
902
+ ? "magenta"
903
+ : "green";
871
904
  const speed = status.speed;
872
905
  // 窄的时候依次:提示变短、去掉提示、用量变短、去掉缓存、模式变短、去掉用量、上下文变短、去掉速度
873
906
  const segments: Segment[] = [
@@ -1023,7 +1056,6 @@ function EditableText({
1023
1056
  const ref = useRef<DOMElement>(null);
1024
1057
  // 只用来在布局变化后重画一次、重新计算光标位置;它给的是相对父元素的坐标,光标要的是相对整个输出
1025
1058
  const metrics = useBoxMetrics(ref);
1026
- const { setCursorPosition } = useCursor();
1027
1059
  const shown = mask ? "•".repeat([...state.value].length) : state.value;
1028
1060
  const cursor = mask
1029
1061
  ? [...state.value.slice(0, state.cursor)].length
@@ -1040,17 +1072,25 @@ function EditableText({
1040
1072
 
1041
1073
  // 光标位置要在渲染时设:useCursor 在这次提交时交给 Ink,跟这一帧一起画出来。
1042
1074
  // 放在 useEffect 里会晚一帧,光标总是停在上一次按键的位置(只移动光标时最明显)。
1043
- // 这里用的是上一次的布局;输入框挪了位置时 useBoxMetrics 会再触发一次渲染来纠正
1075
+ // 这里用的是上一次的布局,提交后要核对一遍:
1076
+ // - 输入框相对父元素挪了位置时 useBoxMetrics 会再触发一次渲染
1077
+ // - 只有祖先挪了位置时(窗口尺寸变化后整帧重画那一帧上下各垫一行,下一帧再去掉)它看不出来,
1078
+ // 光标会停在比输入行低一行的地方,要自己比一下整体位置
1044
1079
  const at =
1045
1080
  active && metrics.hasMeasured && ref.current
1046
1081
  ? origin(ref.current)
1047
1082
  : undefined;
1048
- setCursorPosition(
1049
- at && { x: at.x + prefix.length + col, y: at.y + row },
1050
- );
1083
+ const position = at && { x: at.x + prefix.length + col, y: at.y + row };
1084
+ const [, recheck] = useReducer((n: number) => n + 1, 0);
1085
+ useEffect(() => {
1086
+ if (!at || !ref.current) return;
1087
+ const now = origin(ref.current);
1088
+ if (now.x !== at.x || now.y !== at.y) recheck();
1089
+ });
1051
1090
 
1052
1091
  return (
1053
1092
  <Box ref={ref} flexDirection="column">
1093
+ {active ? <CursorAt position={position} /> : null}
1054
1094
  {state.value === "" && placeholder ? (
1055
1095
  <Text wrap="truncate-end">
1056
1096
  <Text color="cyan">{prefix}</Text>
@@ -1074,6 +1114,16 @@ function EditableText({
1074
1114
  );
1075
1115
  }
1076
1116
 
1117
+ /**
1118
+ * 终端光标只有一个,每个 useCursor 每次提交都会设一遍。没有焦点的输入框(弹出输入框时下面的主输入框)
1119
+ * 不能用它,否则它设的"不显示"会盖掉有焦点的那个,所以只在有焦点时挂上
1120
+ */
1121
+ function CursorAt({ position }: { position: { x: number; y: number } | undefined }) {
1122
+ const { setCursorPosition } = useCursor();
1123
+ setCursorPosition(position);
1124
+ return null;
1125
+ }
1126
+
1077
1127
  /** 元素相对整个动态输出(Static 之外的部分)左上角的位置 */
1078
1128
  function origin(node: DOMElement): { x: number; y: number } {
1079
1129
  let x = 0;