@bypane/cli 0.0.0-stage → 0.1.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.
package/dist/ui.js ADDED
@@ -0,0 +1,1020 @@
1
+ import { center, CheckboxGroup, createChart, createFilter, createImage, createMarkdown, createSpring, defaultTheme, Dialog, drawBox, drawImage, drawLines, drawMatches, drawSparkline, Input, inset, Listbox, matchesShortcut, paint, ScrollArea, split, Table, Tabs, text, Textarea, truncate, widthOf, } from "tui";
2
+ import { loadDemoSurfaces } from "./demo.js";
3
+ import { focusOriginPane, openExternally, sendToOriginPane } from "./multiplexer.js";
4
+ import { formatBytes, formatFeedbackSummary, sortTableRows } from "./state.js";
5
+ const SERIES_COLORS = ["#7aa2ff", "#5fd787", "#f5a623", "#ff7a90", "#bb9af7", "#7dcfff"];
6
+ export const bypaneTheme = {
7
+ ...defaultTheme,
8
+ primary: { fg: "#7aa2ff", bold: true },
9
+ success: { fg: "#5fd787", bold: true },
10
+ destructive: { fg: "#ff7a90", bold: true },
11
+ muted: { fg: "#8b93a6" },
12
+ heading: { fg: "#e6edf7", bold: true },
13
+ border: { fg: "#465066" },
14
+ code: { fg: "#7dcfff" },
15
+ link: { fg: "#7aa2ff", underline: true },
16
+ match: { fg: "#10141c", bg: "#f5a623", bold: true },
17
+ selected: { fg: "#5fd787", bold: true },
18
+ highlight: { fg: "#0e131d", bg: "#7aa2ff", bold: true },
19
+ };
20
+ export function createBypaneUi(store, socketPath, hooks = {}) {
21
+ let noteDialogOpen = false;
22
+ let noteDraft = "";
23
+ let helpOpen = false;
24
+ let filterFocused = false;
25
+ // Per-surface render caches
26
+ const imageCache = new Map();
27
+ const chartCache = new Map();
28
+ const filterCache = new Map();
29
+ const tabSpring = createSpring(0, { stiffness: 260, damping: 26 });
30
+ let mdEngine = null;
31
+ let lastActiveId = "";
32
+ function getMarkdown(frame) {
33
+ if (!mdEngine) {
34
+ mdEngine = createMarkdown(frame.styles, frame.theme);
35
+ }
36
+ return mdEngine;
37
+ }
38
+ function getFilter(id) {
39
+ let f = filterCache.get(id);
40
+ if (!f) {
41
+ f = createFilter();
42
+ filterCache.set(id, f);
43
+ }
44
+ return f;
45
+ }
46
+ function getChart(id) {
47
+ let c = chartCache.get(id);
48
+ if (!c) {
49
+ c = createChart();
50
+ chartCache.set(id, c);
51
+ }
52
+ return c;
53
+ }
54
+ function getImage(surface) {
55
+ if (!surface.rgba || !surface.width || !surface.height)
56
+ return null;
57
+ const cacheKey = `${surface.updatedAt}:${surface.width}x${surface.height}`;
58
+ const cached = imageCache.get(surface.id);
59
+ if (cached && cached.key === cacheKey)
60
+ return cached.image;
61
+ const img = createImage(surface.rgba, surface.width, surface.height);
62
+ imageCache.set(surface.id, { key: cacheKey, image: img });
63
+ return img;
64
+ }
65
+ function rule(frame, y, rect) {
66
+ if (y < rect.y || y >= rect.y + rect.rows)
67
+ return;
68
+ text(frame, rect.x, y, "─".repeat(Math.max(0, rect.columns)), frame.theme.border, rect);
69
+ }
70
+ function badgeText(surface) {
71
+ if (surface.kind === "ask") {
72
+ return surface.answer.status === "pending"
73
+ ? "● "
74
+ : surface.answer.status === "submitted"
75
+ ? "✓ "
76
+ : "× ";
77
+ }
78
+ return surface.unread ? "• " : "";
79
+ }
80
+ function computeTabLabels(columns) {
81
+ const count = store.surfaces.length;
82
+ if (count === 0)
83
+ return [];
84
+ const avail = Math.max(12, columns - 2);
85
+ // Reserve space for padding (" " + label + " ") and index/badge prefix so all tabs fit cleanly.
86
+ const perTabBudget = Math.max(7, Math.floor(avail / count));
87
+ const maxTitleChars = Math.max(4, Math.min(20, perTabBudget - 5));
88
+ let x = 1;
89
+ return store.surfaces.map((s, i) => {
90
+ const prefix = `${i < 9 ? `${i + 1}:` : ""}${badgeText(s)}`;
91
+ const label = `${prefix}${truncate(s.title, maxTitleChars)}`;
92
+ const width = widthOf(` ${label} `);
93
+ const out = { value: s.id, label, x, width };
94
+ x += width;
95
+ return out;
96
+ });
97
+ }
98
+ function drawHeader(frame, rect, active) {
99
+ const brand = "◈ bypane";
100
+ const accentStyle = frame.theme.primary;
101
+ const mutedStyle = frame.theme.muted;
102
+ text(frame, rect.x + 1, rect.y, brand, accentStyle, rect);
103
+ const shortSession = store.session.startsWith("rex-")
104
+ ? "rex"
105
+ : store.session.startsWith("tmux-")
106
+ ? "tmux"
107
+ : store.session.length > 16
108
+ ? `${store.session.slice(0, 14)}…`
109
+ : store.session;
110
+ const rightTag = active?.origin?.label
111
+ ? `${active.origin.label} · ${shortSession}`
112
+ : `${store.surfaces.length} ${store.surfaces.length === 1 ? "surface" : "surfaces"} · ${shortSession}`;
113
+ const rightWidth = widthOf(rightTag);
114
+ const brandWidth = widthOf(brand) + 2;
115
+ if (rect.columns > brandWidth + rightWidth + 2) {
116
+ text(frame, rect.x + rect.columns - rightWidth - 1, rect.y, rightTag, mutedStyle, rect);
117
+ }
118
+ const ruleY = rect.y + rect.rows - 1;
119
+ rule(frame, ruleY, rect);
120
+ if (store.surfaces.length > 0 && rect.columns > 8) {
121
+ const tabs = computeTabLabels(rect.columns);
122
+ const activeTab = tabs.find((t) => t.value === store.activeId) ?? tabs[0];
123
+ if (store.activeId !== lastActiveId) {
124
+ const isFirst = lastActiveId === "";
125
+ lastActiveId = store.activeId;
126
+ if (isFirst) {
127
+ tabSpring.set(activeTab.x);
128
+ }
129
+ else {
130
+ tabSpring.to(activeTab.x);
131
+ }
132
+ // Reset focus so the newly active surface (e.g. an incoming `ask` gate) takes focus immediately.
133
+ frame.focused = "";
134
+ }
135
+ else if (tabSpring.target !== activeTab.x) {
136
+ // Pane resized (e.g. Rex zoom) or badge/label width changed: snap immediately to the new tab position.
137
+ tabSpring.set(activeTab.x);
138
+ }
139
+ const springX = Math.round(tabSpring.update(frame));
140
+ const segW = Math.max(3, Math.min(activeTab.width, rect.columns - 1));
141
+ const xStart = Math.max(rect.x, Math.min(rect.x + rect.columns - segW, rect.x + springX));
142
+ text(frame, xStart, ruleY, "━".repeat(segW), accentStyle, rect);
143
+ }
144
+ }
145
+ function drawTabsBar(frame, rect) {
146
+ if (store.surfaces.length === 0 || rect.rows < 3)
147
+ return;
148
+ const tabs = computeTabLabels(rect.columns);
149
+ const tabRect = {
150
+ x: rect.x + 1,
151
+ y: rect.y + 1,
152
+ columns: Math.max(8, rect.columns - 2),
153
+ rows: 1,
154
+ };
155
+ Tabs.Root(frame, {
156
+ id: "workspace-tabs",
157
+ label: "Surfaces",
158
+ items: tabs.map((t) => ({ value: t.value, label: t.label })),
159
+ value: store.activeId,
160
+ onValueChange: (val) => {
161
+ store.focus(val);
162
+ filterFocused = false;
163
+ },
164
+ }, (root) => Tabs.List(root, tabRect));
165
+ }
166
+ function drawEmptyState(frame, body) {
167
+ const pad = inset(body, [1, 2]);
168
+ if (pad.rows < 4 || pad.columns < 16)
169
+ return;
170
+ let y = pad.y;
171
+ text(frame, pad.x, y++, "Companion workspace ready.", frame.theme.heading, pad);
172
+ text(frame, pad.x, y++, "Push docs, images, tables, charts, logs, or decisions from any agent or shell:", frame.theme.muted, pad);
173
+ y++;
174
+ const examples = [
175
+ ["bypane show README.md", "open Markdown or code with line highlights"],
176
+ ["bypane show screenshot.png", "QuickLook PNG inline (or 'o' to open on host)"],
177
+ ["cat rows.json | bypane table", "interactive filterable table"],
178
+ ["bypane ask --select ...", "human-gated decision prompt"],
179
+ ["bypane mcp", "connect any MCP-capable coding agent"],
180
+ ];
181
+ for (const [cmd, desc] of examples) {
182
+ if (y >= pad.y + pad.rows - 3)
183
+ break;
184
+ const col = text(frame, pad.x + 2, y, cmd, frame.theme.code, pad);
185
+ if (pad.columns > 48) {
186
+ text(frame, Math.max(col + 2, pad.x + 32), y, desc, frame.theme.muted, pad);
187
+ }
188
+ y++;
189
+ }
190
+ y++;
191
+ if (y < pad.y + pad.rows - 1) {
192
+ text(frame, pad.x, y++, `Session: ${store.session}`, frame.theme.muted, pad);
193
+ text(frame, pad.x, y++, `Socket: ${truncate(socketPath, Math.max(10, pad.columns - 10))}`, frame.theme.muted, pad);
194
+ }
195
+ if (y + 1 < pad.y + pad.rows) {
196
+ text(frame, pad.x, y + 1, "Press [d] to load an interactive demo workspace.", frame.theme.primary, pad);
197
+ }
198
+ }
199
+ function drawNotesStrip(frame, rect, surface) {
200
+ if (rect.rows <= 0 || surface.notes.length === 0)
201
+ return;
202
+ rule(frame, rect.y, rect);
203
+ text(frame, rect.x + 1, rect.y, ` Notes (${surface.notes.length}) `, frame.theme.primary, rect);
204
+ for (let i = 0; i < Math.min(rect.rows - 1, surface.notes.length); i++) {
205
+ const note = surface.notes[surface.notes.length - 1 - i];
206
+ text(frame, rect.x + 2, rect.y + 1 + i, `• ${truncate(note.text, Math.max(8, rect.columns - 4))}`, frame.theme.muted, rect);
207
+ }
208
+ }
209
+ function drawMarkdownSurface(frame, body, surface) {
210
+ const hasChecklist = surface.checklist.length > 0;
211
+ const checklistRows = hasChecklist ? Math.min(7, surface.checklist.length + 2) : 0;
212
+ const notesRows = surface.notes.length > 0 ? Math.min(4, surface.notes.length + 1) : 0;
213
+ const [docArea, checkArea, noteArea] = split(body, "rows", [-1, checklistRows, notesRows]);
214
+ if (docArea && docArea.rows > 0) {
215
+ const inner = inset(docArea, [0, 1]);
216
+ const md = getMarkdown(frame);
217
+ const lines = md.lines(surface.content, Math.max(10, inner.columns - 1));
218
+ ScrollArea.Root(frame, {
219
+ id: `md-scroll-${surface.id}`,
220
+ label: `${surface.title} document`,
221
+ total: lines.length,
222
+ }, (area) => ScrollArea.Viewport(area, inner, (vp) => {
223
+ drawLines(frame, vp.rect, lines, vp.api.offset());
224
+ }));
225
+ }
226
+ if (hasChecklist && checkArea && checkArea.rows >= 2) {
227
+ rule(frame, checkArea.y, checkArea);
228
+ const done = surface.checklist.filter((c) => c.checked).length;
229
+ text(frame, checkArea.x + 1, checkArea.y, ` Checklist (${done}/${surface.checklist.length}) · Tab to toggle `, frame.theme.primary, checkArea);
230
+ const listRect = {
231
+ x: checkArea.x + 1,
232
+ y: checkArea.y + 1,
233
+ columns: Math.max(4, checkArea.columns - 2),
234
+ rows: Math.max(1, checkArea.rows - 1),
235
+ };
236
+ const checkedValues = surface.checklist
237
+ .filter((c) => c.checked)
238
+ .map((c) => String(c.index));
239
+ CheckboxGroup.Root(frame, {
240
+ id: `md-check-${surface.id}`,
241
+ label: `${surface.title} checklist`,
242
+ items: surface.checklist.map((c) => ({ value: String(c.index), label: c.text })),
243
+ value: checkedValues,
244
+ onValueChange: (vals) => {
245
+ store.updateChecklist(surface.id, vals);
246
+ },
247
+ }, (root) => CheckboxGroup.Content(root, listRect));
248
+ }
249
+ if (noteArea && notesRows > 0) {
250
+ drawNotesStrip(frame, noteArea, surface);
251
+ }
252
+ }
253
+ function drawFileSurface(frame, body, surface) {
254
+ const notesRows = surface.notes.length > 0 ? Math.min(4, surface.notes.length + 1) : 0;
255
+ const [metaRow, sepRow, contentArea, noteArea] = split(body, "rows", [1, 1, -1, notesRows]);
256
+ if (metaRow) {
257
+ const range = surface.highlightStart !== undefined
258
+ ? ` L${surface.highlightStart}${surface.highlightEnd && surface.highlightEnd !== surface.highlightStart ? `–${surface.highlightEnd}` : ""}`
259
+ : "";
260
+ const meta = `${surface.mimeHint ?? "file"}${surface.sizeBytes !== undefined ? ` · ${formatBytes(surface.sizeBytes)}` : ""}${range}`;
261
+ text(frame, metaRow.x + 1, metaRow.y, truncate(surface.path, Math.max(10, metaRow.columns - widthOf(meta) - 6)), frame.theme.heading, metaRow);
262
+ text(frame, metaRow.x + metaRow.columns - widthOf(meta) - 1, metaRow.y, meta, frame.theme.muted, metaRow);
263
+ }
264
+ if (sepRow)
265
+ rule(frame, sepRow.y, sepRow);
266
+ if (contentArea && contentArea.rows > 0) {
267
+ if (surface.binary) {
268
+ const card = center(contentArea, Math.min(58, Math.max(24, contentArea.columns - 4)), Math.min(9, contentArea.rows));
269
+ const inside = drawBox(frame.cells, card, {
270
+ title: "QuickLook · External File",
271
+ style: frame.styles.style(frame.theme.border),
272
+ });
273
+ let y = inside.y;
274
+ text(frame, inside.x + 1, y++, truncate(surface.title, inside.columns - 2), frame.theme.heading, inside);
275
+ text(frame, inside.x + 1, y++, `${surface.mimeHint ?? "binary"} · ${formatBytes(surface.sizeBytes)}`, frame.theme.muted, inside);
276
+ y++;
277
+ if (y < inside.y + inside.rows) {
278
+ text(frame, inside.x + 1, y++, "Press [o] to open on your host machine", frame.theme.primary, inside);
279
+ }
280
+ if (y < inside.y + inside.rows) {
281
+ text(frame, inside.x + 1, y, truncate(surface.path, inside.columns - 2), frame.theme.muted, inside);
282
+ }
283
+ }
284
+ else {
285
+ const rawLines = (surface.content ?? "").split(/\r?\n/);
286
+ const gutterW = Math.max(3, String(rawLines.length).length) + 2;
287
+ const defaultOffset = surface.highlightStart !== undefined ? Math.max(0, surface.highlightStart - 4) : 0;
288
+ const hiStart = surface.highlightStart ?? -1;
289
+ const hiEnd = surface.highlightEnd ?? hiStart;
290
+ ScrollArea.Root(frame, {
291
+ id: `file-scroll-${surface.id}`,
292
+ label: `${surface.title} file`,
293
+ total: rawLines.length,
294
+ defaultOffset,
295
+ }, (area) => ScrollArea.Viewport(area, inset(contentArea, [0, 1]), (vp) => {
296
+ const offset = vp.api.offset();
297
+ for (let row = 0; row < vp.rect.rows && offset + row < rawLines.length; row++) {
298
+ const lineNum = offset + row + 1;
299
+ const isHi = hiStart > 0 && lineNum >= hiStart && lineNum <= hiEnd;
300
+ const y = vp.rect.y + row;
301
+ if (isHi) {
302
+ paint(frame, { x: vp.rect.x, y, columns: vp.rect.columns, rows: 1 }, { bg: "#1c2840" });
303
+ }
304
+ const numStr = `${String(lineNum).padStart(gutterW - 2, " ")} ${isHi ? "▎" : "│"}`;
305
+ text(frame, vp.rect.x, y, numStr, isHi ? frame.theme.primary : frame.theme.muted, vp.rect);
306
+ const lineText = (rawLines[offset + row] ?? "").replace(/\t/g, " ");
307
+ text(frame, vp.rect.x + gutterW + 1, y, lineText, isHi ? { fg: "#e6edf7", bg: "#1c2840" } : 0, vp.rect);
308
+ }
309
+ }));
310
+ }
311
+ }
312
+ if (noteArea && notesRows > 0)
313
+ drawNotesStrip(frame, noteArea, surface);
314
+ }
315
+ function drawImageSurface(frame, body, surface) {
316
+ const notesRows = surface.notes.length > 0 ? Math.min(4, surface.notes.length + 1) : 0;
317
+ const captionRows = surface.caption ? 1 : 0;
318
+ const [metaRow, sepRow, imgArea, captionRow, noteArea] = split(body, "rows", [1, 1, -1, captionRows, notesRows]);
319
+ if (metaRow) {
320
+ const dims = surface.width && surface.height ? `${surface.width}×${surface.height}` : "image";
321
+ const meta = `${dims}${surface.sizeBytes !== undefined ? ` · ${formatBytes(surface.sizeBytes)}` : ""} · [o] open`;
322
+ text(frame, metaRow.x + 1, metaRow.y, truncate(surface.title, Math.max(10, metaRow.columns - widthOf(meta) - 4)), frame.theme.heading, metaRow);
323
+ text(frame, metaRow.x + metaRow.columns - widthOf(meta) - 1, metaRow.y, meta, frame.theme.muted, metaRow);
324
+ }
325
+ if (sepRow)
326
+ rule(frame, sepRow.y, sepRow);
327
+ if (imgArea && imgArea.rows > 0) {
328
+ const img = getImage(surface);
329
+ if (img) {
330
+ drawImage(frame, inset(imgArea, [0, 1]), img);
331
+ }
332
+ else {
333
+ const card = center(imgArea, Math.min(62, Math.max(24, imgArea.columns - 4)), Math.min(8, imgArea.rows));
334
+ const inside = drawBox(frame.cells, card, {
335
+ title: "Image QuickLook",
336
+ style: frame.styles.style(frame.theme.border),
337
+ });
338
+ text(frame, inside.x + 1, inside.y, truncate(surface.error || "Unable to decode inline preview", inside.columns - 2), frame.theme.muted, inside);
339
+ if (inside.rows > 2) {
340
+ text(frame, inside.x + 1, inside.y + 2, "Press [o] to open on your host machine", frame.theme.primary, inside);
341
+ }
342
+ }
343
+ }
344
+ if (captionRow && surface.caption) {
345
+ text(frame, captionRow.x + 1, captionRow.y, truncate(surface.caption, captionRow.columns - 2), frame.theme.muted, captionRow);
346
+ }
347
+ if (noteArea && notesRows > 0)
348
+ drawNotesStrip(frame, noteArea, surface);
349
+ }
350
+ function drawTableSurface(frame, body, surface) {
351
+ const notesRows = surface.notes.length > 0 ? Math.min(4, surface.notes.length + 1) : 0;
352
+ const showFilterBar = filterFocused || surface.filter.length > 0;
353
+ const [filterRow, sepRow, tableArea, noteArea] = split(body, "rows", [showFilterBar ? 1 : 0, showFilterBar ? 1 : 0, -1, notesRows]);
354
+ const sorted = sortTableRows(surface);
355
+ const searchable = sorted.map((r) => r.cells.join(" \t "));
356
+ const filter = getFilter(`table-${surface.id}`);
357
+ filter.update(searchable, surface.filter);
358
+ const visibleRows = [];
359
+ if (surface.filter.trim().length === 0) {
360
+ for (const r of sorted)
361
+ visibleRows.push({ value: r.id, cells: r.cells });
362
+ }
363
+ else {
364
+ for (let i = 0; i < filter.count; i++) {
365
+ const row = sorted[filter.at(i)];
366
+ if (row)
367
+ visibleRows.push({ value: row.id, cells: row.cells });
368
+ }
369
+ }
370
+ const renderFilter = () => {
371
+ if (!showFilterBar || !filterRow)
372
+ return;
373
+ text(frame, filterRow.x + 1, filterRow.y, "/", frame.theme.primary, filterRow);
374
+ const countText = `${visibleRows.length}/${surface.rows.length}`;
375
+ const cw = widthOf(countText);
376
+ text(frame, filterRow.x + filterRow.columns - cw - 1, filterRow.y, countText, frame.theme.muted, filterRow);
377
+ Input.Root(frame, {
378
+ id: `table-filter-${surface.id}`,
379
+ label: "Filter rows",
380
+ value: surface.filter,
381
+ placeholder: "Filter table rows… (Esc to return)",
382
+ onValueChange: (val) => {
383
+ surface.filter = val;
384
+ },
385
+ onSubmit: () => {
386
+ filterFocused = false;
387
+ frame.focused = "";
388
+ },
389
+ }, (root) => Input.Field(root, {
390
+ x: filterRow.x + 3,
391
+ y: filterRow.y,
392
+ columns: Math.max(8, filterRow.columns - cw - 5),
393
+ rows: 1,
394
+ }));
395
+ if (sepRow)
396
+ rule(frame, sepRow.y, sepRow);
397
+ };
398
+ const renderTable = () => {
399
+ if (!tableArea || tableArea.rows <= 0)
400
+ return;
401
+ const tableInnerW = Math.max(12, tableArea.columns - 2);
402
+ const explicitSum = surface.columns.reduce((acc, c) => acc + (c.width ?? 0), 0);
403
+ const scale = explicitSum > tableInnerW - 10 ? (tableInnerW - 10) / explicitSum : 1;
404
+ const cols = surface.columns.map((c, idx) => ({
405
+ label: surface.sortColumn === idx
406
+ ? `${c.label} ${surface.sortDir === "asc" ? "↑" : "↓"}`
407
+ : c.label,
408
+ width: c.width ? Math.max(5, Math.floor(c.width * scale)) : undefined,
409
+ align: c.align,
410
+ }));
411
+ if (visibleRows.length > 0 &&
412
+ !visibleRows.some((r) => r.value === surface.highlightedRowId)) {
413
+ surface.highlightedRowId = visibleRows[0].value;
414
+ }
415
+ Table.Root(frame, {
416
+ id: `table-${surface.id}`,
417
+ label: surface.title,
418
+ columns: cols,
419
+ rows: visibleRows,
420
+ value: surface.selectedRowId,
421
+ highlighted: surface.highlightedRowId,
422
+ onHighlight: (val) => {
423
+ surface.highlightedRowId = val;
424
+ },
425
+ onValueChange: (val) => {
426
+ surface.selectedRowId = val;
427
+ surface.highlightedRowId = val;
428
+ store.setToast("Row selected — press 's' to send or 'y' to copy", "info", 2000);
429
+ },
430
+ }, (root) => Table.Content(root, inset(tableArea, [0, 1])));
431
+ };
432
+ // Focus filter first while filter mode is active; otherwise focus the table immediately.
433
+ if (filterFocused) {
434
+ renderFilter();
435
+ renderTable();
436
+ }
437
+ else {
438
+ renderTable();
439
+ renderFilter();
440
+ }
441
+ if (noteArea && notesRows > 0)
442
+ drawNotesStrip(frame, noteArea, surface);
443
+ }
444
+ function drawChartSurface(frame, body, surface) {
445
+ const notesRows = surface.notes.length > 0 ? Math.min(4, surface.notes.length + 1) : 0;
446
+ const legendRows = Math.min(4, Math.max(1, surface.series.length));
447
+ const captionRows = surface.caption ? 1 : 0;
448
+ const [legendArea, sepRow, chartArea, sparkRow, captionRow, noteArea] = split(body, "rows", [
449
+ legendRows,
450
+ 1,
451
+ -1,
452
+ body.rows >= 14 && surface.series.length === 1 ? 2 : 0,
453
+ captionRows,
454
+ notesRows,
455
+ ]);
456
+ if (legendArea) {
457
+ for (let i = 0; i < Math.min(legendArea.rows, surface.series.length); i++) {
458
+ const s = surface.series[i];
459
+ const color = s.color || SERIES_COLORS[i % SERIES_COLORS.length];
460
+ const valid = s.values.filter((v) => Number.isFinite(v));
461
+ const min = valid.length ? Math.min(...valid) : 0;
462
+ const max = valid.length ? Math.max(...valid) : 0;
463
+ const last = valid.length ? valid[valid.length - 1] : 0;
464
+ const unit = surface.unit ? ` ${surface.unit}` : "";
465
+ const y = legendArea.y + i;
466
+ const x1 = text(frame, legendArea.x + 1, y, `● ${s.name}`, { fg: color, bold: true }, legendArea);
467
+ const stats = ` last ${last.toFixed(2)}${unit} · min ${min.toFixed(2)} · max ${max.toFixed(2)} (${valid.length} pts)`;
468
+ text(frame, x1, y, truncate(stats, Math.max(0, legendArea.x + legendArea.columns - x1)), frame.theme.muted, legendArea);
469
+ }
470
+ }
471
+ if (sepRow)
472
+ rule(frame, sepRow.y, sepRow);
473
+ if (chartArea && chartArea.rows >= 3) {
474
+ const chart = getChart(surface.id);
475
+ chart.draw(frame, inset(chartArea, [0, 1]), {
476
+ series: surface.series.map((s, i) => ({
477
+ values: s.values,
478
+ style: { fg: (s.color || SERIES_COLORS[i % SERIES_COLORS.length]) },
479
+ })),
480
+ min: surface.min,
481
+ max: surface.max,
482
+ format: (v) => `${Number(v.toFixed(2))}${surface.unit ? surface.unit : ""}`,
483
+ });
484
+ }
485
+ if (sparkRow && sparkRow.rows >= 1 && surface.series[0]) {
486
+ drawSparkline(frame, inset(sparkRow, [0, 1]), surface.series[0].values, {
487
+ style: { fg: (surface.series[0].color || SERIES_COLORS[0]) },
488
+ });
489
+ }
490
+ if (captionRow && surface.caption) {
491
+ text(frame, captionRow.x + 1, captionRow.y, truncate(surface.caption, captionRow.columns - 2), frame.theme.muted, captionRow);
492
+ }
493
+ if (noteArea && notesRows > 0)
494
+ drawNotesStrip(frame, noteArea, surface);
495
+ }
496
+ function drawLogSurface(frame, body, surface) {
497
+ const notesRows = surface.notes.length > 0 ? Math.min(4, surface.notes.length + 1) : 0;
498
+ const showFilterBar = filterFocused || surface.filter.length > 0;
499
+ const [filterRow, sepRow, logArea, noteArea] = split(body, "rows", [
500
+ showFilterBar ? 1 : 0,
501
+ showFilterBar ? 1 : 0,
502
+ -1,
503
+ notesRows,
504
+ ]);
505
+ const filter = getFilter(`log-${surface.id}`);
506
+ filter.update(surface.lines, surface.filter);
507
+ const count = surface.filter ? filter.count : surface.lines.length;
508
+ const renderFilter = () => {
509
+ if (!showFilterBar || !filterRow)
510
+ return;
511
+ text(frame, filterRow.x + 1, filterRow.y, "/", frame.theme.primary, filterRow);
512
+ const countText = `${count}/${surface.lines.length} lines`;
513
+ const cw = widthOf(countText);
514
+ text(frame, filterRow.x + filterRow.columns - cw - 1, filterRow.y, countText, frame.theme.muted, filterRow);
515
+ Input.Root(frame, {
516
+ id: `log-filter-${surface.id}`,
517
+ label: "Filter log",
518
+ value: surface.filter,
519
+ placeholder: "Filter log lines… (Esc to return)",
520
+ onValueChange: (val) => {
521
+ surface.filter = val;
522
+ },
523
+ onSubmit: () => {
524
+ filterFocused = false;
525
+ frame.focused = "";
526
+ },
527
+ }, (root) => Input.Field(root, {
528
+ x: filterRow.x + 3,
529
+ y: filterRow.y,
530
+ columns: Math.max(8, filterRow.columns - cw - 5),
531
+ rows: 1,
532
+ }));
533
+ if (sepRow)
534
+ rule(frame, sepRow.y, sepRow);
535
+ };
536
+ const renderLog = () => {
537
+ if (!logArea || logArea.rows <= 0)
538
+ return;
539
+ ScrollArea.Root(frame, {
540
+ id: `log-scroll-${surface.id}`,
541
+ label: `${surface.title} log`,
542
+ total: count,
543
+ stickToEnd: surface.stickToEnd,
544
+ }, (area) => ScrollArea.Viewport(area, inset(logArea, [0, 1]), (vp) => {
545
+ const offset = vp.api.offset();
546
+ for (let row = 0; row < vp.rect.rows && offset + row < count; row++) {
547
+ const idx = surface.filter ? filter.at(offset + row) : offset + row;
548
+ const line = surface.lines[idx] ?? "";
549
+ drawMatches(frame, vp.rect.x, vp.rect.y + row, line, surface.filter, 0, vp.rect);
550
+ }
551
+ }));
552
+ };
553
+ if (filterFocused) {
554
+ renderFilter();
555
+ renderLog();
556
+ }
557
+ else {
558
+ renderLog();
559
+ renderFilter();
560
+ }
561
+ if (noteArea && notesRows > 0)
562
+ drawNotesStrip(frame, noteArea, surface);
563
+ }
564
+ function drawAskSurface(frame, body, surface) {
565
+ const pad = inset(body, [1, 2]);
566
+ if (pad.rows < 4 || pad.columns < 12)
567
+ return;
568
+ const isPending = surface.answer.status === "pending";
569
+ const badge = surface.answer.status === "pending"
570
+ ? "● ACTION REQUIRED"
571
+ : surface.answer.status === "submitted"
572
+ ? "✓ ANSWERED"
573
+ : "× CANCELLED";
574
+ const badgeStyle = surface.answer.status === "pending"
575
+ ? { fg: "#f5a623", bold: true }
576
+ : surface.answer.status === "submitted"
577
+ ? frame.theme.success
578
+ : frame.theme.destructive;
579
+ const md = getMarkdown(frame);
580
+ const contextLines = surface.context ? md.lines(surface.context, pad.columns) : [];
581
+ const maxContext = Math.min(contextLines.length, Math.max(0, Math.floor(pad.rows * 0.35)));
582
+ const notesRows = surface.notes.length > 0 ? Math.min(4, surface.notes.length + 1) : 0;
583
+ const [badgeRow, promptRow, ctxArea, gapRow, widgetArea, noteArea] = split(pad, "rows", [
584
+ 1,
585
+ 2,
586
+ maxContext,
587
+ 1,
588
+ -1,
589
+ notesRows,
590
+ ]);
591
+ if (badgeRow) {
592
+ text(frame, badgeRow.x, badgeRow.y, badge, badgeStyle, badgeRow);
593
+ if (!isPending && surface.answer.status === "submitted") {
594
+ const valText = ` → ${truncate(JSON.stringify(surface.answer.value), Math.max(8, badgeRow.columns - 20))}`;
595
+ text(frame, badgeRow.x + widthOf(badge), badgeRow.y, valText, frame.theme.heading, badgeRow);
596
+ }
597
+ }
598
+ if (promptRow) {
599
+ const promptLines = md.lines(`**${surface.prompt}**`, promptRow.columns);
600
+ drawLines(frame, promptRow, promptLines, 0);
601
+ }
602
+ if (ctxArea && maxContext > 0) {
603
+ drawLines(frame, ctxArea, contextLines, 0);
604
+ }
605
+ if (gapRow)
606
+ rule(frame, gapRow.y, gapRow);
607
+ if (widgetArea && widgetArea.rows > 0) {
608
+ if (surface.mode === "select" || surface.mode === "confirm") {
609
+ const hint = isPending
610
+ ? "↑/↓ move · Enter submit · Esc cancel"
611
+ : "Answered (press Enter again to update)";
612
+ text(frame, widgetArea.x, widgetArea.y, hint, frame.theme.muted, widgetArea);
613
+ const listRect = {
614
+ x: widgetArea.x,
615
+ y: widgetArea.y + 1,
616
+ columns: widgetArea.columns,
617
+ rows: Math.max(1, widgetArea.rows - 1),
618
+ };
619
+ Listbox.Root(frame, {
620
+ id: `ask-select-${surface.id}`,
621
+ label: surface.prompt,
622
+ items: surface.options,
623
+ value: surface.draftSelect,
624
+ onHighlight: (val) => {
625
+ surface.draftSelect = val;
626
+ },
627
+ onValueChange: (val, details) => {
628
+ // Human-only gate: reject any agent-sourced submission.
629
+ if (details.source === "agent")
630
+ return;
631
+ surface.draftSelect = val;
632
+ if (surface.mode === "confirm") {
633
+ store.submitAsk(surface.id, val === "confirm");
634
+ }
635
+ else {
636
+ store.submitAsk(surface.id, val);
637
+ }
638
+ },
639
+ }, (root) => Listbox.Content(root, listRect));
640
+ }
641
+ else if (surface.mode === "multiselect") {
642
+ const hint = "↑/↓ move · Space toggle · Enter submit · Esc cancel";
643
+ text(frame, widgetArea.x, widgetArea.y, hint, frame.theme.muted, widgetArea);
644
+ const listRect = {
645
+ x: widgetArea.x,
646
+ y: widgetArea.y + 1,
647
+ columns: widgetArea.columns,
648
+ rows: Math.max(1, widgetArea.rows - 1),
649
+ };
650
+ CheckboxGroup.Root(frame, {
651
+ id: `ask-multi-${surface.id}`,
652
+ label: surface.prompt,
653
+ items: surface.options,
654
+ value: surface.draftMulti,
655
+ onValueChange: (vals, details) => {
656
+ if (details.source === "agent")
657
+ return;
658
+ surface.draftMulti = [...vals];
659
+ },
660
+ onSubmit: (vals, key) => {
661
+ // `key` is null when an agent or imperative code calls submit(); require real human key.
662
+ if (!key)
663
+ return;
664
+ surface.draftMulti = [...vals];
665
+ store.submitAsk(surface.id, [...vals]);
666
+ },
667
+ }, (root) => CheckboxGroup.Content(root, listRect));
668
+ }
669
+ else {
670
+ const hint = "Type your answer · Enter submit · Shift+Enter newline · Esc cancel";
671
+ text(frame, widgetArea.x, widgetArea.y, hint, frame.theme.muted, widgetArea);
672
+ const boxRect = {
673
+ x: widgetArea.x,
674
+ y: widgetArea.y + 1,
675
+ columns: widgetArea.columns,
676
+ rows: Math.max(3, Math.min(8, widgetArea.rows - 1)),
677
+ };
678
+ const inside = drawBox(frame.cells, boxRect, { style: frame.styles.style(frame.theme.border) });
679
+ Textarea.Root(frame, {
680
+ id: `ask-text-${surface.id}`,
681
+ label: surface.prompt,
682
+ value: surface.draftText,
683
+ placeholder: surface.placeholder || "Type response…",
684
+ submitShortcut: "enter",
685
+ onValueChange: (val, details) => {
686
+ if (details.source === "agent")
687
+ return;
688
+ surface.draftText = val;
689
+ },
690
+ onSubmit: (val, key) => {
691
+ if (!key)
692
+ return;
693
+ surface.draftText = val;
694
+ store.submitAsk(surface.id, val);
695
+ },
696
+ }, (root) => Textarea.Field(root, inside));
697
+ }
698
+ }
699
+ if (noteArea && notesRows > 0)
700
+ drawNotesStrip(frame, noteArea, surface);
701
+ }
702
+ function drawFooter(frame, rect, active) {
703
+ rule(frame, rect.y, rect);
704
+ const barY = rect.y + 1;
705
+ if (barY >= rect.y + rect.rows)
706
+ return;
707
+ const now = Date.now();
708
+ if (store.toast && store.toast.until > now) {
709
+ const style = store.toast.tone === "success"
710
+ ? frame.theme.success
711
+ : store.toast.tone === "error"
712
+ ? frame.theme.destructive
713
+ : frame.theme.primary;
714
+ text(frame, rect.x + 1, barY, truncate(store.toast.text, rect.columns - 2), style, rect);
715
+ return;
716
+ }
717
+ const hints = [];
718
+ if (!active) {
719
+ hints.push("d demo", "? help", "q quit");
720
+ }
721
+ else {
722
+ if (store.surfaces.length > 1)
723
+ hints.push(`1-${Math.min(9, store.surfaces.length)} tabs`);
724
+ if (active.kind === "table" || active.kind === "log")
725
+ hints.push("/ filter");
726
+ if (active.kind === "table")
727
+ hints.push("r sort", "y copy");
728
+ if (active.kind === "file" || active.kind === "image")
729
+ hints.push("o open");
730
+ if (active.kind === "markdown" && active.checklist.length > 0)
731
+ hints.push("Tab checks");
732
+ hints.push("n note", "s send");
733
+ if (active.origin?.paneId)
734
+ hints.push("g origin");
735
+ hints.push("w close", "?");
736
+ }
737
+ text(frame, rect.x + 1, barY, truncate(hints.join(" · "), rect.columns - 2), frame.theme.muted, rect);
738
+ }
739
+ function drawOverlays(frame) {
740
+ if (noteDialogOpen) {
741
+ const active = store.activeSurface();
742
+ const dialogRect = center(frame.rect, Math.min(60, Math.max(28, frame.rect.columns - 6)), 7);
743
+ Dialog.Root(frame, {
744
+ id: "note-dialog",
745
+ label: "Add note",
746
+ title: `Note on ${active ? truncate(active.title, 26) : "surface"}`,
747
+ open: noteDialogOpen,
748
+ onOpenChange: (open) => {
749
+ noteDialogOpen = open;
750
+ },
751
+ }, (dlg) => Dialog.Content(dlg, dialogRect, (content) => {
752
+ const inner = inset(content.rect, [0, 1]);
753
+ text(frame, inner.x, inner.y, "Leave feedback for the agent (Enter saves, Esc closes):", frame.theme.muted, inner);
754
+ Input.Root(frame, {
755
+ id: "note-input",
756
+ label: "Note text",
757
+ value: noteDraft,
758
+ placeholder: "e.g. skip step 3, check p99 spike…",
759
+ onValueChange: (val) => {
760
+ noteDraft = val;
761
+ },
762
+ onSubmit: (val) => {
763
+ if (active && store.addNote(active.id, val)) {
764
+ store.setToast("Note saved (press 's' to push to agent pane)", "success");
765
+ }
766
+ noteDraft = "";
767
+ noteDialogOpen = false;
768
+ },
769
+ }, (root) => Input.Field(root, {
770
+ x: inner.x,
771
+ y: inner.y + 2,
772
+ columns: Math.max(8, inner.columns),
773
+ rows: 1,
774
+ }));
775
+ }));
776
+ }
777
+ if (helpOpen) {
778
+ const helpRect = center(frame.rect, Math.min(64, Math.max(34, frame.rect.columns - 6)), Math.min(18, frame.rect.rows - 2));
779
+ Dialog.Root(frame, {
780
+ id: "help-dialog",
781
+ label: "Keyboard shortcuts",
782
+ title: "bypane · Shortcuts",
783
+ open: helpOpen,
784
+ onOpenChange: (open) => {
785
+ helpOpen = open;
786
+ },
787
+ }, (dlg) => Dialog.Content(dlg, helpRect, (content) => {
788
+ const inner = inset(content.rect, [0, 1]);
789
+ const rows = [
790
+ ["1–9 / [ / ]", "Switch surfaces (or click tabs)"],
791
+ ["Tab", "Cycle focus (body / checklist / filter / tabs)"],
792
+ ["/ or Ctrl+F", "Filter table or log lines live"],
793
+ ["r", "Cycle sort column / direction (tables)"],
794
+ ["n or Ctrl+E", "Attach a note to the active surface"],
795
+ ["s or Ctrl+S", "Push structured feedback back to agent pane"],
796
+ ["o or Ctrl+O", "Open file/image on host (rex open / open)"],
797
+ ["g", "Jump focus to originating Rex/tmux pane"],
798
+ ["y", "Copy selected row / path / feedback to clipboard"],
799
+ ["w or Ctrl+W", "Close active surface tab"],
800
+ ["d", "Load interactive demo workspace"],
801
+ ["? / Esc", "Toggle / close dialogs"],
802
+ ];
803
+ for (let i = 0; i < rows.length && i < inner.rows; i++) {
804
+ const [k, desc] = rows[i];
805
+ text(frame, inner.x, inner.y + i, k, frame.theme.primary, inner);
806
+ text(frame, inner.x + 16, inner.y + i, truncate(desc, Math.max(8, inner.columns - 16)), frame.theme.muted, inner);
807
+ }
808
+ }));
809
+ }
810
+ }
811
+ function view(frame) {
812
+ const active = store.activeSurface();
813
+ const headerRows = store.surfaces.length > 0 ? 3 : 2;
814
+ const [headerRect, bodyRect, footerRect] = split(frame.rect, "rows", [headerRows, -1, 2]);
815
+ if (headerRect)
816
+ drawHeader(frame, headerRect, active);
817
+ // Draw the active surface BEFORE Tabs.List so default keyboard focus lands immediately on the content/ask widget!
818
+ if (bodyRect && bodyRect.rows > 0) {
819
+ if (!active) {
820
+ drawEmptyState(frame, bodyRect);
821
+ }
822
+ else if (active.kind === "markdown") {
823
+ drawMarkdownSurface(frame, bodyRect, active);
824
+ }
825
+ else if (active.kind === "file") {
826
+ drawFileSurface(frame, bodyRect, active);
827
+ }
828
+ else if (active.kind === "image") {
829
+ drawImageSurface(frame, bodyRect, active);
830
+ }
831
+ else if (active.kind === "table") {
832
+ drawTableSurface(frame, bodyRect, active);
833
+ }
834
+ else if (active.kind === "chart") {
835
+ drawChartSurface(frame, bodyRect, active);
836
+ }
837
+ else if (active.kind === "log") {
838
+ drawLogSurface(frame, bodyRect, active);
839
+ }
840
+ else if (active.kind === "ask") {
841
+ drawAskSurface(frame, bodyRect, active);
842
+ }
843
+ }
844
+ if (headerRect)
845
+ drawTabsBar(frame, headerRect);
846
+ if (footerRect)
847
+ drawFooter(frame, footerRect, active);
848
+ drawOverlays(frame);
849
+ }
850
+ function triggerSendFeedback(frame, active) {
851
+ const summary = formatFeedbackSummary(active);
852
+ const res = hooks.onSendFeedback
853
+ ? hooks.onSendFeedback(active, summary)
854
+ : sendToOriginPane(active.origin, summary);
855
+ if (res.ok) {
856
+ store.setToast(`Sent feedback via ${res.via}`, "success");
857
+ }
858
+ else {
859
+ frame.copy(summary);
860
+ store.setToast("Copied feedback to clipboard (no mux origin pane)", "info");
861
+ }
862
+ }
863
+ function triggerOpenExternal(active) {
864
+ if (active.kind !== "file" && active.kind !== "image") {
865
+ store.setToast("Only file and image surfaces can be opened externally", "info");
866
+ return;
867
+ }
868
+ const res = hooks.onOpenExternal ? hooks.onOpenExternal(active.path) : openExternally(active.path);
869
+ if (res.ok) {
870
+ store.setToast(`Opened via ${res.via}: ${active.title}`, "success");
871
+ }
872
+ else {
873
+ store.setToast(`Open failed: ${res.error ?? "unknown error"}`, "error");
874
+ }
875
+ }
876
+ function keys(key, frame) {
877
+ const active = store.activeSurface();
878
+ // Escape exits filter bar or cancels a pending ask if nothing else trapped it.
879
+ if (key.name === "escape") {
880
+ if (filterFocused) {
881
+ filterFocused = false;
882
+ frame.focused = "";
883
+ return true;
884
+ }
885
+ if (active && active.kind === "ask" && active.answer.status === "pending") {
886
+ store.cancelAsk(active.id);
887
+ return true;
888
+ }
889
+ return false;
890
+ }
891
+ // Ctrl shortcuts work reliably even from inside Lists / Tables (never collide with typeahead or Ctrl+N/P nav).
892
+ if ((matchesShortcut(key, "ctrl+e") || matchesShortcut(key, "alt+n")) && active) {
893
+ noteDraft = "";
894
+ noteDialogOpen = true;
895
+ frame.focused = "";
896
+ return true;
897
+ }
898
+ if (matchesShortcut(key, "ctrl+s") && active) {
899
+ triggerSendFeedback(frame, active);
900
+ return true;
901
+ }
902
+ if (matchesShortcut(key, "ctrl+o") && active) {
903
+ triggerOpenExternal(active);
904
+ return true;
905
+ }
906
+ if (matchesShortcut(key, "ctrl+w") && active) {
907
+ store.close(active.id);
908
+ filterFocused = false;
909
+ frame.focused = "";
910
+ return true;
911
+ }
912
+ if (matchesShortcut(key, "ctrl+f") && active && (active.kind === "table" || active.kind === "log")) {
913
+ filterFocused = true;
914
+ frame.focused = "";
915
+ return true;
916
+ }
917
+ if (key.name !== "char" || key.ctrl || key.alt) {
918
+ return false;
919
+ }
920
+ const ch = key.text;
921
+ if (ch === "?") {
922
+ helpOpen = !helpOpen;
923
+ return true;
924
+ }
925
+ if (ch === "[") {
926
+ filterFocused = false;
927
+ return store.focusDelta(-1);
928
+ }
929
+ if (ch === "]") {
930
+ filterFocused = false;
931
+ return store.focusDelta(1);
932
+ }
933
+ if (ch >= "1" && ch <= "9") {
934
+ const idx = Number(ch) - 1;
935
+ if (idx < store.surfaces.length) {
936
+ filterFocused = false;
937
+ return store.focusIndex(idx);
938
+ }
939
+ }
940
+ if (ch === "/" && active && (active.kind === "table" || active.kind === "log")) {
941
+ filterFocused = true;
942
+ frame.focused = "";
943
+ return true;
944
+ }
945
+ if (ch === "n" && active) {
946
+ noteDraft = "";
947
+ noteDialogOpen = true;
948
+ frame.focused = "";
949
+ return true;
950
+ }
951
+ if (ch === "s" && active) {
952
+ triggerSendFeedback(frame, active);
953
+ return true;
954
+ }
955
+ if (ch === "o" && active) {
956
+ triggerOpenExternal(active);
957
+ return true;
958
+ }
959
+ if (ch === "g" && active?.origin?.paneId) {
960
+ const res = hooks.onFocusOrigin ? hooks.onFocusOrigin(active) : focusOriginPane(active.origin);
961
+ store.setToast(res.ok ? `Focused origin via ${res.via}` : `Focus failed: ${res.error}`, res.ok ? "info" : "error");
962
+ return true;
963
+ }
964
+ if (ch === "y" && active) {
965
+ const textToCopy = active.kind === "file" || active.kind === "image"
966
+ ? active.path
967
+ : formatFeedbackSummary(active);
968
+ frame.copy(textToCopy);
969
+ store.setToast("Copied to clipboard", "success", 1800);
970
+ return true;
971
+ }
972
+ if (ch === "r" && active?.kind === "table" && active.columns.length > 0) {
973
+ if (active.sortColumn < 0) {
974
+ active.sortColumn = 0;
975
+ active.sortDir = "asc";
976
+ }
977
+ else if (active.sortDir === "asc") {
978
+ active.sortDir = "desc";
979
+ }
980
+ else {
981
+ active.sortColumn = (active.sortColumn + 1) % active.columns.length;
982
+ active.sortDir = "asc";
983
+ }
984
+ const col = active.columns[active.sortColumn];
985
+ store.setToast(`Sorted by ${col?.label ?? ""} (${active.sortDir})`, "info", 1600);
986
+ return true;
987
+ }
988
+ if (ch === "w" && active) {
989
+ store.close(active.id);
990
+ filterFocused = false;
991
+ return true;
992
+ }
993
+ if (ch === "d" && store.surfaces.length === 0) {
994
+ loadDemoSurfaces(store);
995
+ store.setToast("Loaded demo workspace", "success", 2000);
996
+ return true;
997
+ }
998
+ if (ch === "q" && key.alone) {
999
+ hooks.onQuit?.();
1000
+ return true;
1001
+ }
1002
+ return false;
1003
+ }
1004
+ function allowAgentStep(step) {
1005
+ // Strictly prevent agents from answering or submitting human-only `ask` gates or editing human notes.
1006
+ if (step.ref.startsWith("ask-")) {
1007
+ return step.action === "highlight" || step.action === "focus";
1008
+ }
1009
+ if (step.ref.startsWith("note-") || step.component === "dialog") {
1010
+ return false;
1011
+ }
1012
+ // Also block untargeted key presses/typing while a pending ask surface is active.
1013
+ const active = store.activeSurface();
1014
+ if (!step.ref && active?.kind === "ask" && active.answer.status === "pending") {
1015
+ return false;
1016
+ }
1017
+ return true;
1018
+ }
1019
+ return { view, keys, allowAgentStep };
1020
+ }