@uncarved/jade 0.5.1 → 0.5.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 (78) hide show
  1. package/README.md +14 -11
  2. package/dist/api.d.ts +1 -1
  3. package/dist/api.js +1 -1
  4. package/dist/install.js +5 -0
  5. package/dist/install.js.map +1 -1
  6. package/package.json +6 -6
  7. package/plugins/approval/index.ts +129 -13
  8. package/plugins/approval/plugin.json +4 -1
  9. package/plugins/approval/policy.ts +127 -7
  10. package/plugins/approval/review.ts +188 -45
  11. package/plugins/ask/index.ts +29 -1
  12. package/plugins/budget/index.ts +44 -6
  13. package/plugins/budget/plugin.json +1 -2
  14. package/plugins/compact/compact.ts +153 -39
  15. package/plugins/compact/index.ts +39 -5
  16. package/plugins/compact/plugin.json +5 -2
  17. package/plugins/cost/cache.ts +114 -0
  18. package/plugins/cost/index.ts +109 -51
  19. package/plugins/cost/plugin.json +3 -2
  20. package/plugins/memory/memory.ts +51 -28
  21. package/plugins/models/configure.ts +344 -125
  22. package/plugins/models/index.ts +44 -9
  23. package/plugins/models/price.ts +291 -0
  24. package/plugins/models/provider.ts +40 -8
  25. package/plugins/models/saved.ts +20 -4
  26. package/plugins/modes/index.ts +3 -4
  27. package/plugins/prompts/prompts.ts +5 -1
  28. package/plugins/task/index.ts +31 -10
  29. package/plugins/task/plugin.json +5 -2
  30. package/plugins/task/subagent.ts +21 -4
  31. package/plugins/todo/index.ts +62 -1
  32. package/plugins/todo/plugin.json +1 -1
  33. package/plugins/tui/frontend.ts +31 -5
  34. package/plugins/tui/index.ts +2 -1
  35. package/plugins/tui/plugin.json +1 -1
  36. package/plugins/tui/screen.ts +17 -5
  37. package/plugins/tui/transcript.ts +518 -507
  38. package/plugins/tui/tui.ts +212 -54
  39. package/plugins/withdraw/index.ts +111 -0
  40. package/plugins/withdraw/plugin.json +8 -0
  41. package/plugins-dist/approval/index.js +103 -9
  42. package/plugins-dist/approval/plugin.json +4 -1
  43. package/plugins-dist/approval/policy.js +112 -9
  44. package/plugins-dist/approval/review.js +141 -34
  45. package/plugins-dist/ask/index.js +28 -2
  46. package/plugins-dist/budget/index.js +30 -7
  47. package/plugins-dist/budget/plugin.json +1 -2
  48. package/plugins-dist/compact/compact.js +106 -32
  49. package/plugins-dist/compact/index.js +27 -3
  50. package/plugins-dist/compact/plugin.json +6 -2
  51. package/plugins-dist/cost/cache.js +100 -0
  52. package/plugins-dist/cost/index.js +87 -38
  53. package/plugins-dist/cost/plugin.json +5 -2
  54. package/plugins-dist/memory/memory.js +51 -32
  55. package/plugins-dist/models/configure.js +285 -105
  56. package/plugins-dist/models/index.js +33 -2
  57. package/plugins-dist/models/price.js +244 -0
  58. package/plugins-dist/models/provider.js +22 -4
  59. package/plugins-dist/models/saved.js +13 -3
  60. package/plugins-dist/modes/index.js +2 -3
  61. package/plugins-dist/prompts/prompts.js +5 -1
  62. package/plugins-dist/task/index.js +25 -7
  63. package/plugins-dist/task/plugin.json +5 -2
  64. package/plugins-dist/task/subagent.js +15 -6
  65. package/plugins-dist/todo/index.js +50 -0
  66. package/plugins-dist/todo/plugin.json +1 -1
  67. package/plugins-dist/tui/frontend.js +26 -4
  68. package/plugins-dist/tui/index.js +2 -1
  69. package/plugins-dist/tui/plugin.json +2 -1
  70. package/plugins-dist/tui/screen.js +7 -3
  71. package/plugins-dist/tui/transcript.js +13 -5
  72. package/plugins-dist/tui/tui.js +197 -50
  73. package/plugins-dist/tui/vendor.js +1388 -1088
  74. package/plugins-dist/withdraw/index.js +88 -0
  75. package/plugins-dist/withdraw/plugin.json +12 -0
  76. package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
  77. package/src/api.ts +1 -1
  78. package/src/install.ts +5 -0
@@ -32,7 +32,7 @@ const KEYS = [
32
32
  " send enter send · ctrl+j, alt+enter or \\ then enter: new line · tab completes /commands",
33
33
  " input ↑↓ history · ctrl+r search history · ↑ on the first line edits queued messages · esc twice or ctrl+c clears the input",
34
34
  " editing ctrl+←/→ or alt+b/f move by words · ctrl+w or alt+backspace delete a word · ctrl+a/e line start/end · ctrl+u/k delete to start/end",
35
- " running esc interrupt (twice to also move queued messages back to the input) · ctrl+c twice to exit · ctrl+t, /tasks or a click on the status bar lists sub-agents and background commands",
35
+ " running esc interrupt (a message the model has not answered yet goes back to the input; twice to also move queued messages back) · ctrl+c twice to exit · ctrl+t, /tasks or a click on the status bar lists sub-agents and background commands",
36
36
  " transcript pgup/pgdn scroll · ctrl+home/end top/bottom · ctrl+o expands the latest item, a click the one under it · drag to select and copy · ctrl+l redraw"
37
37
  ].join(`
38
38
  `);
