-
Notifications
You must be signed in to change notification settings - Fork 452
Expand file tree
/
Copy pathpi-spawn.ts
More file actions
163 lines (146 loc) · 4.59 KB
/
Copy pathpi-spawn.ts
File metadata and controls
163 lines (146 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import * as fs from "node:fs";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
export const PI_CODING_AGENT_PACKAGE = "@earendil-works/pi-coding-agent";
export const PI_SUBAGENT_PI_BINARY_ENV = "PI_SUBAGENT_PI_BINARY";
export function findPiPackageRootFromEntry(
entryPoint: string,
): string | undefined {
let dir = path.dirname(entryPoint);
while (dir !== path.dirname(dir)) {
const packageJsonPath = path.join(dir, "package.json");
if (fs.existsSync(packageJsonPath)) {
const pkg = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8")) as {
name?: unknown;
};
if (pkg.name === PI_CODING_AGENT_PACKAGE) return dir;
}
dir = path.dirname(dir);
}
return undefined;
}
export function resolveInstalledPiPackageRoot(): string | undefined {
return findPiPackageRootFromEntry(
fileURLToPath(import.meta.resolve(PI_CODING_AGENT_PACKAGE)),
);
}
export function resolvePiPackageRoot(): string | undefined {
try {
const entry = process.argv[1];
return entry
? findPiPackageRootFromEntry(fs.realpathSync(entry))
: undefined;
} catch {
// process.argv[1] probing is best-effort; callers can fall back to PATH/package resolution.
return undefined;
}
}
export interface PiSpawnDeps {
platform?: NodeJS.Platform;
execPath?: string;
argv1?: string;
existsSync?: (filePath: string) => boolean;
realpathSync?: (filePath: string) => string;
readFileSync?: (filePath: string, encoding: "utf-8") => string;
resolvePackageJson?: () => string;
resolvePackageEntry?: () => string;
piPackageRoot?: string;
env?: NodeJS.ProcessEnv;
}
interface PiSpawnCommand {
command: string;
args: string[];
}
function isRunnableNodeScript(
filePath: string,
existsSync: (filePath: string) => boolean,
): boolean {
if (!existsSync(filePath)) return false;
return /\.(?:mjs|cjs|js)$/i.test(filePath);
}
function normalizePath(filePath: string): string {
return path.isAbsolute(filePath) ? filePath : path.resolve(filePath);
}
function isStandalonePiExecutable(execPath: string): boolean {
const executableName = execPath.split(/[\\/]/).pop();
return /^pi(?:\.exe)?$/i.test(executableName ?? "");
}
export function resolvePiCliScript(
deps: PiSpawnDeps = {},
): string | undefined {
const existsSync = deps.existsSync ?? fs.existsSync;
const realpathSync = deps.realpathSync ?? fs.realpathSync;
const readFileSync =
deps.readFileSync ??
((filePath, encoding) => fs.readFileSync(filePath, encoding));
const argv1 = deps.argv1 ?? process.argv[1];
if (argv1) {
const argvPath = normalizePath(argv1);
if (isRunnableNodeScript(argvPath, existsSync)) {
try {
const canonicalArgvPath = realpathSync(argvPath);
if (isRunnableNodeScript(canonicalArgvPath, existsSync) && findPiPackageRootFromEntry(canonicalArgvPath)) {
return canonicalArgvPath;
}
} catch {
// Host package metadata is untrusted here; keep resolving the installed Pi CLI.
}
}
}
try {
const resolvePackageJson =
deps.resolvePackageJson ??
(() => {
const root = deps.piPackageRoot ?? resolvePiPackageRoot();
if (root) return path.join(root, "package.json");
const packageRoot = deps.resolvePackageEntry
? findPiPackageRootFromEntry(deps.resolvePackageEntry())
: resolveInstalledPiPackageRoot();
if (!packageRoot)
throw new Error(
`Could not resolve ${PI_CODING_AGENT_PACKAGE} package root`,
);
return path.join(packageRoot, "package.json");
});
const packageJsonPath = resolvePackageJson();
const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8")) as {
bin?: string | Record<string, string>;
};
const binField = packageJson.bin;
const binPath =
typeof binField === "string"
? binField
: (binField?.pi ?? Object.values(binField ?? {})[0]);
if (!binPath) return undefined;
const candidate = path.resolve(path.dirname(packageJsonPath), binPath);
if (isRunnableNodeScript(candidate, existsSync)) {
return candidate;
}
} catch {
// Verified CLI resolution is optional; falling back to `pi` lets PATH handle execution.
return undefined;
}
return undefined;
}
export function getPiSpawnCommand(
args: string[],
deps: PiSpawnDeps = {},
): PiSpawnCommand {
const env = deps.env ?? process.env;
const piBinary = env[PI_SUBAGENT_PI_BINARY_ENV]?.trim();
if (piBinary) {
return { command: piBinary, args };
}
const execPath = deps.execPath ?? process.execPath;
if (isStandalonePiExecutable(execPath)) {
return { command: execPath, args };
}
const piCliPath = resolvePiCliScript(deps);
if (piCliPath) {
return {
command: execPath,
args: [piCliPath, ...args],
};
}
return { command: "pi", args };
}