import { chmodSync, mkdirSync, mkdtempSync, rmSync, writeFileSync, } from "node:fs"; import { tmpdir } from "node:os"; import { delimiter, join } from "node:path"; import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; /** Recording/live-session fixture only; never shipped or discovered by the package. */ export default function clipboardFixture(pi: ExtensionAPI): void { let ready = false; pi.on("tool_call", (event) => { if (event.toolName === "clipboard" && !ready) return { block: true, reason: "Isolated clipboard fixture is unavailable; Linux and successful fixture setup are required. The desktop clipboard was not accessed.", }; return undefined; }); pi.on("cache_warming_decision", () => ({ action: "stop" })); if (process.platform !== "linux") return; let root: string | undefined; let bin = ""; let state = ""; const seed = (type: string | null, data = "") => writeFileSync(state, JSON.stringify({ type, data })); const executable = (name: string, source: string) => { const path = join(bin, name); writeFileSync(path, `#!${process.execPath}\n${source}\n`); chmodSync(path, 0o755); }; pi.on("session_start", () => { ready = false; if (!root) { root = mkdtempSync(join(tmpdir(), "pi-clipboard-fixture-")); bin = join(root, "bin"); state = join(root, "clipboard.json"); process.env["PATH"] = `${bin}${delimiter}${process.env["PATH"] ?? ""}`; } mkdirSync(bin, { recursive: true }); executable( "wl-copy", `const fs = require('node:fs'); const chunks=[]; process.stdin.on('data', c => chunks.push(c)); process.stdin.on('end', () => fs.writeFileSync(${JSON.stringify(state)}, JSON.stringify({type:'text/plain;charset=utf-8',data:Buffer.concat(chunks).toString('base64')})));`, ); executable( "wl-paste", `const fs = require('node:fs'); const value=JSON.parse(fs.readFileSync(${JSON.stringify(state)},'utf8')); if(!value.type){ process.stderr.write('Nothing is copied\\n'); process.exit(1); } if(process.argv.includes('--list-types')) process.stdout.write(value.type+'\\n'); else process.stdout.write(Buffer.from(value.data,'base64'));`, ); seed( "text/plain;charset=utf-8", Buffer.from("Clipboard fixture ready").toString("base64"), ); process.env["WAYLAND_DISPLAY"] = "clipboard-fixture"; process.env["XDG_SESSION_TYPE"] = "wayland"; process.env["DISPLAY"] = ""; // Prevent the SDK's remote-copy fallback from touching the user's terminal clipboard. process.env["SSH_CONNECTION"] = ""; process.env["SSH_CLIENT"] = ""; process.env["MOSH_CONNECTION"] = ""; process.env["PI_CLIPBOARD_OSC52"] = "0"; ready = true; }); pi.registerCommand("fixture-image", { description: "Seed the isolated clipboard with a one-pixel PNG", handler: async (_args, ctx) => { seed( "image/png", "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aX1cAAAAASUVORK5CYII=", ); ctx.ui.notify("Isolated clipboard: PNG image", "info"); }, }); pi.registerCommand("fixture-empty", { description: "Clear the isolated clipboard", handler: async (_args, ctx) => { seed(null); ctx.ui.notify("Isolated clipboard: empty", "info"); }, }); pi.registerCommand("fixture-large", { description: "Seed an oversized isolated clipboard", handler: async (_args, ctx) => { seed("text/plain", Buffer.from("x".repeat(32769)).toString("base64")); ctx.ui.notify("Isolated clipboard: oversized text", "info"); }, }); pi.on("session_shutdown", () => { ready = false; if (root) rmSync(root, { recursive: true, force: true }); }); }