@@ -122,7 +122,7 @@ export class ScreenBridge {
122
122
  select: async (req, signal) => {
123
123
  if (!this.screen)
124
124
  return;
125
- return this.screen.select(req.title, req.options.map((o) => ({ label: o.label, value: o.value, ...o.hint ? { hint: o.hint } : {} })), { ...req.detail ? { body: ansi(req.detail) } : {}, ...signal ? { signal } : {} });
125
+ return this.screen.select(req.title, req.options.map((o) => ({ label: o.label, value: o.value, ...o.hint ? { hint: o.hint } : {}, ...o.group ? { group: o.group } : {}, ...o.current ? { current: true } : {} })), { ...req.detail ? { body: ansi(req.detail) } : {}, ...signal ? { signal } : {} });
126
126
  },
127
127
  input: async (req, signal) => {
128
128
  if (!this.screen)
@@ -191,7 +191,22 @@ export class ScreenBridge {
191
191
  this.#applyJobs();
192
192
  };
193
193
  return {
194
- append: (text) => update({ output: (entry.output + ansi(text)).slice(-MAX_JOB_OUTPUT) }, true),
194
+ append: (text, detail) => {
195
+ const added = ansi(text);
196
+ let details = entry.details ?? [];
197
+ if (detail !== undefined) {
198
+ const full = ansi(detail);
199
+ const clipped = full.length > MAX_JOB_DETAIL ? `${full.slice(0, MAX_JOB_DETAIL)}
200
+ …` : full;
201
+ details = [...details, { line: lineCount(entry.output) + lineCount(added.replace(/\n+$/, "")), text: clipped }];
202
+ }
203
+ const joined = entry.output + added;
204
+ const output = joined.slice(-MAX_JOB_OUTPUT);
205
+ const dropped = lineCount(joined.slice(0, joined.length - output.length));
206
+ if (dropped)
207
+ details = details.flatMap((d) => d.line >= dropped ? [{ ...d, line: d.line - dropped }] : []);
208
+ update({ output, ...details.length || entry.details ? { details } : {} }, true);
209
+ },
195
210
  status: (text, done) => update({ status: String(text), done: done === true }),
196
211
  dispose: () => {
197
212
  const before = this.#jobs.length;
@@ -259,6 +274,9 @@ export class ScreenBridge {
259
274
  }
260
275
  }
261
276
  const MAX_JOB_OUTPUT = 1e5;
277
+ const MAX_JOB_DETAIL = 20000;
278
+ const lineCount = (s) => s.split(`
279
+ `).length - 1;
262
280
  const JOB_REDRAW_MS = 100;
263
281
  const noticeText = (message, level) => level === "error" ? red(`[error] ${message}`) : level === "warning" ? yellow(`[warning] ${message}`) : dim(message);
264
282
  function clipPanel(text) {
@@ -328,7 +346,7 @@ ${USAGE}
328
346
  `);
329
347
  return 2;
330
348
  }
331
- const { cwd, home, commands, keys, renderers, models, completions, index, steer } = services;
349
+ const { cwd, home, commands, keys, renderers, models, completions, index, steer, withdraw } = services;
332
350
  const sessions = opts.sessions;
333
351
  if (args.cont && !index) {
334
352
  process.stderr.write(`jade: --continue needs the jade/sessions plugin, which is not enabled
@@ -534,6 +552,10 @@ ${USAGE}
534
552
  interruptPending = true;
535
553
  for (const ac of commandAborts)
536
554
  ac.abort(new Error("interrupted by user"));
555
+ try {
556
+ if (current)
557
+ withdraw?.arm(current.id);
558
+ } catch {}
537
559
  current?.abort(new Error("interrupted by user"));
538
560
  screen.busy("Interrupting");
539
561
  const n = queue.length + steered.pending().length;
@@ -13,7 +13,8 @@ export default definePlugin({
13
13
  ...optional("models", ctx.service("models@1")),
14
14
  ...optional("completions", ctx.service("completions@1")),
15
15
  ...optional("index", ctx.service("sessions@1")),
16
- ...optional("steer", ctx.service("steer@1"))
16
+ ...optional("steer", ctx.service("steer@1")),
17
+ ...optional("withdraw", ctx.service("withdraw@1"))
17
18
  }, bridge));
18
19
  }
19
20
  });
@@ -16,7 +16,8 @@
16
16
  "models@1",
17
17
  "completions@1",
18
18
  "sessions@1",
19
- "steer@1"
19
+ "steer@1",
20
+ "withdraw@1"
20
21
  ],
