@uncarved/jade 0.4.3 → 0.5.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 (39) hide show
  1. package/README.md +317 -279
  2. package/dist/api.d.ts +1 -1
  3. package/dist/api.js +1 -1
  4. package/dist/session.d.ts +4 -2
  5. package/dist/session.js +54 -6
  6. package/dist/session.js.map +1 -1
  7. package/package.json +3 -3
  8. package/plugins/keys/index.ts +2 -2
  9. package/plugins/memory/index.ts +6 -4
  10. package/plugins/memory/memory.ts +101 -31
  11. package/plugins/models/index.ts +67 -9
  12. package/plugins/models/provider.ts +81 -24
  13. package/plugins/serve/index.ts +229 -0
  14. package/plugins/serve/plugin.json +8 -0
  15. package/plugins/shell/index.ts +35 -1
  16. package/plugins/shell/shell.ts +5 -0
  17. package/plugins/task/index.ts +3 -1
  18. package/plugins/task/subagent.ts +49 -3
  19. package/plugins/tui/frontend.ts +73 -4
  20. package/plugins/tui/screen.ts +23 -0
  21. package/plugins/tui/tui.ts +176 -11
  22. package/plugins-dist/keys/index.js +1 -1
  23. package/plugins-dist/memory/index.js +7 -3
  24. package/plugins-dist/memory/memory.js +81 -34
  25. package/plugins-dist/models/index.js +62 -7
  26. package/plugins-dist/models/provider.js +47 -14
  27. package/plugins-dist/serve/index.js +238 -0
  28. package/plugins-dist/serve/plugin.json +14 -0
  29. package/plugins-dist/shell/index.js +33 -0
  30. package/plugins-dist/shell/shell.js +3 -0
  31. package/plugins-dist/task/index.js +3 -1
  32. package/plugins-dist/task/subagent.js +47 -2
  33. package/plugins-dist/tui/frontend.js +67 -3
  34. package/plugins-dist/tui/screen.js +3 -0
  35. package/plugins-dist/tui/tui.js +132 -9
  36. package/plugins-dist/tui/vendor.js +1388 -1088
  37. package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
  38. package/src/api.ts +1 -1
  39. package/src/session.ts +57 -5
