@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/LICENSE +21 -0
- package/README.md +133 -2
- package/dist/cli.js +676 -0
- package/dist/config.js +42 -0
- package/dist/demo.js +166 -0
- package/dist/ipc.js +201 -0
- package/dist/mcp.js +307 -0
- package/dist/multiplexer.js +279 -0
- package/dist/png.js +208 -0
- package/dist/setup.js +328 -0
- package/dist/skill.js +177 -0
- package/dist/state.js +720 -0
- package/dist/types.js +1 -0
- package/dist/ui.js +1020 -0
- package/package.json +57 -4
- package/skills/bypane/SKILL.md +94 -0
package/dist/state.js
ADDED
|
@@ -0,0 +1,720 @@
|
|
|
1
|
+
import { readFileSync, statSync } from "node:fs";
|
|
2
|
+
import { basename, extname, resolve } from "node:path";
|
|
3
|
+
import { decodePng, isPngBuffer } from "./png.js";
|
|
4
|
+
const CHECKLIST_RE = /^(\s*[-*+]\s+\[)([ xX])(\]\s+)(.*)$/;
|
|
5
|
+
export function extractChecklist(markdown) {
|
|
6
|
+
const lines = markdown.split(/\r?\n/);
|
|
7
|
+
const items = [];
|
|
8
|
+
let idx = 0;
|
|
9
|
+
for (let i = 0; i < lines.length; i++) {
|
|
10
|
+
const match = CHECKLIST_RE.exec(lines[i]);
|
|
11
|
+
if (match) {
|
|
12
|
+
items.push({
|
|
13
|
+
index: idx++,
|
|
14
|
+
line: i,
|
|
15
|
+
checked: match[2].toLowerCase() === "x",
|
|
16
|
+
text: match[4].trim(),
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return items;
|
|
21
|
+
}
|
|
22
|
+
export function applyChecklistState(markdown, checkedIndexes) {
|
|
23
|
+
const lines = markdown.split(/\r?\n/);
|
|
24
|
+
let idx = 0;
|
|
25
|
+
for (let i = 0; i < lines.length; i++) {
|
|
26
|
+
const match = CHECKLIST_RE.exec(lines[i]);
|
|
27
|
+
if (match) {
|
|
28
|
+
const checked = checkedIndexes.has(idx);
|
|
29
|
+
lines[i] = `${match[1]}${checked ? "x" : " "}${match[3]}${match[4]}`;
|
|
30
|
+
idx++;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const content = lines.join("\n");
|
|
34
|
+
return { content, checklist: extractChecklist(content) };
|
|
35
|
+
}
|
|
36
|
+
const IMAGE_EXTS = new Set([".png", ".jpg", ".jpeg", ".webp", ".gif", ".bmp", ".svg", ".ico"]);
|
|
37
|
+
const BINARY_EXTS = new Set([
|
|
38
|
+
".pdf",
|
|
39
|
+
".mp4",
|
|
40
|
+
".mov",
|
|
41
|
+
".webm",
|
|
42
|
+
".mkv",
|
|
43
|
+
".mp3",
|
|
44
|
+
".wav",
|
|
45
|
+
".ogg",
|
|
46
|
+
".zip",
|
|
47
|
+
".tar",
|
|
48
|
+
".gz",
|
|
49
|
+
".tgz",
|
|
50
|
+
".7z",
|
|
51
|
+
".rar",
|
|
52
|
+
".wasm",
|
|
53
|
+
".exe",
|
|
54
|
+
".dmg",
|
|
55
|
+
".pkg",
|
|
56
|
+
".deb",
|
|
57
|
+
".rpm",
|
|
58
|
+
".sqlite",
|
|
59
|
+
".db",
|
|
60
|
+
".parquet",
|
|
61
|
+
".xlsx",
|
|
62
|
+
".docx",
|
|
63
|
+
".pptx",
|
|
64
|
+
".ttf",
|
|
65
|
+
".otf",
|
|
66
|
+
".woff",
|
|
67
|
+
".woff2",
|
|
68
|
+
]);
|
|
69
|
+
function mimeHintFor(filePath) {
|
|
70
|
+
const ext = extname(filePath).toLowerCase();
|
|
71
|
+
const map = {
|
|
72
|
+
".png": "image/png",
|
|
73
|
+
".jpg": "image/jpeg",
|
|
74
|
+
".jpeg": "image/jpeg",
|
|
75
|
+
".webp": "image/webp",
|
|
76
|
+
".gif": "image/gif",
|
|
77
|
+
".svg": "image/svg+xml",
|
|
78
|
+
".pdf": "application/pdf",
|
|
79
|
+
".mp4": "video/mp4",
|
|
80
|
+
".mov": "video/quicktime",
|
|
81
|
+
".webm": "video/webm",
|
|
82
|
+
".zip": "application/zip",
|
|
83
|
+
".json": "application/json",
|
|
84
|
+
".md": "text/markdown",
|
|
85
|
+
".ts": "text/typescript",
|
|
86
|
+
".tsx": "text/typescript",
|
|
87
|
+
".js": "text/javascript",
|
|
88
|
+
".py": "text/x-python",
|
|
89
|
+
".go": "text/x-go",
|
|
90
|
+
".rs": "text/x-rust",
|
|
91
|
+
".sql": "application/sql",
|
|
92
|
+
".csv": "text/csv",
|
|
93
|
+
".html": "text/html",
|
|
94
|
+
};
|
|
95
|
+
return map[ext] ?? (ext ? `file (${ext.slice(1)})` : "file");
|
|
96
|
+
}
|
|
97
|
+
function looksBinaryBuffer(buf) {
|
|
98
|
+
const sampleLen = Math.min(buf.length, 4096);
|
|
99
|
+
let nonText = 0;
|
|
100
|
+
for (let i = 0; i < sampleLen; i++) {
|
|
101
|
+
const b = buf[i];
|
|
102
|
+
if (b === 0)
|
|
103
|
+
return true;
|
|
104
|
+
if (b < 7 || (b > 13 && b < 32 && b !== 27))
|
|
105
|
+
nonText++;
|
|
106
|
+
}
|
|
107
|
+
return sampleLen > 0 && nonText / sampleLen > 0.12;
|
|
108
|
+
}
|
|
109
|
+
export function formatBytes(bytes) {
|
|
110
|
+
if (bytes === undefined || !Number.isFinite(bytes))
|
|
111
|
+
return "";
|
|
112
|
+
if (bytes < 1024)
|
|
113
|
+
return `${bytes} B`;
|
|
114
|
+
if (bytes < 1024 * 1024)
|
|
115
|
+
return `${(bytes / 1024).toFixed(1)} KB`;
|
|
116
|
+
if (bytes < 1024 * 1024 * 1024)
|
|
117
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
118
|
+
return `${(bytes / (1024 * 1024 * 1024)).toFixed(2)} GB`;
|
|
119
|
+
}
|
|
120
|
+
export class WorkspaceStore {
|
|
121
|
+
session;
|
|
122
|
+
surfaces = [];
|
|
123
|
+
activeId = "";
|
|
124
|
+
toast = null;
|
|
125
|
+
idCounter = 1;
|
|
126
|
+
listeners = new Set();
|
|
127
|
+
askWaiters = new Map();
|
|
128
|
+
constructor(session) {
|
|
129
|
+
this.session = session;
|
|
130
|
+
}
|
|
131
|
+
subscribe(fn) {
|
|
132
|
+
this.listeners.add(fn);
|
|
133
|
+
return () => this.listeners.delete(fn);
|
|
134
|
+
}
|
|
135
|
+
notify() {
|
|
136
|
+
for (const fn of this.listeners)
|
|
137
|
+
fn();
|
|
138
|
+
}
|
|
139
|
+
setToast(text, tone = "info", durationMs = 2800) {
|
|
140
|
+
this.toast = { text, tone, until: Date.now() + durationMs };
|
|
141
|
+
this.notify();
|
|
142
|
+
}
|
|
143
|
+
activeSurface() {
|
|
144
|
+
if (this.surfaces.length === 0)
|
|
145
|
+
return undefined;
|
|
146
|
+
const found = this.surfaces.find((s) => s.id === this.activeId);
|
|
147
|
+
if (found)
|
|
148
|
+
return found;
|
|
149
|
+
this.activeId = this.surfaces[0].id;
|
|
150
|
+
return this.surfaces[0];
|
|
151
|
+
}
|
|
152
|
+
get(id) {
|
|
153
|
+
return this.surfaces.find((s) => s.id === id);
|
|
154
|
+
}
|
|
155
|
+
focus(id) {
|
|
156
|
+
const s = this.get(id);
|
|
157
|
+
if (!s)
|
|
158
|
+
return false;
|
|
159
|
+
this.activeId = s.id;
|
|
160
|
+
s.unread = false;
|
|
161
|
+
this.notify();
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
focusIndex(index) {
|
|
165
|
+
const s = this.surfaces[index];
|
|
166
|
+
if (!s)
|
|
167
|
+
return false;
|
|
168
|
+
return this.focus(s.id);
|
|
169
|
+
}
|
|
170
|
+
focusDelta(delta) {
|
|
171
|
+
if (this.surfaces.length <= 1)
|
|
172
|
+
return false;
|
|
173
|
+
const idx = Math.max(0, this.surfaces.findIndex((s) => s.id === this.activeId));
|
|
174
|
+
const next = (idx + delta + this.surfaces.length) % this.surfaces.length;
|
|
175
|
+
return this.focusIndex(next);
|
|
176
|
+
}
|
|
177
|
+
close(id) {
|
|
178
|
+
const targetId = id ?? this.activeId;
|
|
179
|
+
const idx = this.surfaces.findIndex((s) => s.id === targetId);
|
|
180
|
+
if (idx < 0)
|
|
181
|
+
return false;
|
|
182
|
+
const [removed] = this.surfaces.splice(idx, 1);
|
|
183
|
+
if (removed && removed.kind === "ask" && removed.answer.status === "pending") {
|
|
184
|
+
this.resolveAskWaiters(removed.id, { status: "cancelled", submittedAt: Date.now() });
|
|
185
|
+
}
|
|
186
|
+
if (this.activeId === targetId) {
|
|
187
|
+
const next = this.surfaces[Math.min(idx, this.surfaces.length - 1)];
|
|
188
|
+
this.activeId = next ? next.id : "";
|
|
189
|
+
if (next)
|
|
190
|
+
next.unread = false;
|
|
191
|
+
}
|
|
192
|
+
this.notify();
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
addNote(surfaceId, noteText) {
|
|
196
|
+
const trimmed = noteText.trim();
|
|
197
|
+
if (!trimmed)
|
|
198
|
+
return false;
|
|
199
|
+
const s = this.get(surfaceId);
|
|
200
|
+
if (!s)
|
|
201
|
+
return false;
|
|
202
|
+
s.notes.push({
|
|
203
|
+
id: `note-${Date.now()}-${Math.random().toString(36).slice(2, 6)}`,
|
|
204
|
+
text: trimmed,
|
|
205
|
+
createdAt: Date.now(),
|
|
206
|
+
});
|
|
207
|
+
s.updatedAt = Date.now();
|
|
208
|
+
this.notify();
|
|
209
|
+
return true;
|
|
210
|
+
}
|
|
211
|
+
nextId(prefix) {
|
|
212
|
+
return `${prefix}-${this.idCounter++}`;
|
|
213
|
+
}
|
|
214
|
+
present(input) {
|
|
215
|
+
const now = Date.now();
|
|
216
|
+
const existing = input.id ? this.get(input.id) : undefined;
|
|
217
|
+
const id = input.id || this.nextId(input.kind);
|
|
218
|
+
const existingNotes = existing ? existing.notes : [];
|
|
219
|
+
const createdAt = existing ? existing.createdAt : now;
|
|
220
|
+
let surface;
|
|
221
|
+
switch (input.kind) {
|
|
222
|
+
case "markdown":
|
|
223
|
+
surface = this.buildMarkdown(id, input, existingNotes, createdAt, now);
|
|
224
|
+
break;
|
|
225
|
+
case "file":
|
|
226
|
+
surface = this.buildFileOrImage(id, input, existingNotes, createdAt, now);
|
|
227
|
+
break;
|
|
228
|
+
case "image":
|
|
229
|
+
surface = this.buildImage(id, input, existingNotes, createdAt, now);
|
|
230
|
+
break;
|
|
231
|
+
case "table":
|
|
232
|
+
surface = this.buildTable(id, input, existing, existingNotes, createdAt, now);
|
|
233
|
+
break;
|
|
234
|
+
case "chart":
|
|
235
|
+
surface = this.buildChart(id, input, existingNotes, createdAt, now);
|
|
236
|
+
break;
|
|
237
|
+
case "log":
|
|
238
|
+
surface = this.buildLog(id, input, existing, existingNotes, createdAt, now);
|
|
239
|
+
break;
|
|
240
|
+
case "ask":
|
|
241
|
+
surface = this.buildAsk(id, input, existingNotes, createdAt, now);
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
const idx = this.surfaces.findIndex((s) => s.id === id);
|
|
245
|
+
if (idx >= 0) {
|
|
246
|
+
this.surfaces[idx] = surface;
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
this.surfaces.push(surface);
|
|
250
|
+
}
|
|
251
|
+
const shouldFocus = input.focus ?? (this.surfaces.length === 1 || input.kind === "ask" || idx < 0);
|
|
252
|
+
if (shouldFocus) {
|
|
253
|
+
this.activeId = surface.id;
|
|
254
|
+
surface.unread = false;
|
|
255
|
+
}
|
|
256
|
+
else if (this.activeId !== surface.id) {
|
|
257
|
+
surface.unread = true;
|
|
258
|
+
}
|
|
259
|
+
this.notify();
|
|
260
|
+
return surface;
|
|
261
|
+
}
|
|
262
|
+
appendLog(surfaceId, lines) {
|
|
263
|
+
const existing = this.get(surfaceId);
|
|
264
|
+
if (!existing || existing.kind !== "log")
|
|
265
|
+
return null;
|
|
266
|
+
for (const line of lines)
|
|
267
|
+
existing.lines.push(line);
|
|
268
|
+
existing.updatedAt = Date.now();
|
|
269
|
+
if (this.activeId !== existing.id)
|
|
270
|
+
existing.unread = true;
|
|
271
|
+
this.notify();
|
|
272
|
+
return existing;
|
|
273
|
+
}
|
|
274
|
+
updateChecklist(surfaceId, checkedValues) {
|
|
275
|
+
const s = this.get(surfaceId);
|
|
276
|
+
if (!s || s.kind !== "markdown")
|
|
277
|
+
return false;
|
|
278
|
+
const set = new Set();
|
|
279
|
+
for (const v of checkedValues) {
|
|
280
|
+
const n = Number(v);
|
|
281
|
+
if (Number.isInteger(n))
|
|
282
|
+
set.add(n);
|
|
283
|
+
}
|
|
284
|
+
const next = applyChecklistState(s.content, set);
|
|
285
|
+
s.content = next.content;
|
|
286
|
+
s.checklist = next.checklist;
|
|
287
|
+
s.updatedAt = Date.now();
|
|
288
|
+
this.notify();
|
|
289
|
+
return true;
|
|
290
|
+
}
|
|
291
|
+
submitAsk(surfaceId, value) {
|
|
292
|
+
const s = this.get(surfaceId);
|
|
293
|
+
if (!s || s.kind !== "ask")
|
|
294
|
+
return false;
|
|
295
|
+
const answer = {
|
|
296
|
+
status: "submitted",
|
|
297
|
+
value,
|
|
298
|
+
submittedAt: Date.now(),
|
|
299
|
+
};
|
|
300
|
+
s.answer = answer;
|
|
301
|
+
s.updatedAt = answer.submittedAt;
|
|
302
|
+
this.resolveAskWaiters(surfaceId, answer);
|
|
303
|
+
this.setToast(`Submitted: ${s.title}`, "success");
|
|
304
|
+
return true;
|
|
305
|
+
}
|
|
306
|
+
cancelAsk(surfaceId) {
|
|
307
|
+
const s = this.get(surfaceId);
|
|
308
|
+
if (!s || s.kind !== "ask")
|
|
309
|
+
return false;
|
|
310
|
+
const answer = { status: "cancelled", submittedAt: Date.now() };
|
|
311
|
+
s.answer = answer;
|
|
312
|
+
s.updatedAt = answer.submittedAt;
|
|
313
|
+
this.resolveAskWaiters(surfaceId, answer);
|
|
314
|
+
this.setToast(`Cancelled: ${s.title}`, "info");
|
|
315
|
+
return true;
|
|
316
|
+
}
|
|
317
|
+
waitForAsk(surfaceId, timeoutMs) {
|
|
318
|
+
const s = this.get(surfaceId);
|
|
319
|
+
if (!s || s.kind !== "ask") {
|
|
320
|
+
return Promise.resolve({ status: "cancelled", submittedAt: Date.now() });
|
|
321
|
+
}
|
|
322
|
+
if (s.answer.status !== "pending") {
|
|
323
|
+
return Promise.resolve(s.answer);
|
|
324
|
+
}
|
|
325
|
+
return new Promise((resolvePromise) => {
|
|
326
|
+
const waiter = { resolve: resolvePromise };
|
|
327
|
+
if (timeoutMs && timeoutMs > 0) {
|
|
328
|
+
waiter.timer = setTimeout(() => {
|
|
329
|
+
const list = this.askWaiters.get(surfaceId);
|
|
330
|
+
if (list) {
|
|
331
|
+
const i = list.indexOf(waiter);
|
|
332
|
+
if (i >= 0)
|
|
333
|
+
list.splice(i, 1);
|
|
334
|
+
}
|
|
335
|
+
resolvePromise({ status: "pending" });
|
|
336
|
+
}, timeoutMs);
|
|
337
|
+
}
|
|
338
|
+
const list = this.askWaiters.get(surfaceId) ?? [];
|
|
339
|
+
list.push(waiter);
|
|
340
|
+
this.askWaiters.set(surfaceId, list);
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
resolveAskWaiters(surfaceId, answer) {
|
|
344
|
+
const list = this.askWaiters.get(surfaceId);
|
|
345
|
+
if (!list)
|
|
346
|
+
return;
|
|
347
|
+
this.askWaiters.delete(surfaceId);
|
|
348
|
+
for (const w of list) {
|
|
349
|
+
if (w.timer)
|
|
350
|
+
clearTimeout(w.timer);
|
|
351
|
+
w.resolve(answer);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
buildMarkdown(id, input, notes, createdAt, now) {
|
|
355
|
+
return {
|
|
356
|
+
id,
|
|
357
|
+
kind: "markdown",
|
|
358
|
+
title: input.title || "Document",
|
|
359
|
+
content: input.content,
|
|
360
|
+
checklist: extractChecklist(input.content),
|
|
361
|
+
origin: input.origin,
|
|
362
|
+
notes,
|
|
363
|
+
unread: false,
|
|
364
|
+
createdAt,
|
|
365
|
+
updatedAt: now,
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
buildFileOrImage(id, input, notes, createdAt, now) {
|
|
369
|
+
const absPath = resolve(input.origin?.cwd ?? process.cwd(), input.path);
|
|
370
|
+
const ext = extname(absPath).toLowerCase();
|
|
371
|
+
const title = input.title || basename(absPath);
|
|
372
|
+
if (input.content === undefined && IMAGE_EXTS.has(ext) && ext !== ".svg") {
|
|
373
|
+
return this.buildImage(id, {
|
|
374
|
+
id,
|
|
375
|
+
kind: "image",
|
|
376
|
+
title,
|
|
377
|
+
path: absPath,
|
|
378
|
+
origin: input.origin,
|
|
379
|
+
focus: input.focus,
|
|
380
|
+
}, notes, createdAt, now);
|
|
381
|
+
}
|
|
382
|
+
let content = input.content;
|
|
383
|
+
let binary = false;
|
|
384
|
+
let sizeBytes;
|
|
385
|
+
const mimeHint = mimeHintFor(absPath);
|
|
386
|
+
if (content === undefined) {
|
|
387
|
+
try {
|
|
388
|
+
const st = statSync(absPath);
|
|
389
|
+
sizeBytes = st.size;
|
|
390
|
+
if (BINARY_EXTS.has(ext) || st.size > 2 * 1024 * 1024) {
|
|
391
|
+
binary = true;
|
|
392
|
+
}
|
|
393
|
+
else {
|
|
394
|
+
const buf = readFileSync(absPath);
|
|
395
|
+
if (looksBinaryBuffer(buf)) {
|
|
396
|
+
binary = true;
|
|
397
|
+
}
|
|
398
|
+
else {
|
|
399
|
+
content = buf.toString("utf8");
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
catch (err) {
|
|
404
|
+
content = `Unable to read file: ${err.message}`;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
else {
|
|
408
|
+
sizeBytes = Buffer.byteLength(content, "utf8");
|
|
409
|
+
}
|
|
410
|
+
return {
|
|
411
|
+
id,
|
|
412
|
+
kind: "file",
|
|
413
|
+
title,
|
|
414
|
+
path: absPath,
|
|
415
|
+
content,
|
|
416
|
+
binary,
|
|
417
|
+
sizeBytes,
|
|
418
|
+
mimeHint,
|
|
419
|
+
highlightStart: input.highlightStart,
|
|
420
|
+
highlightEnd: input.highlightEnd,
|
|
421
|
+
origin: input.origin,
|
|
422
|
+
notes,
|
|
423
|
+
unread: false,
|
|
424
|
+
createdAt,
|
|
425
|
+
updatedAt: now,
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
buildImage(id, input, notes, createdAt, now) {
|
|
429
|
+
const absPath = resolve(input.origin?.cwd ?? process.cwd(), input.path);
|
|
430
|
+
const title = input.title || basename(absPath);
|
|
431
|
+
let sizeBytes;
|
|
432
|
+
let width;
|
|
433
|
+
let height;
|
|
434
|
+
let rgba;
|
|
435
|
+
let error;
|
|
436
|
+
try {
|
|
437
|
+
const buf = readFileSync(absPath);
|
|
438
|
+
sizeBytes = buf.length;
|
|
439
|
+
if (isPngBuffer(buf)) {
|
|
440
|
+
const decoded = decodePng(buf);
|
|
441
|
+
width = decoded.width;
|
|
442
|
+
height = decoded.height;
|
|
443
|
+
rgba = decoded.rgba;
|
|
444
|
+
}
|
|
445
|
+
else {
|
|
446
|
+
error = `Inline preview supports PNG (${mimeHintFor(absPath)} detected). Press 'o' to open on host.`;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
catch (err) {
|
|
450
|
+
error = err.message;
|
|
451
|
+
}
|
|
452
|
+
return {
|
|
453
|
+
id,
|
|
454
|
+
kind: "image",
|
|
455
|
+
title,
|
|
456
|
+
path: absPath,
|
|
457
|
+
width,
|
|
458
|
+
height,
|
|
459
|
+
sizeBytes,
|
|
460
|
+
caption: input.caption,
|
|
461
|
+
rgba,
|
|
462
|
+
error,
|
|
463
|
+
origin: input.origin,
|
|
464
|
+
notes,
|
|
465
|
+
unread: false,
|
|
466
|
+
createdAt,
|
|
467
|
+
updatedAt: now,
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
buildTable(id, input, existing, notes, createdAt, now) {
|
|
471
|
+
const columns = input.columns.map((c) => typeof c === "string" ? { key: c, label: c } : c);
|
|
472
|
+
const rows = input.rows.map((r, index) => {
|
|
473
|
+
if (Array.isArray(r)) {
|
|
474
|
+
return { id: String(index), cells: r.map((v) => String(v ?? "")) };
|
|
475
|
+
}
|
|
476
|
+
return {
|
|
477
|
+
id: String(r.id ?? index),
|
|
478
|
+
cells: columns.map((col) => {
|
|
479
|
+
const val = r[col.key] ?? r[col.label];
|
|
480
|
+
return val === undefined || val === null ? "" : String(val);
|
|
481
|
+
}),
|
|
482
|
+
};
|
|
483
|
+
});
|
|
484
|
+
const prev = existing && existing.kind === "table" ? existing : undefined;
|
|
485
|
+
return {
|
|
486
|
+
id,
|
|
487
|
+
kind: "table",
|
|
488
|
+
title: input.title || "Table",
|
|
489
|
+
columns,
|
|
490
|
+
rows,
|
|
491
|
+
filter: prev?.filter ?? "",
|
|
492
|
+
sortColumn: prev?.sortColumn ?? -1,
|
|
493
|
+
sortDir: prev?.sortDir ?? "asc",
|
|
494
|
+
selectedRowId: prev?.selectedRowId ?? rows[0]?.id ?? "",
|
|
495
|
+
highlightedRowId: prev?.highlightedRowId ?? rows[0]?.id ?? "",
|
|
496
|
+
origin: input.origin,
|
|
497
|
+
notes,
|
|
498
|
+
unread: false,
|
|
499
|
+
createdAt,
|
|
500
|
+
updatedAt: now,
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
buildChart(id, input, notes, createdAt, now) {
|
|
504
|
+
return {
|
|
505
|
+
id,
|
|
506
|
+
kind: "chart",
|
|
507
|
+
title: input.title || "Chart",
|
|
508
|
+
series: input.series.map((s) => ({
|
|
509
|
+
name: s.name,
|
|
510
|
+
values: s.values.map((v) => Number(v)),
|
|
511
|
+
color: s.color,
|
|
512
|
+
})),
|
|
513
|
+
unit: input.unit,
|
|
514
|
+
min: input.min,
|
|
515
|
+
max: input.max,
|
|
516
|
+
caption: input.caption,
|
|
517
|
+
origin: input.origin,
|
|
518
|
+
notes,
|
|
519
|
+
unread: false,
|
|
520
|
+
createdAt,
|
|
521
|
+
updatedAt: now,
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
buildLog(id, input, existing, notes, createdAt, now) {
|
|
525
|
+
const prev = existing && existing.kind === "log" ? existing : undefined;
|
|
526
|
+
const lines = input.append && prev ? [...prev.lines, ...input.lines] : [...input.lines];
|
|
527
|
+
return {
|
|
528
|
+
id,
|
|
529
|
+
kind: "log",
|
|
530
|
+
title: input.title || "Log",
|
|
531
|
+
lines,
|
|
532
|
+
filter: prev?.filter ?? "",
|
|
533
|
+
stickToEnd: prev?.stickToEnd ?? true,
|
|
534
|
+
origin: input.origin,
|
|
535
|
+
notes,
|
|
536
|
+
unread: false,
|
|
537
|
+
createdAt,
|
|
538
|
+
updatedAt: now,
|
|
539
|
+
};
|
|
540
|
+
}
|
|
541
|
+
buildAsk(id, input, notes, createdAt, now) {
|
|
542
|
+
const options = (input.options ?? []).map((o) => typeof o === "string" ? { value: o, label: o } : o);
|
|
543
|
+
if (input.mode === "confirm" && options.length === 0) {
|
|
544
|
+
options.push({ value: "confirm", label: input.confirmLabel || "Approve / Confirm" }, { value: "cancel", label: input.cancelLabel || "Decline / Cancel" });
|
|
545
|
+
}
|
|
546
|
+
const defaultSelect = typeof input.defaultValue === "string" ? input.defaultValue : (options[0]?.value ?? "");
|
|
547
|
+
const defaultMulti = Array.isArray(input.defaultValue) ? [...input.defaultValue] : [];
|
|
548
|
+
const defaultText = typeof input.defaultValue === "string" ? input.defaultValue : "";
|
|
549
|
+
return {
|
|
550
|
+
id,
|
|
551
|
+
kind: "ask",
|
|
552
|
+
title: input.title || "Decision",
|
|
553
|
+
mode: input.mode,
|
|
554
|
+
prompt: input.prompt,
|
|
555
|
+
context: input.context,
|
|
556
|
+
options,
|
|
557
|
+
defaultValue: input.defaultValue,
|
|
558
|
+
placeholder: input.placeholder,
|
|
559
|
+
confirmLabel: input.confirmLabel,
|
|
560
|
+
cancelLabel: input.cancelLabel,
|
|
561
|
+
draftSelect: defaultSelect,
|
|
562
|
+
draftMulti: defaultMulti,
|
|
563
|
+
draftText: defaultText,
|
|
564
|
+
answer: { status: "pending" },
|
|
565
|
+
origin: input.origin,
|
|
566
|
+
notes,
|
|
567
|
+
unread: false,
|
|
568
|
+
createdAt,
|
|
569
|
+
updatedAt: now,
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
listSummaries() {
|
|
573
|
+
return this.surfaces.map((s) => ({
|
|
574
|
+
id: s.id,
|
|
575
|
+
kind: s.kind,
|
|
576
|
+
title: s.title,
|
|
577
|
+
unread: s.unread,
|
|
578
|
+
active: s.id === this.activeId,
|
|
579
|
+
notesCount: s.notes.length,
|
|
580
|
+
updatedAt: s.updatedAt,
|
|
581
|
+
origin: s.origin,
|
|
582
|
+
status: s.kind === "ask" ? s.answer.status : undefined,
|
|
583
|
+
}));
|
|
584
|
+
}
|
|
585
|
+
serializeSurface(surface) {
|
|
586
|
+
if (surface.kind === "image") {
|
|
587
|
+
const { rgba: _omit, ...rest } = surface;
|
|
588
|
+
return { ...rest, hasPixels: Boolean(surface.rgba) };
|
|
589
|
+
}
|
|
590
|
+
if (surface.kind === "table") {
|
|
591
|
+
const selected = surface.rows.find((r) => r.id === surface.selectedRowId);
|
|
592
|
+
const highlighted = surface.rows.find((r) => r.id === surface.highlightedRowId);
|
|
593
|
+
return {
|
|
594
|
+
id: surface.id,
|
|
595
|
+
kind: surface.kind,
|
|
596
|
+
title: surface.title,
|
|
597
|
+
filter: surface.filter,
|
|
598
|
+
sortColumn: surface.sortColumn,
|
|
599
|
+
sortDir: surface.sortDir,
|
|
600
|
+
rowCount: surface.rows.length,
|
|
601
|
+
columns: surface.columns,
|
|
602
|
+
selectedRow: selected,
|
|
603
|
+
highlightedRow: highlighted,
|
|
604
|
+
notes: surface.notes,
|
|
605
|
+
origin: surface.origin,
|
|
606
|
+
updatedAt: surface.updatedAt,
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
if (surface.kind === "log") {
|
|
610
|
+
return {
|
|
611
|
+
id: surface.id,
|
|
612
|
+
kind: surface.kind,
|
|
613
|
+
title: surface.title,
|
|
614
|
+
lineCount: surface.lines.length,
|
|
615
|
+
filter: surface.filter,
|
|
616
|
+
tail: surface.lines.slice(-20),
|
|
617
|
+
notes: surface.notes,
|
|
618
|
+
origin: surface.origin,
|
|
619
|
+
updatedAt: surface.updatedAt,
|
|
620
|
+
};
|
|
621
|
+
}
|
|
622
|
+
return { ...surface };
|
|
623
|
+
}
|
|
624
|
+
serializeState(surfaceId) {
|
|
625
|
+
if (surfaceId) {
|
|
626
|
+
const s = this.get(surfaceId);
|
|
627
|
+
if (!s)
|
|
628
|
+
return { error: `Surface "${surfaceId}" not found` };
|
|
629
|
+
return this.serializeSurface(s);
|
|
630
|
+
}
|
|
631
|
+
const active = this.activeSurface();
|
|
632
|
+
return {
|
|
633
|
+
session: this.session,
|
|
634
|
+
activeId: this.activeId,
|
|
635
|
+
surfaces: this.listSummaries(),
|
|
636
|
+
active: active ? this.serializeSurface(active) : null,
|
|
637
|
+
};
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
export function sortTableRows(surface) {
|
|
641
|
+
const rows = [...surface.rows];
|
|
642
|
+
if (surface.sortColumn < 0 || surface.sortColumn >= surface.columns.length) {
|
|
643
|
+
return rows;
|
|
644
|
+
}
|
|
645
|
+
const col = surface.sortColumn;
|
|
646
|
+
const dir = surface.sortDir === "asc" ? 1 : -1;
|
|
647
|
+
rows.sort((a, b) => {
|
|
648
|
+
const av = a.cells[col] ?? "";
|
|
649
|
+
const bv = b.cells[col] ?? "";
|
|
650
|
+
const an = Number.parseFloat(av.replace(/[,%$]/g, ""));
|
|
651
|
+
const bn = Number.parseFloat(bv.replace(/[,%$]/g, ""));
|
|
652
|
+
if (!Number.isNaN(an) && !Number.isNaN(bn) && /^\s*[-+]?[\d.,]+/.test(av) && /^\s*[-+]?[\d.,]+/.test(bv)) {
|
|
653
|
+
return (an - bn) * dir;
|
|
654
|
+
}
|
|
655
|
+
return av.localeCompare(bv) * dir;
|
|
656
|
+
});
|
|
657
|
+
return rows;
|
|
658
|
+
}
|
|
659
|
+
/**
|
|
660
|
+
* Formats a compact, clean feedback message for human -> agent push-back (`s` hotkey).
|
|
661
|
+
*/
|
|
662
|
+
export function formatFeedbackSummary(surface) {
|
|
663
|
+
const lines = [`[bypane · ${surface.title} (${surface.kind})]`];
|
|
664
|
+
if (surface.kind === "markdown") {
|
|
665
|
+
if (surface.checklist.length > 0) {
|
|
666
|
+
const done = surface.checklist.filter((c) => c.checked).map((c) => c.text);
|
|
667
|
+
const todo = surface.checklist.filter((c) => !c.checked).map((c) => c.text);
|
|
668
|
+
if (done.length > 0)
|
|
669
|
+
lines.push(`Checked (${done.length}): ${done.join("; ")}`);
|
|
670
|
+
if (todo.length > 0)
|
|
671
|
+
lines.push(`Unchecked (${todo.length}): ${todo.join("; ")}`);
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
else if (surface.kind === "file") {
|
|
675
|
+
const range = surface.highlightStart !== undefined
|
|
676
|
+
? `:${surface.highlightStart}${surface.highlightEnd && surface.highlightEnd !== surface.highlightStart ? `-${surface.highlightEnd}` : ""}`
|
|
677
|
+
: "";
|
|
678
|
+
lines.push(`File: ${surface.path}${range}`);
|
|
679
|
+
}
|
|
680
|
+
else if (surface.kind === "image") {
|
|
681
|
+
lines.push(`Image: ${surface.path}`);
|
|
682
|
+
if (surface.caption)
|
|
683
|
+
lines.push(`Caption: ${surface.caption}`);
|
|
684
|
+
}
|
|
685
|
+
else if (surface.kind === "table") {
|
|
686
|
+
const row = surface.rows.find((r) => r.id === surface.selectedRowId) ??
|
|
687
|
+
surface.rows.find((r) => r.id === surface.highlightedRowId);
|
|
688
|
+
if (row) {
|
|
689
|
+
const pairs = surface.columns.map((c, i) => `${c.label}=${row.cells[i] ?? ""}`);
|
|
690
|
+
lines.push(`Selected row: ${pairs.join(", ")}`);
|
|
691
|
+
}
|
|
692
|
+
if (surface.filter)
|
|
693
|
+
lines.push(`Filter: "${surface.filter}"`);
|
|
694
|
+
}
|
|
695
|
+
else if (surface.kind === "chart") {
|
|
696
|
+
const stats = surface.series.map((s) => {
|
|
697
|
+
const last = s.values[s.values.length - 1];
|
|
698
|
+
return `${s.name}=${last ?? "n/a"}${surface.unit ? ` ${surface.unit}` : ""}`;
|
|
699
|
+
});
|
|
700
|
+
lines.push(`Series: ${stats.join(", ")}`);
|
|
701
|
+
}
|
|
702
|
+
else if (surface.kind === "log") {
|
|
703
|
+
lines.push(`Lines: ${surface.lines.length}${surface.filter ? ` (filter: "${surface.filter}")` : ""}`);
|
|
704
|
+
}
|
|
705
|
+
else if (surface.kind === "ask") {
|
|
706
|
+
if (surface.answer.status === "submitted") {
|
|
707
|
+
lines.push(`Answer: ${JSON.stringify(surface.answer.value)}`);
|
|
708
|
+
}
|
|
709
|
+
else {
|
|
710
|
+
lines.push(`Status: ${surface.answer.status}`);
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
if (surface.notes.length > 0) {
|
|
714
|
+
lines.push("Notes:");
|
|
715
|
+
for (const n of surface.notes) {
|
|
716
|
+
lines.push(`- ${n.text}`);
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
return lines.join("\n");
|
|
720
|
+
}
|