import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; import { CapsBlinkClient, defaultConnect } from "../src/client.ts"; import { checkPlatformSupport } from "../src/platform.ts"; import { resolveRuntimePaths } from "../src/runtime-paths.ts"; import { packageRootFrom, resolveDaemonScriptPath, spawnDaemon, } from "../src/spawn.ts"; /** * Blink the built-in Caps Lock LED while this Pi session has an active agent. * Machine-wide coordination is via a shared Swift daemon lease socket. */ export default function capsBlinkExtension(pi: ExtensionAPI): void { const support = checkPlatformSupport(); let client: CapsBlinkClient | null = null; let unsupportedWarned = false; const warn = (message: string): void => { console.warn(message); }; pi.on("session_start", (_event, ctx) => { client?.dispose(); client = null; if (!support.ok) { if (!unsupportedWarned) { unsupportedWarned = true; warn(`pi-caps-blink: disabled — ${support.reason}`); } return; } try { const paths = resolveRuntimePaths(); const packageRoot = packageRootFrom(import.meta.url); client = new CapsBlinkClient({ socketPath: paths.socketPath, runtimeDir: paths.runtimeDir, scriptPath: resolveDaemonScriptPath(packageRoot), connect: defaultConnect, spawnDaemon, setTimeout: (fn, ms) => setTimeout(fn, ms), clearTimeout: (id) => clearTimeout(id as NodeJS.Timeout), random: Math.random, now: () => Date.now(), warn, }); } catch (err) { if (!unsupportedWarned) { unsupportedWarned = true; const detail = err instanceof Error ? err.message : String(err); warn(`pi-caps-blink: disabled — ${detail}`); } return; } if (!ctx.isIdle()) { client.setDesiredBusy(true); } }); pi.on("agent_start", () => { client?.setDesiredBusy(true); }); pi.on("agent_settled", (_event, ctx) => { if (ctx.isIdle()) { client?.setDesiredBusy(false); } }); pi.on("session_shutdown", () => { client?.setDesiredBusy(false); client?.dispose(); client = null; }); }