@@ -128,7 +128,7 @@ export class Store {
128
128
  #repaintTimer;
129
129
  paused = false;
130
130
  constructor(status, size) {
131
- this.state = { version: 0, scroll: 0, progress: [], queued: [], dialogs: [], status, size };
131
+ this.state = { version: 0, scroll: 0, progress: [], queued: [], dialogs: [], jobs: [], status, size };
132
132
  }
133
133
  subscribe = (fn) => {
134
134
  this.#listeners.add(fn);
@@ -161,6 +161,12 @@ export class Store {
161
161
  this.paused = true;
162
162
  this.#repaintTimer = setTimeout(() => this.#repaintNow(), REPAINT_GAP_MS);
163
163
  }
164
+ openJobs() {
165
+ if (this.state.dialogs.length)
166
+ return;
167
+ const d = { kind: "jobs", resolve: () => this.update({ dialogs: this.state.dialogs.filter((x) => x !== d) }, true) };
168
+ this.update({ dialogs: [...this.state.dialogs, d] }, true);
169
+ }
164
170
  #repaintNow(patch = {}) {
165
171
  clearTimeout(this.#repaintTimer);
166
172
  this.paused = false;
@@ -494,6 +500,13 @@ export function tuiScreen(opts) {
494
500
  if (!found)
495
501
  store.append([` ${text}`], o?.details !== undefined ? { details: o.details } : undefined);
496
502
  },
503
+ note(id, text) {
504
+ if (store.amend(id, (l) => void (l.text = text)))
505
+ return;
506
+ flushLive(true);
507
+ store.update({ live: undefined });
508
+ store.append([text], { id });
509
+ },
497
510
  progress(id, line) {
498
511
  const rest = store.state.progress.filter((p) => p.id !== id);
499
512
  store.update({
@@ -553,6 +566,10 @@ export function tuiScreen(opts) {
553
566
  queued(items) {
554
567
  store.update({ queued: [...items] });
555
568
  },
569
+ jobs(jobs) {
570
+ store.update({ jobs });
571
+ },
572
+ showJobs: () => store.openJobs(),
556
573
  draft: {
557
574
  text: () => draft.current?.get() ?? "",
558
575
  insert: (text) => draft.current?.apply((s) => edit.insert(s, text.replace(/\r\n?/g, `
@@ -715,9 +732,10 @@ function App({
715
732
  draft
716
733
  }) {
717
734
  const state = useSyncExternalStore(store.subscribe, store.get);
718
- const dialog = state.dialogs[0];
735
+ const dialog = state.dialogs.find((d) => d.kind !== "jobs") ?? state.dialogs[0];
719
736
  const columns = Math.max(1, state.size.columns - 1);
720
737
  const rows = state.size.rows;
738
+ const statusWidth = Math.max(10, columns - 2);
721
739
  const compact = rows < COMPACT_ROWS;
722
740
  const [ticks, tickNow] = useReducer((n) => n + 1, 0);
723
741
  const tick = () => store.paused || tickNow();
@@ -729,7 +747,7 @@ function App({
729
747
  return () => clearInterval(t);
730
748
  }, [busy]);
731
749
  const now = Date.now();
732
- const mood = moodOf({ busy: state.busy?.label, asking: dialog !== undefined, doneAt: state.doneAt }, now);
750
+ const mood = moodOf({ busy: state.busy?.label, asking: dialog !== undefined && dialog.kind !== "jobs", doneAt: state.doneAt }, now);
733
751
  const mascot = useRef(undefined);
734
752
  mascot.current ??= new Mascot;
735
753
  const showBot = !compact && columns >= 50;
@@ -814,8 +832,15 @@ function App({
814
832
  return store.repaint();
815
833
  if (key.ctrl && input === "o")
816
834
  return store.toggleLatest(start, view);
835
+ if (key.ctrl && input === "t")
836
+ return store.openJobs();
817
837
  const mouse = mouseEvent(input);
818
838
  if (mouse) {
839
+ if (mouse.button === 0 && mouse.press && mouse.y === rows - 2) {
840
+ const cols = jobsColumns(statusSegments(state.status, state.jobs, statusWidth));
841
+ if (cols && mouse.x >= cols[0] && mouse.x <= cols[1])
842
+ return store.openJobs();
843
+ }
819
844
  const at = (clamp) => {
820
845
  const i = mouse.y - top;
821
846
  if (!clamp && (i < 0 || i >= shown.length))
@@ -873,7 +898,7 @@ function App({
873
898
  pressed.current = undefined;
874
899
  setSelection(undefined);
875
900
  }
876
- if (dialog?.kind === "select")
901
+ if (dialog?.kind === "select" || dialog?.kind === "jobs")
877
902
  return;
878
903
  if (key.pageUp)
879
904
  store.scrollBy(page, max);
@@ -903,7 +928,7 @@ function App({
903
928
  const cols = selection && selectedCols(selection, start + i - front);
904
929
  return h(Text, { key: i, wrap: "truncate-end" }, cols ? highlight(l || " ", ...cols) : l || " ");
905
930
  }), scrolled ? h(Text, { dimColor: true, wrap: "truncate-end" }, `── ↓ ${scroll} more lines · wheel / PgDn to scroll · ctrl+end to jump to the bottom ──`) : null), h(Box, { flexDirection: "column", flexShrink: 0 }, state.busy && !dialog ? h(Spinner, { label: state.busy.label, since: state.busy.since }) : null, dialog ? null : (state.status.panels ?? []).flatMap((p) => p.text.split(`
906
- `).map((l, i) => h(Text, { key: `${p.plugin}:${i}`, wrap: "truncate-end" }, " ", l || " "))), state.queued.map((q, i) => h(Text, { key: i, dimColor: true, wrap: "truncate-end" }, " ↳ queued: ", q.replace(/\s+/g, " "))), dialog ? dialog.kind === "select" ? h(SelectDialog, { key: dialogKey(dialog), dialog }) : h(InputDialog, { key: dialogKey(dialog), dialog }) : null, h(Prompt, {
931
+ `).map((l, i) => h(Text, { key: `${p.plugin}:${i}`, wrap: "truncate-end" }, " ", l || " "))), state.queued.map((q, i) => h(Text, { key: i, dimColor: true, wrap: "truncate-end" }, " ↳ queued: ", q.replace(/\s+/g, " "))), dialog ? dialog.kind === "select" ? h(SelectDialog, { key: dialogKey(dialog), dialog }) : dialog.kind === "jobs" ? h(JobsDialog, { key: dialogKey(dialog), dialog, jobs: state.jobs }) : h(InputDialog, { key: dialogKey(dialog), dialog }) : null, h(Prompt, {
907
932
  commands,
908
933
  handlers,
909
934
  history,
@@ -917,7 +942,7 @@ function App({
917
942
  flash: copied,
918
943
  place: inputPlace,
919
944
  onCopy: copyText
920
- }), h(StatusBar, { status: state.status, width: Math.max(10, columns - 2) }))))));
945
+ }), h(StatusBar, { status: state.status, jobs: state.jobs, width: statusWidth }))))));
921
946
  }
