Make Terminal a native feature

- Move the Terminal plugin into src/lib/features/terminal/ on the host Svelte runtime, with xterm 6.0.0 and addon-fit 0.11.0.
- Buttons, shortcuts, Help rows, and the close guard follow the Terminal switch. The first use loads the module.
- Add Settings → Terminal and the terminal settings section. The first start copies the old runestone.terminal key.
- The 16 ANSI colors come from the --rs-ansi-* tokens.
- Off asks first when a shell runs in any window (pty_running).
- Rust: no pty-data or pty-exit events, an owner check for write, resize, and close, output joined up to 64 KiB or 8 ms, and the sessions of a reloaded page close.
- Remove api.pty, the terminal contribution, TerminalSlot, and the plugin loader of the smoke harness.
- Check only the tested switches in the Background smoke test.
- Update the tests, the menu inventory, the docs, and UI-058.
This commit is contained in:
Knotty Wyvern
2026-10-29 19:02:58 -07:00
parent 53b9cea7f0
commit 833f793244
78 changed files with 2971 additions and 659 deletions
+2 -54
View File
@@ -4,14 +4,14 @@ import { registerCompletionInput } from "../completion/input.ts";
import { showActionMenu } from "../ui/managedActionMenu.ts";
import { backgroundHub } from "./backgrounds.ts";
import { serviceHub as defaultServiceHub, type ServiceHub } from "./services.ts";
import type { Dispose, FilesApi, FilesTransport, PluginApi, PluginCommand, PluginModule, PluginView, PluginDocumentType, PluginDocumentContext, PtyApi, PtyOpenOptions, PtySession, PtyTransport } from "./sdk.ts";
import type { Dispose, FilesApi, FilesTransport, PluginApi, PluginCommand, PluginModule, PluginView, PluginDocumentType, PluginDocumentContext } from "./sdk.ts";
/** The host services that a session gives to its plugin. The host binds them to the plugin ID. */
export type SessionServices = Pick<PluginApi, "workspace" | "settings" | "assets"> & {
/** True gives the plugin `api.backgrounds`: the background frames of each target. */
openFile?: PluginApi["openFile"]; backgrounds?: boolean;
completionTransport?: NonNullable<PluginApi["completion"]>["transport"];
pty?: PtyTransport; files?: FilesTransport; serviceHub?: ServiceHub;
files?: FilesTransport; serviceHub?: ServiceHub;
};
export function deadline<T>(work: Promise<T>, milliseconds = 5000): Promise<T> {
@@ -37,7 +37,6 @@ export class ExternalSession {
private backgroundSubscriptions = new Set<() => void>();
private serviceOwner = Symbol('plugin services');
private serviceSubscriptions = new Set<() => void>();
private ptySessions = new Map<string, () => Promise<void>>();
readonly id: string;
private services: SessionServices;
private changed: () => void;
@@ -65,7 +64,6 @@ export class ExternalSession {
};
const api: PluginApi = {
id: this.id, signal: this.controller.signal,
...(this.services.pty ? { pty: this.ptyApi(this.services.pty) } : {}),
...(this.services.files ? { files: this.filesApi(this.services.files) } : {}),
services: this.servicesApi(this.services.serviceHub ?? defaultServiceHub),
...(this.services.backgrounds ? { backgrounds: {
@@ -175,52 +173,6 @@ export class ExternalSession {
this.check();
return request().then(value => { this.check(); return value; });
}
private ptyApi(pty: PtyTransport): PtyApi {
const size = (cols: unknown, rows: unknown) => {
if (![cols, rows].every(value => Number.isInteger(value) && (value as number) > 0 && (value as number) <= 10_000)) throw new Error("Terminal columns and rows must be positive integers.");
};
const validate = (options: PtyOpenOptions) => {
this.check();
if (!options || typeof options.onData !== "function" || (options.onExit !== undefined && typeof options.onExit !== "function")) throw new Error("A terminal session needs an onData function.");
if (typeof options.cwd !== "string") throw new Error("Supply the workspace folder as cwd.");
size(options.cols, options.rows);
};
const open = async (options: PtyOpenOptions): Promise<PtySession> => {
const id = crypto.randomUUID();
let closed = false, exited = false, closing: Promise<void> | undefined;
const exit = (code: number | null) => {
if (exited) return;
exited = true; this.ptySessions.delete(id);
if (!closed && !this.stopped) options.onExit?.(code);
};
const opening = pty.open({ id, cwd: options.cwd, cols: options.cols, rows: options.rows, ...(options.profile === undefined ? {} : { profile: options.profile }), ...(options.directory === undefined ? {} : { directory: options.directory }) },
data => { if (!closed && !this.stopped) options.onData(data); }, exit);
// Wait for the start, so that a close cannot leave a late shell running.
const close = () => closing ??= (async () => {
closed = true;
try { await opening; } catch { this.ptySessions.delete(id); return; }
this.ptySessions.delete(id);
await pty.close(id);
})();
this.ptySessions.set(id, close);
try { await opening; } catch (error) { this.ptySessions.delete(id); closed = true; throw error; }
if (this.stopped) { await close(); this.check(); }
const live = () => { this.check(); if (closed) throw new Error("This terminal is closed."); };
return {
id,
write: data => { live(); if (typeof data !== "string") throw new Error("Terminal input must be text."); return pty.write(id, data); },
resize: (cols, rows) => { live(); size(cols, rows); return pty.resize(id, cols, rows); },
close,
};
};
return {
version: 1,
profiles: () => this.backgroundRequest(() => pty.profiles()),
open: options => { validate(options); return open(options); },
openWindow: () => { this.check(); return pty.openWindow(); },
dictation: () => { this.check(); return pty.dictation(); },
};
}
private filesApi(files: FilesTransport): FilesApi {
const text = (value: unknown, label: string) => { if (typeof value !== "string" || !value) throw new Error(`Supply the ${label} as text.`); };
return {
@@ -293,10 +245,6 @@ export class ExternalSession {
try { await deadline(Promise.resolve().then(cleanup)); } catch (error) { errors.push(error); }
}
this.cleanups = [];
// Stop the terminal sessions that the plugin cleanup left open.
for (const close of [...this.ptySessions.values()]) {
try { await deadline(close()); } catch (error) { errors.push(error); }
}
if (errors.length) throw new Error(errors.map(String).join("\n"));
})();
return this.stopping;