Files
magpie/internal/gui/assets/plugins/sessions/settings.js
T
yetone ead0bf0059 agents, sessions: open in a terminal on Windows and Linux too, and copy any CLI agent's launch line
lileo0668 on Discord: clicking an agent should start it in a terminal,
and a history session should resume there. Opening a terminal was the
Mac app's only (Open in terminal on an Agents row, and Sessions' Open in
the selected terminal and Continue with another agent), and Copy launch
command was only for agy, Cursor (local) and Claude Science.

- terminal_open.go: openTerminal off a Mac.
  - Windows: Windows PowerShell (the system default), PowerShell 7 and
    Windows Terminal. The line, PowerShell's as Sessions already copies
    it, goes as -EncodedCommand: no script file, so no execution policy,
    no file encoding and no quoting in between. A shell gets its own
    console through cmd's start (magpie's children are CREATE_NO_WINDOW);
    wt is started directly, on pwsh or else powershell.
  - Linux: $TERMINAL, then xdg-terminal-exec, then x-terminal-emulator,
    then the first of GNOME Terminal, Konsole, Xfce Terminal, kitty,
    Alacritty, Ghostty, foot, WezTerm, Terminator and XTerm, each with its
    own words before the command. The line runs from a script of its own
    in sh, then the user's shell, as the Mac's .command file does.
  - A terminal that quits failing within a second says so, with what it
    printed ("cannot open display").
- Settings' Session terminal row lists the terminals found on Windows and
  Linux too; no row when none is found.
- Every agent row with a program now carries `command`, the line the
  server builds (Launch, or the program quoted for this system's shell;
  an agent in a WSL distro is started through wsl.exe on Windows). The
  row menu (right-click or the grip) has Copy launch command after Open in
  terminal, on the web build and in the panel too.

Run:
- mote (macOS 27, go1.26.8): go vet of internal/gui and internal/settings
  for darwin, for linux with -tags nogui, and for windows; GOOS=windows
  and GOOS=linux (nogui) builds of the binary; go test ./internal/gui
  ./internal/settings ./internal/agent. Everything passes except
  TestMainWindowCorners/macOS_26_SDK, which is #3949.