922
947
  function Bot({ pixels }) {
923
948
  return h(Box, { flexDirection: "column", flexShrink: 0, marginRight: 1 }, ...cells(pixels).map((row, i) => h(Text, { key: i }, ...row.map((r, j) => h(Text, { key: j, color: r.fg, backgroundColor: r.bg }, r.text)))));
@@ -971,7 +996,7 @@ export function fitSegments(segments, width) {
971
996
  }
972
997
  return kept;
973
998
  }
974
- function StatusBar({ status, width }) {
999
+ function statusSegments(status, jobs, width) {
975
1000
  const ctx = status.context;
976
1001
  const pct = ctx ? Math.round(ctx.used / ctx.limit * 100) : 0;
977
1002
  const nearFull = pct >= 80;
@@ -987,8 +1012,10 @@ function StatusBar({ status, width }) {
987
1012
  };
988
1013
  const kept = (status.plugins ?? []).filter((p) => typeof p.text !== "string" && p.text.keep);
989
1014
  const others = (status.plugins ?? []).filter((p) => !kept.includes(p));
1015
+ const running = runningJobs(jobs);
990
1016
  const segments = [
991
1017
  ...kept.map((p) => plugin(p, true)),
1018
+ ...running ? [{ text: `${running} · click or ctrl+t to view`, short: running, shortAt: 6.5, color: "cyan", jobs: true }] : [],
992
1019
  ctx ? {
993
1020
  text: `ctx ${tokens(ctx.used)}/${tokens(ctx.limit)} (${pct}%)`,
994
1021
  short: `ctx ${pct}%`,
@@ -1003,7 +1030,7 @@ function StatusBar({ status, width }) {
1003
1030
  dim: true
1004
1031
  }
1005
1032
  ] : [],
1006
- ...status.cache !== undefined ? [{ text: `cache ${Math.round(status.cache * 100)}%`, dropAt: 7, dim: true }] : [],
1033
+ ...status.cache !== undefined ? [{ text: `cache ${Math.round(status.cache * 1000) / 10}%`, dropAt: 7, dim: true }] : [],
1007
1034
  ...status.spent ? [
1008
1035
  {
1009
1036
  text: `session ${tokens(status.spent.input)} in / ${tokens(status.spent.output)} out`,
@@ -1022,7 +1049,22 @@ function StatusBar({ status, width }) {
1022
1049
  dim: true
1023
1050
  }
1024
1051
  ];
1025
- const shown = fitSegments(segments, width);
1052
+ return fitSegments(segments, width);
1053
+ }
1054
+ function jobsColumns(segments) {
1055
+ let x = 1;
1056
+ for (const [i, s] of segments.entries()) {
1057
+ if (i)
1058
+ x += 3;
1059
+ const w = visibleWidth(s.text);
1060
+ if (s.jobs)
1061
+ return [x, x + w - 1];
1062
+ x += w;
1063
+ }
1064
+ return;
1065
+ }
1066
+ function StatusBar({ status, jobs, width }) {
1067
+ const shown = statusSegments(status, jobs, width);
1026
1068
  return h(Box, { paddingX: 1 }, h(Text, { wrap: "truncate-end" }, shown.map((s, i) => h(Text, { key: i }, i ? h(Text, { dimColor: true }, " · ") : null, h(Text, { color: s.color, dimColor: s.dim }, s.text)))));
1027
1069
  }
1028
1070
  function Hint({ variants, width }) {
@@ -1643,6 +1685,87 @@ function SelectDialog({
1643
1685
  variants: filtering ? ["↑↓ select · type to filter · enter confirm · esc clear/cancel", "↑↓ · enter · esc"] : ["↑↓ select · enter confirm · esc cancel", "↑↓ · enter · esc"]
1644
1686
  }));
1645
1687
  }
1688
+ export function runningJobs(jobs) {
1689
+ const counts = new Map;
1690
+ for (const j of jobs)
1691
+ if (!j.done)
1692
+ counts.set(j.kind, (counts.get(j.kind) ?? 0) + 1);
1693
+ return [...counts].map(([kind, n]) => `${n} ${kind}${n > 1 ? "s" : ""}`).join(" · ");
1694
+ }
1695
+ const JOB_TAIL_LINES = 2000;
1696
+ function JobsDialog({ dialog, jobs }) {
1697
+ const selection = useContext(SelectionContext);
1698
+ const [getSelected, setSelected] = useLive(0);
1699
+ const [getViewing, setViewing] = useLive(undefined);
1700
+ const [getBack, setBack] = useLive(0);
1701
+ const { rows, columns } = useScreenSize();
1702
+ const width = Math.max(10, columns - 4);
1703
+ const viewing = jobs.find((j) => j.id === getViewing());
1704
+ const selected = Math.min(getSelected(), Math.max(0, jobs.length - 1));
1705
+ const room = Math.max(3, rows - 14);
1706
+ const lines = viewing ? wrap(viewing.output.replace(/\n+$/, "").split(`
1707
+ `).slice(-JOB_TAIL_LINES).join(`
1708
+ `), width).map(visibleRow) : [];
1709
+ const maxBack = Math.max(0, lines.length - room);
1710
+ const back = Math.min(getBack(), maxBack);
1711
+ const scrolling = useRef({ maxBack, room });
1712
+ scrolling.current = { maxBack, room };
1713
+ useInput((input, key) => {
1714
+ if (key.ctrl && input === "c" && selection.active())
1715
+ return selection.clear();
1716
+ const current = jobs.find((j) => j.id === getViewing());
1717
+ const target = current ?? jobs[Math.min(getSelected(), jobs.length - 1)];
1718
+ if (input === "x" && target && !target.done && target.stop)
1719
+ return target.stop();
1720
+ if (current) {
1721
+ const { maxBack, room } = scrolling.current;
1722
+ const by = (n) => setBack(Math.max(0, Math.min(maxBack, Math.min(getBack(), maxBack) + n)));
1723
+ if (key.pageUp)
1724
+ return by(room - 1);
1725
+ if (key.pageDown)
1726
+ return by(-(room - 1));
1727
+ if (key.upArrow || input === "k")
1728
+ return by(1);
1729
+ if (key.downArrow || input === "j")
1730
+ return by(-1);
1731
+ if (key.escape || key.leftArrow || key.ctrl && input === "c") {
1732
+ setBack(0);
1733
+ return setViewing(undefined);
1734
+ }
1735
+ return;
1736
+ }
1737
+ const n = jobs.length;
1738
+ if (key.upArrow || input === "k")
1739
+ return n && setSelected((Math.min(getSelected(), n - 1) - 1 + n) % n);
1740
+ if (key.downArrow || input === "j")
1741
+ return n && setSelected((Math.min(getSelected(), n - 1) + 1) % n);
1742
+ if ((key.return || key.rightArrow) && target) {
1743
+ setBack(0);
1744
+ return setViewing(target.id);
1745
+ }
1746
+ if (key.escape || key.ctrl && input === "c")
1747
+ return dialog.resolve(undefined);
1748
+ });
1749
+ const mark = (j) => j.done ? h(Text, { dimColor: true }, "○ ") : h(Text, { color: "green" }, "● ");
1750
+ const canStop = (j) => j !== undefined && !j.done && j.stop !== undefined;
1751
+ const frame = { flexDirection: "column", borderStyle: "round", borderColor: "cyan", paddingX: 1, marginTop: 1 };
1752
+ if (viewing) {
1753
+ const shown = lines.slice(lines.length - room - back, lines.length - back);
1754
+ return h(Box, frame, h(Text, { wrap: "truncate-end" }, mark(viewing), h(Text, { bold: true }, `${viewing.kind} · ${viewing.title}`), viewing.status ? h(Text, { dimColor: true }, ` · ${viewing.status}`) : null), lines.length > room + back ? h(Text, { dimColor: true }, ` ↑ ${lines.length - room - back} more lines`) : null, lines.length === 0 ? h(Text, { dimColor: true }, " (no output yet)") : null, shown.map((l, i) => h(Text, { key: i, wrap: "truncate-end" }, l || " ")), back > 0 ? h(Text, { dimColor: true }, ` ↓ ${back} more lines`) : null, h(Hint, {
1755
+ width,
1756
+ variants: [`PgUp/PgDn ↑↓ scroll${canStop(viewing) ? " · x stop" : ""} · esc back`, `↑↓${canStop(viewing) ? " · x" : ""} · esc`]
1757
+ }));
1758
+ }
1759
+ const visible = roomFor(rows, VISIBLE);
1760
+ const start = around(selected, jobs.length, visible);
1761
+ return h(Box, frame, h(Text, { bold: true }, "Background tasks"), jobs.length === 0 ? h(Text, { dimColor: true }, "Nothing is running in the background.") : null, start > 0 ? h(Text, { dimColor: true }, ` ↑ ${start} more`) : null, jobs.slice(start, start + visible).map((j, k) => {
1762
+ const on = start + k === selected;
1763
+ return h(Text, { key: j.id, wrap: "truncate-end" }, h(Text, { color: on ? "cyan" : undefined, bold: on }, on ? "❯ " : " "), mark(j), h(Text, { color: on ? "cyan" : undefined, bold: on }, `${j.kind} · ${j.title}`), j.status ? h(Text, { dimColor: true }, ` · ${j.status}`) : null);
1764
+ }), start + visible < jobs.length ? h(Text, { dimColor: true }, ` ↓ ${jobs.length - start - visible} more`) : null, h(Hint, {
1765
+ width,
1766
+ variants: jobs.length ? [`↑↓ select · enter view output${canStop(jobs[selected]) ? " · x stop" : ""} · esc close`, `↑↓ · enter${canStop(jobs[selected]) ? " · x" : ""} · esc`] : ["esc close"]
1767
+ }));
1768
+ }
1646
1769
  function InputDialog({
1647
1770
  dialog
1648
1771
  }) {