repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/nushell/runtime.ts

Raw
File outline
import { spawn } from "node:child_process";
import { constants, existsSync } from "node:fs";
import { access } from "node:fs/promises";
import { delimiter, join } from "node:path";
import type { BashOperations } from "@earendil-works/pi-coding-agent";
import type { Client } from "@modelcontextprotocol/sdk/client/index.js";
import type { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
import type { DebugLog } from "./debug.ts";

const SAFE_NU_CODES = new Set([
	"nu::shell::error",
	"nu::shell::external_command",
	"nu::shell::division_by_zero",
]);

const FRESH_ARGS = [
	"--no-config-file",
	"--no-history",
	"--error-style",
	"fancy",
	"--table-mode",
	"markdown",
	"-c",
];

export function findNu(): string | undefined {
	const path = Object.entries(process.env).find(
		([key]) => key.toLowerCase() === "path",
	)?.[1];
	const names = process.platform === "win32" ? ["nu.exe"] : ["nu"];
	for (const directory of (path ?? "").split(delimiter)) {
		if (!directory) continue;
		for (const name of names) {
			const candidate = join(directory, name);
			if (existsSync(candidate)) return candidate;
		}
	}
	return undefined;
}

function stopProcess(pid: number | undefined): void {
	if (!pid) return;
	if (process.platform === "win32") {
		const root = process.env.SystemRoot ?? "C:\\Windows";
		spawn(
			join(root, "System32", "taskkill.exe"),
			["/F", "/T", "/PID", String(pid)],
			{
				stdio: "ignore",
				windowsHide: true,
			},
		).unref();
	} else {
		try {
			process.kill(-pid, "SIGKILL");
		} catch {
			try {
				process.kill(pid, "SIGKILL");
			} catch {
				/* already exited */
			}
		}
	}
}

export function freshOperations(
	binary: string,
	debug?: DebugLog,
): BashOperations {
	return {
		async exec(command, cwd, { onData, signal, timeout, env }) {
			const started = debug ? performance.now() : 0;
			const callId = debug ? globalThis.crypto.randomUUID() : undefined;
			debug?.("fresh.start", {
				callId,
				commandLength: command.length,
				timeout,
			});
			let childPid: number | undefined;
			let exitCode: number | null = null;
			let timedOut = false;
			let stdoutBytes = 0;
			let stderrBytes = 0;
			try {
				if (signal?.aborted) throw new Error("aborted");
				if (
					timeout !== undefined &&
					(!Number.isFinite(timeout) ||
						timeout <= 0 ||
						timeout * 1000 > 2_147_483_647)
				)
					throw new Error(
						"Invalid timeout: must be a positive number of seconds within the timer range",
					);
				await access(cwd, constants.F_OK);
				const child = spawn(binary, [...FRESH_ARGS, command], {
					cwd,
					env,
					detached: process.platform !== "win32",
					stdio: ["ignore", "pipe", "pipe"],
					windowsHide: true,
				});
				childPid = child.pid;
				const onAbort = () => stopProcess(child.pid);
				const timer =
					timeout === undefined
						? undefined
						: setTimeout(() => {
								timedOut = true;
								onAbort();
							}, timeout * 1000);
				signal?.addEventListener("abort", onAbort, { once: true });
				if (signal?.aborted) onAbort();
				if (debug) {
					child.stdout.on("data", (chunk: Buffer) => {
						stdoutBytes += chunk.length;
						onData(chunk);
					});
					child.stderr.on("data", (chunk: Buffer) => {
						stderrBytes += chunk.length;
						onData(chunk);
					});
				} else {
					child.stdout.on("data", onData);
					child.stderr.on("data", onData);
				}
				try {
					exitCode = await new Promise<number | null>((resolve, reject) => {
						child.once("error", reject);
						child.once("exit", resolve);
					});
					if (signal?.aborted) throw new Error("aborted");
					if (timedOut) throw new Error(`timeout:${timeout}`);
					return { exitCode: exitCode ?? (child.signalCode ? 1 : null) };
				} finally {
					if (timer) clearTimeout(timer);
					signal?.removeEventListener("abort", onAbort);
				}
			} finally {
				debug?.("fresh.finish", {
					callId,
					childPid,
					durationMs: performance.now() - started,
					exitCode,
					outcome: signal?.aborted
						? "aborted"
						: timedOut
							? "timeout"
							: exitCode === 0
								? "success"
								: exitCode === null
									? "failed"
									: "nonzero",
					stdoutBytes,
					stderrBytes,
				});
			}
		},
	};
}

type Worker = {
	client: Client;
	transport: StdioClientTransport;
	types: typeof import("@modelcontextprotocol/sdk/types.js");
};

export class NuSession {
	private worker: Worker | undefined;
	private debug?: DebugLog;
	constructor(debug?: DebugLog) {
		this.debug = debug;
	}
	setDebug(debug?: DebugLog): void {
		this.debug = debug;
	}
	private pending: Promise<unknown> = Promise.resolve();
	private generation = 0;

	private async start(
		binary: string,
		cwd: string,
		debug?: DebugLog,
		callId?: string,
	): Promise<Worker> {
		const started = debug ? performance.now() : 0;
		debug?.("session.connect.start", { callId });
		const { Client } = await import(
			"@modelcontextprotocol/sdk/client/index.js"
		);
		const { StdioClientTransport } = await import(
			"@modelcontextprotocol/sdk/client/stdio.js"
		);
		// Load MCP schemas with the first worker, not during inert Pi startup.
		const types = await import("@modelcontextprotocol/sdk/types.js");
		const client = new Client({ name: "pi-ext-nushell", version: "1" });
		const transport = new StdioClientTransport({
			command: binary,
			args: [
				"--no-config-file",
				"--no-history",
				"--mcp",
				"--mcp-transport",
				"stdio",
			],
			cwd,
			env: Object.fromEntries(
				Object.entries(process.env).filter(
					(entry): entry is [string, string] => entry[1] !== undefined,
				),
			),
			stderr: "pipe",
		});
		// Nu MCP logs requests and responses to stderr, including command text.
		// Drain it; never print it into Pi's TUI or JSON mode.
		transport.stderr?.on("data", () => {});
		try {
			await client.connect(transport);
		} catch (error) {
			debug?.("session.connect.error", {
				callId,
				durationMs: performance.now() - started,
				errorType: error instanceof Error ? error.name : typeof error,
			});
			await client.close();
			throw error;
		}
		debug?.("session.connect.ready", {
			callId,
			workerPid: transport.pid,
			durationMs: performance.now() - started,
		});
		return { client, transport, types };
	}

	async evaluate(
		binary: string,
		cwd: string,
		command: string,
		signal?: AbortSignal,
	): Promise<string> {
		const work = async () => {
			const debug = this.debug;
			const started = debug ? performance.now() : 0;
			const callId = debug ? globalThis.crypto.randomUUID() : undefined;
			debug?.("session.request.start", {
				callId,
				commandLength: command.length,
			});
			let worker: Worker | undefined;
			let startedCall = false;
			try {
				if (signal?.aborted)
					throw new Error("Nu session request aborted before execution");
				const generation = this.generation;
				worker = this.worker ?? (await this.start(binary, cwd, debug, callId));
				if (generation !== this.generation || signal?.aborted) {
					if (worker !== this.worker) await worker.client.close();
					throw new Error("Nu session reset before execution");
				}
				this.worker = worker;
				startedCall = true;
				const { CallToolResultSchema } = worker.types;
				const result = CallToolResultSchema.parse(
					await worker.client.callTool(
						{ name: "evaluate", arguments: { input: command } },
						CallToolResultSchema,
						{ signal, timeout: 110_000 },
					),
				);
				const text = result.content
					.filter((part) => part.type === "text")
					.map((part) => part.text)
					.join("\n");
				if (result.isError)
					throw new Error(text || "Nushell evaluation failed");
				debug?.("session.request.finish", {
					callId,
					workerPid: worker.transport.pid,
					durationMs: performance.now() - started,
					outputBytes: Buffer.byteLength(text),
				});
				return text || "(no output)";
			} catch (error) {
				const rawCode =
					debug && error instanceof Error
						? /\bnu::[a-z_]+::[a-z_]+\b/.exec(error.message)?.[0]
						: undefined;
				debug?.("session.request.error", {
					callId,
					workerPid: worker?.transport.pid,
					durationMs: performance.now() - started,
					aborted: signal?.aborted ?? false,
					errorType: error instanceof Error ? error.name : typeof error,
					code:
						worker && error instanceof worker.types.McpError
							? error.code
							: undefined,
					nuCode: rawCode && SAFE_NU_CODES.has(rawCode) ? rawCode : undefined,
				});
				if (startedCall && worker) {
					const { McpError, ErrorCode } = worker.types;
					if (
						signal?.aborted ||
						(error instanceof McpError &&
							(error.code === ErrorCode.RequestTimeout ||
								(error.code === ErrorCode.InternalError &&
									error.message.includes(
										"Operation promoted to background job (id:",
									))))
					) {
						await this.reset("interrupted");
						throw new Error(
							"Nu session interrupted; state reset. Detached external commands may still be running; inspect before retrying.",
						);
					}
					if (worker.transport.pid === null) await this.reset("worker_exit");
				}
				throw error;
			}
		};
		const next = this.pending.then(work, work);
		this.pending = next.catch(() => {});
		return next;
	}

	async reset(reason = "requested"): Promise<void> {
		this.generation++;
		const worker = this.worker;
		this.worker = undefined;
		this.debug?.("session.reset", {
			reason,
			workerPid: worker?.transport.pid ?? null,
		});
		if (worker) await worker.client.close();
	}
}