/** * Runtime detection footer segment producer. * * Detects project language runtime from config files and emits * a footer:segment with zone "workspace". */ import type { ExtensionAPI } from "@mariozechner/pi-coding-agent"; import { execFile } from "node:child_process"; import { existsSync, readdirSync } from "node:fs"; import { join } from "node:path"; import { promisify } from "node:util"; const execFileAsync = promisify(execFile); const VERSION_TIMEOUT_MS = 2500; type RuntimeInfo = { name: string; symbol: string; version?: string }; type Candidate = { name: string; symbol: string; detect: (cwd: string, entries: string[]) => boolean; version: (cwd: string) => Promise }; const hasFile = (cwd: string, names: string[]) => names.some((n) => existsSync(join(cwd, n))); const hasEntry = (entries: string[], names: string[]) => names.some((n) => entries.includes(n)); async function runVer(cmd: string, args: string[] = [], cwd?: string): Promise { try { const { stdout, stderr } = await execFileAsync(cmd, args, { cwd, timeout: VERSION_TIMEOUT_MS }); const text = `${stdout}\n${stderr}`.trim(); return text || undefined; } catch { return undefined; } } function pv(v?: string) { return v?.startsWith("v") ? v : v ? `v${v}` : undefined; } const runtimes: Candidate[] = [ { name: "bun", symbol: "", detect: (c) => hasFile(c, ["bun.lock", "bun.lockb"]), version: async () => pv(await runVer("bun", ["--version"])) }, { name: "deno", symbol: "", detect: (c) => hasFile(c, ["deno.json", "deno.jsonc", "deno.lock"]), version: async () => pv((await runVer("deno", ["--version"]))?.match(/deno\s+([0-9][^\s]*)/i)?.[1]) }, { name: "lua", symbol: "", detect: (c, e) => hasFile(c, ["stylua.toml", ".stylua.toml", ".luarc.json", ".luarc.jsonc", "init.lua"]) || hasEntry(e, ["lua"]) || e.some((x) => x.endsWith(".lua")), version: async () => pv((await runVer("lua", ["-v"]))?.match(/Lua\s+([0-9][^\s]*)/i)?.[1] ?? (await runVer("luajit", ["-v"]))?.match(/LuaJIT\s+([0-9][^\s]*)/i)?.[1]) }, { name: "nodejs", symbol: "", detect: (c) => hasFile(c, ["package.json", ".nvmrc", ".node-version"]), version: async () => pv(await runVer("node", ["--version"])) }, { name: "python", symbol: "", detect: (c) => hasFile(c, ["pyproject.toml", "requirements.txt", "setup.py", "setup.cfg", "Pipfile", ".python-version"]), version: async () => pv((await runVer("python3", ["--version"]))?.match(/Python\s+([0-9][^\s]*)/i)?.[1] ?? (await runVer("python", ["--version"]))?.match(/Python\s+([0-9][^\s]*)/i)?.[1]) }, { name: "golang", symbol: "", detect: (c) => hasFile(c, ["go.mod"]), version: async () => pv((await runVer("go", ["version"]))?.match(/go version go([0-9][^\s]*)/i)?.[1]) }, { name: "rust", symbol: "󱘗", detect: (c) => hasFile(c, ["Cargo.toml"]), version: async () => pv((await runVer("rustc", ["--version"]))?.match(/rustc\s+([0-9][^\s]*)/i)?.[1]) }, { name: "java", symbol: "", detect: (c) => hasFile(c, ["pom.xml", "build.gradle", "build.gradle.kts"]), version: async () => pv((await runVer("java", ["-version"]))?.match(/"([0-9][^"]*)"/)?.[1]) }, { name: "ruby", symbol: "", detect: (c) => hasFile(c, ["Gemfile", ".ruby-version"]), version: async () => pv((await runVer("ruby", ["--version"]))?.match(/ruby\s+([0-9][^\s]*)/i)?.[1]) }, { name: "php", symbol: "", detect: (c) => hasFile(c, ["composer.json"]), version: async () => pv((await runVer("php", ["--version"]))?.match(/PHP\s+([0-9][^\s]*)/i)?.[1]) }, ]; const RUNTIME_COLORS: Record = { nodejs: "success", deno: "syntaxType", golang: "syntaxType", bun: "warning", python: "warning", java: "warning", rust: "error", ruby: "error", lua: "accent", php: "accent", }; async function detect(cwd: string): Promise { let entries: string[] = []; try { entries = readdirSync(cwd); } catch { /* not a dir */ } for (const rt of runtimes) { if (rt.detect(cwd, entries)) { return { name: rt.name, symbol: rt.symbol, version: await rt.version(cwd) }; } } return undefined; } export default function runtimeExtension(pi: ExtensionAPI) { let currentCwd = ""; let hasUI = false; let refreshInFlight = false; const refresh = async () => { if (!hasUI || refreshInFlight) return; refreshInFlight = true; try { const rt = await detect(currentCwd); if (rt) { const label = rt.version ? `${rt.symbol} ${rt.version}` : rt.symbol; pi.events.emit("footer:segment", { id: "runtime", text: label, color: RUNTIME_COLORS[rt.name] ?? "text", zone: "workspace", order: 3 }); } else { pi.events.emit("footer:segment", { id: "runtime", text: undefined }); } } finally { refreshInFlight = false; } }; pi.on("session_start", async (_e, ctx) => { hasUI = ctx.hasUI; currentCwd = ctx.cwd; refresh(); }); pi.on("agent_end", async () => { if (hasUI) refresh(); }); }