@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/cli.js
ADDED
|
@@ -0,0 +1,676 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { readFileSync, realpathSync } from "node:fs";
|
|
3
|
+
import { basename, extname, resolve } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { runApp } from "tui";
|
|
6
|
+
import { createAgent } from "tui/agent";
|
|
7
|
+
import { loadDemoSurfaces } from "./demo.js";
|
|
8
|
+
import { sendIpc, socketPathFor, startIpcServer } from "./ipc.js";
|
|
9
|
+
import { serveMcpStdio } from "./mcp.js";
|
|
10
|
+
import { detectCallerOrigin, resolveSessionKey } from "./multiplexer.js";
|
|
11
|
+
import { runSetupWizard, runSkillSubcommand } from "./setup.js";
|
|
12
|
+
import { WorkspaceStore } from "./state.js";
|
|
13
|
+
import { bypaneTheme, createBypaneUi } from "./ui.js";
|
|
14
|
+
const HELP_TEXT = `bypane — 2 ms terminal companion pane for coding agents
|
|
15
|
+
|
|
16
|
+
Usage:
|
|
17
|
+
bypane [--session <id>] [--demo] Start the live TUI pane
|
|
18
|
+
bypane setup [--yes] Interactive setup wizard (skills, layout, MCP)
|
|
19
|
+
bypane skill <setup|show|status|remove> Manage the bypane agent skill
|
|
20
|
+
bypane mcp [--session <id>] Start stdio MCP server
|
|
21
|
+
bypane show <file[:start[-end]]> [flags] Show Markdown, code, image, or binary QuickLook
|
|
22
|
+
bypane md [text] [--title <t>] [--id <id>] Show Markdown (arg or stdin)
|
|
23
|
+
bypane table [file] [--title <t>] [--id <id>] Show JSON/CSV/TSV table (file or stdin)
|
|
24
|
+
bypane chart --series name:1,2,3 [flags] Show braille chart (flags or JSON stdin)
|
|
25
|
+
bypane log [--title <t>] [--id <id>] [--append] Stream lines from stdin into a log surface
|
|
26
|
+
bypane ask --prompt <q> [flags] Ask human a gated decision (blocks until submit)
|
|
27
|
+
bypane list | state [id] | close [id] | demo Inspect or manage active workspace
|
|
28
|
+
|
|
29
|
+
Global flags:
|
|
30
|
+
--session, -s <name> Override workspace session key (default: Rex/tmux window or cwd hash)
|
|
31
|
+
--no-dock Do not auto-split a Rex/tmux pane when bypane isn't running
|
|
32
|
+
--help, -h Show this help
|
|
33
|
+
`;
|
|
34
|
+
function parseArgv(argv) {
|
|
35
|
+
const flags = {};
|
|
36
|
+
const rest = [];
|
|
37
|
+
const multiFlags = new Set(["option", "series"]);
|
|
38
|
+
const setFlag = (key, val) => {
|
|
39
|
+
if (multiFlags.has(key)) {
|
|
40
|
+
const prev = flags[key];
|
|
41
|
+
const list = Array.isArray(prev) ? prev : prev ? [String(prev)] : [];
|
|
42
|
+
list.push(String(val));
|
|
43
|
+
flags[key] = list;
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
flags[key] = val;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
for (let i = 0; i < argv.length; i++) {
|
|
50
|
+
const arg = argv[i];
|
|
51
|
+
if (arg === "--") {
|
|
52
|
+
rest.push(...argv.slice(i + 1));
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
if (arg.startsWith("--")) {
|
|
56
|
+
const eq = arg.indexOf("=");
|
|
57
|
+
if (eq >= 0) {
|
|
58
|
+
setFlag(arg.slice(2, eq), arg.slice(eq + 1));
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
const key = arg.slice(2);
|
|
62
|
+
const next = argv[i + 1];
|
|
63
|
+
const isBoolFlag = key === "demo" ||
|
|
64
|
+
key === "help" ||
|
|
65
|
+
key === "yes" ||
|
|
66
|
+
key === "force" ||
|
|
67
|
+
key === "append" ||
|
|
68
|
+
key === "no-wait" ||
|
|
69
|
+
key === "no-dock" ||
|
|
70
|
+
key === "no-focus" ||
|
|
71
|
+
key === "no-install" ||
|
|
72
|
+
key === "raw";
|
|
73
|
+
if (!isBoolFlag && next !== undefined && !next.startsWith("-")) {
|
|
74
|
+
setFlag(key, next);
|
|
75
|
+
i++;
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
setFlag(key, true);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
else if (arg.startsWith("-") && arg.length === 2) {
|
|
83
|
+
const short = arg[1];
|
|
84
|
+
const map = {
|
|
85
|
+
s: "session",
|
|
86
|
+
t: "title",
|
|
87
|
+
h: "help",
|
|
88
|
+
y: "yes",
|
|
89
|
+
m: "mode",
|
|
90
|
+
p: "prompt",
|
|
91
|
+
o: "option",
|
|
92
|
+
u: "unit",
|
|
93
|
+
};
|
|
94
|
+
const key = map[short] ?? short;
|
|
95
|
+
const next = argv[i + 1];
|
|
96
|
+
if (key !== "help" && key !== "yes" && next !== undefined && !next.startsWith("-")) {
|
|
97
|
+
setFlag(key, next);
|
|
98
|
+
i++;
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
setFlag(key, true);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
rest.push(arg);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const knownCommands = new Set([
|
|
109
|
+
"setup",
|
|
110
|
+
"skill",
|
|
111
|
+
"mcp",
|
|
112
|
+
"show",
|
|
113
|
+
"md",
|
|
114
|
+
"markdown",
|
|
115
|
+
"table",
|
|
116
|
+
"chart",
|
|
117
|
+
"log",
|
|
118
|
+
"ask",
|
|
119
|
+
"list",
|
|
120
|
+
"state",
|
|
121
|
+
"close",
|
|
122
|
+
"snapshot",
|
|
123
|
+
"demo",
|
|
124
|
+
"help",
|
|
125
|
+
]);
|
|
126
|
+
const first = rest[0];
|
|
127
|
+
if (first && knownCommands.has(first)) {
|
|
128
|
+
return { command: first, positionals: rest.slice(1), flags };
|
|
129
|
+
}
|
|
130
|
+
return { command: "tui", positionals: rest, flags };
|
|
131
|
+
}
|
|
132
|
+
function strFlag(flags, key) {
|
|
133
|
+
const v = flags[key];
|
|
134
|
+
return typeof v === "string" ? v : undefined;
|
|
135
|
+
}
|
|
136
|
+
function listFlag(flags, key) {
|
|
137
|
+
const v = flags[key];
|
|
138
|
+
if (Array.isArray(v))
|
|
139
|
+
return v;
|
|
140
|
+
if (typeof v === "string")
|
|
141
|
+
return [v];
|
|
142
|
+
return [];
|
|
143
|
+
}
|
|
144
|
+
async function readStdinAll() {
|
|
145
|
+
if (process.stdin.isTTY)
|
|
146
|
+
return "";
|
|
147
|
+
const chunks = [];
|
|
148
|
+
for await (const chunk of process.stdin) {
|
|
149
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
150
|
+
}
|
|
151
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
152
|
+
}
|
|
153
|
+
function parseFileTarget(spec) {
|
|
154
|
+
const match = /^(.*):(\d+)(?:-(\d+))?$/.exec(spec);
|
|
155
|
+
if (!match)
|
|
156
|
+
return { path: spec };
|
|
157
|
+
const start = Number(match[2]);
|
|
158
|
+
const end = match[3] ? Number(match[3]) : start;
|
|
159
|
+
return { path: match[1], start, end };
|
|
160
|
+
}
|
|
161
|
+
function parseDelimited(raw, sep) {
|
|
162
|
+
const lines = raw
|
|
163
|
+
.split(/\r?\n/)
|
|
164
|
+
.map((l) => l.trimEnd())
|
|
165
|
+
.filter((l) => l.length > 0);
|
|
166
|
+
if (lines.length === 0)
|
|
167
|
+
return { columns: [], rows: [] };
|
|
168
|
+
const splitLine = (line) => {
|
|
169
|
+
if (sep === "\t")
|
|
170
|
+
return line.split("\t");
|
|
171
|
+
const out = [];
|
|
172
|
+
let cur = "";
|
|
173
|
+
let inQuotes = false;
|
|
174
|
+
for (let i = 0; i < line.length; i++) {
|
|
175
|
+
const ch = line[i];
|
|
176
|
+
if (ch === '"') {
|
|
177
|
+
if (inQuotes && line[i + 1] === '"') {
|
|
178
|
+
cur += '"';
|
|
179
|
+
i++;
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
inQuotes = !inQuotes;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
else if (ch === sep && !inQuotes) {
|
|
186
|
+
out.push(cur.trim());
|
|
187
|
+
cur = "";
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
cur += ch;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
out.push(cur.trim());
|
|
194
|
+
return out;
|
|
195
|
+
};
|
|
196
|
+
const header = splitLine(lines[0]);
|
|
197
|
+
const columns = header.map((h, i) => ({ key: h || `col${i}`, label: h || `Col ${i + 1}` }));
|
|
198
|
+
const rows = lines.slice(1).map(splitLine);
|
|
199
|
+
return { columns, rows };
|
|
200
|
+
}
|
|
201
|
+
function parseTableInput(raw) {
|
|
202
|
+
const trimmed = raw.trim();
|
|
203
|
+
if (!trimmed)
|
|
204
|
+
return { columns: [], rows: [] };
|
|
205
|
+
if (trimmed.startsWith("[") || trimmed.startsWith("{")) {
|
|
206
|
+
try {
|
|
207
|
+
const parsed = JSON.parse(trimmed);
|
|
208
|
+
if (Array.isArray(parsed)) {
|
|
209
|
+
if (parsed.length === 0)
|
|
210
|
+
return { columns: [], rows: [] };
|
|
211
|
+
if (Array.isArray(parsed[0])) {
|
|
212
|
+
const header = parsed[0].map((v, i) => String(v ?? `col${i}`));
|
|
213
|
+
return {
|
|
214
|
+
columns: header.map((h) => ({ key: h, label: h })),
|
|
215
|
+
rows: parsed.slice(1).map((r) => (Array.isArray(r) ? r.map((v) => String(v ?? "")) : [String(r)])),
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
const keySet = new Set();
|
|
219
|
+
for (const item of parsed) {
|
|
220
|
+
if (item && typeof item === "object" && !Array.isArray(item)) {
|
|
221
|
+
for (const k of Object.keys(item))
|
|
222
|
+
keySet.add(k);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
const columns = [...keySet].map((k) => ({ key: k, label: k }));
|
|
226
|
+
return { columns, rows: parsed };
|
|
227
|
+
}
|
|
228
|
+
if (parsed && typeof parsed === "object" && "columns" in parsed && "rows" in parsed) {
|
|
229
|
+
const obj = parsed;
|
|
230
|
+
return {
|
|
231
|
+
columns: obj.columns.map((c) => (typeof c === "string" ? { key: c, label: c } : c)),
|
|
232
|
+
rows: obj.rows,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
catch {
|
|
237
|
+
// Fall through to TSV/CSV parser.
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
const firstLine = trimmed.split(/\r?\n/)[0] ?? "";
|
|
241
|
+
const sep = firstLine.includes("\t") ? "\t" : ",";
|
|
242
|
+
return parseDelimited(trimmed, sep);
|
|
243
|
+
}
|
|
244
|
+
export function createIpcHandler(store, getAgentDriver, redraw) {
|
|
245
|
+
return async function handleIpc(req) {
|
|
246
|
+
switch (req.op) {
|
|
247
|
+
case "ping":
|
|
248
|
+
return { id: req.id, ok: true, data: { session: store.session, surfaces: store.surfaces.length } };
|
|
249
|
+
case "present": {
|
|
250
|
+
const surface = store.present(req.payload);
|
|
251
|
+
redraw();
|
|
252
|
+
return { id: req.id, ok: true, data: store.serializeSurface(surface) };
|
|
253
|
+
}
|
|
254
|
+
case "append_log": {
|
|
255
|
+
const updated = store.appendLog(req.surfaceId, req.lines);
|
|
256
|
+
if (!updated) {
|
|
257
|
+
return { id: req.id, ok: false, error: `Log surface "${req.surfaceId}" not found` };
|
|
258
|
+
}
|
|
259
|
+
redraw();
|
|
260
|
+
return { id: req.id, ok: true, data: { id: updated.id, lineCount: updated.lines.length } };
|
|
261
|
+
}
|
|
262
|
+
case "ask_wait": {
|
|
263
|
+
const surface = store.present(req.payload);
|
|
264
|
+
redraw();
|
|
265
|
+
const answer = await store.waitForAsk(surface.id, req.timeoutMs);
|
|
266
|
+
return { id: req.id, ok: true, data: { id: surface.id, answer } };
|
|
267
|
+
}
|
|
268
|
+
case "list":
|
|
269
|
+
return { id: req.id, ok: true, data: { session: store.session, surfaces: store.listSummaries() } };
|
|
270
|
+
case "get_state":
|
|
271
|
+
return { id: req.id, ok: true, data: store.serializeState(req.surfaceId) };
|
|
272
|
+
case "focus": {
|
|
273
|
+
const ok = store.focus(req.surfaceId);
|
|
274
|
+
redraw();
|
|
275
|
+
return ok
|
|
276
|
+
? { id: req.id, ok: true, data: { activeId: store.activeId } }
|
|
277
|
+
: { id: req.id, ok: false, error: `Surface "${req.surfaceId}" not found` };
|
|
278
|
+
}
|
|
279
|
+
case "close": {
|
|
280
|
+
const ok = store.close(req.surfaceId);
|
|
281
|
+
redraw();
|
|
282
|
+
return ok
|
|
283
|
+
? { id: req.id, ok: true, data: { activeId: store.activeId } }
|
|
284
|
+
: { id: req.id, ok: false, error: "Surface not found" };
|
|
285
|
+
}
|
|
286
|
+
case "snapshot": {
|
|
287
|
+
const driver = getAgentDriver();
|
|
288
|
+
if (!driver)
|
|
289
|
+
return { id: req.id, ok: false, error: "Agent driver not initialized" };
|
|
290
|
+
return { id: req.id, ok: true, data: driver.snapshot() };
|
|
291
|
+
}
|
|
292
|
+
case "act": {
|
|
293
|
+
const driver = getAgentDriver();
|
|
294
|
+
if (!driver)
|
|
295
|
+
return { id: req.id, ok: false, error: "Agent driver not initialized" };
|
|
296
|
+
const res = driver.act({ steps: req.steps });
|
|
297
|
+
redraw();
|
|
298
|
+
return { id: req.id, ok: res.ok, data: res, error: res.ok ? undefined : res.error };
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
async function pushDemoToSession(session, origin, reqId, autoDock) {
|
|
304
|
+
const tmp = new WorkspaceStore(session);
|
|
305
|
+
loadDemoSurfaces(tmp);
|
|
306
|
+
for (const s of tmp.surfaces) {
|
|
307
|
+
let payload;
|
|
308
|
+
if (s.kind === "markdown") {
|
|
309
|
+
payload = { id: s.id, kind: "markdown", title: s.title, content: s.content, origin, focus: s.id === "demo-plan" };
|
|
310
|
+
}
|
|
311
|
+
else if (s.kind === "image") {
|
|
312
|
+
payload = { id: s.id, kind: "image", title: s.title, path: s.path, caption: s.caption, origin, focus: false };
|
|
313
|
+
}
|
|
314
|
+
else if (s.kind === "table") {
|
|
315
|
+
payload = { id: s.id, kind: "table", title: s.title, columns: s.columns, rows: s.rows.map((r) => r.cells), origin, focus: false };
|
|
316
|
+
}
|
|
317
|
+
else if (s.kind === "chart") {
|
|
318
|
+
payload = { id: s.id, kind: "chart", title: s.title, series: s.series, unit: s.unit, caption: s.caption, origin, focus: false };
|
|
319
|
+
}
|
|
320
|
+
else if (s.kind === "ask") {
|
|
321
|
+
payload = {
|
|
322
|
+
id: s.id,
|
|
323
|
+
kind: "ask",
|
|
324
|
+
title: s.title,
|
|
325
|
+
mode: s.mode,
|
|
326
|
+
prompt: s.prompt,
|
|
327
|
+
context: s.context,
|
|
328
|
+
options: s.options,
|
|
329
|
+
defaultValue: s.defaultValue,
|
|
330
|
+
origin,
|
|
331
|
+
focus: false,
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
await sendIpc(session, { id: `${reqId}-${s.id}`, op: "present", payload }, { autoDock });
|
|
338
|
+
}
|
|
339
|
+
process.stdout.write(`Loaded demo surfaces into bypane session "${session}".\n`);
|
|
340
|
+
}
|
|
341
|
+
async function runTuiMode(session, loadDemo) {
|
|
342
|
+
const store = new WorkspaceStore(session);
|
|
343
|
+
if (loadDemo)
|
|
344
|
+
loadDemoSurfaces(store);
|
|
345
|
+
const sockPath = socketPathFor(session);
|
|
346
|
+
let app = null;
|
|
347
|
+
let driver = null;
|
|
348
|
+
const ui = createBypaneUi(store, sockPath, {
|
|
349
|
+
onQuit: () => app?.stop(),
|
|
350
|
+
});
|
|
351
|
+
app = runApp({
|
|
352
|
+
mouse: true,
|
|
353
|
+
theme: bypaneTheme,
|
|
354
|
+
view: ui.view,
|
|
355
|
+
keys: ui.keys,
|
|
356
|
+
});
|
|
357
|
+
driver = createAgent(app, {
|
|
358
|
+
allow: (step) => ui.allowAgentStep(step),
|
|
359
|
+
});
|
|
360
|
+
const unsub = store.subscribe(() => app?.redraw());
|
|
361
|
+
const toastTimer = setInterval(() => {
|
|
362
|
+
if (store.toast && store.toast.until <= Date.now()) {
|
|
363
|
+
store.toast = null;
|
|
364
|
+
app?.redraw();
|
|
365
|
+
}
|
|
366
|
+
}, 400);
|
|
367
|
+
const server = startIpcServer(session, createIpcHandler(store, () => driver, () => app?.redraw()));
|
|
368
|
+
const code = await app.done();
|
|
369
|
+
clearInterval(toastTimer);
|
|
370
|
+
unsub();
|
|
371
|
+
server.close();
|
|
372
|
+
process.exit(code);
|
|
373
|
+
}
|
|
374
|
+
async function main() {
|
|
375
|
+
const parsed = parseArgv(process.argv.slice(2));
|
|
376
|
+
if (parsed.flags.help || parsed.command === "help") {
|
|
377
|
+
process.stdout.write(HELP_TEXT);
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
const session = resolveSessionKey(strFlag(parsed.flags, "session"));
|
|
381
|
+
const autoDock = !parsed.flags["no-dock"];
|
|
382
|
+
const focus = !parsed.flags["no-focus"];
|
|
383
|
+
const origin = detectCallerOrigin("cli");
|
|
384
|
+
const reqId = `cli-${Date.now()}`;
|
|
385
|
+
if (parsed.command === "tui") {
|
|
386
|
+
await runTuiMode(session, Boolean(parsed.flags.demo));
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
if (parsed.command === "mcp") {
|
|
390
|
+
serveMcpStdio(session);
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
if (parsed.command === "setup") {
|
|
394
|
+
await runSetupWizard({
|
|
395
|
+
yes: Boolean(parsed.flags.yes),
|
|
396
|
+
skipInstall: Boolean(parsed.flags["no-install"]),
|
|
397
|
+
onLaunchDemo: async () => {
|
|
398
|
+
await pushDemoToSession(session, origin, reqId, autoDock);
|
|
399
|
+
},
|
|
400
|
+
});
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
if (parsed.command === "skill") {
|
|
404
|
+
const code = runSkillSubcommand(parsed.positionals[0], parsed.flags);
|
|
405
|
+
if (code !== 0)
|
|
406
|
+
process.exit(code);
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
if (parsed.command === "demo") {
|
|
410
|
+
await pushDemoToSession(session, origin, reqId, autoDock);
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
if (parsed.command === "show") {
|
|
414
|
+
const target = parsed.positionals[0];
|
|
415
|
+
if (!target) {
|
|
416
|
+
process.stderr.write("Usage: bypane show <file[:start[-end]]>\n");
|
|
417
|
+
process.exit(1);
|
|
418
|
+
}
|
|
419
|
+
const { path: rawPath, start, end } = parseFileTarget(target);
|
|
420
|
+
const absPath = resolve(rawPath);
|
|
421
|
+
const ext = extname(absPath).toLowerCase();
|
|
422
|
+
const id = strFlag(parsed.flags, "id");
|
|
423
|
+
const title = strFlag(parsed.flags, "title") || basename(absPath);
|
|
424
|
+
let payload;
|
|
425
|
+
if ((ext === ".md" || ext === ".markdown") && start === undefined && !parsed.flags.raw) {
|
|
426
|
+
const content = readFileSync(absPath, "utf8");
|
|
427
|
+
payload = { id, kind: "markdown", title, content, origin, focus };
|
|
428
|
+
}
|
|
429
|
+
else if ([".png", ".jpg", ".jpeg", ".webp", ".gif"].includes(ext)) {
|
|
430
|
+
payload = {
|
|
431
|
+
id,
|
|
432
|
+
kind: "image",
|
|
433
|
+
title,
|
|
434
|
+
path: absPath,
|
|
435
|
+
caption: strFlag(parsed.flags, "caption"),
|
|
436
|
+
origin,
|
|
437
|
+
focus,
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
else {
|
|
441
|
+
payload = {
|
|
442
|
+
id,
|
|
443
|
+
kind: "file",
|
|
444
|
+
title,
|
|
445
|
+
path: absPath,
|
|
446
|
+
highlightStart: start,
|
|
447
|
+
highlightEnd: end,
|
|
448
|
+
origin,
|
|
449
|
+
focus,
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
const res = await sendIpc(session, { id: reqId, op: "present", payload }, { autoDock });
|
|
453
|
+
if (!res.ok) {
|
|
454
|
+
process.stderr.write(`Error: ${res.error}\n`);
|
|
455
|
+
process.exit(1);
|
|
456
|
+
}
|
|
457
|
+
process.stdout.write(`${JSON.stringify(res.data)}\n`);
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
if (parsed.command === "md" || parsed.command === "markdown") {
|
|
461
|
+
const inline = parsed.positionals.join(" ");
|
|
462
|
+
const content = inline || (await readStdinAll());
|
|
463
|
+
if (!content) {
|
|
464
|
+
process.stderr.write("Usage: bypane md \"# Hello\" or cat plan.md | bypane md\n");
|
|
465
|
+
process.exit(1);
|
|
466
|
+
}
|
|
467
|
+
const res = await sendIpc(session, {
|
|
468
|
+
id: reqId,
|
|
469
|
+
op: "present",
|
|
470
|
+
payload: {
|
|
471
|
+
id: strFlag(parsed.flags, "id"),
|
|
472
|
+
kind: "markdown",
|
|
473
|
+
title: strFlag(parsed.flags, "title") || "Document",
|
|
474
|
+
content,
|
|
475
|
+
origin,
|
|
476
|
+
focus,
|
|
477
|
+
},
|
|
478
|
+
}, { autoDock });
|
|
479
|
+
if (!res.ok) {
|
|
480
|
+
process.stderr.write(`Error: ${res.error}\n`);
|
|
481
|
+
process.exit(1);
|
|
482
|
+
}
|
|
483
|
+
process.stdout.write(`${JSON.stringify(res.data)}\n`);
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
if (parsed.command === "table") {
|
|
487
|
+
const file = parsed.positionals[0];
|
|
488
|
+
const raw = file ? readFileSync(resolve(file), "utf8") : await readStdinAll();
|
|
489
|
+
const { columns, rows } = parseTableInput(raw);
|
|
490
|
+
const res = await sendIpc(session, {
|
|
491
|
+
id: reqId,
|
|
492
|
+
op: "present",
|
|
493
|
+
payload: {
|
|
494
|
+
id: strFlag(parsed.flags, "id"),
|
|
495
|
+
kind: "table",
|
|
496
|
+
title: strFlag(parsed.flags, "title") || (file ? basename(file) : "Table"),
|
|
497
|
+
columns,
|
|
498
|
+
rows,
|
|
499
|
+
origin,
|
|
500
|
+
focus,
|
|
501
|
+
},
|
|
502
|
+
}, { autoDock });
|
|
503
|
+
if (!res.ok) {
|
|
504
|
+
process.stderr.write(`Error: ${res.error}\n`);
|
|
505
|
+
process.exit(1);
|
|
506
|
+
}
|
|
507
|
+
process.stdout.write(`${JSON.stringify(res.data)}\n`);
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
if (parsed.command === "chart") {
|
|
511
|
+
const seriesFlags = listFlag(parsed.flags, "series");
|
|
512
|
+
let series = [];
|
|
513
|
+
if (seriesFlags.length > 0) {
|
|
514
|
+
series = seriesFlags.map((spec, idx) => {
|
|
515
|
+
const colon = spec.indexOf(":");
|
|
516
|
+
const name = colon >= 0 ? spec.slice(0, colon) : `series-${idx + 1}`;
|
|
517
|
+
const nums = (colon >= 0 ? spec.slice(colon + 1) : spec)
|
|
518
|
+
.split(",")
|
|
519
|
+
.map((s) => Number(s.trim()))
|
|
520
|
+
.filter((n) => Number.isFinite(n));
|
|
521
|
+
return { name, values: nums };
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
else {
|
|
525
|
+
const raw = (await readStdinAll()).trim();
|
|
526
|
+
if (raw) {
|
|
527
|
+
const parsedJson = JSON.parse(raw);
|
|
528
|
+
if (Array.isArray(parsedJson) && typeof parsedJson[0] === "number") {
|
|
529
|
+
series = [{ name: "value", values: parsedJson }];
|
|
530
|
+
}
|
|
531
|
+
else if (Array.isArray(parsedJson)) {
|
|
532
|
+
series = parsedJson;
|
|
533
|
+
}
|
|
534
|
+
else if (parsedJson && typeof parsedJson === "object" && "series" in parsedJson) {
|
|
535
|
+
series = parsedJson.series;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
const res = await sendIpc(session, {
|
|
540
|
+
id: reqId,
|
|
541
|
+
op: "present",
|
|
542
|
+
payload: {
|
|
543
|
+
id: strFlag(parsed.flags, "id"),
|
|
544
|
+
kind: "chart",
|
|
545
|
+
title: strFlag(parsed.flags, "title") || "Chart",
|
|
546
|
+
series,
|
|
547
|
+
unit: strFlag(parsed.flags, "unit"),
|
|
548
|
+
caption: strFlag(parsed.flags, "caption"),
|
|
549
|
+
origin,
|
|
550
|
+
focus,
|
|
551
|
+
},
|
|
552
|
+
}, { autoDock });
|
|
553
|
+
if (!res.ok) {
|
|
554
|
+
process.stderr.write(`Error: ${res.error}\n`);
|
|
555
|
+
process.exit(1);
|
|
556
|
+
}
|
|
557
|
+
process.stdout.write(`${JSON.stringify(res.data)}\n`);
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
if (parsed.command === "log") {
|
|
561
|
+
const raw = await readStdinAll();
|
|
562
|
+
const lines = raw ? raw.split(/\r?\n/) : parsed.positionals;
|
|
563
|
+
if (lines.length > 0 && lines[lines.length - 1] === "")
|
|
564
|
+
lines.pop();
|
|
565
|
+
const res = await sendIpc(session, {
|
|
566
|
+
id: reqId,
|
|
567
|
+
op: "present",
|
|
568
|
+
payload: {
|
|
569
|
+
id: strFlag(parsed.flags, "id"),
|
|
570
|
+
kind: "log",
|
|
571
|
+
title: strFlag(parsed.flags, "title") || "Log",
|
|
572
|
+
lines,
|
|
573
|
+
append: Boolean(parsed.flags.append),
|
|
574
|
+
origin,
|
|
575
|
+
focus,
|
|
576
|
+
},
|
|
577
|
+
}, { autoDock });
|
|
578
|
+
if (!res.ok) {
|
|
579
|
+
process.stderr.write(`Error: ${res.error}\n`);
|
|
580
|
+
process.exit(1);
|
|
581
|
+
}
|
|
582
|
+
process.stdout.write(`${JSON.stringify(res.data)}\n`);
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
if (parsed.command === "ask") {
|
|
586
|
+
const prompt = strFlag(parsed.flags, "prompt") || parsed.positionals.join(" ");
|
|
587
|
+
if (!prompt) {
|
|
588
|
+
process.stderr.write("Usage: bypane ask --prompt \"Question\" [--mode select|multiselect|text|confirm] [--option a --option b]\n");
|
|
589
|
+
process.exit(1);
|
|
590
|
+
}
|
|
591
|
+
const options = listFlag(parsed.flags, "option");
|
|
592
|
+
const mode = strFlag(parsed.flags, "mode") || (options.length > 0 ? "select" : "confirm");
|
|
593
|
+
const payload = {
|
|
594
|
+
id: strFlag(parsed.flags, "id"),
|
|
595
|
+
kind: "ask",
|
|
596
|
+
title: strFlag(parsed.flags, "title") || "Decision",
|
|
597
|
+
mode,
|
|
598
|
+
prompt,
|
|
599
|
+
context: strFlag(parsed.flags, "context"),
|
|
600
|
+
options: options.length > 0 ? options : undefined,
|
|
601
|
+
defaultValue: strFlag(parsed.flags, "default"),
|
|
602
|
+
placeholder: strFlag(parsed.flags, "placeholder"),
|
|
603
|
+
origin,
|
|
604
|
+
focus: true,
|
|
605
|
+
};
|
|
606
|
+
const wait = !parsed.flags["no-wait"];
|
|
607
|
+
const res = await sendIpc(session, wait ? { id: reqId, op: "ask_wait", payload } : { id: reqId, op: "present", payload }, { autoDock });
|
|
608
|
+
if (!res.ok) {
|
|
609
|
+
process.stderr.write(`Error: ${res.error}\n`);
|
|
610
|
+
process.exit(1);
|
|
611
|
+
}
|
|
612
|
+
process.stdout.write(`${JSON.stringify(res.data)}\n`);
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
615
|
+
if (parsed.command === "list") {
|
|
616
|
+
const res = await sendIpc(session, { id: reqId, op: "list" }, { autoDock: false });
|
|
617
|
+
if (!res.ok) {
|
|
618
|
+
process.stderr.write(`Error: ${res.error}\n`);
|
|
619
|
+
process.exit(1);
|
|
620
|
+
}
|
|
621
|
+
process.stdout.write(`${JSON.stringify(res.data, null, 2)}\n`);
|
|
622
|
+
return;
|
|
623
|
+
}
|
|
624
|
+
if (parsed.command === "state") {
|
|
625
|
+
const res = await sendIpc(session, { id: reqId, op: "get_state", surfaceId: parsed.positionals[0] }, { autoDock: false });
|
|
626
|
+
if (!res.ok) {
|
|
627
|
+
process.stderr.write(`Error: ${res.error}\n`);
|
|
628
|
+
process.exit(1);
|
|
629
|
+
}
|
|
630
|
+
process.stdout.write(`${JSON.stringify(res.data, null, 2)}\n`);
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
if (parsed.command === "close") {
|
|
634
|
+
const res = await sendIpc(session, { id: reqId, op: "close", surfaceId: parsed.positionals[0] }, { autoDock: false });
|
|
635
|
+
if (!res.ok) {
|
|
636
|
+
process.stderr.write(`Error: ${res.error}\n`);
|
|
637
|
+
process.exit(1);
|
|
638
|
+
}
|
|
639
|
+
process.stdout.write(`${JSON.stringify(res.data)}\n`);
|
|
640
|
+
return;
|
|
641
|
+
}
|
|
642
|
+
if (parsed.command === "snapshot") {
|
|
643
|
+
const res = await sendIpc(session, { id: reqId, op: "snapshot" }, { autoDock: false });
|
|
644
|
+
if (!res.ok) {
|
|
645
|
+
process.stderr.write(`Error: ${res.error}\n`);
|
|
646
|
+
process.exit(1);
|
|
647
|
+
}
|
|
648
|
+
process.stdout.write(`${JSON.stringify(res.data, null, 2)}\n`);
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
// Only run CLI when executed directly (allows importing helpers in tests cleanly).
|
|
653
|
+
function isDirectEntry() {
|
|
654
|
+
if (process.env.NODE_TEST_CONTEXT)
|
|
655
|
+
return false;
|
|
656
|
+
const arg1 = process.argv[1];
|
|
657
|
+
if (!arg1)
|
|
658
|
+
return true;
|
|
659
|
+
try {
|
|
660
|
+
return realpathSync(arg1) === realpathSync(fileURLToPath(import.meta.url));
|
|
661
|
+
}
|
|
662
|
+
catch {
|
|
663
|
+
return (arg1.endsWith("/cli.ts") ||
|
|
664
|
+
arg1.endsWith("/cli.js") ||
|
|
665
|
+
arg1.endsWith("\\cli.ts") ||
|
|
666
|
+
arg1.endsWith("\\cli.js") ||
|
|
667
|
+
arg1.endsWith("/bypane") ||
|
|
668
|
+
arg1 === "bypane");
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
if (isDirectEntry()) {
|
|
672
|
+
main().catch((err) => {
|
|
673
|
+
process.stderr.write(`bypane: ${err.message}\n`);
|
|
674
|
+
process.exit(1);
|
|
675
|
+
});
|
|
676
|
+
}
|