-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathlaunch.js
More file actions
206 lines (183 loc) · 6.16 KB
/
launch.js
File metadata and controls
206 lines (183 loc) · 6.16 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// SPDX-License-Identifier: MIT
// Copyright (c) PromptKit Contributors
// Detects LLM CLIs on PATH and launches bootstrap sessions.
const { execFileSync, spawn } = require("child_process");
const fs = require("fs");
const path = require("path");
const os = require("os");
function pathDirs() {
return (process.env.PATH || "").split(path.delimiter).filter(Boolean);
}
function windowsPathExts() {
return (process.env.PATHEXT || ".EXE;.COM;.BAT;.CMD")
.split(";")
.map((e) => e.toLowerCase());
}
function isExactFileOnPath(fileName) {
for (const dir of pathDirs()) {
try {
fs.accessSync(path.join(dir, fileName), fs.constants.F_OK);
return true;
} catch {
// not found in this directory, continue
}
}
return false;
}
function isOnPath(cmd) {
// Search PATH entries directly rather than shelling out to `which`/`where`.
// This avoids requiring `which` to be on PATH itself (important in test
// environments where PATH is restricted to a mock directory).
const exts = process.platform === "win32" ? windowsPathExts() : [""];
// On Windows, X_OK is not meaningful — any file with a matching PATHEXT
// extension is considered executable, so we check for existence (F_OK) only.
const accessFlag = process.platform === "win32" ? fs.constants.F_OK : fs.constants.X_OK;
for (const dir of pathDirs()) {
for (const ext of exts) {
try {
fs.accessSync(path.join(dir, cmd + ext), accessFlag);
return true;
} catch {
// not found in this directory, continue
}
}
}
return false;
}
function resolveSpawnCommand(cmd) {
if (process.platform !== "win32") return cmd;
const shim = `${cmd}.cmd`;
return isExactFileOnPath(shim) ? shim : cmd;
}
function detectCli() {
// Check for GitHub Copilot CLI first (most common)
if (isOnPath("copilot")) return "copilot";
// Check for gh with copilot extension
if (isOnPath("gh")) {
try {
execFileSync("gh", ["copilot", "--help"], { stdio: "ignore" });
return "gh-copilot";
} catch {
// gh exists but no copilot extension
}
}
if (isOnPath("claude")) return "claude";
if (isOnPath("codex")) return "codex";
return null;
}
function copyContentToTemp(contentDir) {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "promptkit-"));
copyDirRecursive(contentDir, tmpDir);
return tmpDir;
}
function copyDirRecursive(src, dest) {
fs.mkdirSync(dest, { recursive: true });
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
if (entry.isDirectory()) {
copyDirRecursive(srcPath, destPath);
} else {
fs.copyFileSync(srcPath, destPath);
}
}
}
function launchInteractive(contentDir, cliName, { dryRun = false } = {}) {
let detected = null;
const cli = cliName || (detected = detectCli());
if (!cli) {
console.error(
"No supported LLM CLI found on PATH.\n\n" +
"Install one of:\n" +
" - GitHub Copilot CLI: gh extension install github/gh-copilot\n" +
" - Claude Code: https://docs.anthropic.com/en/docs/claude-code\n" +
" - OpenAI Codex CLI: https://github.com/openai/codex\n\n" +
"Alternatively, load bootstrap.md in your LLM manually from:\n" +
` ${contentDir}`
);
process.exit(1);
}
// Warn the user when auto-detection chose a fallback CLI
if (!cliName && detected !== "copilot" && detected !== "gh-copilot") {
console.warn(
`Warning: GitHub Copilot CLI not found on PATH. ` +
`Falling back to '${cli}'.\n` +
`To use a specific CLI, pass --cli <name> (e.g., --cli copilot).\n`
);
}
// Preserve the user's working directory before staging content.
const originalCwd = process.cwd();
// Copy content to a temp directory so the LLM can read the files
const tmpDir = copyContentToTemp(contentDir);
console.log(`PromptKit content staged at: ${tmpDir}`);
console.log(`Launching ${cli}...\n`);
// Use an absolute path so the LLM can locate bootstrap.md regardless of
// which directory it treats as its working directory.
const bootstrapPrompt = `Read and execute ${path.join(tmpDir, "bootstrap.md")}`;
let cmd, args;
switch (cli) {
case "copilot":
cmd = resolveSpawnCommand("copilot");
// --add-dir grants file access to the staging directory.
args = ["--add-dir", tmpDir, "-i", bootstrapPrompt];
break;
case "gh-copilot":
cmd = "gh";
args = ["copilot", "--add-dir", tmpDir, "-i", bootstrapPrompt];
break;
case "claude":
// --add-dir grants file access to the staging directory.
cmd = resolveSpawnCommand("claude");
args = ["--add-dir", tmpDir, bootstrapPrompt];
break;
case "codex":
cmd = resolveSpawnCommand("codex");
args = ["--add-dir", tmpDir, bootstrapPrompt];
break;
default:
console.error(`Unknown CLI: ${cli}`);
process.exit(1);
}
// In dry-run mode, print the spawn command and args then exit without
// actually launching the LLM CLI. Useful for CI smoke tests and debugging.
if (dryRun) {
console.log("[DRY RUN] Would spawn:");
console.log(` cmd: ${cmd}`);
console.log(` args: ${JSON.stringify(args)}`);
console.log(` cwd: ${originalCwd}`);
try {
fs.rmSync(tmpDir, { recursive: true, force: true });
} catch {
// best effort cleanup
}
return;
}
// All CLIs are spawned from the user's original directory so the LLM
// session reflects the directory the user was working in.
const child = spawn(cmd, args, {
cwd: originalCwd,
stdio: "inherit",
});
child.on("error", (err) => {
console.error(`Failed to launch ${cli}: ${err.message}`);
try {
fs.rmSync(tmpDir, { recursive: true });
} catch {
// best effort cleanup
}
process.exit(1);
});
child.on("exit", (code, signal) => {
// Clean up temp dir
try {
fs.rmSync(tmpDir, { recursive: true });
} catch {
// best effort cleanup
}
if (signal) {
process.kill(process.pid, signal);
}
process.exit(code || 0);
});
}
module.exports = { detectCli, launchInteractive, copyContentToTemp };