-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbase-cli-executor.js
More file actions
340 lines (286 loc) · 9.71 KB
/
base-cli-executor.js
File metadata and controls
340 lines (286 loc) · 9.71 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
const core = require('@actions/core');
const { spawn } = require('child_process');
const { promisify } = require('util');
const fs = require('fs');
const path = require('path');
/**
* Base class for CLI executors
* Implements common logic for running AI CLI tools with named pipes
* Uses template method pattern - subclasses override specific methods
*/
class BaseCLIExecutor {
constructor() {
this.tempDir = process.env.RUNNER_TEMP || '/tmp';
this.targetRepoDir = process.env.GITHUB_WORKSPACE || process.cwd();
}
/**
* Get CLI-specific command name (must be overridden)
* @returns {string} Command name (e.g., 'claude', 'q')
*/
getCommandName() {
throw new Error('getCommandName() must be implemented by subclass');
}
/**
* Get CLI-specific command arguments (must be overridden)
* @returns {Array<string>} Command arguments
*/
getCommandArgs() {
throw new Error('getCommandArgs() must be implemented by subclass');
}
/**
* Get CLI-specific environment variables (must be overridden)
* @returns {Object} Environment variables
*/
getEnvironmentVariables() {
return { ...process.env };
}
/**
* Setup CLI-specific configuration (can be overridden)
* @returns {Promise<string|null>} Configuration file path or null
*/
async setupConfiguration() {
// Default: no setup needed
return null;
}
/**
* Parse CLI output into final result (must be overridden)
* @param {string} output Raw CLI output
* @returns {string} Parsed result
*/
parseOutput(output) {
throw new Error('parseOutput() must be implemented by subclass');
}
/**
* Get the working directory for CLI execution
* @returns {string} Working directory path
*/
getWorkingDirectory() {
return this.targetRepoDir;
}
/**
* Test if CLI command is available
* @returns {Promise<boolean>} True if CLI is available
*/
async testCLIAvailable() {
const { execSync } = require('child_process');
const commandName = this.getCommandName();
try {
execSync(`${commandName} --help`, {
encoding: 'utf8',
timeout: 10000,
stdio: 'pipe'
});
return true;
} catch (error) {
throw new Error(`${commandName} CLI not found in PATH`);
}
}
/**
* Create named pipe for large prompt input
* @param {string} pipePath Path to named pipe
*/
async createNamedPipe(pipePath) {
const execAsync = promisify(require('child_process').exec);
try {
await execAsync(`rm -f "${pipePath}"`);
} catch (e) {
// Ignore if file doesn't exist
}
await execAsync(`mkfifo "${pipePath}"`);
}
/**
* Write prompt to temporary file
* @param {string} promptContent Prompt content
* @param {string} tempPromptFile Path to temp file
*/
writePromptToFile(promptContent, tempPromptFile) {
fs.writeFileSync(tempPromptFile, promptContent);
}
/**
* Start process to stream prompt file to pipe
* @param {string} tempPromptFile Path to temp file
* @param {string} pipePath Path to named pipe
* @returns {{catProcess: ChildProcess, pipeWriteStream: WriteStream}}
*/
startPromptStreaming(tempPromptFile, pipePath) {
const catProcess = spawn('cat', [tempPromptFile], {
stdio: ['ignore', 'pipe', 'inherit'],
});
const pipeWriteStream = fs.createWriteStream(pipePath);
catProcess.stdout.pipe(pipeWriteStream);
catProcess.on('error', (error) => {
core.error(`Error reading prompt file: ${error.message}`);
pipeWriteStream.destroy();
});
return { catProcess, pipeWriteStream };
}
/**
* Spawn CLI process with provided configuration
* @param {string} command CLI command name
* @param {Array<string>} args Command arguments
* @param {Object} env Environment variables
* @param {string} cwd Working directory
* @returns {ChildProcess} Spawned process
*/
spawnCLIProcess(command, args, env, cwd) {
return spawn(command, args, {
stdio: ['pipe', 'pipe', 'pipe'], // Capture stderr for output
cwd: cwd,
env: env
});
}
/**
* Pipe named pipe to CLI process stdin
* @param {string} pipePath Path to named pipe
* @param {ChildProcess} cliProcess CLI process
* @returns {ChildProcess} Pipe read process
*/
connectPipeToCLI(pipePath, cliProcess) {
const pipeReadProcess = spawn('cat', [pipePath]);
pipeReadProcess.stdout.pipe(cliProcess.stdin);
pipeReadProcess.on('error', (error) => {
core.error(`Error reading from named pipe: ${error.message}`);
cliProcess.kill('SIGTERM');
});
return pipeReadProcess;
}
/**
* Capture CLI process output
* @param {ChildProcess} cliProcess CLI process
* @returns {Promise<string>} Captured output
*/
captureOutput(cliProcess) {
return new Promise((resolve, reject) => {
let stdoutData = '';
let stderrData = '';
// Capture stdout
cliProcess.stdout.on('data', (data) => {
const text = data.toString();
// Allow subclasses to customize output handling
if (this.onOutputData) {
this.onOutputData(text);
} else {
process.stdout.write(text);
}
stdoutData += text;
});
// Capture stderr
cliProcess.stderr.on('data', (data) => {
const text = data.toString();
process.stderr.write(text); // Still show in workflow logs
stderrData += text;
});
cliProcess.stdout.on('error', (error) => {
core.error(`Error reading ${this.getCommandName()} stdout: ${error.message}`);
reject(error);
});
cliProcess.stderr.on('error', (error) => {
core.error(`Error reading ${this.getCommandName()} stderr: ${error.message}`);
reject(error);
});
cliProcess.on('close', (code) => {
// Log final captured sizes
core.debug(`[SUMMARY] Total stdout: ${stdoutData.length} chars, Total stderr: ${stderrData.length} chars`);
// Prefer stdout if it has content (structured output), otherwise use stderr
// Amazon Q CLI may write to either depending on version and mode
const output = stdoutData || stderrData;
core.debug(`[SUMMARY] Using ${stdoutData ? 'stdout' : 'stderr'} as output source`);
if (this.onRawOutput) {
this.onRawOutput(output);
}
resolve({ output, exitCode: code || 0 });
});
cliProcess.on('error', (error) => {
core.error(`${this.getCommandName()} process error: ${error.message}`);
reject(error);
});
});
}
/**
* Cleanup processes and files
* @param {Array<ChildProcess>} processes Processes to kill
* @param {Array<string>} files Files to delete
*/
async cleanup(processes, files) {
const execAsync = promisify(require('child_process').exec);
// Cleanup processes
for (const proc of processes) {
try {
if (proc && proc.kill) {
proc.kill('SIGTERM');
}
} catch (e) {
// Process may already be dead
}
}
// Cleanup files
for (const file of files) {
try {
if (file.includes('pipe')) {
await execAsync(`rm -f "${file}"`);
} else if (fs.existsSync(file)) {
fs.unlinkSync(file);
}
} catch (e) {
// Ignore cleanup errors
}
}
}
/**
* Main execution method - template method pattern
* Orchestrates the entire CLI execution workflow
* @param {string} promptContent Prompt content to execute
* @returns {Promise<string>} Execution result
*/
async execute(promptContent) {
const commandName = this.getCommandName();
try {
core.info(`Running ${commandName} CLI investigation...`);
// Test if CLI is available
await this.testCLIAvailable();
// Setup CLI-specific configuration
const configPath = await this.setupConfiguration();
// Prepare file paths
const tempPromptFile = path.join(this.tempDir, `${commandName}-prompt.txt`);
const pipePath = path.join(this.tempDir, `${commandName}_prompt_pipe`);
// Write prompt to file
this.writePromptToFile(promptContent, tempPromptFile);
// Create named pipe
await this.createNamedPipe(pipePath);
// Start streaming prompt to pipe
const { catProcess, pipeWriteStream } = this.startPromptStreaming(tempPromptFile, pipePath);
// Get command args and env
const args = this.getCommandArgs();
const env = this.getEnvironmentVariables();
// Spawn CLI process
const cliProcess = this.spawnCLIProcess(commandName, args, env, this.targetRepoDir);
// Handle CLI process errors
cliProcess.on('error', (error) => {
core.error(`Error spawning ${commandName} process: ${error.message}`);
pipeWriteStream.destroy();
throw error;
});
// Connect pipe to CLI stdin
const pipeReadProcess = this.connectPipeToCLI(pipePath, cliProcess);
// Capture output and wait for completion (NO TIMEOUT)
const { output, exitCode } = await this.captureOutput(cliProcess);
// Cleanup processes and files
const filesToClean = [pipePath, tempPromptFile];
if (configPath) {
filesToClean.push(configPath);
}
await this.cleanup([catProcess, pipeReadProcess], filesToClean);
// Check exit code and parse output
if (exitCode === 0) {
core.info(`${commandName} CLI completed successfully`);
const result = this.parseOutput(output.trim());
return result || 'Investigation completed, but no output was generated.';
} else {
throw new Error(`${commandName} CLI exited with code ${exitCode}`);
}
} catch (error) {
throw new Error(`${commandName} CLI execution failed: ${error.message}`);
}
}
}
module.exports = { BaseCLIExecutor };