import * as fs from "node:fs"; import * as path from "node:path"; export function getPiInvocation(args: string[]): { command: string; args: string[]; } { const currentScript = process.argv[1]; const isBunVirtualScript = currentScript?.startsWith("/$bunfs/root/"); if ( currentScript && !isBunVirtualScript && fs.existsSync(currentScript) && isPiEntrypoint(currentScript) ) { return { command: process.execPath, args: [currentScript, ...args] }; } const execName = path.basename(process.execPath).toLowerCase(); const isGenericRuntime = /^(node|bun)(\.exe)?$/.test(execName); if (!isGenericRuntime) { return { command: process.execPath, args }; } return { command: "pi", args }; } function isPiEntrypoint(scriptPath: string): boolean { const scriptName = path.basename(scriptPath).toLowerCase(); return ( /^pi(\.(cjs|js|mjs|ts))?$/.test(scriptName) || (scriptName === "cli.js" && scriptPath.includes("pi-coding-agent")) ); }