| 653 | } |
| 654 | |
| 655 | export const resolveWindowsAgentCandidates = ( |
| 656 | cmd: string, |
| 657 | env: NodeJS.ProcessEnv = process.env, |
| 658 | nodeExecPath = process.execPath |
| 659 | ): AgentCommand[] => { |
| 660 | const rawPathExt = (env.PATHEXT ?? ".COM;.EXE;.BAT;.CMD") |
| 661 | .split(";") |
| 662 | .map(ext => ext.trim().toLowerCase()) |
| 663 | .filter(ext => winRunnableExts.has(ext)) |
| 664 | const exts = rawPathExt.length ? rawPathExt : [".exe", ".com", ".cmd", ".bat"] |
| 665 | const lowerCmd = cmd.toLowerCase() |
| 666 | const hasExt = [...winRunnableExts].some(ext => lowerCmd.endsWith(ext)) |
| 667 | const paths: string[] = [] |
| 668 | if (cmd.includes("/") || cmd.includes("\\")) { |
| 669 | const base = resolve(cmd) |
| 670 | if (hasExt) paths.push(base) |
| 671 | else for (const ext of exts) paths.push(`${base}${ext}`) |
| 672 | } else { |
| 673 | const pathEntries = (env.PATH ?? env.Path ?? "") |
| 674 | .split(";") |
| 675 | .map(entry => entry.trim().replace(/^"(.*)"$/, "$1")) |
| 676 | .filter(Boolean) |
| 677 | for (const dir of pathEntries) { |
| 678 | if (hasExt) paths.push(join(dir, cmd)) |
| 679 | else for (const ext of exts) paths.push(join(dir, `${cmd}${ext}`)) |
| 680 | } |
| 681 | } |
| 682 | const commands: AgentCommand[] = [] |
| 683 | for (const candidate of paths) { |
| 684 | if (!statSync(candidate, { throwIfNoEntry: false })?.isFile()) continue |
| 685 | if (/\.(?:cmd|bat)$/i.test(candidate)) { |
| 686 | const unwrapped = unwrapWindowsCmdShim(candidate, nodeExecPath) |
| 687 | if (unwrapped) commands.push(unwrapped) |
| 688 | } else commands.push({ command: candidate, args: [] }) |
| 689 | } |
| 690 | return commands |
| 691 | } |
| 692 | |
| 693 | const findAgentBinary = (preferred?: string): AgentCommand | undefined => { |
| 694 | const candidates = preferred ? [preferred] : ["claude", "codex"] |