@bivy/bivy 0.17.3-staging.3 → 0.17.3-staging.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/bin/bivy.mjs +137 -9
  2. package/dist/agents/claude-code/runtime.js +17 -0
  3. package/dist/apps/annotate.js +94 -0
  4. package/dist/apps/display-viewer.js +84 -0
  5. package/dist/apps/display.js +145 -0
  6. package/dist/apps/gateway.js +356 -26
  7. package/dist/apps/input.js +96 -0
  8. package/dist/apps/inspector.js +167 -0
  9. package/dist/apps/macos-display.js +169 -0
  10. package/dist/apps/macos-display.swift +726 -0
  11. package/dist/apps/preview-shell.js +402 -37
  12. package/dist/apps/preview-stream.js +17 -3
  13. package/dist/apps/registry.js +92 -18
  14. package/dist/apps/remote-preview.js +10 -2
  15. package/dist/apps/review.js +109 -0
  16. package/dist/apps/rfb.js +175 -0
  17. package/dist/apps/screenshot.js +200 -0
  18. package/dist/apps/service.js +497 -9
  19. package/dist/apps/styles.css +166 -9
  20. package/dist/apps/tokens.css +6 -0
  21. package/dist/apps/types.js +16 -0
  22. package/dist/apps/x11.js +273 -0
  23. package/dist/attach.js +37 -4
  24. package/dist/config-cli.js +2 -0
  25. package/dist/controllers/app-commands.js +16 -4
  26. package/dist/controllers/artifact-commands.js +42 -0
  27. package/dist/controllers/presence-commands.js +19 -0
  28. package/dist/node-config.js +6 -3
  29. package/dist/policy/session-limit.js +63 -0
  30. package/dist/protocol/client-command-routes.js +3 -1
  31. package/dist/protocol/client-command-schemas.js +20 -3
  32. package/dist/server.js +246 -23
  33. package/dist/session/attachment-store.js +15 -0
  34. package/dist/session/event-log.js +25 -3
  35. package/dist/session/presence.js +82 -0
  36. package/dist/session/run-terminal.js +114 -70
  37. package/dist/session/session-new-dedupe.js +1 -1
  38. package/package.json +2 -1