21
22
  "frontend": {
22
23
  "name": "tui",
@@ -67,7 +67,7 @@ export function safeScreen(screen) {
67
67
  log: (text, o) => screen.log(s(text), o && { ...o, details: opt(o.details) }),
68
68
  amend: (id, text, o) => screen.amend(id, s(text), o && { ...o, details: opt(o.details) }),
69
69
  ...screen.note ? { note: (id, text) => screen.note(id, s(text)) } : {},
70
- ...screen.jobs ? { jobs: (jobs) => screen.jobs(jobs.map((j) => ({ ...j, title: s(j.title), status: s(j.status), output: s(j.output) }))) } : {},
70
+ ...screen.jobs ? { jobs: (jobs) => screen.jobs(jobs.map((j) => ({ ...j, title: s(j.title), status: s(j.status), output: s(j.output), ...j.details ? { details: j.details.map((d) => ({ line: d.line, text: s(d.text) })) } : {} }))) } : {},
71
71
  ...screen.showJobs ? { showJobs: () => screen.showJobs() } : {},
72
72
  progress: (id, line, o) => screen.progress(id, opt(line), o),
73
73
  stream: (kind, text) => {
@@ -101,7 +101,7 @@ export function safeScreen(screen) {
101
101
  screen.status(out);
102
102
  },
103
103
  queued: (items) => screen.queued(items.map(s)),
104
- select: (title, choices, o) => screen.select(s(title), choices.map((c) => ({ ...c, label: s(c.label), hint: opt(c.hint) })), o && { ...o, body: opt(o.body) }),
104
+ select: (title, choices, o) => screen.select(s(title), choices.map((c) => ({ ...c, label: s(c.label), hint: opt(c.hint), group: opt(c.group) })), o && { ...o, body: opt(o.body) }),
105
105
  input: (title, o) => screen.input(s(title), o && { ...o, initial: opt(o.initial), placeholder: opt(o.placeholder) }),
106
106
  draft: screen.draft && {
107
107
  text: () => screen.draft.text(),
@@ -201,7 +201,11 @@ export function plainScreen(opts) {
201
201
  line(yellow(title));
202
202
  if (o?.body)
203
203
  line(o.body);
204
- choices.forEach((c, i) => line(` ${i + 1}. ${c.label}${c.hint ? dim(` ${c.hint}`) : ""}`));
204
+ choices.forEach((c, i) => {
205
+ if (c.group !== undefined && c.group !== choices[i - 1]?.group)
206
+ line(dim(` ${c.group}`));
207
+ line(` ${i + 1}. ${c.label}${c.current ? " (current)" : ""}${c.hint ? dim(` ${c.hint}`) : ""}`);
208
+ });
205
209
  if (o?.signal?.aborted)
206
210
  return;
207
211
  const n = Number(await ask(`Choose 1-${choices.length} (empty to cancel): `, o?.signal));
@@ -33,17 +33,23 @@ export function transcript(opts) {
33
33
  addUsage(usageOf(e.data?.usage));
34
34
  }
35
35
  let reportsCache;
36
+ const cached = { input: 0, read: 0 };
36
37
  function showContext(e) {
37
38
  const u = e.data.usage;
38
39
  if (u?.inputTokens === undefined)
39
40
  return;
40
41
  const by = e.origin.by === "model" ? `${e.origin.provider}/${e.origin.model}` : "";
41
- if (u.cacheReadTokens !== undefined)
42
+ if (u.cacheReadTokens !== undefined && reportsCache !== by) {
42
43
  reportsCache = by;
43
- screen.status({
44
- context: { used: u.inputTokens + (u.outputTokens ?? 0), limit: opts.contextWindow() },
45
- cache: reportsCache === by && u.inputTokens > 0 ? (u.cacheReadTokens ?? 0) / u.inputTokens : undefined
46
- });
44
+ cached.input = cached.read = 0;
45
+ }
46
+ let cache;
47
+ if (reportsCache === by && u.inputTokens > 0) {
48
+ cached.input += u.inputTokens;
49
+ cached.read += u.cacheReadTokens ?? 0;
50
+ cache = { last: (u.cacheReadTokens ?? 0) / u.inputTokens, avg: cached.read / cached.input };
51
+ }
52
+ screen.status({ context: { used: u.inputTokens + (u.outputTokens ?? 0), limit: opts.contextWindow() }, cache });
47
53
  }
48
54
  const speed = { first: 0, estimate: 0, shown: 0 };
49
55
  const startSpeed = () => {
@@ -165,6 +171,7 @@ ${bold(`› ${t.replace(/\n/g, `
165
171
  for (const k of Object.keys(usage))
166
172
  usage[k] = 0;
167
173
  reportsCache = undefined;
174
+ cached.input = cached.read = 0;
168
175
  toolNames.clear();
169
176
  for (const id of running)
170
177
  screen.progress(id, undefined);
@@ -173,6 +180,7 @@ ${bold(`› ${t.replace(/\n/g, `
173
180
  befores.clear();
174
181
  inputs.clear();
175
182
  asks.clear();
183
+ streamed = false;
176
184
  self.lastEnd = undefined;
177
185
  }
178
186
  };
@@ -322,8 +322,8 @@ export class Store {
322
322
  this.update({ scroll }, true);
323
323
  }
324
324
  }
325
- const MOUSE_ON = "\x1B[?1000h\x1B[?1002h\x1B[?1006h";
326
- const MOUSE_OFF = "\x1B[?1006l\x1B[?1002l\x1B[?1000l";
325
+ const MOUSE_ON = "\x1B[?1000h\x1B[?1002h\x1B[?1003h\x1B[?1006h";
326
+ const MOUSE_OFF = "\x1B[?1006l\x1B[?1003l\x1B[?1002l\x1B[?1000l";
327
327
  const CURSOR_BLINK = "\x1B[5 q\x1B[?12h";
328
328
  const CURSOR_RESET = "\x1B[0 q";
329
329
  const NOTIFY_AFTER = 1e4;
@@ -651,6 +651,7 @@ function mouseEvent(input) {
651
651
  }
652
652
  const WHEEL_LINES = 3;
653
653
  const DRAG = 32;
654
+ const MOVE = 35;
654
655
  const COPIED_MS = 2000;
655
656
  const same = (a, b) => a.row === b.row && a.col === b.col;
656
657
  function selectedCols(s, row) {
@@ -761,6 +762,7 @@ function App({
761
762
  }, [busy, showBot, ticks, mood, moving, state.doneAt, state.size]);
762
763
  const ref = useRef(null);
763
764
  const { height, top } = useBoxMetrics(ref);
765
+ const headerRef = useRef(null);
764
766
  const width = Math.max(10, columns - 2);
765
767
  const done = store.lines(width, state.scroll + 2 * rows);
766
768
  const front = store.front;
@@ -817,6 +819,8 @@ function App({
817
819
  const copiedTimer = useRef(undefined);
818
820
  useEffect(() => () => clearTimeout(copiedTimer.current), []);
819
821
  const inputPlace = useRef(undefined);
822
+ const [hover, setHover] = useState(false);
823
+ const hovering = useRef(false);
820
824
  const copySelection = (s) => copyText(selectedText(s, (i) => rowAt(i + front)));
821
825
  const copyText = (text) => {
822
826
  copy(text);
@@ -836,10 +840,21 @@ function App({
836
840
  return store.openJobs();
837
841
  const mouse = mouseEvent(input);
838
842
  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
+ const cols = mouse.y === rows - 2 ? jobsColumns(statusSegments(state.status, state.jobs, statusWidth)) : undefined;
844
+ const overJobs = cols !== undefined && mouse.x >= cols[0] && mouse.x <= cols[1];
845
+ if (overJobs !== hovering.current) {
846
+ hovering.current = overJobs;
847
+ setHover(overJobs);
848
+ }
849
+ if (mouse.button === MOVE)
850
+ return;
851
+ if (mouse.button === 0 && mouse.press && overJobs)
852
+ return store.openJobs();
853
+ if (mouse.button === 0 && mouse.press && headerRef.current) {
854
+ const o = origin(headerRef.current);
855
+ const hit = headerFields(state.status, version, compact).find((f) => mouse.y - o.y === f.row && mouse.x - o.x >= f.from && mouse.x - o.x < f.to);
856
+ if (hit)
857
+ return copyText(hit.text);
843
858
  }
844
859
  const at = (clamp) => {
845
860
  const i = mouse.y - top;
@@ -849,7 +864,7 @@ function App({
849
864
  };
850
865
  const input = inputPlace.current;
851
866
  const overInput = input && input.total > input.rows && mouse.y >= input.y - 1 && mouse.y <= input.y + input.rows;
852
- if ((mouse.button === 64 || mouse.button === 65) && overInput)
867
+ if ((mouse.button === 64 || mouse.button === 65) && (overInput || dialog?.kind === "jobs" || dialog?.kind === "select"))
853
868
  return;
854
869
  if (mouse.button === 64)
855
870
  store.scrollBy(WHEEL_LINES, max);
@@ -915,7 +930,7 @@ function App({
915
930
  height: repaint ? rows + 1 : Math.max(1, rows - 1),
916
931
  paddingY: repaint ? 1 : 0,
917
932
  overflowY: "hidden"
918
- }, h(Box, { flexDirection: "column", flexGrow: 1, overflowY: "hidden" }, h(Header, { status: state.status, version, compact, bot, completions: handlers.completions?.() ?? [] }), h(Box, {
933
+ }, h(Box, { flexDirection: "column", flexGrow: 1, overflowY: "hidden" }, h(Header, { status: state.status, version, compact, bot, textRef: headerRef, completions: handlers.completions?.() ?? [] }), h(Box, {
919
934
  ref,
920
935
  flexDirection: "column",
921
936
  flexGrow: 1,
@@ -942,7 +957,7 @@ function App({
942
957
  flash: copied,
943
958
  place: inputPlace,
944
959
  onCopy: copyText
945
- }), h(StatusBar, { status: state.status, jobs: state.jobs, width: statusWidth }))))));
960
+ }), h(StatusBar, { status: state.status, jobs: state.jobs, width: statusWidth, hover }))))));
946
961
  }
947
962
  function Bot({ pixels }) {
948
963
  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)))));
@@ -952,7 +967,8 @@ function Header({
952
967
  status,
953
968
  version,
954
969
  compact,
955
- bot
970
+ bot,
971
+ textRef
956
972
  }) {
957
973
  const where = [
958
974
  h(Text, { key: "model", color: "cyan" }, status.model),
@@ -962,8 +978,28 @@ function Header({
962
978
  ];
963
979
  const name = h(Text, { key: "name", color: "cyan", bold: true }, "jade");
964
980
  if (compact)
965
- return h(Box, { flexShrink: 0, paddingX: 1 }, h(Text, { wrap: "truncate-end" }, name, h(Text, { key: "dot", dimColor: true }, " · "), ...where));
966
- return h(Box, { flexShrink: 0, borderStyle: "round", borderColor: "cyan", paddingX: 1 }, bot ? h(Bot, { pixels: bot }) : null, h(Box, { flexDirection: "column", flexGrow: 1, flexShrink: 1 }, h(Text, { wrap: "truncate-end" }, name, h(Text, { dimColor: true }, ` v${version}`), status.session ? h(Text, { dimColor: true }, ` · session ${status.session}`) : null), h(Text, { wrap: "truncate-end" }, ...where), h(Text, { dimColor: true, wrap: "truncate-end" }, ["/help for commands", ...completions.map((c) => `${c.trigger} ${c.label}`)].join(" · "))));
981
+ return h(Box, { ref: textRef, flexShrink: 0, paddingX: 1 }, h(Text, { wrap: "truncate-end" }, name, h(Text, { key: "dot", dimColor: true }, " · "), ...where));
982
+ return h(Box, { flexShrink: 0, borderStyle: "round", borderColor: "cyan", paddingX: 1 }, bot ? h(Bot, { pixels: bot }) : null, h(Box, { ref: textRef, flexDirection: "column", flexGrow: 1, flexShrink: 1 }, h(Text, { wrap: "truncate-end" }, name, h(Text, { dimColor: true }, ` v${version}`), status.session ? h(Text, { dimColor: true }, ` · session ${status.session}`) : null), h(Text, { wrap: "truncate-end" }, ...where), h(Text, { dimColor: true, wrap: "truncate-end" }, ["/help for commands", ...completions.map((c) => `${c.trigger} ${c.label}`)].join(" · "))));
983
+ }
984
+ export function headerFields(status, version, compact) {
985
+ const out = [];
986
+ const line = (row, x, parts) => {
987
+ for (const [text, copy] of parts) {
988
+ const w = visibleWidth(text);
989
+ if (copy && text)
990
+ out.push({ row, from: x, to: x + w, text });
991
+ x += w;
992
+ }
993
+ };
994
+ const where = [[status.model, true], [" · ", false], [status.cwd, true], ...status.branch ? [[" (", false], [status.branch, true]] : []];
995
+ if (compact)
996
+ line(0, 1, [["jade · ", false], ...where]);
997
+ else {
998
+ if (status.session)
999
+ line(0, 0, [[`jade v${version} · session `, false], [status.session, true]]);
1000
+ line(1, 0, where);
1001
+ }
1002
+ return out;
967
1003
  }
968
1004
  const dialogIds = new WeakMap;
969
1005
  let dialogCounter = 0;
@@ -996,10 +1032,13 @@ export function fitSegments(segments, width) {
996
1032
  }
997
1033
  return kept;
998
1034
  }
999
- function statusSegments(status, jobs, width) {
1035
+ export function statusSegments(status, jobs, width, hover = false) {
1000
1036
  const ctx = status.context;
1001
1037
  const pct = ctx ? Math.round(ctx.used / ctx.limit * 100) : 0;
1002
1038
  const nearFull = pct >= 80;
1039
+ const cache = status.cache;
1040
+ const percent = (r) => `${Math.round(r * 1000) / 10}%`;
1041
+ const cacheLow = cache !== undefined && cache.last < 0.5 && cache.last < cache.avg;
1003
1042
  const speed = status.speed;
1004
1043
  const plugin = (p, keep) => {
1005
1044
  const seg = typeof p.text === "string" ? { text: p.text } : p.text;
@@ -1015,7 +1054,7 @@ function statusSegments(status, jobs, width) {
1015
1054
  const running = runningJobs(jobs);
1016
1055
  const segments = [
1017
1056
  ...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 }] : [],
1057
+ ...running ? [{ text: running, color: "cyan", inverse: hover, jobs: true }] : [],
1019
1058
  ctx ? {
1020
1059
  text: `ctx ${tokens(ctx.used)}/${tokens(ctx.limit)} (${pct}%)`,
1021
1060
  short: `ctx ${pct}%`,
@@ -1030,7 +1069,16 @@ function statusSegments(status, jobs, width) {
1030
1069
  dim: true
1031
1070
  }
1032
1071
  ] : [],
1033
- ...status.cache !== undefined ? [{ text: `cache ${Math.round(status.cache * 1000) / 10}%`, dropAt: 7, dim: true }] : [],
1072
+ ...cache ? [
1073
+ {
1074
+ text: `cache ${percent(cache.last)} (avg ${percent(cache.avg)})`,
1075
+ short: `cache ${percent(cache.last)}`,
1076
+ shortAt: 7.8,
1077
+ dropAt: 7,
1078
+ color: cacheLow ? "yellow" : undefined,
1079
+ dim: !cacheLow
1080
+ }
1081
+ ] : [],
1034
1082
  ...status.spent ? [
1035
1083
  {
1036
1084
  text: `session ${tokens(status.spent.input)} in / ${tokens(status.spent.output)} out`,
@@ -1040,14 +1088,7 @@ function statusSegments(status, jobs, width) {
1040
1088
  dim: true
1041
1089
  }
1042
1090
  ] : [],
1043
- ...others.map((p) => plugin(p, false)),
1044
- {
1045
- text: "PgUp/PgDn scroll · click ▸ or ctrl+o to expand · drag to select and copy",
1046
- short: "ctrl+o to expand",
1047
- shortAt: 10,
1048
- dropAt: 9,
1049
- dim: true
1050
- }
1091
+ ...others.map((p) => plugin(p, false))
1051
1092
  ];
1052
1093
  return fitSegments(segments, width);
1053
1094
  }
@@ -1063,9 +1104,9 @@ function jobsColumns(segments) {
1063
1104
  }
1064
1105
  return;
1065
1106
  }
1066
- function StatusBar({ status, jobs, width }) {
1067
- const shown = statusSegments(status, jobs, width);
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)))));
1107
+ function StatusBar({ status, jobs, width, hover }) {
1108
+ const shown = statusSegments(status, jobs, width, hover);
1109
+ 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, inverse: s.inverse }, s.text)))));
1069
1110
  }