- Windows (win-run, xiaomi-gpu): go test ./internal/gui ./internal/settings.
  The new tests pass, among them TestAgentLineWindows, where real
  PowerShell runs the encoded line on a claude.cmd in a folder named
  `it's a $(New-Item made1) & New-Item made2 #` and runs nothing else.
  The six reds are filed: TestLodyImportHandoff,
  TestImportStatusUnreadableIsUnknown and TestAionUiImportRow (#4115),
  TestUpdateThatWontStart (#1827), and TestPluginAgentMissingSaysSo and
  TestGatewayHeldByAContainer (#5324).
- Linux (wsl-run, magpie-desktop): go test -tags nogui -timeout 45m
  ./internal/gui ./internal/settings: both ok (internal/gui 630s; at
  the default 10m it timed out with nothing hung)
- Real use. A throwaway test called openTerminal, not committed.
  - On xiaomi-gpu's console desktop (schtasks /IT): system default and
    wt each opened a window and ran a line with ' and 中文. pwsh isn't
    installed there and was refused by name.
  - On magpie-desktop under Xvfb: the system default
    (x-terminal-emulator, the xfce4-terminal wrapper) and xfce4-terminal
    each ran it (TERM=xterm-256color, set by the terminal). xterm isn't
    installed there and was refused. Without a display, xfce4-terminal's
    "cannot open display" came back as the error.
- GUI (mote, Playwright Chromium and WebKit): agent-open-terminal (five
  languages, 980px and 420px; Copy sends the server's line to /api/copy
  unchanged), session-terminal (Win32 and Linux platforms at 420px, and
  no row when nothing is found), settings-general, and the agent-*,
  sessions-*, session-*, gui-de, gui-ja and gui-zh-tw files with
  fold-show-grip, grip-inset, tap-click, menu-input-scroll and
  click-scroll (102 files): 1689 pass, 5 fail. The fails:
  menu-input-scroll, 4 of 4 red alone on origin/main 152c3852f too
  (#3402), and i18n-keys' "a plugin's words are its own" is
  red on origin/main too (#5332, #5327, #5426, #5482).
- gofmt -l is clean. TestCoreScriptBudget passes; no budget was raised.

Broken by hand:
- PowerShell quoting turned into POSIX quoting: TestAgentLineWindows red.
- cmd's start dropped: TestWinTerminalArgv red.
- A terminal's failure ignored: TestOpenLinuxTerminalRuns red.
- app.js and settings.js put back as they were: 32 GUI tests red
  (agent-open-terminal, session-terminal).

Not tried:
- A real GNOME, KDE, kitty, Alacritty, Ghostty, foot or WezTerm. Their
  words come from their docs.
- Omarchy's xdg-terminal-exec.
- PowerShell 7 on a real desktop (not installed on the box).
- The packaged GUI app on Windows or Linux; the real runs called
  openTerminal from a test binary.

Refs #4474.

Reported-by: lileo0668 (Discord)
2026-11-06 16:02:04 +08:00

101 lines
4.8 KiB
JavaScript

// The sessions plugin's part of Settings: General's Session terminal and
// Privacy's Agents' session history. Run after app.js, whose helpers it
// shares (el, segs, svg, api, writingPrefs, savePrefs, openProtoMenu,
// status), and before start.js draws Settings; drawn whether the plugin is
// on or off.
function renderSessionTerminal(s, keep) {
const row = $("#sessionTerminalRow");
// a Mac's, and Windows' and Linux's when a terminal is found there
const mac = document.body.classList.contains("mac");
const apps = [...(s.terminalApps || [])];
row.hidden = web || !mac && !apps.length;
if (row.hidden) return;
const select = $("#sessionTerminalSelect");
if (mac && !apps.some((app) => app.id === "com.apple.Terminal")) apps.unshift({ id: "com.apple.Terminal", name: "Terminal" });
const chosen = s.sessionTerminal || "system";
// a default that is not a terminal opens Terminal, as the server does
const defaultApp = apps.find((app) => app.id === s.terminalDefault) || apps.find((app) => app.id === "com.apple.Terminal") || apps[0];
const options = [{ id: "system", name: t("System default ({name})", { name: defaultApp.name }) }];
for (const app of apps) {
if (app.id === defaultApp.id && app.id !== chosen) continue;
options.push({ id: app.id, name: app.id === defaultApp.id ? t("{name} (fixed)", { name: app.name }) : app.name });
}
if (!options.some((app) => app.id === chosen)) {
options.push({ id: chosen, name: t("Unavailable app ({id})", { id: chosen }) });
}
// the app's own menu, not a native select
select.dataset.value = chosen;
select.replaceChildren(el("span", "", options.find((app) => app.id === chosen).name), svg(CHEV, 11, 1.6));
select.onclick = (e) => {
e.stopPropagation();
if (select.classList.contains("open")) return closeProtoMenu();
openProtoMenu(select, options.map((app) => ({ v: app.id, name: app.name, note: "", literalName: true })), chosen, (v) => {
if (v === chosen) return;
// drawn at once; the save draws it again with what was kept
select.dataset.value = v;
select.firstChild.textContent = options.find((app) => app.id === v).name;
savePrefs({ ...keep, sessionTerminal: v === "system" ? "" : v });
}, "Session terminal", "sess-menu");
};
}
// renderSessionReads: which agents' own session history magpie reads
// (RustCats on X: a new install showed every agent's sessions before
// anyone agreed). The Sessions page asks once; here it changes later.
// "Agents found later" is whether an agent with no row of its own is
// read, and drawn from the settings at once; each agent found on this
// computer has a row of its own, read from /api/sessions/reads when the
// Privacy part is shown (loadSessionReads).
let sessionReads = null; // /api/sessions/reads, as last read
let sessionReadsSeq = 0;
function renderSessionReads(s) {
const box = $("#sessionReadsList");
const r = sessionReads || { asked: !!s?.sessionReads?.asked, all: !!s?.sessionReads?.all, found: [] };
// gateway mode reads no agent's session history here, whatever these
// say (ownSessionsOff), so they are left out
const gw = !!(s?.gatewayOn || r.gateway);
box.hidden = gw;
box.previousElementSibling.hidden = gw;
if (gw) return;
const save = (body) => {
const n = ++sessionReadsSeq;
writingPrefs(api("sessions/reads", body))
.then((got) => { if (n === sessionReadsSeq) { sessionReads = got; renderSessionReads(prefs); } })
.catch((e) => { status(t(e.message), "err"); renderSessionReads(prefs); });
};
const row = (name, sub, on, fn) => {
const rw = el("div", "row pref");
const who = el("div", "who");
who.append(el("div", "name", name));
if (sub) who.append(el("div", "sub", sub));
const val = el("div", "val");
val.append(segs([["off", t("Off")], ["on", t("On")]], on ? "on" : "off", (v) => {
if ((v === "on") !== on) fn(v === "on");
}));
rw.append(who, val);
return rw;
};
const rows = [row(t("Agents found later"), t("Read the session history of an agent with no row here, one installed later too. Off, magpie reads only the agents turned on below; what its gateway served is counted either way"),
r.asked && r.all, (on) => {
// the agents with a row keep what their rows show
const agents = {};
for (const a of r.found || []) agents[a.agent] = !!a.on;
save(Object.keys(agents).length ? { all: on, agents } : { all: on });
})];
for (const a of r.found || []) {
const rw = row(a.name, "", a.on, (on) => save({ agents: { [a.agent]: on } }));
rw.dataset.agent = a.agent;
rows.push(rw);
}
box.replaceChildren(...rows);
}
function loadSessionReads() {
const n = ++sessionReadsSeq;
api("sessions/reads").then((got) => {
if (n !== sessionReadsSeq) return;
sessionReads = got;
renderSessionReads(prefs);
}).catch(() => {}); // the row drawn from the settings stays
}