package/bin/bivy.mjs CHANGED
@@ -974,7 +974,7 @@ function looksLikeRemote(value) {
974
974
 
975
975
  function extractRunFlags(args) {
976
976
  const rest = [];
977
- let name, model, node, workspace;
977
+ let name, model, node, workspace, bivySession;
978
978
  let chat = false;
979
979
  let noOpen = false;
980
980
  let noFollow = false;
@@ -993,11 +993,14 @@ function extractRunFlags(args) {
993
993
  if (a.startsWith("--node=")) { node = a.slice("--node=".length); continue; }
994
994
  if (a === "--workspace" && args[i + 1] !== undefined) { workspace = args[++i]; continue; }
995
995
  if (a.startsWith("--workspace=")) { workspace = a.slice("--workspace=".length); continue; }
996
+ // Internal (set by `bivy resume`): the Bivy session this run continues, so
997
+ // the node can lock that session's chat while the terminal drives it.
998
+ if (a === "--bivy-session" && args[i + 1] !== undefined) { bivySession = args[++i]; continue; }
996
999
  if (a.startsWith("--clone=")) { clone = a.slice("--clone=".length); continue; }
997
1000
  if (a === "--clone") { clone = looksLikeRemote(args[i + 1]) ? args[++i] : true; continue; }
998
1001
  rest.push(a);
999
1002
  }
1000
- return { name: name?.trim() || undefined, model: model?.trim() || undefined, node: node?.trim() || undefined, workspace: workspace?.trim() || undefined, chat, noOpen, noFollow, clone, rest };
1003
+ return { name: name?.trim() || undefined, model: model?.trim() || undefined, node: node?.trim() || undefined, workspace: workspace?.trim() || undefined, bivySession: bivySession?.trim() || undefined, chat, noOpen, noFollow, clone, rest };
1001
1004
  }
1002
1005
 
1003
1006
  // A safe-ish workspace dir name from a remote or path (basename minus .git).
@@ -1899,7 +1902,7 @@ function governedChatAgentId(id) {
1899
1902
 
1900
1903
  async function cmdRun(args = []) {
1901
1904
  if (!(await ensureDeps())) process.exit(1);
1902
- const { name, model, node, workspace, chat, noOpen, noFollow, clone, rest } = extractRunFlags(args);
1905
+ const { name, model, node, workspace, bivySession, chat, noOpen, noFollow, clone, rest } = extractRunFlags(args);
1903
1906
  // Bare `bivy` (empty rest) resolves to the configured default agent; an
1904
1907
  // explicit `bivy run <agent>` keeps that agent verbatim.
1905
1908
  const [agentIdArg, ...extraArgs] = rest;
@@ -1999,7 +2002,8 @@ async function cmdRun(args = []) {
1999
2002
  // Pin a session id at launch when the agent's CLI supports it (and the caller
2000
2003
  // didn't already choose one), so the on-disk session is a known, deterministic
2001
2004
  // resume target — the anchor for later "continue as chat" adoption.
2002
- const pinnedSessionId = pinRunSessionId(agentId, resolved.spec);
2005
+ const pinnedSessionId = bivySession ? undefined : pinRunSessionId(agentId, resolved.spec);
2006
+ if (bivySession) resolved.spec.sessionId = bivySession;
2003
2007
  if (pinnedSessionId) {
2004
2008
  console.log(c.dim(`session id ${pinnedSessionId} — resume in a terminal with '${agentId} --resume ${pinnedSessionId}'`));
2005
2009
  }
@@ -2426,6 +2430,16 @@ async function cmdApp(args = []) {
2426
2430
  console.log(`Usage: bivy app publish <manifest.json> [--session <id>]
2427
2431
  bivy app list [--session <id>]
2428
2432
  bivy app remove <app-id> [--session <id>]
2433
+ bivy app shot [app-id] [--widths 390,1280] [--themes light,dark] [--path /page] [--session <id>]
2434
+ bivy app present [view] [--path /page] [--note "…"] [--session <id>]
2435
+ bivy app share [app-id] [--view <view>] [--session <id>]
2436
+ bivy app run [--name <name>] [--restart-on-change] [--session <id>] -- <command> [args…]
2437
+ bivy app click <x> <y> [--right|--middle] [--double] [--app <app>]
2438
+ bivy app type <text> [--app <app>]
2439
+ bivy app key <keys> [--app <app>] e.g. enter, cmd+s, ctrl+shift+t
2440
+ bivy app scroll <x> <y> [--up|--down|--left|--right] [--steps <n>] [--app <app>]
2441
+ bivy app drag <x> <y> <to-x> <to-y> [--app <app>]
2442
+ bivy app move <x> <y> [--app <app>]
2429
2443
 
2430
2444
  Manifest: {"version":1,"name":"My app","views":[
2431
2445
  {"kind":"web","name":"Website","source":{"kind":"service","port":3000}},
@@ -2433,19 +2447,89 @@ Manifest: {"version":1,"name":"My app","views":[
2433
2447
  ]}
2434
2448
 
2435
2449
  Static web source: {"kind":"static","directory":"./dist"}
2436
- Publishing does not start commands. Open Apps in the session menu to view or run.
2450
+ Server Bivy runs: {"kind":"service","port":5173,"start":{"command":"pnpm","args":["dev"]}}
2451
+ Desktop GUI app (Linux with TigerVNC's Xvnc, or macOS 13+):
2452
+ {"kind":"display","name":"Editor","command":"cargo","args":["run"],"restartOnChange":true}
2453
+ It runs on a private display (Linux) or shows its own windows (macOS),
2454
+ streamed into the preview and sized to the viewer.
2455
+ "restartOnChange" restarts it after a turn that changed files. Shortcut with
2456
+ no manifest: bivy app run -- cargo run
2457
+ Publishing does not start commands. Open Apps in the session menu to view or run;
2458
+ a server with "start" or a desktop app starts on first open and is restarted if
2459
+ it exits.
2437
2460
  Terminal commands run with the node user's permissions, not in a new sandbox.
2461
+ "shot" screenshots the session's web views (or one app) with a local headless
2462
+ Chrome/Chromium (desktop apps: straight from their display) and prints the PNG paths — look at them before saying the UI is
2463
+ done. It is off by default: turn it on in Bivy → Settings → this machine, or
2464
+ run: bivy config set sessions.appScreenshots true
2465
+ click/type/key/scroll/drag/move use a desktop app like a person would, in the
2466
+ pixels of its screenshot (bivy app shot), and print a new screenshot after
2467
+ (when screenshots are on) — look at it to see what happened. --app picks the
2468
+ app (default: the one opened last). Keys: a character or enter, tab, escape,
2469
+ backspace, delete, arrows (up…), home, end, pageup, pagedown, f1–f12, with
2470
+ modifiers shift, ctrl, alt/option, cmd. On a Mac, the pointer and keyboard
2471
+ focus move to the app while it's used.
2472
+ "present" says a change is ready for the user to look at: they get a card in
2473
+ the chat with the app at phone width (and before/after), and it opens the live
2474
+ preview. Use it when you finish a visible change, or when the user asks to see
2475
+ it. [view] is an app or view name or ID (default: the one opened last).
2476
+ "share" mints a reusable link to one web view and prints its URL and expiry
2477
+ (24 hours, or until Revoke access in Apps, the app is removed or the machine
2478
+ restarts). [app-id] is an app ID or name, --view a view ID or name (default: the
2479
+ one opened last). Anyone who has the link can use the app, including a live
2480
+ server's backend, until it expires or is revoked: only post it where everyone
2481
+ who can read it may use the app.
2438
2482
  Web previews require operator setup; see docs/apps.md.`);
2439
2483
  return;
2440
2484
  }
2441
- if (!["publish", "list", "remove"].includes(action)) throw new Error("Unknown app command. Run bivy app --help.");
2485
+ if (INPUT_ACTIONS[action]) return appInput(action, rest);
2486
+ if (!["publish", "list", "remove", "shot", "run", "present", "share"].includes(action)) throw new Error("Unknown app command. Run bivy app --help.");
2487
+ // `run -- <command> [args…]`: publish a one-window desktop app without a manifest file.
2488
+ let runManifest;
2489
+ if (action === "run") {
2490
+ const split = rest.indexOf("--");
2491
+ if (split < 0 || !rest[split + 1]) throw new Error("Usage: bivy app run [--name <name>] [--restart-on-change] -- <command> [args…]");
2492
+ const [command, ...commandArgs] = rest.splice(split).slice(1);
2493
+ const nameIndex = rest.indexOf("--name");
2494
+ const name = nameIndex >= 0 ? rest.splice(nameIndex, 2)[1] : path.basename(command);
2495
+ if (!name) throw new Error("--name requires a value.");
2496
+ const restartIndex = rest.indexOf("--restart-on-change");
2497
+ if (restartIndex >= 0) rest.splice(restartIndex, 1);
2498
+ runManifest = { version: 1, name: name.slice(0, 100), views: [{ kind: "display", name: "Window", command, args: commandArgs, ...(restartIndex >= 0 ? { restartOnChange: true } : {}) }] };
2499
+ }
2500
+ const shot = {};
2501
+ if (action === "share") {
2502
+ const i = rest.indexOf("--view");
2503
+ if (i >= 0) {
2504
+ const value = rest[i + 1];
2505
+ if (!value || value.startsWith("--")) throw new Error("--view requires a view ID or name.");
2506
+ shot.view = value;
2507
+ rest.splice(i, 2);
2508
+ }
2509
+ }
2510
+ if (action === "shot" || action === "present") {
2511
+ for (const flag of action === "shot" ? ["--widths", "--themes", "--path"] : ["--path", "--note"]) {
2512
+ const i = rest.indexOf(flag);
2513
+ if (i < 0) continue;
2514
+ const value = rest[i + 1];
2515
+ if (!value || value.startsWith("--")) throw new Error(`${flag} requires a value.`);
2516
+ if (flag === "--widths") shot.widths = value.split(",").map((w) => Number(w.trim()));
2517
+ else if (flag === "--themes") shot.themes = value.split(",").map((t) => t.trim());
2518
+ else if (flag === "--note") shot.note = value;
2519
+ else shot.path = value;
2520
+ rest.splice(i, 2);
2521
+ }
2522
+ }
2442
2523
  const sessionIndex = rest.indexOf("--session");
2443
2524
  if (sessionIndex >= 0 && (!rest[sessionIndex + 1] || rest[sessionIndex + 1].startsWith("--"))) throw new Error("--session requires an id.");
2444
2525
  const sessionId = resolveAttachSessionId({ sessionFlag: sessionIndex >= 0 ? rest[sessionIndex + 1] : undefined, env: process.env });
2445
2526
  if (!sessionId) throw new Error("Set --session <id> or run inside a Bivy agent session.");
2446
2527
  const positional = rest.filter((_, i) => i !== sessionIndex && (sessionIndex < 0 || i !== sessionIndex + 1));
2447
- if (positional.some((a) => a.startsWith("-")) || positional.length !== (action === "list" ? 0 : 1)) throw new Error("Invalid arguments. Run bivy app --help.");
2448
- const body = { sessionId };
2528
+ if (positional.some((a) => a.startsWith("-")) || (action === "shot" || action === "present" || action === "share" ? positional.length > 1 : positional.length !== (action === "list" || action === "run" ? 0 : 1))) throw new Error("Invalid arguments. Run bivy app --help.");
2529
+ const body = { sessionId, ...shot };
2530
+ if (action === "shot" && positional[0]) body.appId = positional[0];
2531
+ if (action === "present" && positional[0]) body.target = positional[0];
2532
+ if (action === "share" && positional[0]) body.appId = positional[0];
2449
2533
  if (action === "publish") {
2450
2534
  const file = path.resolve(positional[0]);
2451
2535
  if (fs.statSync(file).size > 64 * 1024) throw new Error("App manifest exceeds 64 KiB.");
@@ -2457,7 +2541,12 @@ Web previews require operator setup; see docs/apps.md.`);
2457
2541
  }
2458
2542
  }
2459
2543
  body.manifest = manifest;
2460
- } else if (action === "remove") body.appId = positional[0];
2544
+ } else if (action === "run") body.manifest = runManifest;
2545
+ else if (action === "remove") body.appId = positional[0];
2546
+ await appRequest(action === "run" ? "publish" : action, body);
2547
+ }
2548
+
2549
+ async function appRequest(action, body) {
2461
2550
  const config = loadConfig();
2462
2551
  if (!(await ensureNodeRunning(config))) throw new Error("Could not reach the Bivy node.");
2463
2552
  const token = await localDeviceToken(config);
@@ -2466,9 +2555,47 @@ Web previews require operator setup; see docs/apps.md.`);
2466
2555
  });
2467
2556
  const result = await response.json();
2468
2557
  if (!response.ok || result.error) throw new Error(result.error || `App command failed (${response.status}).`);
2558
+ if (action === "share") {
2559
+ const { type: _type, requestId: _requestId, ...link } = result;
2560
+ console.log(JSON.stringify({ ...link, expires: new Date(link.expiresAt).toISOString() }, null, 2));
2561
+ console.error("Anyone with this link can use the app, including its live backend, until it expires or you revoke it (Apps → Revoke access).");
2562
+ return;
2563
+ }
2469
2564
  console.log(JSON.stringify(result, null, 2));
2470
2565
  }
2471
2566
 
2567
+ // Computer use in desktop apps: each command's positional arguments, and the
2568
+ // flags that shape its action. The node validates the action.
2569
+ const INPUT_ACTIONS = {
2570
+ click: { args: ["x", "y"], flags: { "--right": { button: "right" }, "--middle": { button: "middle" }, "--double": { count: 2 } } },
2571
+ move: { args: ["x", "y"], flags: {} },
2572
+ drag: { args: ["x", "y", "toX", "toY"], flags: {} },
2573
+ scroll: { args: ["x", "y"], flags: { "--up": { direction: "up" }, "--down": { direction: "down" }, "--left": { direction: "left" }, "--right": { direction: "right" } }, defaults: { direction: "down" }, values: { "--steps": "steps" } },
2574
+ type: { args: ["text"], flags: {}, text: true },
2575
+ key: { args: ["keys"], flags: {}, text: true },
2576
+ };
2577
+ async function appInput(kind, rest) {
2578
+ const spec = INPUT_ACTIONS[kind];
2579
+ const action = { kind, ...spec.defaults };
2580
+ const options = {};
2581
+ const positional = [];
2582
+ for (let i = 0; i < rest.length; i++) {
2583
+ const arg = rest[i];
2584
+ if (spec.flags[arg]) Object.assign(action, spec.flags[arg]);
2585
+ else if (arg === "--app" || arg === "--session" || spec.values?.[arg]) {
2586
+ const value = rest[++i];
2587
+ if (value === undefined || value.startsWith("--")) throw new Error(`${arg} requires a value.`);
2588
+ if (spec.values?.[arg]) action[spec.values[arg]] = Number(value); else options[arg] = value;
2589
+ } else if (arg.startsWith("--")) throw new Error(`Unknown option ${arg}. Run bivy app --help.`);
2590
+ else positional.push(arg);
2591
+ }
2592
+ if (positional.length !== spec.args.length) throw new Error(`Usage: bivy app ${kind} ${spec.args.map((name) => `<${name}>`).join(" ")}. Run bivy app --help.`);
2593
+ spec.args.forEach((name, i) => { action[name] = spec.text ? positional[i] : Number(positional[i]); });
2594
+ const sessionId = resolveAttachSessionId({ sessionFlag: options["--session"], env: process.env });
2595
+ if (!sessionId) throw new Error("Set --session <id> or run inside a Bivy agent session.");
2596
+ await appRequest("input", { sessionId, action, ...(options["--app"] ? { target: options["--app"] } : {}) });
2597
+ }
2598
+
2472
2599
  // Map a saved session's runtime id to the `bivy run` agent whose native CLI can
2473
2600
  // resume it in a terminal. Only agents with a real native resume qualify; other
2474
2601
  // runtimes (generic-cli, SDK-only) have no terminal resume and open in the web app.
@@ -2507,6 +2634,7 @@ async function resumeSessionItem(item, config, token) {
2507
2634
  const resumeArgs = agentResumeArgs(agentId, resumeRef);
2508
2635
  const runArgs = [agentId, ...resumeArgs];
2509
2636
  if (item.workspace) runArgs.push("--workspace", item.workspace); // native resume finds the session by its original cwd
2637
+ if (item.id) runArgs.push("--bivy-session", item.id);
2510
2638
  console.log(c.dim(`Resuming ${c.cyan(item.name)} with ${agentId} ${resumeArgs.join(" ")}…`));
2511
2639
  await cmdRun(runArgs);
2512
2640
  }
@@ -532,6 +532,9 @@ function loadClaudeTranscript(sessionId) {
532
532
  // re-drive, or a session the user resumed), so it's noise — we keep only a
533
533
  // trailing one, where the session actually ended interrupted. See below.
534
534
  const restartNoticeIdx = [];
535
+ // The latest user prompt, and the index of an API-error reply just kept.
536
+ let lastPrompt;
537
+ let lastErrorIdx = -1;
535
538
  try {
536
539
  for (const line of fs.readFileSync(file, "utf8").split(/\r?\n/)) {
537
540
  if (!line.trim())
@@ -567,7 +570,21 @@ function loadClaudeTranscript(sessionId) {
567
570
  continue;
568
571
  if (typeof content !== "string" && !Array.isArray(content))
569
572
  continue;
573
+ // A re-driven prompt: an API-error reply (e.g. a 401 before a credential
574
+ // refresh) followed — past any restart notice — by the same prompt again.
575
+ // The turn was recovered, so drop both; otherwise history shows the error and
576
+ // repeats the prompt at the bottom of the chat.
577
+ if (role === "user" && lastErrorIdx >= 0 && JSON.stringify(content) === JSON.stringify(lastPrompt)) {
578
+ messages.splice(lastErrorIdx, 1);
579
+ restartNoticeIdx.forEach((idx, i) => { if (idx > lastErrorIdx)
580
+ restartNoticeIdx[i] = idx - 1; });
581
+ lastErrorIdx = -1;
582
+ continue;
583
+ }
584
+ if (role === "user" && !(Array.isArray(content) && content.some((block) => block?.type === "tool_result")))
585
+ lastPrompt = content;
570
586
  messages.push({ role, content, timestamp: new Date(entry?.timestamp ?? entry?.createdAt ?? Date.now()).getTime() });
587
+ lastErrorIdx = entry?.isApiErrorMessage === true ? messages.length - 1 : -1;
571
588
  }
572
589
  }
573
590
  catch {
@@ -0,0 +1,94 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-only
2
+ // Copyright (c) 2026 Petter André Sjulstad
3
+ import { readFileSync } from "node:fs";
4
+ import { decodePng } from "./review.js";
5
+ import { encodePng } from "./rfb.js";
6
+ const MAX_STROKES = 50;
7
+ const MAX_POINTS = 10_000;
8
+ /** Validates strokes from the client (untrusted shape): bounded, finite. */
9
+ export function readStrokes(input) {
10
+ if (!Array.isArray(input) || !input.length || input.length > MAX_STROKES)
11
+ throw new Error("Draw at least one mark (at most 50).");
12
+ let total = 0;
13
+ return input.map((raw) => {
14
+ const stroke = raw;
15
+ if ((stroke?.tool !== "pen" && stroke?.tool !== "box") || !Array.isArray(stroke.points) || !stroke.points.length)
16
+ throw new Error("Invalid mark.");
17
+ total += stroke.points.length;
18
+ if (total > MAX_POINTS)
19
+ throw new Error("Too many points in the marks.");
20
+ const points = stroke.points.map((p) => {
21
+ if (!Array.isArray(p) || p.length !== 2 || !p.every((n) => typeof n === "number" && Number.isFinite(n) && Math.abs(n) < 1e6))
22
+ throw new Error("Invalid mark point.");
23
+ return [p[0], p[1]];
24
+ });
25
+ return { tool: stroke.tool, points };
26
+ });
27
+ }
28
+ /** Whether a picture retaken on the machine may not match what the user saw:
29
+ * the page kept state a fresh browser doesn't have (storage, cookies, typed
30
+ * input, an open menu or dialog, anything the user did since it loaded), or
31
+ * the retake couldn't scroll to where they were. A Compare screenshot or a
32
+ * desktop app's frame is the exact picture, so never approximate. */
33
+ export function approximate(source, signals = {}, scrolledTo = true) {
34
+ if (source === "exact")
35
+ return false;
36
+ return !scrolledTo || Object.values(signals).some(Boolean);
37
+ }
38
+ /** The annotation colours, read from the design tokens (single source). */
39
+ function tokenColor(name, fallback) {
40
+ let hex = fallback;
41
+ try {
42
+ hex = new RegExp(`--${name}:\\s*(#[0-9a-fA-F]{6})`).exec(readFileSync(new URL("./tokens.css", import.meta.url), "utf8"))?.[1] ?? fallback;
43
+ }
44
+ catch { /* fallback */ }
45
+ return [1, 3, 5].map((i) => parseInt(hex.slice(i, i + 2), 16));
46
+ }
47
+ const INK = tokenColor("annotate", "#ff2d78");
48
+ const HALO = tokenColor("annotate-halo", "#ffffff");
49
+ /** Mark width in CSS pixels, and its halo on each side. */
50
+ const WIDTH = 4;
51
+ const HALO_WIDTH = 1.5;
52
+ /** Draws marks onto a PNG. `scale` maps CSS pixels to image pixels;
53
+ * `offset` (CSS px) is subtracted first — the scroll position the picture
54
+ * was taken at. Halos go under every mark first, so crossings stay clean. */
55
+ export function composite(png, strokes, map) {
56
+ const image = decodePng(png);
57
+ const { width, height, channels } = image;
58
+ const rgb = Buffer.alloc(width * height * 3);
59
+ for (let p = 0; p < width * height; p++)
60
+ image.pixels.copy(rgb, p * 3, p * channels, p * channels + 3);
61
+ const off = map.offset ?? { x: 0, y: 0 };
62
+ const toImage = ([x, y]) => [(x - off.x) * map.scale, (y - off.y) * map.scale];
63
+ const paths = strokes.map((stroke) => {
64
+ const points = stroke.points.map(toImage);
65
+ if (stroke.tool === "pen")
66
+ return points;
67
+ const [a, b] = [points[0], points[points.length - 1]];
68
+ return [a, [b[0], a[1]], b, [a[0], b[1]], a];
69
+ });
70
+ const disc = (cx, cy, r, color) => {
71
+ for (let y = Math.max(0, Math.floor(cy - r)); y <= Math.min(height - 1, Math.ceil(cy + r)); y++) {
72
+ for (let x = Math.max(0, Math.floor(cx - r)); x <= Math.min(width - 1, Math.ceil(cx + r)); x++) {
73
+ if ((x - cx) ** 2 + (y - cy) ** 2 <= r * r)
74
+ rgb.set(color, (y * width + x) * 3);
75
+ }
76
+ }
77
+ };
78
+ const trace = (path, r, color) => {
79
+ const step = Math.max(0.5, r / 2);
80
+ for (let i = 0; i < path.length; i++) {
81
+ const [x0, y0] = path[i];
82
+ const [x1, y1] = path[i + 1] ?? path[i];
83
+ const n = Math.max(1, Math.ceil(Math.hypot(x1 - x0, y1 - y0) / step));
84
+ for (let k = 0; k <= n; k++)
85
+ disc(x0 + ((x1 - x0) * k) / n, y0 + ((y1 - y0) * k) / n, r, color);
86
+ }
87
+ };
88
+ const r = (WIDTH / 2) * map.scale;
89
+ for (const path of paths)
90
+ trace(path, r + HALO_WIDTH * map.scale, HALO);
91
+ for (const path of paths)
92
+ trace(path, r, INK);
93
+ return encodePng(width, height, rgb);
94
+ }
@@ -0,0 +1,84 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-only
2
+ // Copyright (c) 2026 Petter André Sjulstad
3
+ export const DISPLAY_SOCKET_PATH = "/__bivy/display";
4
+ export const DISPLAY_STATS_PATH = "/__bivy/display-stats";
5
+ export const NOVNC_PATH = "/__bivy/novnc/";
6
+ /** The modifier apps paste with, as a keysym and noVNC key code: ⌘ on a Mac. */
7
+ const PASTE_MODIFIER = { darwin: [0xffeb, "MetaLeft"] };
8
+ /** The page a display view serves on its own origin: noVNC's client streaming
9
+ * the app's display. It asks the display to match its size in device pixels
10
+ * (`scale` is what the display was started at), scales down when the display
11
+ * is larger, reconnects while the app starts, carries the clipboard both ways
12
+ * on a tap, gives phones a keyboard, and reports stream latency to Bivy. */
13
+ export function displayViewer(nonce, name, scale, platform = process.platform) {
14
+ const [modifier, modifierCode] = PASTE_MODIFIER[platform] ?? [0xffe3, "ControlLeft"];
15
+ const title = name.replace(/[<>&"]/g, (c) => `&#${c.charCodeAt(0)};`);
16
+ return `<!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, interactive-widget=resizes-content"><title>${title}</title>
17
+ <style nonce="${nonce}">:root{color-scheme:light dark;font-family:system-ui,sans-serif}html,body{margin:0;height:100%;overflow:hidden;background:Canvas;color:CanvasText}
18
+ #screen{position:fixed;inset:0}#status{position:fixed;inset:0;display:grid;place-items:center;margin:0;padding:24px;text-align:center;line-height:1.5}[hidden]{display:none!important}
19
+ #tools{position:fixed;left:12px;bottom:72px;display:flex;flex-direction:column;align-items:flex-start;gap:8px}
20
+ button,#paste{font:inherit;font-size:14px;min-height:44px;padding:0 14px;border-radius:22px;border:1px solid GrayText;background:Canvas;color:CanvasText}
21
+ #keys{display:none;min-width:44px;padding:0}@media (pointer:coarse){#keys{display:block}}
22
+ #paste{display:flex;gap:8px;align-items:center;padding:4px 4px 4px 14px}#paste input{font:inherit;min-height:36px;border:0;background:transparent;color:inherit;width:12em}
23
+ #typing{position:fixed;left:0;top:0;width:1px;height:1px;opacity:0;border:0;padding:0}</style>
24
+ <div id="screen"></div><p id="status" role="status">Starting the app…</p>
25
+ <div id="tools"><button id="copy" type="button" hidden>Copy from app</button>
26
+ <form id="paste" hidden><input id="paste-text" aria-label="Text to paste into the app" placeholder="Paste here"><button type="submit">Send</button></form>
27
+ <button id="paste-open" type="button">Paste</button><button id="keys" type="button" aria-label="Show keyboard">⌨</button></div>
28
+ <input id="typing" aria-label="Type into the app" autocapitalize="off" autocomplete="off" autocorrect="off" spellcheck="false">
29
+ <script type="module" nonce="${nonce}">import RFB from "${NOVNC_PATH}core/rfb.js";
30
+ const $=id=>document.getElementById(id),screen=$("screen"),status=$("status"),typing=$("typing"),SCALE=${scale === 2 ? 2 : 1};
31
+ let rfb,wait=500,remote="";
32
+ const say=text=>{status.textContent=text;status.hidden=!text;};
33
+
34
+ // Latency: time from each input sent to the next frame back, in this browser.
35
+ const lat=[];let pending=0,bytes=0,since=performance.now();
36
+ // (Patched in place: noVNC rejects a WebSocket subclass.)
37
+ const send=WebSocket.prototype.send,probed=new WeakSet();
38
+ WebSocket.prototype.send=function(data){
39
+ if(!probed.has(this)){probed.add(this);this.addEventListener("message",e=>{bytes+=e.data.byteLength??e.data.size??0;if(pending){lat.push(performance.now()-pending);pending=0;}});}
40
+ const first=data instanceof ArrayBuffer?new Uint8Array(data)[0]:ArrayBuffer.isView(data)?new Uint8Array(data.buffer,data.byteOffset,1)[0]:-1;
41
+ if((first===4||first===5)&&!pending)pending=performance.now();
42
+ return send.call(this,data);
43
+ };
44
+ setInterval(()=>{
45
+ if(lat.length<5)return;
46
+ const sorted=lat.splice(0).sort((a,b)=>a-b),at=p=>sorted[Math.min(sorted.length-1,Math.floor(sorted.length*p))];
47
+ const secs=(performance.now()-since)/1000;since=performance.now();
48
+ fetch("${DISPLAY_STATS_PATH}",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({latencyMs:{p50:at(.5),p95:at(.95)},kBps:bytes/1024/secs,viewport:{width:innerWidth,height:innerHeight,scale:devicePixelRatio}})}).catch(()=>{});
49
+ bytes=0;
50
+ },10000);
51
+
52
+ function connect(){
53
+ rfb=new RFB(screen,(location.protocol==="https:"?"wss://":"ws://")+location.host+"${DISPLAY_SOCKET_PATH}",{shared:true});
54
+ // Ask for device pixels, so a display started at 2× stays sharp once scaled
55
+ // to fit. Only the resize request uses them; scaling still fits CSS pixels.
56
+ let ask=1;const measure=rfb._screenSize.bind(rfb),request=rfb._requestRemoteResize.bind(rfb);
57
+ rfb._screenSize=()=>{const s=measure();return{w:Math.round(s.w*ask),h:Math.round(s.h*ask)};};
58
+ rfb._requestRemoteResize=()=>{ask=SCALE;try{request();}finally{ask=1;}};
59
+ rfb.resizeSession=true;rfb.scaleViewport=true;rfb.focusOnClick=true;
60
+ rfb.addEventListener("connect",()=>{wait=500;say("");});
61
+ rfb.addEventListener("disconnect",()=>{say("Starting the app…");setTimeout(connect,wait);wait=Math.min(wait*2,5000);});
62
+ // The app copied text: offer it; the browser only lets a tap write the clipboard.
63
+ rfb.addEventListener("clipboard",e=>{remote=e.detail.text;$("copy").hidden=!remote;$("copy").textContent="Copy from app";});
64
+ }
65
+ connect();
66
+
67
+ $("copy").onclick=async()=>{try{await navigator.clipboard.writeText(remote);$("copy").textContent="Copied";setTimeout(()=>{$("copy").hidden=true;},1500);}catch{$("copy").textContent="Couldn’t copy";}};
68
+ // Paste: hand the text to the app's clipboard, then press Ctrl+V (⌘V on a Mac) in it.
69
+ const paste=text=>{if(!text)return;rfb.clipboardPasteFrom(text);rfb.sendKey(${modifier},"${modifierCode}",true);rfb.sendKey(0x76,"KeyV");rfb.sendKey(${modifier},"${modifierCode}",false);rfb.focus();};
70
+ $("paste-open").onclick=async()=>{
71
+ try{const text=await navigator.clipboard.readText();if(text){paste(text);return;}}catch{}
72
+ // No clipboard access here: paste into a field instead.
73
+ $("paste").hidden=false;$("paste-text").focus();
74
+ };
75
+ $("paste").onsubmit=e=>{e.preventDefault();paste($("paste-text").value);$("paste-text").value="";$("paste").hidden=true;};
76
+ $("paste-text").addEventListener("keydown",e=>{if(e.key==="Escape"){$("paste").hidden=true;rfb.focus();}});
77
+
78
+ // Phones: a soft keyboard needs a text field; forward what's typed as keys.
79
+ const SPECIAL={Backspace:0xff08,Enter:0xff0d,Tab:0xff09,Escape:0xff1b,ArrowLeft:0xff51,ArrowUp:0xff52,ArrowRight:0xff53,ArrowDown:0xff54};
80
+ $("keys").onclick=()=>typing.focus();
81
+ typing.addEventListener("keydown",e=>{const key=SPECIAL[e.key];if(key){e.preventDefault();rfb.sendKey(key,null);}});
82
+ typing.addEventListener("input",()=>{for(const ch of typing.value){const cp=ch.codePointAt(0);rfb.sendKey(cp<256?cp:0x01000000+cp,null);}typing.value="";});
83
+ </script></html>`;
84
+ }
@@ -0,0 +1,145 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-only
2
+ // Copyright (c) 2026 Petter André Sjulstad
3
+ import fs from "node:fs";
4
+ import os from "node:os";
5
+ import path from "node:path";
6
+ import { spawn, execFileSync } from "node:child_process";
7
+ import { FitWindowManager, writeXauthority } from "./x11.js";
8
+ import { MacDisplayHost } from "./macos-display.js";
9
+ /** Environment for programs on a preview display. X11 for every toolkit, so an
10
+ * app never escapes to the machine's own Wayland/X session. */
11
+ const APP_ENV = {
12
+ WAYLAND_DISPLAY: "",
13
+ GDK_BACKEND: "x11",
14
+ QT_QPA_PLATFORM: "xcb",
15
+ SDL_VIDEODRIVER: "x11",
16
+ ELECTRON_OZONE_PLATFORM_HINT: "x11",
17
+ NO_AT_BRIDGE: "1",
18
+ };
19
+ /** How each toolkit is told to draw at 2× for a high-density viewer (Chromium
20
+ * and Electron read the Xft.dpi the window manager publishes). */
21
+ const SCALE_ENV = {
22
+ 1: {},
23
+ 2: { GDK_SCALE: "2", QT_SCALE_FACTOR: "2", J2D_UISCALE: "2" },
24
+ };
25
+ export const DISPLAY_SCALES = Object.keys(SCALE_ENV).map(Number);
26
+ /** TigerVNC's X server; BIVY_XVNC overrides. */
27
+ const XVNC = ["Xtigervnc", "Xvnc"];
28
+ const START_MS = 10_000;
29
+ export function findXvnc(env = process.env) {
30
+ if (env.BIVY_XVNC)
31
+ return fs.existsSync(env.BIVY_XVNC) ? env.BIVY_XVNC : undefined;
32
+ for (const name of XVNC) {
33
+ try {
34
+ return execFileSync("which", [name], { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"], env }).trim() || undefined;
35
+ }
36
+ catch { /* next */ }
37
+ }
38
+ return undefined;
39
+ }
40
+ /** One private X display per view, started on demand: an Xvnc server that
41
+ * listens on no TCP port, an authority cookie only its programs get, and a
42
+ * window manager that fits windows to whatever size the viewer asks for. */
43
+ export class DisplayHost {
44
+ options;
45
+ displays = new Map();
46
+ constructor(options = {}) {
47
+ this.options = options;
48
+ }
49
+ /** Why desktop views can't run here, or undefined when they can. */
50
+ unavailable() {
51
+ if ((this.options.platform ?? process.platform) !== "linux")
52
+ return "Desktop app views need a Linux or macOS machine.";
53
+ if (!(this.options.xvnc ?? findXvnc)())
54
+ return "Desktop app views need TigerVNC's X server. Install it (Debian/Ubuntu: sudo apt install tigervnc-standalone-server), or set BIVY_XVNC to its path.";
55
+ return undefined;
56
+ }
57
+ running(id) { return this.displays.get(id)?.ready; }
58
+ /** `scale` (1 or 2) is fixed when the display starts: programs pick it up once. */
59
+ ensure(id, name, scale = 1) {
60
+ const existing = this.displays.get(id);
61
+ if (existing)
62
+ return existing.ready;
63
+ const state = { dir: fs.mkdtempSync(path.join(os.tmpdir(), "bivy-display-")) };
64
+ state.ready = this.start(state, name, SCALE_ENV[scale] ? scale : 1).then((display) => {
65
+ // If the X server dies, forget it: the next open starts a fresh one.
66
+ state.child.once("exit", () => { if (this.displays.get(id) === state)
67
+ this.stop(id); });
68
+ return display;
69
+ }, (error) => { this.stop(id); throw error; });
70
+ this.displays.set(id, state);
71
+ return state.ready;
72
+ }
73
+ async start(state, name, scale) {
74
+ const reason = this.unavailable();
75
+ if (reason)
76
+ throw new Error(reason);
77
+ const xvnc = (this.options.xvnc ?? findXvnc)();
78
+ const x11Dir = this.options.x11Dir ?? "/tmp/.X11-unix";
79
+ const authority = path.join(state.dir, "Xauthority");
80
+ const socket = path.join(state.dir, "vnc.sock");
81
+ const cookie = writeXauthority(authority);
82
+ // Another X server may take a number between our check and its lock.
83
+ for (let attempt = 0; attempt < 5; attempt++) {
84
+ const number = freeDisplay(x11Dir);
85
+ const child = spawn(xvnc, [`:${number}`, "-auth", authority, "-rfbunixpath", socket, "-rfbunixmode", "0600", "-rfbport", "-1",
86
+ "-SecurityTypes", "None", "-AlwaysShared=1", "-AcceptSetDesktopSize=1", "-SendPrimary=0", "-nolisten", "tcp", "-geometry", `${1280 * scale}x${800 * scale}`, "-depth", "24", "-dpi", String(96 * scale), "-desktop", name.slice(0, 60)], { stdio: "ignore" });
87
+ state.child = child;
88
+ const x = path.join(x11Dir, `X${number}`);
89
+ const up = await new Promise((resolve) => {
90
+ const deadline = Date.now() + START_MS;
91
+ const exited = () => resolve(false);
92
+ child.once("exit", exited);
93
+ child.once("error", exited);
94
+ const poll = () => {
95
+ if (child.exitCode !== null)
96
+ return;
97
+ if (fs.existsSync(socket) && fs.existsSync(x)) {
98
+ child.off("exit", exited);
99
+ resolve(true);
100
+ return;
101
+ }
102
+ if (Date.now() > deadline) {
103
+ child.kill("SIGKILL");
104
+ return;
105
+ }
106
+ setTimeout(poll, 50);
107
+ };
108
+ poll();
109
+ });
110
+ if (!up)
111
+ continue;
112
+ const wm = new FitWindowManager();
113
+ await wm.start(x, cookie, 96 * scale);
114
+ return { socket, wm, scale, env: { ...APP_ENV, ...SCALE_ENV[scale], DISPLAY: `:${number}`, XAUTHORITY: authority } };
115
+ }
116
+ throw new Error("Couldn't start a display for this app.");
117
+ }
118
+ stop(id) {
119
+ const state = this.displays.get(id);
120
+ if (!state)
121
+ return;
122
+ this.displays.delete(id);
123
+ void state.ready.then((display) => display.wm.stop(), () => { });
124
+ state.child?.kill("SIGTERM");
125
+ fs.rmSync(state.dir, { recursive: true, force: true });
126
+ }
127
+ stopAll() { for (const id of [...this.displays.keys()])
128
+ this.stop(id); }
129
+ }
130
+ function freeDisplay(x11Dir) {
131
+ for (let n = 100; n < 1000; n++) {
132
+ if (!fs.existsSync(path.join(x11Dir, `X${n}`)) && !fs.existsSync(`/tmp/.X${n}-lock`))
133
+ return n;
134
+ }
135
+ throw new Error("No free X display number.");
136
+ }
137
+ /** How each platform gives a desktop app a preview display: a private X
138
+ * server on Linux, the app's own windows on a Mac. Both serve VNC on a
139
+ * private socket, so everything downstream is shared. */
140
+ const HOSTS = {
141
+ darwin: (dataDir) => new MacDisplayHost({ cacheDir: path.join(dataDir, "bin") }),
142
+ };
143
+ export function createDisplayHost(dataDir, platform = process.platform) {
144
+ return HOSTS[platform]?.(dataDir) ?? new DisplayHost({ platform });
145
+ }