@bypane/cli 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +188 -2
- package/dist/app-host.js +408 -0
- package/dist/app-protocol.js +1 -0
- package/dist/app-worker.js +556 -0
- package/dist/cli.js +810 -0
- package/dist/config.js +42 -0
- package/dist/demo.js +430 -0
- package/dist/expr.js +460 -0
- package/dist/ipc.js +201 -0
- package/dist/mcp.js +416 -0
- package/dist/multiplexer.js +281 -0
- package/dist/png.js +208 -0
- package/dist/primitives.js +669 -0
- package/dist/selection.js +170 -0
- package/dist/setup.js +329 -0
- package/dist/skill.js +219 -0
- package/dist/state.js +1147 -0
- package/dist/types.js +1 -0
- package/dist/ui-surface.js +744 -0
- package/dist/ui.js +1466 -0
- package/examples/spring-studio.ts +420 -0
- package/package.json +58 -4
- package/skills/bypane/SKILL.md +136 -0
package/dist/ui.js
ADDED
|
@@ -0,0 +1,1466 @@
|
|
|
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, wrap, } from "tui";
|
|
2
|
+
import { AppHostManager } from "./app-host.js";
|
|
3
|
+
import { loadDemoSurfaces } from "./demo.js";
|
|
4
|
+
import { focusOriginPane, openExternally, sendToOriginPane } from "./multiplexer.js";
|
|
5
|
+
import { extractSelectionText, markdownLineText, paintSelectionHighlight, wordBoundsAtColumn, } from "./selection.js";
|
|
6
|
+
import { captureNoteContext, formatBytes, formatFeedbackSummary, sortTableRows } from "./state.js";
|
|
7
|
+
import { drawUiSurface, uiFooterHints } from "./ui-surface.js";
|
|
8
|
+
const SERIES_COLORS = ["#7aa2ff", "#5fd787", "#f5a623", "#ff7a90", "#bb9af7", "#7dcfff"];
|
|
9
|
+
export const bypaneTheme = {
|
|
10
|
+
...defaultTheme,
|
|
11
|
+
primary: { fg: "#7aa2ff", bold: true },
|
|
12
|
+
success: { fg: "#5fd787", bold: true },
|
|
13
|
+
destructive: { fg: "#ff7a90", bold: true },
|
|
14
|
+
muted: { fg: "#8b93a6" },
|
|
15
|
+
heading: { fg: "#e6edf7", bold: true },
|
|
16
|
+
border: { fg: "#465066" },
|
|
17
|
+
code: { fg: "#7dcfff" },
|
|
18
|
+
link: { fg: "#7aa2ff", underline: true },
|
|
19
|
+
match: { fg: "#10141c", bg: "#f5a623", bold: true },
|
|
20
|
+
selected: { fg: "#5fd787", bold: true },
|
|
21
|
+
highlight: { fg: "#0e131d", bg: "#7aa2ff", bold: true },
|
|
22
|
+
};
|
|
23
|
+
export function createBypaneUi(store, socketPath, hooks = {}) {
|
|
24
|
+
let noteDialogOpen = false;
|
|
25
|
+
let noteDraft = "";
|
|
26
|
+
let helpOpen = false;
|
|
27
|
+
let filterFocused = false;
|
|
28
|
+
// Per-surface render caches
|
|
29
|
+
const imageCache = new Map();
|
|
30
|
+
const chartCache = new Map();
|
|
31
|
+
const canvasCache = new Map();
|
|
32
|
+
const filterCache = new Map();
|
|
33
|
+
const tabSpring = createSpring(0, { stiffness: 260, damping: 26 });
|
|
34
|
+
let mdEngine = null;
|
|
35
|
+
let lastActiveId = "";
|
|
36
|
+
let lastFrame = null;
|
|
37
|
+
const lastWorkerFocus = new Map();
|
|
38
|
+
const focusedAppRev = new Map();
|
|
39
|
+
const dragSelections = new Map();
|
|
40
|
+
function clearSelection(surface) {
|
|
41
|
+
if (!surface)
|
|
42
|
+
return;
|
|
43
|
+
dragSelections.delete(surface.id);
|
|
44
|
+
if (surface.kind === "markdown" || surface.kind === "file") {
|
|
45
|
+
surface.selection = undefined;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function attachTextSelection(frame, surface, vpRect, offset, scrollTo, plainLines, textStartX, layoutWidth, joinWrapped, trackLineNumbers) {
|
|
49
|
+
let sel = dragSelections.get(surface.id);
|
|
50
|
+
if (sel && joinWrapped && sel.layoutWidth !== layoutWidth) {
|
|
51
|
+
dragSelections.delete(surface.id);
|
|
52
|
+
surface.selection = undefined;
|
|
53
|
+
sel = undefined;
|
|
54
|
+
}
|
|
55
|
+
if (sel && (sel.dragging || surface.selection?.text)) {
|
|
56
|
+
paintSelectionHighlight(frame, vpRect, offset, textStartX, sel, plainLines.length);
|
|
57
|
+
}
|
|
58
|
+
if (!frame.mouse || plainLines.length === 0 || vpRect.rows <= 0 || vpRect.columns <= 0)
|
|
59
|
+
return;
|
|
60
|
+
const maxTextCol = Math.max(0, vpRect.x + vpRect.columns - textStartX - 1);
|
|
61
|
+
frame.regions.push({
|
|
62
|
+
rect: vpRect,
|
|
63
|
+
owner: frame.owner,
|
|
64
|
+
trapped: frame.trapDepth > 0,
|
|
65
|
+
press: null,
|
|
66
|
+
mouse: (k) => {
|
|
67
|
+
const m = k.mouse;
|
|
68
|
+
if (!m)
|
|
69
|
+
return false;
|
|
70
|
+
// Let ScrollArea handle wheel scrolling cleanly.
|
|
71
|
+
if (m.action === "scroll-up" ||
|
|
72
|
+
m.action === "scroll-down" ||
|
|
73
|
+
m.action === "scroll-left" ||
|
|
74
|
+
m.action === "scroll-right") {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
const docRow = Math.max(0, Math.min(plainLines.length - 1, offset + (m.y - vpRect.y)));
|
|
78
|
+
const docCol = Math.max(0, Math.min(maxTextCol, m.x - textStartX));
|
|
79
|
+
const cur = dragSelections.get(surface.id);
|
|
80
|
+
if (m.action === "press" && m.button === "left") {
|
|
81
|
+
const now = Date.now();
|
|
82
|
+
const isDouble = cur?.lastClickAt !== undefined &&
|
|
83
|
+
now - cur.lastClickAt < 380 &&
|
|
84
|
+
cur.lastClickRow === docRow &&
|
|
85
|
+
Math.abs((cur.lastClickCol ?? -99) - docCol) <= 1;
|
|
86
|
+
if (isDouble) {
|
|
87
|
+
const bounds = wordBoundsAtColumn(plainLines[docRow] ?? "", docCol);
|
|
88
|
+
if (bounds) {
|
|
89
|
+
const wordSel = {
|
|
90
|
+
anchorRow: docRow,
|
|
91
|
+
anchorCol: bounds.startCol,
|
|
92
|
+
focusRow: docRow,
|
|
93
|
+
focusCol: bounds.endCol,
|
|
94
|
+
dragging: false,
|
|
95
|
+
layoutWidth,
|
|
96
|
+
lastClickAt: 0,
|
|
97
|
+
};
|
|
98
|
+
dragSelections.set(surface.id, wordSel);
|
|
99
|
+
const extracted = extractSelectionText(plainLines, wordSel, joinWrapped);
|
|
100
|
+
surface.selection = extracted.text
|
|
101
|
+
? {
|
|
102
|
+
text: extracted.text,
|
|
103
|
+
...(trackLineNumbers
|
|
104
|
+
? { startLine: extracted.startRow + 1, endLine: extracted.endRow + 1 }
|
|
105
|
+
: {}),
|
|
106
|
+
}
|
|
107
|
+
: undefined;
|
|
108
|
+
if (surface.selection) {
|
|
109
|
+
store.setToast("Selected word — press 'n' to comment, 'y' to copy, Esc to clear", "info", 2600);
|
|
110
|
+
}
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
surface.selection = undefined;
|
|
115
|
+
dragSelections.set(surface.id, {
|
|
116
|
+
anchorRow: docRow,
|
|
117
|
+
anchorCol: docCol,
|
|
118
|
+
focusRow: docRow,
|
|
119
|
+
focusCol: docCol,
|
|
120
|
+
dragging: true,
|
|
121
|
+
layoutWidth,
|
|
122
|
+
lastClickAt: now,
|
|
123
|
+
lastClickRow: docRow,
|
|
124
|
+
lastClickCol: docCol,
|
|
125
|
+
});
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
if (m.action === "drag" && m.button === "left" && cur?.dragging) {
|
|
129
|
+
if (m.y <= vpRect.y && offset > 0) {
|
|
130
|
+
scrollTo(offset - 1);
|
|
131
|
+
}
|
|
132
|
+
else if (m.y >= vpRect.y + vpRect.rows - 1 && offset + vpRect.rows < plainLines.length) {
|
|
133
|
+
scrollTo(offset + 1);
|
|
134
|
+
}
|
|
135
|
+
cur.focusRow = docRow;
|
|
136
|
+
cur.focusCol = docCol;
|
|
137
|
+
const extracted = extractSelectionText(plainLines, cur, joinWrapped);
|
|
138
|
+
surface.selection = extracted.text
|
|
139
|
+
? {
|
|
140
|
+
text: extracted.text,
|
|
141
|
+
...(trackLineNumbers
|
|
142
|
+
? { startLine: extracted.startRow + 1, endLine: extracted.endRow + 1 }
|
|
143
|
+
: {}),
|
|
144
|
+
}
|
|
145
|
+
: undefined;
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
if (m.action === "release" && cur?.dragging) {
|
|
149
|
+
cur.dragging = false;
|
|
150
|
+
if (cur.anchorRow === cur.focusRow && cur.anchorCol === cur.focusCol) {
|
|
151
|
+
surface.selection = undefined;
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
const extracted = extractSelectionText(plainLines, cur, joinWrapped);
|
|
155
|
+
surface.selection = extracted.text
|
|
156
|
+
? {
|
|
157
|
+
text: extracted.text,
|
|
158
|
+
...(trackLineNumbers
|
|
159
|
+
? { startLine: extracted.startRow + 1, endLine: extracted.endRow + 1 }
|
|
160
|
+
: {}),
|
|
161
|
+
}
|
|
162
|
+
: undefined;
|
|
163
|
+
if (surface.selection) {
|
|
164
|
+
store.setToast("Selected fragment — press 'n' to comment, 'y' to copy, Esc to clear", "info", 2600);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
return false;
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
const appHost = new AppHostManager(store, {
|
|
174
|
+
onRedraw: () => {
|
|
175
|
+
lastFrame?.redraw();
|
|
176
|
+
},
|
|
177
|
+
onSubmit: (surface) => {
|
|
178
|
+
if (lastFrame && (surface.origin?.paneId || hooks.onSendFeedback)) {
|
|
179
|
+
triggerSendFeedback(lastFrame, surface);
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
onSend: (surface, message) => {
|
|
183
|
+
if (!lastFrame)
|
|
184
|
+
return;
|
|
185
|
+
const base = formatFeedbackSummary(surface);
|
|
186
|
+
const payload = message.trim() ? `${base}\n${message.trim()}` : base;
|
|
187
|
+
const res = hooks.onSendFeedback
|
|
188
|
+
? hooks.onSendFeedback(surface, payload)
|
|
189
|
+
: sendToOriginPane(surface.origin, payload);
|
|
190
|
+
if (res.ok) {
|
|
191
|
+
const now = Date.now();
|
|
192
|
+
for (const n of surface.notes) {
|
|
193
|
+
if (n.sentAt === undefined)
|
|
194
|
+
n.sentAt = now;
|
|
195
|
+
}
|
|
196
|
+
store.setToast(`Sent update via ${res.via}`, "success");
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
lastFrame.copy(payload);
|
|
200
|
+
store.setToast("Copied update to clipboard (no mux origin pane)", "info");
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
onCopy: (val) => {
|
|
204
|
+
lastFrame?.copy(val);
|
|
205
|
+
store.setToast("Copied to clipboard", "success", 1800);
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
const unsubscribeStore = store.subscribe(() => {
|
|
209
|
+
appHost.reconcile(store.activeSurface()?.id ?? "");
|
|
210
|
+
});
|
|
211
|
+
function getMarkdown(frame) {
|
|
212
|
+
if (!mdEngine) {
|
|
213
|
+
mdEngine = createMarkdown(frame.styles, frame.theme);
|
|
214
|
+
}
|
|
215
|
+
return mdEngine;
|
|
216
|
+
}
|
|
217
|
+
function getFilter(id) {
|
|
218
|
+
let f = filterCache.get(id);
|
|
219
|
+
if (!f) {
|
|
220
|
+
f = createFilter();
|
|
221
|
+
filterCache.set(id, f);
|
|
222
|
+
}
|
|
223
|
+
return f;
|
|
224
|
+
}
|
|
225
|
+
function getChart(id) {
|
|
226
|
+
let c = chartCache.get(id);
|
|
227
|
+
if (!c) {
|
|
228
|
+
c = createChart();
|
|
229
|
+
chartCache.set(id, c);
|
|
230
|
+
}
|
|
231
|
+
return c;
|
|
232
|
+
}
|
|
233
|
+
function getImage(surface) {
|
|
234
|
+
if (!surface.rgba || !surface.width || !surface.height)
|
|
235
|
+
return null;
|
|
236
|
+
const cacheKey = `${surface.updatedAt}:${surface.width}x${surface.height}`;
|
|
237
|
+
const cached = imageCache.get(surface.id);
|
|
238
|
+
if (cached && cached.key === cacheKey)
|
|
239
|
+
return cached.image;
|
|
240
|
+
const img = createImage(surface.rgba, surface.width, surface.height);
|
|
241
|
+
imageCache.set(surface.id, { key: cacheKey, image: img });
|
|
242
|
+
return img;
|
|
243
|
+
}
|
|
244
|
+
function rule(frame, y, rect) {
|
|
245
|
+
if (y < rect.y || y >= rect.y + rect.rows)
|
|
246
|
+
return;
|
|
247
|
+
text(frame, rect.x, y, "─".repeat(Math.max(0, rect.columns)), frame.theme.border, rect);
|
|
248
|
+
}
|
|
249
|
+
function badgeText(surface) {
|
|
250
|
+
if (surface.kind === "ask") {
|
|
251
|
+
return surface.answer.status === "pending"
|
|
252
|
+
? "● "
|
|
253
|
+
: surface.answer.status === "submitted"
|
|
254
|
+
? "✓ "
|
|
255
|
+
: "× ";
|
|
256
|
+
}
|
|
257
|
+
if (surface.kind === "ui" && surface.submission.status !== "idle") {
|
|
258
|
+
return surface.submission.status === "pending"
|
|
259
|
+
? "● "
|
|
260
|
+
: surface.submission.status === "submitted"
|
|
261
|
+
? "✓ "
|
|
262
|
+
: "× ";
|
|
263
|
+
}
|
|
264
|
+
if (surface.kind === "app") {
|
|
265
|
+
if (surface.runtimeStatus === "error")
|
|
266
|
+
return "! ";
|
|
267
|
+
if (surface.submission.status !== "idle") {
|
|
268
|
+
return surface.submission.status === "pending"
|
|
269
|
+
? "● "
|
|
270
|
+
: surface.submission.status === "submitted"
|
|
271
|
+
? "✓ "
|
|
272
|
+
: "× ";
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return surface.unread ? "• " : "";
|
|
276
|
+
}
|
|
277
|
+
function computeTabLabels(columns) {
|
|
278
|
+
const count = store.surfaces.length;
|
|
279
|
+
if (count === 0)
|
|
280
|
+
return [];
|
|
281
|
+
const avail = Math.max(12, columns - 2);
|
|
282
|
+
// Reserve space for padding (" " + label + " ") and index/badge prefix ("1:• ") so all tabs fit cleanly.
|
|
283
|
+
const perTabBudget = Math.max(7, Math.floor(avail / count));
|
|
284
|
+
const maxTitleChars = Math.max(3, Math.min(20, perTabBudget - 6));
|
|
285
|
+
let x = 1;
|
|
286
|
+
return store.surfaces.map((s, i) => {
|
|
287
|
+
const prefix = `${i < 9 ? `${i + 1}:` : ""}${badgeText(s)}`;
|
|
288
|
+
const label = `${prefix}${truncate(s.title, maxTitleChars)}`;
|
|
289
|
+
const width = widthOf(` ${label} `);
|
|
290
|
+
const out = { value: s.id, label, x, width };
|
|
291
|
+
x += width;
|
|
292
|
+
return out;
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
function drawHeader(frame, rect, active) {
|
|
296
|
+
const brand = "◈ bypane";
|
|
297
|
+
const accentStyle = frame.theme.primary;
|
|
298
|
+
const mutedStyle = frame.theme.muted;
|
|
299
|
+
text(frame, rect.x + 1, rect.y, brand, accentStyle, rect);
|
|
300
|
+
const shortSession = store.session.startsWith("rex-")
|
|
301
|
+
? "rex"
|
|
302
|
+
: store.session.startsWith("tmux-")
|
|
303
|
+
? "tmux"
|
|
304
|
+
: store.session.length > 16
|
|
305
|
+
? `${store.session.slice(0, 14)}…`
|
|
306
|
+
: store.session;
|
|
307
|
+
const rightTag = active?.origin?.label
|
|
308
|
+
? `${active.origin.label} · ${shortSession}`
|
|
309
|
+
: `${store.surfaces.length} ${store.surfaces.length === 1 ? "surface" : "surfaces"} · ${shortSession}`;
|
|
310
|
+
const rightWidth = widthOf(rightTag);
|
|
311
|
+
const brandWidth = widthOf(brand) + 2;
|
|
312
|
+
if (rect.columns > brandWidth + rightWidth + 2) {
|
|
313
|
+
text(frame, rect.x + rect.columns - rightWidth - 1, rect.y, rightTag, mutedStyle, rect);
|
|
314
|
+
}
|
|
315
|
+
const ruleY = rect.y + rect.rows - 1;
|
|
316
|
+
rule(frame, ruleY, rect);
|
|
317
|
+
if (store.surfaces.length > 0 && rect.columns > 8) {
|
|
318
|
+
const tabs = computeTabLabels(rect.columns);
|
|
319
|
+
const activeTab = tabs.find((t) => t.value === store.activeId) ?? tabs[0];
|
|
320
|
+
if (store.activeId !== lastActiveId) {
|
|
321
|
+
const isFirst = lastActiveId === "";
|
|
322
|
+
lastActiveId = store.activeId;
|
|
323
|
+
if (isFirst) {
|
|
324
|
+
tabSpring.set(activeTab.x);
|
|
325
|
+
}
|
|
326
|
+
else {
|
|
327
|
+
tabSpring.to(activeTab.x);
|
|
328
|
+
}
|
|
329
|
+
// Reset focus so the newly active surface (e.g. an incoming `ask` gate) takes focus immediately.
|
|
330
|
+
frame.focused = "";
|
|
331
|
+
}
|
|
332
|
+
else if (tabSpring.target !== activeTab.x) {
|
|
333
|
+
// Pane resized (e.g. Rex zoom) or badge/label width changed: snap immediately to the new tab position.
|
|
334
|
+
tabSpring.set(activeTab.x);
|
|
335
|
+
}
|
|
336
|
+
const springX = Math.round(tabSpring.update(frame));
|
|
337
|
+
const segW = Math.max(3, Math.min(activeTab.width, rect.columns - 1));
|
|
338
|
+
const xStart = Math.max(rect.x, Math.min(rect.x + rect.columns - segW, rect.x + springX));
|
|
339
|
+
text(frame, xStart, ruleY, "━".repeat(segW), accentStyle, rect);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
function drawTabsBar(frame, rect) {
|
|
343
|
+
if (store.surfaces.length === 0 || rect.rows < 3)
|
|
344
|
+
return;
|
|
345
|
+
const tabs = computeTabLabels(rect.columns);
|
|
346
|
+
const tabRect = {
|
|
347
|
+
x: rect.x + 1,
|
|
348
|
+
y: rect.y + 1,
|
|
349
|
+
columns: Math.max(8, rect.columns - 2),
|
|
350
|
+
rows: 1,
|
|
351
|
+
};
|
|
352
|
+
Tabs.Root(frame, {
|
|
353
|
+
id: "workspace-tabs",
|
|
354
|
+
label: "Surfaces",
|
|
355
|
+
items: tabs.map((t) => ({ value: t.value, label: t.label })),
|
|
356
|
+
value: store.activeId,
|
|
357
|
+
onValueChange: (val) => {
|
|
358
|
+
store.focus(val);
|
|
359
|
+
filterFocused = false;
|
|
360
|
+
},
|
|
361
|
+
}, (root) => Tabs.List(root, tabRect));
|
|
362
|
+
}
|
|
363
|
+
function drawEmptyState(frame, body) {
|
|
364
|
+
const pad = inset(body, [1, 2]);
|
|
365
|
+
if (pad.rows < 4 || pad.columns < 16)
|
|
366
|
+
return;
|
|
367
|
+
let y = pad.y;
|
|
368
|
+
text(frame, pad.x, y++, "Companion workspace ready.", frame.theme.heading, pad);
|
|
369
|
+
text(frame, pad.x, y++, "Push docs, images, tables, charts, logs, or decisions from any agent or shell:", frame.theme.muted, pad);
|
|
370
|
+
y++;
|
|
371
|
+
const examples = [
|
|
372
|
+
["bypane show README.md", "open Markdown or code with line highlights"],
|
|
373
|
+
["bypane show screenshot.png", "QuickLook PNG inline (or 'o' to open on host)"],
|
|
374
|
+
["cat rows.json | bypane table", "interactive filterable table"],
|
|
375
|
+
["bypane ask --select ...", "human-gated decision prompt"],
|
|
376
|
+
["bypane mcp", "connect any MCP-capable coding agent"],
|
|
377
|
+
];
|
|
378
|
+
for (const [cmd, desc] of examples) {
|
|
379
|
+
if (y >= pad.y + pad.rows - 3)
|
|
380
|
+
break;
|
|
381
|
+
const col = text(frame, pad.x + 2, y, cmd, frame.theme.code, pad);
|
|
382
|
+
if (pad.columns > 48) {
|
|
383
|
+
text(frame, Math.max(col + 2, pad.x + 32), y, desc, frame.theme.muted, pad);
|
|
384
|
+
}
|
|
385
|
+
y++;
|
|
386
|
+
}
|
|
387
|
+
y++;
|
|
388
|
+
if (y < pad.y + pad.rows - 1) {
|
|
389
|
+
text(frame, pad.x, y++, `Session: ${store.session}`, frame.theme.muted, pad);
|
|
390
|
+
text(frame, pad.x, y++, `Socket: ${truncate(socketPath, Math.max(10, pad.columns - 10))}`, frame.theme.muted, pad);
|
|
391
|
+
}
|
|
392
|
+
if (y + 1 < pad.y + pad.rows) {
|
|
393
|
+
text(frame, pad.x, y + 1, "Press [d] to load an interactive demo workspace.", frame.theme.primary, pad);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
function drawNotesStrip(frame, rect, surface) {
|
|
397
|
+
if (rect.rows <= 0 || surface.notes.length === 0)
|
|
398
|
+
return;
|
|
399
|
+
rule(frame, rect.y, rect);
|
|
400
|
+
text(frame, rect.x + 1, rect.y, ` Notes (${surface.notes.length}) `, frame.theme.primary, rect);
|
|
401
|
+
const avail = Math.max(8, rect.columns - 4);
|
|
402
|
+
const maxCtx = Math.max(10, Math.floor(avail * 0.45));
|
|
403
|
+
for (let i = 0; i < Math.min(rect.rows - 1, surface.notes.length); i++) {
|
|
404
|
+
const note = surface.notes[surface.notes.length - 1 - i];
|
|
405
|
+
const bullet = note.sentAt !== undefined ? "✓" : "•";
|
|
406
|
+
const line = note.context ? `[${truncate(note.context, maxCtx)}] ${note.text}` : note.text;
|
|
407
|
+
text(frame, rect.x + 2, rect.y + 1 + i, `${bullet} ${truncate(line, avail)}`, frame.theme.muted, rect);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
function drawMarkdownSurface(frame, body, surface) {
|
|
411
|
+
const hasChecklist = surface.checklist.length > 0;
|
|
412
|
+
const checklistRows = hasChecklist ? Math.min(7, surface.checklist.length + 2) : 0;
|
|
413
|
+
const notesRows = surface.notes.length > 0 ? Math.min(4, surface.notes.length + 1) : 0;
|
|
414
|
+
const [docArea, checkArea, noteArea] = split(body, "rows", [-1, checklistRows, notesRows]);
|
|
415
|
+
if (docArea && docArea.rows > 0) {
|
|
416
|
+
const inner = inset(docArea, [0, 1]);
|
|
417
|
+
const layoutWidth = Math.max(10, inner.columns - 1);
|
|
418
|
+
const md = getMarkdown(frame);
|
|
419
|
+
const lines = md.lines(surface.content, layoutWidth);
|
|
420
|
+
const plainLines = lines.map(markdownLineText);
|
|
421
|
+
ScrollArea.Root(frame, {
|
|
422
|
+
id: `md-scroll-${surface.id}`,
|
|
423
|
+
label: `${surface.title} document`,
|
|
424
|
+
total: lines.length,
|
|
425
|
+
}, (area) => ScrollArea.Viewport(area, inner, (vp) => {
|
|
426
|
+
const offset = vp.api.offset();
|
|
427
|
+
drawLines(frame, vp.rect, lines, offset);
|
|
428
|
+
attachTextSelection(frame, surface, vp.rect, offset, (n) => vp.api.scrollTo(n), plainLines, vp.rect.x, layoutWidth, true, false);
|
|
429
|
+
}));
|
|
430
|
+
}
|
|
431
|
+
if (hasChecklist && checkArea && checkArea.rows >= 2) {
|
|
432
|
+
rule(frame, checkArea.y, checkArea);
|
|
433
|
+
const done = surface.checklist.filter((c) => c.checked).length;
|
|
434
|
+
text(frame, checkArea.x + 1, checkArea.y, ` Checklist (${done}/${surface.checklist.length}) · Tab to toggle `, frame.theme.primary, checkArea);
|
|
435
|
+
const listRect = {
|
|
436
|
+
x: checkArea.x + 1,
|
|
437
|
+
y: checkArea.y + 1,
|
|
438
|
+
columns: Math.max(4, checkArea.columns - 2),
|
|
439
|
+
rows: Math.max(1, checkArea.rows - 1),
|
|
440
|
+
};
|
|
441
|
+
const checkedValues = surface.checklist
|
|
442
|
+
.filter((c) => c.checked)
|
|
443
|
+
.map((c) => String(c.index));
|
|
444
|
+
CheckboxGroup.Root(frame, {
|
|
445
|
+
id: `md-check-${surface.id}`,
|
|
446
|
+
label: `${surface.title} checklist`,
|
|
447
|
+
items: surface.checklist.map((c) => ({ value: String(c.index), label: c.text })),
|
|
448
|
+
value: checkedValues,
|
|
449
|
+
onValueChange: (vals) => {
|
|
450
|
+
store.updateChecklist(surface.id, vals);
|
|
451
|
+
},
|
|
452
|
+
}, (root) => CheckboxGroup.Content(root, listRect));
|
|
453
|
+
}
|
|
454
|
+
if (noteArea && notesRows > 0) {
|
|
455
|
+
drawNotesStrip(frame, noteArea, surface);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
function drawFileSurface(frame, body, surface) {
|
|
459
|
+
const notesRows = surface.notes.length > 0 ? Math.min(4, surface.notes.length + 1) : 0;
|
|
460
|
+
const [metaRow, sepRow, contentArea, noteArea] = split(body, "rows", [1, 1, -1, notesRows]);
|
|
461
|
+
if (metaRow) {
|
|
462
|
+
const range = surface.highlightStart !== undefined
|
|
463
|
+
? ` L${surface.highlightStart}${surface.highlightEnd && surface.highlightEnd !== surface.highlightStart ? `–${surface.highlightEnd}` : ""}`
|
|
464
|
+
: "";
|
|
465
|
+
const meta = `${surface.mimeHint ?? "file"}${surface.sizeBytes !== undefined ? ` · ${formatBytes(surface.sizeBytes)}` : ""}${range}`;
|
|
466
|
+
text(frame, metaRow.x + 1, metaRow.y, truncate(surface.path, Math.max(10, metaRow.columns - widthOf(meta) - 6)), frame.theme.heading, metaRow);
|
|
467
|
+
text(frame, metaRow.x + metaRow.columns - widthOf(meta) - 1, metaRow.y, meta, frame.theme.muted, metaRow);
|
|
468
|
+
}
|
|
469
|
+
if (sepRow)
|
|
470
|
+
rule(frame, sepRow.y, sepRow);
|
|
471
|
+
if (contentArea && contentArea.rows > 0) {
|
|
472
|
+
if (surface.binary) {
|
|
473
|
+
const card = center(contentArea, Math.min(58, Math.max(24, contentArea.columns - 4)), Math.min(9, contentArea.rows));
|
|
474
|
+
const inside = drawBox(frame.cells, card, {
|
|
475
|
+
title: "QuickLook · External File",
|
|
476
|
+
style: frame.styles.style(frame.theme.border),
|
|
477
|
+
});
|
|
478
|
+
let y = inside.y;
|
|
479
|
+
text(frame, inside.x + 1, y++, truncate(surface.title, inside.columns - 2), frame.theme.heading, inside);
|
|
480
|
+
text(frame, inside.x + 1, y++, `${surface.mimeHint ?? "binary"} · ${formatBytes(surface.sizeBytes)}`, frame.theme.muted, inside);
|
|
481
|
+
y++;
|
|
482
|
+
if (y < inside.y + inside.rows) {
|
|
483
|
+
text(frame, inside.x + 1, y++, "Press [o] to open on your host machine", frame.theme.primary, inside);
|
|
484
|
+
}
|
|
485
|
+
if (y < inside.y + inside.rows) {
|
|
486
|
+
text(frame, inside.x + 1, y, truncate(surface.path, inside.columns - 2), frame.theme.muted, inside);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
else {
|
|
490
|
+
const rawLines = (surface.content ?? "").split(/\r?\n/).map((l) => l.replace(/\t/g, " "));
|
|
491
|
+
const gutterW = Math.max(3, String(rawLines.length).length) + 2;
|
|
492
|
+
const defaultOffset = surface.highlightStart !== undefined ? Math.max(0, surface.highlightStart - 4) : 0;
|
|
493
|
+
const hiStart = surface.highlightStart ?? -1;
|
|
494
|
+
const hiEnd = surface.highlightEnd ?? hiStart;
|
|
495
|
+
ScrollArea.Root(frame, {
|
|
496
|
+
id: `file-scroll-${surface.id}`,
|
|
497
|
+
label: `${surface.title} file`,
|
|
498
|
+
total: rawLines.length,
|
|
499
|
+
defaultOffset,
|
|
500
|
+
}, (area) => ScrollArea.Viewport(area, inset(contentArea, [0, 1]), (vp) => {
|
|
501
|
+
const offset = vp.api.offset();
|
|
502
|
+
for (let row = 0; row < vp.rect.rows && offset + row < rawLines.length; row++) {
|
|
503
|
+
const lineNum = offset + row + 1;
|
|
504
|
+
const isHi = hiStart > 0 && lineNum >= hiStart && lineNum <= hiEnd;
|
|
505
|
+
const y = vp.rect.y + row;
|
|
506
|
+
if (isHi) {
|
|
507
|
+
paint(frame, { x: vp.rect.x, y, columns: vp.rect.columns, rows: 1 }, { bg: "#1c2840" });
|
|
508
|
+
}
|
|
509
|
+
const numStr = `${String(lineNum).padStart(gutterW - 2, " ")} ${isHi ? "▎" : "│"}`;
|
|
510
|
+
text(frame, vp.rect.x, y, numStr, isHi ? frame.theme.primary : frame.theme.muted, vp.rect);
|
|
511
|
+
const lineText = rawLines[offset + row] ?? "";
|
|
512
|
+
text(frame, vp.rect.x + gutterW + 1, y, lineText, isHi ? { fg: "#e6edf7", bg: "#1c2840" } : 0, vp.rect);
|
|
513
|
+
}
|
|
514
|
+
attachTextSelection(frame, surface, vp.rect, offset, (n) => vp.api.scrollTo(n), rawLines, vp.rect.x + gutterW + 1, vp.rect.columns, false, true);
|
|
515
|
+
}));
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
if (noteArea && notesRows > 0)
|
|
519
|
+
drawNotesStrip(frame, noteArea, surface);
|
|
520
|
+
}
|
|
521
|
+
function drawImageSurface(frame, body, surface) {
|
|
522
|
+
const notesRows = surface.notes.length > 0 ? Math.min(4, surface.notes.length + 1) : 0;
|
|
523
|
+
const captionRows = surface.caption ? 1 : 0;
|
|
524
|
+
const [metaRow, sepRow, imgArea, captionRow, noteArea] = split(body, "rows", [1, 1, -1, captionRows, notesRows]);
|
|
525
|
+
if (metaRow) {
|
|
526
|
+
const dims = surface.width && surface.height ? `${surface.width}×${surface.height}` : "image";
|
|
527
|
+
const meta = `${dims}${surface.sizeBytes !== undefined ? ` · ${formatBytes(surface.sizeBytes)}` : ""} · [o] open`;
|
|
528
|
+
text(frame, metaRow.x + 1, metaRow.y, truncate(surface.title, Math.max(10, metaRow.columns - widthOf(meta) - 4)), frame.theme.heading, metaRow);
|
|
529
|
+
text(frame, metaRow.x + metaRow.columns - widthOf(meta) - 1, metaRow.y, meta, frame.theme.muted, metaRow);
|
|
530
|
+
}
|
|
531
|
+
if (sepRow)
|
|
532
|
+
rule(frame, sepRow.y, sepRow);
|
|
533
|
+
if (imgArea && imgArea.rows > 0) {
|
|
534
|
+
const img = getImage(surface);
|
|
535
|
+
if (img) {
|
|
536
|
+
drawImage(frame, inset(imgArea, [0, 1]), img);
|
|
537
|
+
}
|
|
538
|
+
else {
|
|
539
|
+
const card = center(imgArea, Math.min(62, Math.max(24, imgArea.columns - 4)), Math.min(8, imgArea.rows));
|
|
540
|
+
const inside = drawBox(frame.cells, card, {
|
|
541
|
+
title: "Image QuickLook",
|
|
542
|
+
style: frame.styles.style(frame.theme.border),
|
|
543
|
+
});
|
|
544
|
+
text(frame, inside.x + 1, inside.y, truncate(surface.error || "Unable to decode inline preview", inside.columns - 2), frame.theme.muted, inside);
|
|
545
|
+
if (inside.rows > 2) {
|
|
546
|
+
text(frame, inside.x + 1, inside.y + 2, "Press [o] to open on your host machine", frame.theme.primary, inside);
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
if (captionRow && surface.caption) {
|
|
551
|
+
text(frame, captionRow.x + 1, captionRow.y, truncate(surface.caption, captionRow.columns - 2), frame.theme.muted, captionRow);
|
|
552
|
+
}
|
|
553
|
+
if (noteArea && notesRows > 0)
|
|
554
|
+
drawNotesStrip(frame, noteArea, surface);
|
|
555
|
+
}
|
|
556
|
+
function drawTableSurface(frame, body, surface) {
|
|
557
|
+
const notesRows = surface.notes.length > 0 ? Math.min(4, surface.notes.length + 1) : 0;
|
|
558
|
+
const showFilterBar = filterFocused || surface.filter.length > 0;
|
|
559
|
+
const [filterRow, sepRow, tableArea, noteArea] = split(body, "rows", [showFilterBar ? 1 : 0, showFilterBar ? 1 : 0, -1, notesRows]);
|
|
560
|
+
const sorted = sortTableRows(surface);
|
|
561
|
+
const searchable = sorted.map((r) => r.cells.join(" \t "));
|
|
562
|
+
const filter = getFilter(`table-${surface.id}`);
|
|
563
|
+
filter.update(searchable, surface.filter);
|
|
564
|
+
const visibleRows = [];
|
|
565
|
+
if (surface.filter.trim().length === 0) {
|
|
566
|
+
for (const r of sorted)
|
|
567
|
+
visibleRows.push({ value: r.id, cells: r.cells });
|
|
568
|
+
}
|
|
569
|
+
else {
|
|
570
|
+
for (let i = 0; i < filter.count; i++) {
|
|
571
|
+
const row = sorted[filter.at(i)];
|
|
572
|
+
if (row)
|
|
573
|
+
visibleRows.push({ value: row.id, cells: row.cells });
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
const renderFilter = () => {
|
|
577
|
+
if (!showFilterBar || !filterRow)
|
|
578
|
+
return;
|
|
579
|
+
text(frame, filterRow.x + 1, filterRow.y, "/", frame.theme.primary, filterRow);
|
|
580
|
+
const countText = `${visibleRows.length}/${surface.rows.length}`;
|
|
581
|
+
const cw = widthOf(countText);
|
|
582
|
+
text(frame, filterRow.x + filterRow.columns - cw - 1, filterRow.y, countText, frame.theme.muted, filterRow);
|
|
583
|
+
Input.Root(frame, {
|
|
584
|
+
id: `table-filter-${surface.id}`,
|
|
585
|
+
label: "Filter rows",
|
|
586
|
+
value: surface.filter,
|
|
587
|
+
placeholder: "Filter table rows… (Esc to return)",
|
|
588
|
+
onValueChange: (val) => {
|
|
589
|
+
surface.filter = val;
|
|
590
|
+
},
|
|
591
|
+
onSubmit: () => {
|
|
592
|
+
filterFocused = false;
|
|
593
|
+
frame.focused = "";
|
|
594
|
+
},
|
|
595
|
+
}, (root) => Input.Field(root, {
|
|
596
|
+
x: filterRow.x + 3,
|
|
597
|
+
y: filterRow.y,
|
|
598
|
+
columns: Math.max(8, filterRow.columns - cw - 5),
|
|
599
|
+
rows: 1,
|
|
600
|
+
}));
|
|
601
|
+
if (sepRow)
|
|
602
|
+
rule(frame, sepRow.y, sepRow);
|
|
603
|
+
};
|
|
604
|
+
const renderTable = () => {
|
|
605
|
+
if (!tableArea || tableArea.rows <= 0)
|
|
606
|
+
return;
|
|
607
|
+
const tableInnerW = Math.max(12, tableArea.columns - 2);
|
|
608
|
+
const explicitSum = surface.columns.reduce((acc, c) => acc + (c.width ?? 0), 0);
|
|
609
|
+
const scale = explicitSum > tableInnerW - 10 ? (tableInnerW - 10) / explicitSum : 1;
|
|
610
|
+
const cols = surface.columns.map((c, idx) => ({
|
|
611
|
+
label: surface.sortColumn === idx
|
|
612
|
+
? `${c.label} ${surface.sortDir === "asc" ? "↑" : "↓"}`
|
|
613
|
+
: c.label,
|
|
614
|
+
width: c.width ? Math.max(5, Math.floor(c.width * scale)) : undefined,
|
|
615
|
+
align: c.align,
|
|
616
|
+
}));
|
|
617
|
+
if (visibleRows.length > 0 &&
|
|
618
|
+
!visibleRows.some((r) => r.value === surface.highlightedRowId)) {
|
|
619
|
+
surface.highlightedRowId = visibleRows[0].value;
|
|
620
|
+
}
|
|
621
|
+
Table.Root(frame, {
|
|
622
|
+
id: `table-${surface.id}`,
|
|
623
|
+
label: surface.title,
|
|
624
|
+
columns: cols,
|
|
625
|
+
rows: visibleRows,
|
|
626
|
+
value: surface.selectedRowId,
|
|
627
|
+
highlighted: surface.highlightedRowId,
|
|
628
|
+
onHighlight: (val) => {
|
|
629
|
+
surface.highlightedRowId = val;
|
|
630
|
+
},
|
|
631
|
+
onValueChange: (val) => {
|
|
632
|
+
surface.selectedRowId = val;
|
|
633
|
+
surface.highlightedRowId = val;
|
|
634
|
+
store.setToast("Row selected — press 's' to send or 'y' to copy", "info", 2000);
|
|
635
|
+
},
|
|
636
|
+
}, (root) => Table.Content(root, inset(tableArea, [0, 1])));
|
|
637
|
+
};
|
|
638
|
+
// Focus filter first while filter mode is active; otherwise focus the table immediately.
|
|
639
|
+
if (filterFocused) {
|
|
640
|
+
renderFilter();
|
|
641
|
+
renderTable();
|
|
642
|
+
}
|
|
643
|
+
else {
|
|
644
|
+
renderTable();
|
|
645
|
+
renderFilter();
|
|
646
|
+
}
|
|
647
|
+
if (noteArea && notesRows > 0)
|
|
648
|
+
drawNotesStrip(frame, noteArea, surface);
|
|
649
|
+
}
|
|
650
|
+
function drawChartSurface(frame, body, surface) {
|
|
651
|
+
const notesRows = surface.notes.length > 0 ? Math.min(4, surface.notes.length + 1) : 0;
|
|
652
|
+
const legendRows = Math.min(4, Math.max(1, surface.series.length));
|
|
653
|
+
const captionRows = surface.caption ? 1 : 0;
|
|
654
|
+
const [legendArea, sepRow, chartArea, sparkRow, captionRow, noteArea] = split(body, "rows", [
|
|
655
|
+
legendRows,
|
|
656
|
+
1,
|
|
657
|
+
-1,
|
|
658
|
+
body.rows >= 14 && surface.series.length === 1 ? 2 : 0,
|
|
659
|
+
captionRows,
|
|
660
|
+
notesRows,
|
|
661
|
+
]);
|
|
662
|
+
if (legendArea) {
|
|
663
|
+
for (let i = 0; i < Math.min(legendArea.rows, surface.series.length); i++) {
|
|
664
|
+
const s = surface.series[i];
|
|
665
|
+
const color = s.color || SERIES_COLORS[i % SERIES_COLORS.length];
|
|
666
|
+
const valid = s.values.filter((v) => Number.isFinite(v));
|
|
667
|
+
const min = valid.length ? Math.min(...valid) : 0;
|
|
668
|
+
const max = valid.length ? Math.max(...valid) : 0;
|
|
669
|
+
const last = valid.length ? valid[valid.length - 1] : 0;
|
|
670
|
+
const unit = surface.unit ? ` ${surface.unit}` : "";
|
|
671
|
+
const y = legendArea.y + i;
|
|
672
|
+
const x1 = text(frame, legendArea.x + 1, y, `● ${s.name}`, { fg: color, bold: true }, legendArea);
|
|
673
|
+
const stats = ` last ${last.toFixed(2)}${unit} · min ${min.toFixed(2)} · max ${max.toFixed(2)} (${valid.length} pts)`;
|
|
674
|
+
text(frame, x1, y, truncate(stats, Math.max(0, legendArea.x + legendArea.columns - x1)), frame.theme.muted, legendArea);
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
if (sepRow)
|
|
678
|
+
rule(frame, sepRow.y, sepRow);
|
|
679
|
+
if (chartArea && chartArea.rows >= 3) {
|
|
680
|
+
const chart = getChart(surface.id);
|
|
681
|
+
chart.draw(frame, inset(chartArea, [0, 1]), {
|
|
682
|
+
series: surface.series.map((s, i) => ({
|
|
683
|
+
values: s.values,
|
|
684
|
+
style: { fg: (s.color || SERIES_COLORS[i % SERIES_COLORS.length]) },
|
|
685
|
+
})),
|
|
686
|
+
min: surface.min,
|
|
687
|
+
max: surface.max,
|
|
688
|
+
format: (v) => `${Number(v.toFixed(2))}${surface.unit ? surface.unit : ""}`,
|
|
689
|
+
});
|
|
690
|
+
}
|
|
691
|
+
if (sparkRow && sparkRow.rows >= 1 && surface.series[0]) {
|
|
692
|
+
drawSparkline(frame, inset(sparkRow, [0, 1]), surface.series[0].values, {
|
|
693
|
+
style: { fg: (surface.series[0].color || SERIES_COLORS[0]) },
|
|
694
|
+
});
|
|
695
|
+
}
|
|
696
|
+
if (captionRow && surface.caption) {
|
|
697
|
+
text(frame, captionRow.x + 1, captionRow.y, truncate(surface.caption, captionRow.columns - 2), frame.theme.muted, captionRow);
|
|
698
|
+
}
|
|
699
|
+
if (noteArea && notesRows > 0)
|
|
700
|
+
drawNotesStrip(frame, noteArea, surface);
|
|
701
|
+
}
|
|
702
|
+
function drawLogSurface(frame, body, surface) {
|
|
703
|
+
const notesRows = surface.notes.length > 0 ? Math.min(4, surface.notes.length + 1) : 0;
|
|
704
|
+
const showFilterBar = filterFocused || surface.filter.length > 0;
|
|
705
|
+
const [filterRow, sepRow, logArea, noteArea] = split(body, "rows", [
|
|
706
|
+
showFilterBar ? 1 : 0,
|
|
707
|
+
showFilterBar ? 1 : 0,
|
|
708
|
+
-1,
|
|
709
|
+
notesRows,
|
|
710
|
+
]);
|
|
711
|
+
const filter = getFilter(`log-${surface.id}`);
|
|
712
|
+
filter.update(surface.lines, surface.filter);
|
|
713
|
+
const count = surface.filter ? filter.count : surface.lines.length;
|
|
714
|
+
const renderFilter = () => {
|
|
715
|
+
if (!showFilterBar || !filterRow)
|
|
716
|
+
return;
|
|
717
|
+
text(frame, filterRow.x + 1, filterRow.y, "/", frame.theme.primary, filterRow);
|
|
718
|
+
const countText = `${count}/${surface.lines.length} lines`;
|
|
719
|
+
const cw = widthOf(countText);
|
|
720
|
+
text(frame, filterRow.x + filterRow.columns - cw - 1, filterRow.y, countText, frame.theme.muted, filterRow);
|
|
721
|
+
Input.Root(frame, {
|
|
722
|
+
id: `log-filter-${surface.id}`,
|
|
723
|
+
label: "Filter log",
|
|
724
|
+
value: surface.filter,
|
|
725
|
+
placeholder: "Filter log lines… (Esc to return)",
|
|
726
|
+
onValueChange: (val) => {
|
|
727
|
+
surface.filter = val;
|
|
728
|
+
},
|
|
729
|
+
onSubmit: () => {
|
|
730
|
+
filterFocused = false;
|
|
731
|
+
frame.focused = "";
|
|
732
|
+
},
|
|
733
|
+
}, (root) => Input.Field(root, {
|
|
734
|
+
x: filterRow.x + 3,
|
|
735
|
+
y: filterRow.y,
|
|
736
|
+
columns: Math.max(8, filterRow.columns - cw - 5),
|
|
737
|
+
rows: 1,
|
|
738
|
+
}));
|
|
739
|
+
if (sepRow)
|
|
740
|
+
rule(frame, sepRow.y, sepRow);
|
|
741
|
+
};
|
|
742
|
+
const renderLog = () => {
|
|
743
|
+
if (!logArea || logArea.rows <= 0)
|
|
744
|
+
return;
|
|
745
|
+
ScrollArea.Root(frame, {
|
|
746
|
+
id: `log-scroll-${surface.id}`,
|
|
747
|
+
label: `${surface.title} log`,
|
|
748
|
+
total: count,
|
|
749
|
+
stickToEnd: surface.stickToEnd,
|
|
750
|
+
}, (area) => ScrollArea.Viewport(area, inset(logArea, [0, 1]), (vp) => {
|
|
751
|
+
const offset = vp.api.offset();
|
|
752
|
+
for (let row = 0; row < vp.rect.rows && offset + row < count; row++) {
|
|
753
|
+
const idx = surface.filter ? filter.at(offset + row) : offset + row;
|
|
754
|
+
const line = surface.lines[idx] ?? "";
|
|
755
|
+
drawMatches(frame, vp.rect.x, vp.rect.y + row, line, surface.filter, 0, vp.rect);
|
|
756
|
+
}
|
|
757
|
+
}));
|
|
758
|
+
};
|
|
759
|
+
if (filterFocused) {
|
|
760
|
+
renderFilter();
|
|
761
|
+
renderLog();
|
|
762
|
+
}
|
|
763
|
+
else {
|
|
764
|
+
renderLog();
|
|
765
|
+
renderFilter();
|
|
766
|
+
}
|
|
767
|
+
if (noteArea && notesRows > 0)
|
|
768
|
+
drawNotesStrip(frame, noteArea, surface);
|
|
769
|
+
}
|
|
770
|
+
function drawAskSurface(frame, body, surface) {
|
|
771
|
+
const pad = inset(body, [1, 2]);
|
|
772
|
+
if (pad.rows < 4 || pad.columns < 12)
|
|
773
|
+
return;
|
|
774
|
+
const isPending = surface.answer.status === "pending";
|
|
775
|
+
const badge = surface.answer.status === "pending"
|
|
776
|
+
? "● ACTION REQUIRED"
|
|
777
|
+
: surface.answer.status === "submitted"
|
|
778
|
+
? "✓ ANSWERED"
|
|
779
|
+
: "× CANCELLED";
|
|
780
|
+
const badgeStyle = surface.answer.status === "pending"
|
|
781
|
+
? { fg: "#f5a623", bold: true }
|
|
782
|
+
: surface.answer.status === "submitted"
|
|
783
|
+
? frame.theme.success
|
|
784
|
+
: frame.theme.destructive;
|
|
785
|
+
const md = getMarkdown(frame);
|
|
786
|
+
const contextLines = surface.context ? md.lines(surface.context, pad.columns) : [];
|
|
787
|
+
const maxContext = Math.min(contextLines.length, Math.max(0, Math.floor(pad.rows * 0.35)));
|
|
788
|
+
const notesRows = surface.notes.length > 0 ? Math.min(4, surface.notes.length + 1) : 0;
|
|
789
|
+
const [badgeRow, promptRow, ctxArea, gapRow, widgetArea, noteArea] = split(pad, "rows", [
|
|
790
|
+
1,
|
|
791
|
+
2,
|
|
792
|
+
maxContext,
|
|
793
|
+
1,
|
|
794
|
+
-1,
|
|
795
|
+
notesRows,
|
|
796
|
+
]);
|
|
797
|
+
if (badgeRow) {
|
|
798
|
+
text(frame, badgeRow.x, badgeRow.y, badge, badgeStyle, badgeRow);
|
|
799
|
+
if (!isPending && surface.answer.status === "submitted") {
|
|
800
|
+
const valText = ` → ${truncate(JSON.stringify(surface.answer.value), Math.max(8, badgeRow.columns - 20))}`;
|
|
801
|
+
text(frame, badgeRow.x + widthOf(badge), badgeRow.y, valText, frame.theme.heading, badgeRow);
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
if (promptRow) {
|
|
805
|
+
const promptLines = md.lines(`**${surface.prompt}**`, promptRow.columns);
|
|
806
|
+
drawLines(frame, promptRow, promptLines, 0);
|
|
807
|
+
}
|
|
808
|
+
if (ctxArea && maxContext > 0) {
|
|
809
|
+
drawLines(frame, ctxArea, contextLines, 0);
|
|
810
|
+
}
|
|
811
|
+
if (gapRow)
|
|
812
|
+
rule(frame, gapRow.y, gapRow);
|
|
813
|
+
if (widgetArea && widgetArea.rows > 0) {
|
|
814
|
+
if (surface.mode === "select" || surface.mode === "confirm") {
|
|
815
|
+
const hint = isPending
|
|
816
|
+
? "↑/↓ move · Enter submit · Esc cancel"
|
|
817
|
+
: "Answered (press Enter again to update)";
|
|
818
|
+
text(frame, widgetArea.x, widgetArea.y, hint, frame.theme.muted, widgetArea);
|
|
819
|
+
const listRect = {
|
|
820
|
+
x: widgetArea.x,
|
|
821
|
+
y: widgetArea.y + 1,
|
|
822
|
+
columns: widgetArea.columns,
|
|
823
|
+
rows: Math.max(1, widgetArea.rows - 1),
|
|
824
|
+
};
|
|
825
|
+
Listbox.Root(frame, {
|
|
826
|
+
id: `ask-select-${surface.id}`,
|
|
827
|
+
label: surface.prompt,
|
|
828
|
+
items: surface.options,
|
|
829
|
+
value: surface.draftSelect,
|
|
830
|
+
onHighlight: (val) => {
|
|
831
|
+
surface.draftSelect = val;
|
|
832
|
+
},
|
|
833
|
+
onValueChange: (val, details) => {
|
|
834
|
+
// Human-only gate: reject any agent-sourced submission.
|
|
835
|
+
if (details.source === "agent")
|
|
836
|
+
return;
|
|
837
|
+
surface.draftSelect = val;
|
|
838
|
+
if (surface.mode === "confirm") {
|
|
839
|
+
store.submitAsk(surface.id, val === "confirm");
|
|
840
|
+
}
|
|
841
|
+
else {
|
|
842
|
+
store.submitAsk(surface.id, val);
|
|
843
|
+
}
|
|
844
|
+
},
|
|
845
|
+
}, (root) => Listbox.Content(root, listRect));
|
|
846
|
+
}
|
|
847
|
+
else if (surface.mode === "multiselect") {
|
|
848
|
+
const hint = "↑/↓ move · Space toggle · Enter submit · Esc cancel";
|
|
849
|
+
text(frame, widgetArea.x, widgetArea.y, hint, frame.theme.muted, widgetArea);
|
|
850
|
+
const listRect = {
|
|
851
|
+
x: widgetArea.x,
|
|
852
|
+
y: widgetArea.y + 1,
|
|
853
|
+
columns: widgetArea.columns,
|
|
854
|
+
rows: Math.max(1, widgetArea.rows - 1),
|
|
855
|
+
};
|
|
856
|
+
CheckboxGroup.Root(frame, {
|
|
857
|
+
id: `ask-multi-${surface.id}`,
|
|
858
|
+
label: surface.prompt,
|
|
859
|
+
items: surface.options,
|
|
860
|
+
value: surface.draftMulti,
|
|
861
|
+
onValueChange: (vals, details) => {
|
|
862
|
+
if (details.source === "agent")
|
|
863
|
+
return;
|
|
864
|
+
surface.draftMulti = [...vals];
|
|
865
|
+
},
|
|
866
|
+
onSubmit: (vals, key) => {
|
|
867
|
+
// `key` is null when an agent or imperative code calls submit(); require real human key.
|
|
868
|
+
if (!key)
|
|
869
|
+
return;
|
|
870
|
+
surface.draftMulti = [...vals];
|
|
871
|
+
store.submitAsk(surface.id, [...vals]);
|
|
872
|
+
},
|
|
873
|
+
}, (root) => CheckboxGroup.Content(root, listRect));
|
|
874
|
+
}
|
|
875
|
+
else {
|
|
876
|
+
const hint = "Type your answer · Enter submit · Shift+Enter newline · Esc cancel";
|
|
877
|
+
text(frame, widgetArea.x, widgetArea.y, hint, frame.theme.muted, widgetArea);
|
|
878
|
+
const boxRect = {
|
|
879
|
+
x: widgetArea.x,
|
|
880
|
+
y: widgetArea.y + 1,
|
|
881
|
+
columns: widgetArea.columns,
|
|
882
|
+
rows: Math.max(3, Math.min(8, widgetArea.rows - 1)),
|
|
883
|
+
};
|
|
884
|
+
const inside = drawBox(frame.cells, boxRect, { style: frame.styles.style(frame.theme.border) });
|
|
885
|
+
Textarea.Root(frame, {
|
|
886
|
+
id: `ask-text-${surface.id}`,
|
|
887
|
+
label: surface.prompt,
|
|
888
|
+
value: surface.draftText,
|
|
889
|
+
placeholder: surface.placeholder || "Type response…",
|
|
890
|
+
submitShortcut: "enter",
|
|
891
|
+
onValueChange: (val, details) => {
|
|
892
|
+
if (details.source === "agent")
|
|
893
|
+
return;
|
|
894
|
+
surface.draftText = val;
|
|
895
|
+
},
|
|
896
|
+
onSubmit: (val, key) => {
|
|
897
|
+
if (!key)
|
|
898
|
+
return;
|
|
899
|
+
surface.draftText = val;
|
|
900
|
+
store.submitAsk(surface.id, val);
|
|
901
|
+
},
|
|
902
|
+
}, (root) => Textarea.Field(root, inside));
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
if (noteArea && notesRows > 0)
|
|
906
|
+
drawNotesStrip(frame, noteArea, surface);
|
|
907
|
+
}
|
|
908
|
+
function drawAppSurface(frame, body, surface) {
|
|
909
|
+
const showStatusRow = surface.submission.status !== "idle";
|
|
910
|
+
const notesRows = surface.notes.length > 0 ? Math.min(4, surface.notes.length + 1) : 0;
|
|
911
|
+
const [statusRow, mainArea, noteArea] = split(body, "rows", [showStatusRow ? 1 : 0, -1, notesRows]);
|
|
912
|
+
if (showStatusRow && statusRow) {
|
|
913
|
+
const sub = surface.submission;
|
|
914
|
+
if (sub.status === "pending") {
|
|
915
|
+
text(frame, statusRow.x + 1, statusRow.y, "● ACTION REQUIRED", { fg: "#f5a623", bold: true }, statusRow);
|
|
916
|
+
text(frame, statusRow.x + 20, statusRow.y, "Interact below to submit (Esc cancels)", frame.theme.muted, statusRow);
|
|
917
|
+
}
|
|
918
|
+
else if (sub.status === "submitted") {
|
|
919
|
+
const badge = `✓ SUBMITTED (${sub.action})`;
|
|
920
|
+
const x1 = text(frame, statusRow.x + 1, statusRow.y, badge, frame.theme.success, statusRow);
|
|
921
|
+
text(frame, x1 + 2, statusRow.y, "Interact and resubmit anytime", frame.theme.muted, statusRow);
|
|
922
|
+
}
|
|
923
|
+
else if (sub.status === "cancelled") {
|
|
924
|
+
text(frame, statusRow.x + 1, statusRow.y, "× CANCELLED", frame.theme.destructive, statusRow);
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
if (mainArea && mainArea.rows > 0 && mainArea.columns > 0) {
|
|
928
|
+
appHost.sync(surface, mainArea.columns, mainArea.rows, true);
|
|
929
|
+
if (surface.runtimeStatus === "error") {
|
|
930
|
+
const card = inset(mainArea, [1, 2]);
|
|
931
|
+
if (card.rows >= 4 && card.columns >= 16) {
|
|
932
|
+
const inner = drawBox(frame.cells, card, {
|
|
933
|
+
title: `App Error · ${truncate(surface.title, 28)}`,
|
|
934
|
+
style: frame.styles.style(frame.theme.destructive),
|
|
935
|
+
});
|
|
936
|
+
const pad = inset(inner, [0, 1]);
|
|
937
|
+
const errLines = wrap(surface.runtimeError || "Unknown worker error", Math.max(10, pad.columns));
|
|
938
|
+
let y = pad.y;
|
|
939
|
+
for (const line of errLines) {
|
|
940
|
+
if (y >= pad.y + pad.rows - 2)
|
|
941
|
+
break;
|
|
942
|
+
text(frame, pad.x, y++, line, frame.theme.destructive, pad);
|
|
943
|
+
}
|
|
944
|
+
if (pad.rows >= 2) {
|
|
945
|
+
text(frame, pad.x, pad.y + pad.rows - 1, truncate("Fix the module and re-push with the same --id to hot-reload.", pad.columns), frame.theme.muted, pad);
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
else {
|
|
950
|
+
const meta = appHost.getFrameMeta(surface.id);
|
|
951
|
+
const slotPrefix = `app-slot:${surface.id}:`;
|
|
952
|
+
const viewportId = `app-viewport:${surface.id}`;
|
|
953
|
+
const isAppFocused = !noteDialogOpen &&
|
|
954
|
+
!helpOpen &&
|
|
955
|
+
(frame.focused === viewportId || frame.focused.startsWith(slotPrefix));
|
|
956
|
+
const blitted = appHost.blit(surface.id, frame, mainArea, isAppFocused);
|
|
957
|
+
if (!blitted) {
|
|
958
|
+
const pad = inset(mainArea, [1, 2]);
|
|
959
|
+
text(frame, pad.x, pad.y, `Starting custom tui app (${surface.title})…`, frame.theme.muted, pad);
|
|
960
|
+
}
|
|
961
|
+
else if (focusedAppRev.get(surface.id) !== surface.revision) {
|
|
962
|
+
focusedAppRev.set(surface.id, surface.revision);
|
|
963
|
+
if (!noteDialogOpen &&
|
|
964
|
+
!helpOpen &&
|
|
965
|
+
(frame.focused === "" || frame.focused.startsWith("workspace-tabs"))) {
|
|
966
|
+
frame.focused =
|
|
967
|
+
meta.focusables.length > 0
|
|
968
|
+
? `${slotPrefix}${meta.focused || meta.focusables[0]}`
|
|
969
|
+
: viewportId;
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
const forwardKey = (k) => {
|
|
973
|
+
// Always keep Ctrl shortcuts responsive for bypane workspace chrome.
|
|
974
|
+
if (matchesShortcut(k, "ctrl+e") ||
|
|
975
|
+
matchesShortcut(k, "alt+n") ||
|
|
976
|
+
matchesShortcut(k, "ctrl+s") ||
|
|
977
|
+
matchesShortcut(k, "ctrl+w")) {
|
|
978
|
+
return false;
|
|
979
|
+
}
|
|
980
|
+
if (k.name === "escape" && surface.submission.status === "pending" && !meta.hasTrap) {
|
|
981
|
+
return false;
|
|
982
|
+
}
|
|
983
|
+
if (!meta.textInputFocused && !meta.hasTrap && k.name === "char" && !k.ctrl && !k.alt) {
|
|
984
|
+
const ch = k.text;
|
|
985
|
+
if ((ch >= "1" && ch <= "9") || ch === "[" || ch === "]" || ch === "?") {
|
|
986
|
+
return false;
|
|
987
|
+
}
|
|
988
|
+
if (!surface.captureKeys && (ch === "n" || ch === "s" || ch === "w" || ch === "g" || ch === "y" || ch === "q")) {
|
|
989
|
+
return false;
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
appHost.sendKey(surface.id, k);
|
|
993
|
+
return true;
|
|
994
|
+
};
|
|
995
|
+
const trapped = frame.trapDepth > 0;
|
|
996
|
+
if (meta.focusables.length > 0) {
|
|
997
|
+
for (const wfId of meta.focusables) {
|
|
998
|
+
frame.focusables.push({
|
|
999
|
+
id: `${slotPrefix}${wfId}`,
|
|
1000
|
+
trapped,
|
|
1001
|
+
keys: forwardKey,
|
|
1002
|
+
});
|
|
1003
|
+
}
|
|
1004
|
+
const prevWorker = lastWorkerFocus.get(surface.id) ?? "";
|
|
1005
|
+
if (meta.focused && meta.focused !== prevWorker) {
|
|
1006
|
+
// Worker moved focus internally (e.g. via mouse click): mirror to host focus slot.
|
|
1007
|
+
lastWorkerFocus.set(surface.id, meta.focused);
|
|
1008
|
+
if (!noteDialogOpen && !helpOpen && !frame.focused.startsWith("workspace-tabs")) {
|
|
1009
|
+
frame.focused = `${slotPrefix}${meta.focused}`;
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
else if (frame.focused.startsWith(slotPrefix)) {
|
|
1013
|
+
// Host moved focus via Tab / Shift+Tab: push to worker.
|
|
1014
|
+
const targetWf = frame.focused.slice(slotPrefix.length);
|
|
1015
|
+
if (targetWf && targetWf !== prevWorker) {
|
|
1016
|
+
lastWorkerFocus.set(surface.id, targetWf);
|
|
1017
|
+
appHost.sendFocus(surface.id, targetWf);
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
else if (frame.focused === "" && meta.focused) {
|
|
1021
|
+
lastWorkerFocus.set(surface.id, meta.focused);
|
|
1022
|
+
frame.focused = `${slotPrefix}${meta.focused}`;
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
else {
|
|
1026
|
+
frame.focusables.push({
|
|
1027
|
+
id: viewportId,
|
|
1028
|
+
trapped,
|
|
1029
|
+
keys: forwardKey,
|
|
1030
|
+
});
|
|
1031
|
+
}
|
|
1032
|
+
if (frame.mouse) {
|
|
1033
|
+
const owner = meta.focusables.length > 0
|
|
1034
|
+
? `${slotPrefix}${meta.focused || meta.focusables[0]}`
|
|
1035
|
+
: viewportId;
|
|
1036
|
+
frame.regions.push({
|
|
1037
|
+
rect: mainArea,
|
|
1038
|
+
owner,
|
|
1039
|
+
trapped,
|
|
1040
|
+
press: null,
|
|
1041
|
+
mouse: (k) => {
|
|
1042
|
+
if (!k.mouse)
|
|
1043
|
+
return false;
|
|
1044
|
+
appHost.sendMouse(surface.id, {
|
|
1045
|
+
...k.mouse,
|
|
1046
|
+
x: k.mouse.x - mainArea.x,
|
|
1047
|
+
y: k.mouse.y - mainArea.y,
|
|
1048
|
+
}, k.ctrl, k.alt, k.shift);
|
|
1049
|
+
return true;
|
|
1050
|
+
},
|
|
1051
|
+
});
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
if (noteArea && notesRows > 0)
|
|
1056
|
+
drawNotesStrip(frame, noteArea, surface);
|
|
1057
|
+
}
|
|
1058
|
+
function drawFooter(frame, rect, active) {
|
|
1059
|
+
rule(frame, rect.y, rect);
|
|
1060
|
+
const barY = rect.y + 1;
|
|
1061
|
+
if (barY >= rect.y + rect.rows)
|
|
1062
|
+
return;
|
|
1063
|
+
const now = Date.now();
|
|
1064
|
+
if (store.toast && store.toast.until > now) {
|
|
1065
|
+
const style = store.toast.tone === "success"
|
|
1066
|
+
? frame.theme.success
|
|
1067
|
+
: store.toast.tone === "error"
|
|
1068
|
+
? frame.theme.destructive
|
|
1069
|
+
: frame.theme.primary;
|
|
1070
|
+
text(frame, rect.x + 1, barY, truncate(store.toast.text, rect.columns - 2), style, rect);
|
|
1071
|
+
return;
|
|
1072
|
+
}
|
|
1073
|
+
const hints = [];
|
|
1074
|
+
if (!active) {
|
|
1075
|
+
hints.push("d demo", "? help", "q quit");
|
|
1076
|
+
}
|
|
1077
|
+
else {
|
|
1078
|
+
if ((active.kind === "markdown" || active.kind === "file") && active.selection?.text) {
|
|
1079
|
+
hints.push("n comment selection", "y copy", "Esc clear");
|
|
1080
|
+
}
|
|
1081
|
+
else {
|
|
1082
|
+
if (store.surfaces.length > 1)
|
|
1083
|
+
hints.push(`1-${Math.min(9, store.surfaces.length)} tabs`);
|
|
1084
|
+
if (active.kind === "table" || active.kind === "log")
|
|
1085
|
+
hints.push("/ filter");
|
|
1086
|
+
if (active.kind === "table")
|
|
1087
|
+
hints.push("r sort", "y copy");
|
|
1088
|
+
if (active.kind === "file" || active.kind === "image")
|
|
1089
|
+
hints.push("o open");
|
|
1090
|
+
if (active.kind === "markdown" && active.checklist.length > 0)
|
|
1091
|
+
hints.push("Tab checks");
|
|
1092
|
+
if (active.kind === "markdown" || (active.kind === "file" && !active.binary))
|
|
1093
|
+
hints.push("drag select");
|
|
1094
|
+
if (active.kind === "ui")
|
|
1095
|
+
hints.push(...uiFooterHints(active.root));
|
|
1096
|
+
if (active.kind === "app" && active.hints && active.hints.length > 0)
|
|
1097
|
+
hints.push(...active.hints);
|
|
1098
|
+
if (active.kind === "app" && active.captureKeys) {
|
|
1099
|
+
hints.push("Ctrl+E note", "Ctrl+S send", "Ctrl+W close", "?");
|
|
1100
|
+
}
|
|
1101
|
+
else {
|
|
1102
|
+
hints.push("n note", "s send");
|
|
1103
|
+
if (active.origin?.paneId)
|
|
1104
|
+
hints.push("g origin");
|
|
1105
|
+
hints.push("w close", "?");
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
text(frame, rect.x + 1, barY, truncate(hints.join(" · "), rect.columns - 2), frame.theme.muted, rect);
|
|
1110
|
+
}
|
|
1111
|
+
function drawOverlays(frame) {
|
|
1112
|
+
if (noteDialogOpen) {
|
|
1113
|
+
const active = store.activeSurface();
|
|
1114
|
+
const noteCtx = active ? captureNoteContext(active) : undefined;
|
|
1115
|
+
const dialogRect = center(frame.rect, Math.min(60, Math.max(28, frame.rect.columns - 6)), noteCtx ? 8 : 7);
|
|
1116
|
+
Dialog.Root(frame, {
|
|
1117
|
+
id: "note-dialog",
|
|
1118
|
+
label: "Add note",
|
|
1119
|
+
title: `Note on ${active ? truncate(active.title, 26) : "surface"}`,
|
|
1120
|
+
open: noteDialogOpen,
|
|
1121
|
+
onOpenChange: (open) => {
|
|
1122
|
+
noteDialogOpen = open;
|
|
1123
|
+
},
|
|
1124
|
+
}, (dlg) => Dialog.Content(dlg, dialogRect, (content) => {
|
|
1125
|
+
const inner = inset(content.rect, [0, 1]);
|
|
1126
|
+
const canAutoSend = Boolean(active && (active.origin?.paneId || hooks.onSendFeedback));
|
|
1127
|
+
text(frame, inner.x, inner.y, canAutoSend
|
|
1128
|
+
? "Leave feedback for the agent (Enter sends · Esc closes):"
|
|
1129
|
+
: "Leave feedback for the agent (Enter saves · Esc closes):", frame.theme.muted, inner);
|
|
1130
|
+
if (noteCtx) {
|
|
1131
|
+
const prefixEnd = text(frame, inner.x, inner.y + 1, "Scope: ", frame.theme.primary, inner);
|
|
1132
|
+
text(frame, prefixEnd, inner.y + 1, truncate(noteCtx, Math.max(6, inner.columns - 7)), frame.theme.code, inner);
|
|
1133
|
+
}
|
|
1134
|
+
Input.Root(frame, {
|
|
1135
|
+
id: "note-input",
|
|
1136
|
+
label: "Note text",
|
|
1137
|
+
value: noteDraft,
|
|
1138
|
+
placeholder: "e.g. skip step 3, check p99 spike…",
|
|
1139
|
+
onValueChange: (val) => {
|
|
1140
|
+
noteDraft = val;
|
|
1141
|
+
},
|
|
1142
|
+
onSubmit: (val) => {
|
|
1143
|
+
const created = active ? store.addNote(active.id, val) : null;
|
|
1144
|
+
if (active && created) {
|
|
1145
|
+
if (active.origin?.paneId || hooks.onSendFeedback) {
|
|
1146
|
+
triggerSendFeedback(frame, active, created);
|
|
1147
|
+
}
|
|
1148
|
+
else {
|
|
1149
|
+
store.setToast("Note saved (press 's' to copy)", "success");
|
|
1150
|
+
}
|
|
1151
|
+
clearSelection(active);
|
|
1152
|
+
}
|
|
1153
|
+
noteDraft = "";
|
|
1154
|
+
noteDialogOpen = false;
|
|
1155
|
+
},
|
|
1156
|
+
}, (root) => Input.Field(root, {
|
|
1157
|
+
x: inner.x,
|
|
1158
|
+
y: inner.y + (noteCtx ? 3 : 2),
|
|
1159
|
+
columns: Math.max(8, inner.columns),
|
|
1160
|
+
rows: 1,
|
|
1161
|
+
}));
|
|
1162
|
+
}));
|
|
1163
|
+
}
|
|
1164
|
+
if (helpOpen) {
|
|
1165
|
+
const helpRect = center(frame.rect, Math.min(64, Math.max(34, frame.rect.columns - 6)), Math.min(18, frame.rect.rows - 2));
|
|
1166
|
+
Dialog.Root(frame, {
|
|
1167
|
+
id: "help-dialog",
|
|
1168
|
+
label: "Keyboard shortcuts",
|
|
1169
|
+
title: "bypane · Shortcuts",
|
|
1170
|
+
open: helpOpen,
|
|
1171
|
+
onOpenChange: (open) => {
|
|
1172
|
+
helpOpen = open;
|
|
1173
|
+
},
|
|
1174
|
+
}, (dlg) => Dialog.Content(dlg, helpRect, (content) => {
|
|
1175
|
+
const inner = inset(content.rect, [0, 1]);
|
|
1176
|
+
const rows = [
|
|
1177
|
+
["1–9 / [ / ]", "Switch surfaces (or click tabs)"],
|
|
1178
|
+
["Tab", "Cycle focus (body / checklist / filter / tabs)"],
|
|
1179
|
+
["/ or Ctrl+F", "Filter table or log lines live"],
|
|
1180
|
+
["r", "Cycle sort column / direction (tables)"],
|
|
1181
|
+
["n or Ctrl+E", "Attach a note to the active surface"],
|
|
1182
|
+
["s or Ctrl+S", "Push structured feedback back to agent pane"],
|
|
1183
|
+
["o or Ctrl+O", "Open file/image on host (rex open / open)"],
|
|
1184
|
+
["g", "Jump focus to originating Rex/tmux pane"],
|
|
1185
|
+
["y", "Copy selected row / path / feedback to clipboard"],
|
|
1186
|
+
["w or Ctrl+W", "Close active surface tab"],
|
|
1187
|
+
["d", "Load interactive demo workspace"],
|
|
1188
|
+
["? / Esc", "Toggle / close dialogs"],
|
|
1189
|
+
];
|
|
1190
|
+
for (let i = 0; i < rows.length && i < inner.rows; i++) {
|
|
1191
|
+
const [k, desc] = rows[i];
|
|
1192
|
+
text(frame, inner.x, inner.y + i, k, frame.theme.primary, inner);
|
|
1193
|
+
text(frame, inner.x + 16, inner.y + i, truncate(desc, Math.max(8, inner.columns - 16)), frame.theme.muted, inner);
|
|
1194
|
+
}
|
|
1195
|
+
}));
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
function view(frame) {
|
|
1199
|
+
lastFrame = frame;
|
|
1200
|
+
const active = store.activeSurface();
|
|
1201
|
+
appHost.reconcile(active?.id ?? "");
|
|
1202
|
+
const headerRows = store.surfaces.length > 0 ? 3 : 2;
|
|
1203
|
+
const [headerRect, bodyRect, footerRect] = split(frame.rect, "rows", [headerRows, -1, 2]);
|
|
1204
|
+
if (headerRect)
|
|
1205
|
+
drawHeader(frame, headerRect, active);
|
|
1206
|
+
// Draw the active surface BEFORE Tabs.List so default keyboard focus lands immediately on the content/ask widget!
|
|
1207
|
+
if (bodyRect && bodyRect.rows > 0) {
|
|
1208
|
+
if (!active) {
|
|
1209
|
+
drawEmptyState(frame, bodyRect);
|
|
1210
|
+
}
|
|
1211
|
+
else if (active.kind === "markdown") {
|
|
1212
|
+
drawMarkdownSurface(frame, bodyRect, active);
|
|
1213
|
+
}
|
|
1214
|
+
else if (active.kind === "file") {
|
|
1215
|
+
drawFileSurface(frame, bodyRect, active);
|
|
1216
|
+
}
|
|
1217
|
+
else if (active.kind === "image") {
|
|
1218
|
+
drawImageSurface(frame, bodyRect, active);
|
|
1219
|
+
}
|
|
1220
|
+
else if (active.kind === "table") {
|
|
1221
|
+
drawTableSurface(frame, bodyRect, active);
|
|
1222
|
+
}
|
|
1223
|
+
else if (active.kind === "chart") {
|
|
1224
|
+
drawChartSurface(frame, bodyRect, active);
|
|
1225
|
+
}
|
|
1226
|
+
else if (active.kind === "log") {
|
|
1227
|
+
drawLogSurface(frame, bodyRect, active);
|
|
1228
|
+
}
|
|
1229
|
+
else if (active.kind === "ask") {
|
|
1230
|
+
drawAskSurface(frame, bodyRect, active);
|
|
1231
|
+
}
|
|
1232
|
+
else if (active.kind === "ui") {
|
|
1233
|
+
drawUiSurface(frame, bodyRect, active, store, {
|
|
1234
|
+
charts: chartCache,
|
|
1235
|
+
canvases: canvasCache,
|
|
1236
|
+
getMarkdown,
|
|
1237
|
+
drawNotesStrip,
|
|
1238
|
+
onUiSubmit: (f, surface) => {
|
|
1239
|
+
if (surface.origin?.paneId || hooks.onSendFeedback) {
|
|
1240
|
+
triggerSendFeedback(f, surface);
|
|
1241
|
+
}
|
|
1242
|
+
},
|
|
1243
|
+
});
|
|
1244
|
+
}
|
|
1245
|
+
else if (active.kind === "app") {
|
|
1246
|
+
drawAppSurface(frame, bodyRect, active);
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
if (headerRect)
|
|
1250
|
+
drawTabsBar(frame, headerRect);
|
|
1251
|
+
if (footerRect)
|
|
1252
|
+
drawFooter(frame, footerRect, active);
|
|
1253
|
+
drawOverlays(frame);
|
|
1254
|
+
}
|
|
1255
|
+
function triggerSendFeedback(frame, active, singleNote) {
|
|
1256
|
+
const summary = formatFeedbackSummary(active, singleNote ? { singleNote } : undefined);
|
|
1257
|
+
const res = hooks.onSendFeedback
|
|
1258
|
+
? hooks.onSendFeedback(active, summary)
|
|
1259
|
+
: sendToOriginPane(active.origin, summary);
|
|
1260
|
+
if (res.ok) {
|
|
1261
|
+
const now = Date.now();
|
|
1262
|
+
if (singleNote) {
|
|
1263
|
+
singleNote.sentAt = now;
|
|
1264
|
+
}
|
|
1265
|
+
else {
|
|
1266
|
+
for (const n of active.notes) {
|
|
1267
|
+
if (n.sentAt === undefined)
|
|
1268
|
+
n.sentAt = now;
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
store.setToast(singleNote ? `Sent note via ${res.via}` : `Sent feedback via ${res.via}`, "success");
|
|
1272
|
+
}
|
|
1273
|
+
else {
|
|
1274
|
+
frame.copy(summary);
|
|
1275
|
+
store.setToast("Copied feedback to clipboard (no mux origin pane)", "info");
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
function triggerOpenExternal(active) {
|
|
1279
|
+
if (active.kind !== "file" && active.kind !== "image") {
|
|
1280
|
+
store.setToast("Only file and image surfaces can be opened externally", "info");
|
|
1281
|
+
return;
|
|
1282
|
+
}
|
|
1283
|
+
const res = hooks.onOpenExternal ? hooks.onOpenExternal(active.path) : openExternally(active.path);
|
|
1284
|
+
if (res.ok) {
|
|
1285
|
+
store.setToast(`Opened via ${res.via}: ${active.title}`, "success");
|
|
1286
|
+
}
|
|
1287
|
+
else {
|
|
1288
|
+
store.setToast(`Open failed: ${res.error ?? "unknown error"}`, "error");
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
function keys(key, frame) {
|
|
1292
|
+
const active = store.activeSurface();
|
|
1293
|
+
// Escape exits filter bar or cancels a pending ask if nothing else trapped it.
|
|
1294
|
+
if (key.name === "escape") {
|
|
1295
|
+
if (filterFocused) {
|
|
1296
|
+
filterFocused = false;
|
|
1297
|
+
frame.focused = "";
|
|
1298
|
+
return true;
|
|
1299
|
+
}
|
|
1300
|
+
if (active && (active.kind === "markdown" || active.kind === "file") && active.selection) {
|
|
1301
|
+
clearSelection(active);
|
|
1302
|
+
return true;
|
|
1303
|
+
}
|
|
1304
|
+
if (active && active.kind === "ask" && active.answer.status === "pending") {
|
|
1305
|
+
store.cancelAsk(active.id);
|
|
1306
|
+
return true;
|
|
1307
|
+
}
|
|
1308
|
+
if (active && active.kind === "ui" && active.submission.status === "pending") {
|
|
1309
|
+
store.cancelUi(active.id);
|
|
1310
|
+
return true;
|
|
1311
|
+
}
|
|
1312
|
+
if (active && active.kind === "app" && active.submission.status === "pending") {
|
|
1313
|
+
store.cancelApp(active.id);
|
|
1314
|
+
return true;
|
|
1315
|
+
}
|
|
1316
|
+
return false;
|
|
1317
|
+
}
|
|
1318
|
+
// Ctrl shortcuts work reliably even from inside Lists / Tables (never collide with typeahead or Ctrl+N/P nav).
|
|
1319
|
+
if ((matchesShortcut(key, "ctrl+e") || matchesShortcut(key, "alt+n")) && active) {
|
|
1320
|
+
noteDraft = "";
|
|
1321
|
+
noteDialogOpen = true;
|
|
1322
|
+
frame.focused = "";
|
|
1323
|
+
return true;
|
|
1324
|
+
}
|
|
1325
|
+
if (matchesShortcut(key, "ctrl+s") && active) {
|
|
1326
|
+
triggerSendFeedback(frame, active);
|
|
1327
|
+
return true;
|
|
1328
|
+
}
|
|
1329
|
+
if (matchesShortcut(key, "ctrl+o") && active) {
|
|
1330
|
+
triggerOpenExternal(active);
|
|
1331
|
+
return true;
|
|
1332
|
+
}
|
|
1333
|
+
if (matchesShortcut(key, "ctrl+w") && active) {
|
|
1334
|
+
store.close(active.id);
|
|
1335
|
+
filterFocused = false;
|
|
1336
|
+
frame.focused = "";
|
|
1337
|
+
return true;
|
|
1338
|
+
}
|
|
1339
|
+
if (matchesShortcut(key, "ctrl+f") && active && (active.kind === "table" || active.kind === "log")) {
|
|
1340
|
+
filterFocused = true;
|
|
1341
|
+
frame.focused = "";
|
|
1342
|
+
return true;
|
|
1343
|
+
}
|
|
1344
|
+
if (key.name !== "char" || key.ctrl || key.alt) {
|
|
1345
|
+
return false;
|
|
1346
|
+
}
|
|
1347
|
+
const ch = key.text;
|
|
1348
|
+
if (ch === "?") {
|
|
1349
|
+
helpOpen = !helpOpen;
|
|
1350
|
+
return true;
|
|
1351
|
+
}
|
|
1352
|
+
if (ch === "[") {
|
|
1353
|
+
filterFocused = false;
|
|
1354
|
+
return store.focusDelta(-1);
|
|
1355
|
+
}
|
|
1356
|
+
if (ch === "]") {
|
|
1357
|
+
filterFocused = false;
|
|
1358
|
+
return store.focusDelta(1);
|
|
1359
|
+
}
|
|
1360
|
+
if (ch >= "1" && ch <= "9") {
|
|
1361
|
+
const idx = Number(ch) - 1;
|
|
1362
|
+
if (idx < store.surfaces.length) {
|
|
1363
|
+
filterFocused = false;
|
|
1364
|
+
return store.focusIndex(idx);
|
|
1365
|
+
}
|
|
1366
|
+
}
|
|
1367
|
+
if (ch === "/" && active && (active.kind === "table" || active.kind === "log")) {
|
|
1368
|
+
filterFocused = true;
|
|
1369
|
+
frame.focused = "";
|
|
1370
|
+
return true;
|
|
1371
|
+
}
|
|
1372
|
+
if (ch === "n" && active) {
|
|
1373
|
+
noteDraft = "";
|
|
1374
|
+
noteDialogOpen = true;
|
|
1375
|
+
frame.focused = "";
|
|
1376
|
+
return true;
|
|
1377
|
+
}
|
|
1378
|
+
if (ch === "s" && active) {
|
|
1379
|
+
triggerSendFeedback(frame, active);
|
|
1380
|
+
return true;
|
|
1381
|
+
}
|
|
1382
|
+
if (ch === "o" && active) {
|
|
1383
|
+
triggerOpenExternal(active);
|
|
1384
|
+
return true;
|
|
1385
|
+
}
|
|
1386
|
+
if (ch === "g" && active?.origin?.paneId) {
|
|
1387
|
+
const res = hooks.onFocusOrigin ? hooks.onFocusOrigin(active) : focusOriginPane(active.origin);
|
|
1388
|
+
store.setToast(res.ok ? `Focused origin via ${res.via}` : `Focus failed: ${res.error}`, res.ok ? "info" : "error");
|
|
1389
|
+
return true;
|
|
1390
|
+
}
|
|
1391
|
+
if (ch === "y" && active) {
|
|
1392
|
+
if ((active.kind === "markdown" || active.kind === "file") && active.selection?.text) {
|
|
1393
|
+
frame.copy(active.selection.text);
|
|
1394
|
+
store.setToast("Copied selected fragment to clipboard", "success", 1800);
|
|
1395
|
+
return true;
|
|
1396
|
+
}
|
|
1397
|
+
const textToCopy = active.kind === "file" || active.kind === "image"
|
|
1398
|
+
? active.path
|
|
1399
|
+
: formatFeedbackSummary(active, { includeAllNotes: true });
|
|
1400
|
+
frame.copy(textToCopy);
|
|
1401
|
+
store.setToast("Copied to clipboard", "success", 1800);
|
|
1402
|
+
return true;
|
|
1403
|
+
}
|
|
1404
|
+
if (ch === "r" && active?.kind === "table" && active.columns.length > 0) {
|
|
1405
|
+
if (active.sortColumn < 0) {
|
|
1406
|
+
active.sortColumn = 0;
|
|
1407
|
+
active.sortDir = "asc";
|
|
1408
|
+
}
|
|
1409
|
+
else if (active.sortDir === "asc") {
|
|
1410
|
+
active.sortDir = "desc";
|
|
1411
|
+
}
|
|
1412
|
+
else {
|
|
1413
|
+
active.sortColumn = (active.sortColumn + 1) % active.columns.length;
|
|
1414
|
+
active.sortDir = "asc";
|
|
1415
|
+
}
|
|
1416
|
+
const col = active.columns[active.sortColumn];
|
|
1417
|
+
store.setToast(`Sorted by ${col?.label ?? ""} (${active.sortDir})`, "info", 1600);
|
|
1418
|
+
return true;
|
|
1419
|
+
}
|
|
1420
|
+
if (ch === "w" && active) {
|
|
1421
|
+
store.close(active.id);
|
|
1422
|
+
filterFocused = false;
|
|
1423
|
+
return true;
|
|
1424
|
+
}
|
|
1425
|
+
if (ch === "d" && store.surfaces.length === 0) {
|
|
1426
|
+
loadDemoSurfaces(store);
|
|
1427
|
+
store.setToast("Loaded demo workspace", "success", 2000);
|
|
1428
|
+
return true;
|
|
1429
|
+
}
|
|
1430
|
+
if (ch === "q" && key.alone) {
|
|
1431
|
+
hooks.onQuit?.();
|
|
1432
|
+
return true;
|
|
1433
|
+
}
|
|
1434
|
+
return false;
|
|
1435
|
+
}
|
|
1436
|
+
function allowAgentStep(step) {
|
|
1437
|
+
// Strictly prevent agents from answering or submitting human-only gates or editing human notes.
|
|
1438
|
+
if (step.ref.startsWith("ask-")) {
|
|
1439
|
+
return step.action === "highlight" || step.action === "focus";
|
|
1440
|
+
}
|
|
1441
|
+
if (step.ref.startsWith("ui-submit-")) {
|
|
1442
|
+
return step.action === "focus";
|
|
1443
|
+
}
|
|
1444
|
+
if (step.ref.startsWith("note-") || step.component === "dialog") {
|
|
1445
|
+
return false;
|
|
1446
|
+
}
|
|
1447
|
+
// Also block untargeted key presses/typing while a pending ask, ui, or app gate is active.
|
|
1448
|
+
const active = store.activeSurface();
|
|
1449
|
+
if (active?.kind === "app" &&
|
|
1450
|
+
active.submission.status === "pending" &&
|
|
1451
|
+
(!step.ref || step.ref.startsWith("app-slot:") || step.ref.startsWith("app-viewport:"))) {
|
|
1452
|
+
return false;
|
|
1453
|
+
}
|
|
1454
|
+
if (!step.ref &&
|
|
1455
|
+
((active?.kind === "ask" && active.answer.status === "pending") ||
|
|
1456
|
+
(active?.kind === "ui" && active.submission.status === "pending"))) {
|
|
1457
|
+
return false;
|
|
1458
|
+
}
|
|
1459
|
+
return true;
|
|
1460
|
+
}
|
|
1461
|
+
function dispose() {
|
|
1462
|
+
unsubscribeStore();
|
|
1463
|
+
appHost.disposeAll();
|
|
1464
|
+
}
|
|
1465
|
+
return { view, keys, allowAgentStep, dispose };
|
|
1466
|
+
}
|