Files
AgentHub/server/lib/native-dialog.js
T
nntrivi2001 6c605849d8 feat: workspace aliases, tab groups of split panes, native path picker (v2.1.0)
- /api/workspaces and `agenthub workspace(s)` / `--workspace` as aliases of
  /api/lanes and `agenthub lanes` / `--lane`; old names print one stderr note.
  REPL `help lanes|workspace|workspaces` resolves through the aliases.
- A workspace holds up to ten sessions (slots 0-9); every console tab is a
  group of 1-4 split panes (PaneGroups, agenthub.workspace.paneLayout);
  parseGroups keeps the stored active tab when an earlier one is dropped.
- Browse button beside path fields: POST /api/lanes/pick-path opens the OS
  picker on the server's desktop — loopback only and no forwarding header
  (EREMOTE), a display failure is ENODIALOG not a Cancel, the dialog is
  killed when the request closes, a folder field falls back to the in-app
  browser on any failure.
- Changes rail counts chip, image viewer (/git/raw), status bar session
  counts and per-day cost (`date=` on /api/pricing/cost).
- /agenthub-open and /agenthub-doctor run from the INSTALLED build
  (supersededBy), doctor offers fixes behind two confirmations; plugin-open
  treats a locked bootstrap as non-fatal.
- New-terminal shell waits for the first attach (issue #55).
- repo-onboarding / debug-live-issue skills removed.
- OpenAPI: pick-path, lane/date on /pricing/cost, tool on /run/history,
  /api/workspaces mount.
2026-10-06 11:31:26 +07:00

181 lines
6.4 KiB
JavaScript

/**
* @file native-dialog.js
* @description Opens the operating system's own folder/file picker ON THE
* SERVER'S DESKTOP and returns the absolute path the user chose. A browser's
* native picker never hands a page an absolute path, and the dashboard is
* local-first, so the server asks the desktop itself: zenity or kdialog on
* Linux (with the graphical session's variables from `desktop-env.js`, which a
* systemd user service lacks), `osascript` on macOS, PowerShell's WinForms
* dialogs on Windows. Runs with `execFile` (never a shell, never sync — a
* dialog waits on a human for minutes), one dialog at a time. Nothing from the
* request reaches argv except the starting folder, resolved to an absolute
* path first. macOS and Windows are code-only, untested on those systems.
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
*/
const { execFile } = require("node:child_process");
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const { desktopEnv } = require("./desktop-env");
const TITLES = { dir: "Select a folder", file: "Select a file" };
const TIMEOUT_MS = 10 * 60 * 1000;
const defaultExec = (cmd, args, env, signal) =>
new Promise((resolve) => {
execFile(
cmd,
args,
{ env, timeout: TIMEOUT_MS, windowsHide: true, encoding: "utf8", signal },
(err, stdout, stderr) =>
resolve({
missing: err?.code === "ENOENT",
code: err ? (typeof err.code === "number" ? err.code : -1) : 0,
stdout: String(stdout || ""),
stderr: String(stderr || ""),
})
);
});
let execImpl = defaultExec;
/** Test seam — same pattern as desktop-env.js; `null` restores the real one. */
function __setExecImpl(fn) {
execImpl = fn || defaultExec;
}
/** An existing folder to open the dialog in: `start` (or its parent when it is a file), else home. */
function startDir(start) {
if (typeof start === "string" && start.trim()) {
const raw = start.trim().replace(/^~(?=$|[\\/])/, os.homedir());
const abs = path.resolve(raw);
for (const p of [abs, path.dirname(abs)]) {
try {
if (fs.statSync(p).isDirectory()) return p;
} catch {
// missing — try the parent, then home
}
}
}
return os.homedir();
}
/** The candidate dialog commands for a platform, in order of preference. */
function dialogCommands(kind, dir, platform = process.platform) {
const title = TITLES[kind];
if (platform === "darwin") {
const what = kind === "dir" ? "folder" : "file";
return [
[
"osascript",
[
"-e",
"on run argv",
"-e",
`POSIX path of (choose ${what} with prompt (item 1 of argv) default location (POSIX file (item 2 of argv)))`,
"-e",
"end run",
title,
dir,
],
],
];
}
if (platform === "win32") {
const dialog =
kind === "dir"
? "$d = New-Object System.Windows.Forms.FolderBrowserDialog; $d.SelectedPath = $env:AGENTHUB_PICK_START; $d.Description = $env:AGENTHUB_PICK_TITLE"
: "$d = New-Object System.Windows.Forms.OpenFileDialog; $d.InitialDirectory = $env:AGENTHUB_PICK_START; $d.Title = $env:AGENTHUB_PICK_TITLE";
const result = kind === "dir" ? "$d.SelectedPath" : "$d.FileName";
return [
[
"powershell.exe",
[
"-NoProfile",
"-STA",
"-Command",
`[Console]::OutputEncoding = [Text.Encoding]::UTF8; Add-Type -AssemblyName System.Windows.Forms; ${dialog}; if ($d.ShowDialog() -eq 'OK') { [Console]::Out.Write(${result}) } else { exit 1 }`,
],
],
];
}
const sep = dir.endsWith("/") ? "" : "/";
return [
[
"zenity",
[
"--file-selection",
`--title=${title}`,
`--filename=${dir}${sep}`,
...(kind === "dir" ? ["--directory"] : []),
],
],
[
"kdialog",
[kind === "dir" ? "--getexistingdirectory" : "--getopenfilename", dir, "--title", title],
],
];
}
// A dialog that could not reach the desktop exits 1 too — a stale DISPLAY
// handed over by `systemctl --user show-environment` after a logout, say.
const NO_DISPLAY_RE =
/cannot open display|could not connect to display|failed to open display|cannot connect to x server|unable to init server/i;
/**
* Whether an exit status of 1 means the user pressed Cancel. zenity/kdialog
* may print GTK/Qt warnings on a normal Cancel, so only a display failure
* counts against it; osascript says Cancel as error -128 and nothing else.
*/
function isCancel(cmd, stderr = "") {
if (cmd === "osascript") return /-128/.test(stderr);
if (cmd === "powershell.exe") return !stderr.trim();
return !NO_DISPLAY_RE.test(stderr);
}
function fail(code, message) {
return Object.assign(new Error(message), { code });
}
let busy = false;
/**
* Show the native picker and wait for the user.
* @param {"dir"|"file"} kind
* @param {string} [start] Folder (or file) to open the dialog at.
* @param {AbortSignal} [signal] Closes the dialog — the browser went away.
* @returns {Promise<string|null>} The chosen absolute path, or null when cancelled.
* @throws `ENODIALOG` (no desktop or no dialog program), `EBUSY` (one is already open), `EDIALOG`.
*/
async function pickPath(kind, start, signal) {
if (busy) throw fail("EBUSY", "a picker is already open on the desktop");
const env = { ...process.env, ...desktopEnv() };
if (process.platform === "linux" && !env.DISPLAY && !env.WAYLAND_DISPLAY) {
throw fail("ENODIALOG", "no graphical desktop session to show a picker on");
}
const dir = startDir(start);
env.AGENTHUB_PICK_START = dir;
env.AGENTHUB_PICK_TITLE = TITLES[kind];
busy = true;
try {
for (const [cmd, args] of dialogCommands(kind, dir)) {
const r = await execImpl(cmd, args, env, signal);
if (r.missing) continue;
if (r.code === 1) {
if (isCancel(cmd, r.stderr)) return null; // every one of these exits 1 on Cancel
throw fail("ENODIALOG", `${cmd} could not open a picker on the desktop`);
}
const out = r.stdout.replace(/[\r\n]+$/, "");
if (r.code !== 0 || !out) throw fail("EDIALOG", `${cmd} exited with ${r.code}`);
// osascript ends a folder with "/"; "/" itself stays.
return out.replace(/\/+$/, "") || "/";
}
throw fail("ENODIALOG", "no native picker found (install zenity or kdialog)");
} finally {
busy = false;
}
}
module.exports = { pickPath, dialogCommands, startDir, isCancel, __setExecImpl };