1070
1111
  function Hint({ variants, width }) {
1071
1112
  const text = variants.find((v) => visibleWidth(v) <= width) ?? variants.at(-1);
@@ -1602,7 +1643,7 @@ function SelectDialog({
1602
1643
  dialog
1603
1644
  }) {
1604
1645
  const selection = useContext(SelectionContext);
1605
- const [getSelected, setSelected] = useLive(0);
1646
+ const [getSelected, setSelected] = useLive(Math.max(0, dialog.choices.findIndex((c) => c.current)));
1606
1647
  const [getQuery, setQuery] = useLive("");
1607
1648
  const [getTop, setTop] = useLive(0);
1608
1649
  const scrolling = useRef({ maxTop: 0, page: 1 });
@@ -1611,7 +1652,7 @@ function SelectDialog({
1611
1652
  const query = getQuery();
1612
1653
  const filtered = () => {
1613
1654
  const q = getQuery().toLowerCase();
1614
- return dialog.choices.map((c, i) => ({ c, i })).filter(({ c }) => !q || `${c.label} ${c.hint ?? ""}`.toLowerCase().includes(q));
1655
+ return dialog.choices.map((c, i) => ({ c, i })).filter(({ c }) => !q || `${c.label} ${c.hint ?? ""} ${c.group ?? ""}`.toLowerCase().includes(q));
1615
1656
  };
1616
1657
  const items = filtered();
1617
1658
  const n = items.length;
@@ -1635,6 +1676,16 @@ function SelectDialog({
1635
1676
  const items = filtered();
1636
1677
  const n = items.length;
1637
1678
  const selected = Math.min(getSelected(), Math.max(0, n - 1));
1679
+ const mouse = mouseEvent(input);
1680
+ if (mouse) {
1681
+ const wheel = mouse.button === 64 ? -1 : mouse.button === 65 ? 1 : 0;
1682
+ if (!wheel)
1683
+ return;
1684
+ const { maxTop } = scrolling.current;
1685
+ if (maxTop > 0)
1686
+ return setTop(Math.max(0, Math.min(maxTop, Math.min(getTop(), maxTop) + wheel * WHEEL_LINES)));
1687
+ return n && setSelected(Math.max(0, Math.min(n - 1, selected + wheel)));
1688
+ }
1638
1689
  if (key.upArrow || !filtering && input === "k")
1639
1690
  return n && setSelected((selected - 1 + n) % n);
1640
1691
  if (key.downArrow || !filtering && input === "j")
@@ -1676,11 +1727,30 @@ function SelectDialog({
1676
1727
  if (Number.isInteger(digit) && digit >= 1 && digit <= Math.min(n, 9))
1677
1728
  dialog.resolve(items[digit - 1].c.value);
1678
1729
  });
1679
- const start = around(selected, n, visible);
1680
- return h(Box, { flexDirection: "column", borderStyle: "round", borderColor: "yellow", paddingX: 1, marginTop: 1 }, h(Text, { bold: true, wrap: "truncate-end" }, dialog.title), body.map((l, i) => h(Text, { key: i, wrap: "truncate-end" }, l || " ")), filtering ? h(Text, { wrap: "truncate-end" }, h(Text, { color: "cyan" }, "filter› "), query ? h(Text, null, query) : h(Text, { dimColor: true }, `type to filter ${dialog.choices.length} items`)) : null, h(Box, { flexDirection: "column", marginTop: filtering ? 0 : 1 }, start > 0 ? h(Text, { dimColor: true }, ` ↑ ${start} more`) : null, n === 0 ? h(Text, { dimColor: true }, " no match") : null, items.slice(start, start + visible).map(({ c, i }, j) => {
1681
- const on = start + j === selected;
1682
- return h(Text, { key: i, wrap: "truncate-end" }, h(Text, { color: on ? "cyan" : undefined, bold: on }, on ? "❯ " : " ", filtering ? "" : `${i + 1}. `, c.label), c.hint ? h(Text, { dimColor: true }, ` ${c.hint}`) : null);
1683
- }), start + visible < n ? h(Text, { dimColor: true }, ` ↓ ${n - start - visible} more`) : null), h(Hint, {
1730
+ const shownFrom = (from) => {
1731
+ const out = [];
1732
+ for (let k = from, used = 0;k < n; k++) {
1733
+ const g = items[k].c.group;
1734
+ const head = g !== undefined && (k === from || items[k - 1].c.group !== g) ? g : undefined;
1735
+ const rows = head === undefined ? 1 : 2;
1736
+ if (used + rows > visible && out.length)
1737
+ break;
1738
+ out.push({ k, ...head === undefined ? {} : { head } });
1739
+ used += rows;
1740
+ }
1741
+ return out;
1742
+ };
1743
+ let start = around(selected, n, visible);
1744
+ let shown = shownFrom(start);
1745
+ while (start < selected && !shown.some((r) => r.k === selected))
1746
+ shown = shownFrom(++start);
1747
+ const end = shown.length ? shown.at(-1).k + 1 : start;
1748
+ return h(Box, { flexDirection: "column", borderStyle: "round", borderColor: "yellow", paddingX: 1, marginTop: 1 }, h(Text, { bold: true, wrap: "truncate-end" }, dialog.title), body.map((l, i) => h(Text, { key: i, wrap: "truncate-end" }, l || " ")), filtering ? h(Text, { wrap: "truncate-end" }, h(Text, { color: "cyan" }, "filter› "), query ? h(Text, null, query) : h(Text, { dimColor: true }, `type to filter ${dialog.choices.length} items`)) : null, h(Box, { flexDirection: "column", marginTop: filtering ? 0 : 1 }, start > 0 ? h(Text, { dimColor: true }, ` ↑ ${start} more`) : null, n === 0 ? h(Text, { dimColor: true }, " no match") : null, shown.flatMap(({ k, head }) => {
1749
+ const { c, i } = items[k];
1750
+ const on = k === selected;
1751
+ const row = h(Text, { key: i, wrap: "truncate-end" }, h(Text, { color: on ? "cyan" : c.current ? "green" : undefined, bold: on || c.current }, on ? "❯ " : " ", filtering ? "" : `${i + 1}. `, c.label), c.current ? h(Text, { color: "green", bold: true }, " ● current") : null, c.hint ? h(Text, { dimColor: true }, ` ${c.hint}`) : null);
1752
+ return head === undefined ? [row] : [h(Text, { key: `group-${i}`, bold: true, dimColor: true, wrap: "truncate-end" }, ` ${head}`), row];
1753
+ }), end < n ? h(Text, { dimColor: true }, ` ↓ ${n - end} more`) : null), h(Hint, {
1684
1754
  width: Math.max(10, columns - 4),
1685
1755
  variants: filtering ? ["↑↓ select · type to filter · enter confirm · esc clear/cancel", "↑↓ · enter · esc"] : ["↑↓ select · enter confirm · esc cancel", "↑↓ · enter · esc"]
1686
1756
  }));
@@ -1693,32 +1763,110 @@ export function runningJobs(jobs) {
1693
1763
  return [...counts].map(([kind, n]) => `${n} ${kind}${n > 1 ? "s" : ""}`).join(" · ");
1694
1764
  }
1695
1765
  const JOB_TAIL_LINES = 2000;
1766
+ export function jobRows(job, width, open) {
1767
+ const output = job.output.replace(/\n+$/, "");
1768
+ if (!output)
1769
+ return [];
1770
+ const all = output.split(`
1771
+ `);
1772
+ const from = Math.max(0, all.length - JOB_TAIL_LINES);
1773
+ const rows = (text, line) => wrap(text, width).map((r) => line === undefined ? { text: visibleRow(r) } : { text: visibleRow(r), line });
1774
+ const details = new Map((job.details ?? []).map((d) => [d.line, d.text]));
1775
+ if (details.size === 0)
1776
+ return rows(all.slice(from).join(`
1777
+ `));
1778
+ const out = [];
1779
+ let plain = [];
1780
+ const flush = () => {
1781
+ if (plain.length)
1782
+ out.push(...rows(plain.join(`
1783
+ `)));
1784
+ plain = [];
1785
+ };
1786
+ for (let i = from;i < all.length; i++) {
1787
+ const detail = details.get(i);
1788
+ if (detail === undefined) {
1789
+ plain.push(all[i]);
1790
+ continue;
1791
+ }
1792
+ flush();
1793
+ const on = open.has(i);
1794
+ out.push(...rows(`${all[i]}${dim(on ? " ▾" : " ▸")}`, i));
1795
+ if (on)
1796
+ out.push(...rows(detail.replace(/\s+$/, "").replace(/^/gm, " "), i));
1797
+ }
1798
+ flush();
1799
+ return out;
1800
+ }
1696
1801
  function JobsDialog({ dialog, jobs }) {
1697
1802
  const selection = useContext(SelectionContext);
1698
1803
  const [getSelected, setSelected] = useLive(0);
1699
1804
  const [getViewing, setViewing] = useLive(undefined);
1700
1805
  const [getBack, setBack] = useLive(0);
1806
+ const [getOpen, setOpen] = useLive(new Set);
1701
1807
  const { rows, columns } = useScreenSize();
1702
1808
  const width = Math.max(10, columns - 4);
1703
1809
  const viewing = jobs.find((j) => j.id === getViewing());
1704
1810
  const selected = Math.min(getSelected(), Math.max(0, jobs.length - 1));
1705
1811
  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) : [];
1812
+ const lines = viewing ? jobRows(viewing, width, getOpen()) : [];
1709
1813
  const maxBack = Math.max(0, lines.length - room);
1710
1814
  const back = Math.min(getBack(), maxBack);
1711
- const scrolling = useRef({ maxBack, room });
1712
- scrolling.current = { maxBack, room };
1815
+ const shown = lines.slice(lines.length - room - back, lines.length - back);
1816
+ const visible = roomFor(rows, VISIBLE);
1817
+ const start = around(selected, jobs.length, visible);
1818
+ const box = useRef(null);
1819
+ const drawn = useRef({ maxBack, room, lines, shown, width, start, visible });
1820
+ drawn.current = { maxBack, room, lines, shown, width, start, visible };
1821
+ const view = (id) => {
1822
+ setBack(0);
1823
+ setOpen(new Set);
1824
+ setViewing(id);
1825
+ };
1713
1826
  useInput((input, key) => {
1714
1827
  if (key.ctrl && input === "c" && selection.active())
1715
1828
  return selection.clear();
1716
1829
  const current = jobs.find((j) => j.id === getViewing());
1830
+ const mouse = mouseEvent(input);
1831
+ if (mouse) {
1832
+ const wheel = mouse.button === 64 ? 1 : mouse.button === 65 ? -1 : 0;
1833
+ const click = mouse.button === 0 && mouse.press;
1834
+ if (!wheel && !click)
1835
+ return;
1836
+ const d = drawn.current;
1837
+ const row = box.current ? mouse.y - origin(box.current).y - 2 : -1;
1838
+ if (current) {
1839
+ if (wheel)
1840
+ return setBack(Math.max(0, Math.min(d.maxBack, Math.min(getBack(), d.maxBack) + wheel * WHEEL_LINES)));
1841
+ const above = d.lines.length > d.room + Math.min(getBack(), d.maxBack) ? 1 : 0;
1842
+ const line = d.shown[row - above]?.line;
1843
+ if (line === undefined)
1844
+ return;
1845
+ const open = new Set(getOpen());
1846
+ if (!open.delete(line))
1847
+ open.add(line);
1848
+ const added = jobRows(current, d.width, open).length - d.lines.length;
1849
+ const first = d.shown.findIndex((r) => r.line === line);
1850
+ const below = d.shown.length - 1 - d.shown.findLastIndex((r) => r.line === line);
1851
+ const up = added > 0 ? Math.min(first, Math.max(0, added - below)) : 0;
1852
+ setOpen(open);
1853
+ return setBack(Math.max(0, Math.min(getBack(), d.maxBack) + added - up));
1854
+ }
1855
+ const n = jobs.length;
1856
+ if (wheel)
1857
+ return n && setSelected(Math.max(0, Math.min(n - 1, Math.min(getSelected(), n - 1) - wheel)));
1858
+ const i = d.start + row - (d.start > 0 ? 1 : 0);
1859
+ if (i >= d.start && i < Math.min(n, d.start + d.visible)) {
1860
+ setSelected(i);
1861
+ return view(jobs[i].id);
1862
+ }
1863
+ return;
1864
+ }
1717
1865
  const target = current ?? jobs[Math.min(getSelected(), jobs.length - 1)];
1718
1866
  if (input === "x" && target && !target.done && target.stop)
1719
1867
  return target.stop();
1720
1868
  if (current) {
1721
- const { maxBack, room } = scrolling.current;
1869
+ const { maxBack, room } = drawn.current;
1722
1870
  const by = (n) => setBack(Math.max(0, Math.min(maxBack, Math.min(getBack(), maxBack) + n)));
1723
1871
  if (key.pageUp)
1724
1872
  return by(room - 1);
@@ -1739,31 +1887,30 @@ function JobsDialog({ dialog, jobs }) {
1739
1887
  return n && setSelected((Math.min(getSelected(), n - 1) - 1 + n) % n);
1740
1888
  if (key.downArrow || input === "j")
1741
1889
  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
- }
1890
+ if ((key.return || key.rightArrow) && target)
1891
+ return view(target.id);
1746
1892
  if (key.escape || key.ctrl && input === "c")
1747
1893
  return dialog.resolve(undefined);
1748
1894
  });
1749
1895
  const mark = (j) => j.done ? h(Text, { dimColor: true }, "○ ") : h(Text, { color: "green" }, "● ");
1750
1896
  const canStop = (j) => j !== undefined && !j.done && j.stop !== undefined;
1751
- const frame = { flexDirection: "column", borderStyle: "round", borderColor: "cyan", paddingX: 1, marginTop: 1 };
1897
+ const frame = { ref: box, flexDirection: "column", borderStyle: "round", borderColor: "cyan", paddingX: 1, marginTop: 1 };
1752
1898
  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, {
1899
+ const expandable = lines.some((l) => l.line !== undefined);
1900
+ 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.text || " ")), back > 0 ? h(Text, { dimColor: true }, ` ↓ ${back} more lines`) : null, h(Hint, {
1755
1901
  width,
1756
- variants: [`PgUp/PgDn ↑↓ scroll${canStop(viewing) ? " · x stop" : ""} · esc back`, `↑↓${canStop(viewing) ? " · x" : ""} · esc`]
1902
+ variants: [
1903
+ `wheel PgUp/PgDn ↑↓ scroll${expandable ? " · click ▸ to expand" : ""}${canStop(viewing) ? " · x stop" : ""} · esc back`,
1904
+ `↑↓${expandable ? " · click ▸" : ""}${canStop(viewing) ? " · x" : ""} · esc`
1905
+ ]
1757
1906
  }));
1758
1907
  }
1759
- const visible = roomFor(rows, VISIBLE);
1760
- const start = around(selected, jobs.length, visible);
1761
1908
  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
1909
  const on = start + k === selected;
1763
1910
  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
1911
  }), start + visible < jobs.length ? h(Text, { dimColor: true }, ` ↓ ${jobs.length - start - visible} more`) : null, h(Hint, {
1765
1912
  width,
1766
- variants: jobs.length ? [`↑↓ select · enter view output${canStop(jobs[selected]) ? " · x stop" : ""} · esc close`, `↑↓ · enter${canStop(jobs[selected]) ? " · x" : ""} · esc`] : ["esc close"]
1913
+ variants: jobs.length ? [`↑↓ select · enter or click view output${canStop(jobs[selected]) ? " · x stop" : ""} · esc close`, `↑↓ · enter${canStop(jobs[selected]) ? " · x" : ""} · esc`] : ["esc close"]
1767
1914
  }));
1768
1915
  }
1769
1916
  function InputDialog({