-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent-runner.js
More file actions
361 lines (299 loc) · 10.2 KB
/
Copy pathagent-runner.js
File metadata and controls
361 lines (299 loc) · 10.2 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/usr/bin/env node
import { spawn } from 'child_process';
import { writeFile, mkdir } from 'fs/promises';
import { join } from 'path';
class AgentRunner {
constructor() {
this.agentId = process.env.AGENT_ID;
this.repoUrl = process.env.REPO_URL;
this.featureName = process.env.FEATURE_NAME;
this.branchName = process.env.BRANCH_NAME;
this.llmProvider = process.env.LLM_PROVIDER;
this.llmModel = process.env.LLM_MODEL;
this.cliModel = process.env.CLI_MODEL || this.llmModel;
this.requestPrompt = process.env.REQUEST_PROMPT;
this.githubToken = process.env.GITHUB_TOKEN;
this.timeoutMinutes = parseInt(process.env.TIMEOUT_MINUTES || '15');
this.workspaceDir = '/workspace';
this.resultsDir = `/results/${this.agentId}`;
this.startTime = Date.now();
}
async run() {
try {
console.log(`🤖 Agent ${this.agentId} starting...`);
// Create results directory
await mkdir(this.resultsDir, { recursive: true });
// Clone repository
await this.cloneRepository();
// Generate code using CLI tool
await this.generateCodeWithCLI();
// Get list of changed files
const changedFiles = await this.getChangedFiles();
// Run tests and validation
const testResults = await this.runTests();
const buildStatus = await this.runBuild();
const playwrightResults = await this.runPlaywright();
// Calculate metrics
const metrics = await this.calculateMetrics(changedFiles);
// Commit and push changes
await this.commitAndPush();
// Write results
const result = {
agent_id: this.agentId,
branch_name: this.branchName,
success: true,
changes_made: changedFiles,
test_results: testResults,
build_status: buildStatus,
playwright_results: playwrightResults,
metrics: {
...metrics,
execution_time_ms: Date.now() - this.startTime
}
};
await this.writeResult(result);
console.log(`✅ Agent ${this.agentId} completed successfully`);
} catch (error) {
console.error(`❌ Agent ${this.agentId} failed:`, error);
const result = {
agent_id: this.agentId,
branch_name: this.branchName,
success: false,
changes_made: [],
build_status: 'failed',
metrics: {
files_changed: 0,
lines_added: 0,
lines_removed: 0,
complexity_score: 0,
quality_score: 0,
execution_time_ms: Date.now() - this.startTime
},
error: error.message
};
await this.writeResult(result);
}
}
async cloneRepository() {
return new Promise((resolve, reject) => {
const gitProcess = spawn('git', [
'clone', this.repoUrl, this.workspaceDir
]);
gitProcess.on('close', (code) => {
if (code === 0) {
// Create and checkout branch
const branchProcess = spawn('git', [
'checkout', '-b', this.branchName
], { cwd: this.workspaceDir });
branchProcess.on('close', (branchCode) => {
if (branchCode === 0) {
resolve();
} else {
reject(new Error(`Failed to create branch ${this.branchName}`));
}
});
} else {
reject(new Error('Failed to clone repository'));
}
});
});
}
async generateCodeWithCLI() {
const fullPrompt = `${this.requestPrompt}
Please implement the requested feature by making the necessary code changes. Work directly in the codebase and make all required modifications.`;
let cliCommand;
let cliArgs;
switch (this.llmProvider) {
case 'openai':
cliCommand = 'codex';
cliArgs = [
'--model', this.cliModel,
'--approval-mode', 'full-auto',
'--quiet',
'--prompt', fullPrompt
];
break;
case 'anthropic':
cliCommand = 'claude';
cliArgs = [
'--model', this.cliModel,
'--output-format', 'stream-json',
'-p', fullPrompt
];
break;
case 'google':
cliCommand = 'gemini';
cliArgs = [
'--model', this.cliModel,
'--prompt', fullPrompt
];
break;
default:
throw new Error(`Unsupported LLM provider: ${this.llmProvider}`);
}
return new Promise((resolve, reject) => {
console.log(`Running: ${cliCommand} ${cliArgs.join(' ')}`);
const cliProcess = spawn(cliCommand, cliArgs, {
cwd: this.workspaceDir,
stdio: ['pipe', 'pipe', 'pipe'],
timeout: this.timeoutMinutes * 60 * 1000
});
let stdout = '';
let stderr = '';
cliProcess.stdout?.on('data', (data) => {
stdout += data.toString();
process.stdout.write(data);
});
cliProcess.stderr?.on('data', (data) => {
stderr += data.toString();
process.stderr.write(data);
});
cliProcess.on('close', (code) => {
if (code === 0) {
console.log(`✅ ${cliCommand} completed successfully`);
resolve();
} else {
reject(new Error(`${cliCommand} failed with code ${code}\n${stderr}`));
}
});
cliProcess.on('error', (error) => {
reject(new Error(`Failed to start ${cliCommand}: ${error.message}`));
});
});
}
async getChangedFiles() {
try {
const result = await this.runCommand(['git', 'diff', '--name-only', 'HEAD']);
const untracked = await this.runCommand(['git', 'ls-files', '--others', '--exclude-standard']);
const changed = result.stdout.trim().split('\n').filter(f => f.length > 0);
const untrackedFiles = untracked.stdout.trim().split('\n').filter(f => f.length > 0);
return [...changed, ...untrackedFiles];
} catch (error) {
return [];
}
}
async runTests() {
// Try to detect and run the appropriate test command
const testCommands = ['npm test', 'yarn test', 'pytest', 'go test', 'cargo test'];
for (const command of testCommands) {
try {
const result = await this.runCommand(command.split(' '));
if (result.exitCode === 0) {
return this.parseTestResults(result.stdout);
}
} catch (error) {
// Continue to next test command
}
}
return { passed: 0, failed: 0, total: 0, duration_ms: 0 };
}
async runBuild() {
const buildCommands = ['npm run build', 'yarn build', 'tsc', 'go build', 'cargo build'];
for (const command of buildCommands) {
try {
const result = await this.runCommand(command.split(' '));
if (result.exitCode === 0) {
return 'success';
}
} catch (error) {
// Continue to next build command
}
}
return 'failed';
}
async runPlaywright() {
try {
const result = await this.runCommand(['npx', 'playwright', 'test']);
return this.parsePlaywrightResults(result.stdout);
} catch (error) {
return { passed: 0, failed: 0, total: 0, screenshots: [], duration_ms: 0 };
}
}
async runCommand(command) {
return new Promise((resolve) => {
const process = spawn(command[0], command.slice(1), {
cwd: this.workspaceDir,
stdio: ['pipe', 'pipe', 'pipe']
});
let stdout = '';
let stderr = '';
process.stdout?.on('data', (data) => {
stdout += data.toString();
});
process.stderr?.on('data', (data) => {
stderr += data.toString();
});
process.on('close', (code) => {
resolve({
exitCode: code,
stdout,
stderr
});
});
});
}
parseTestResults(output) {
// Simple parsing - would need to be more sophisticated for different test frameworks
const passed = (output.match(/(\d+) passing/i) || [0, 0])[1];
const failed = (output.match(/(\d+) failing/i) || [0, 0])[1];
return {
passed: parseInt(passed),
failed: parseInt(failed),
total: parseInt(passed) + parseInt(failed),
duration_ms: 0 // Would need to parse timing info
};
}
parsePlaywrightResults(output) {
// Parse Playwright test results
const passed = (output.match(/(\d+) passed/i) || [0, 0])[1];
const failed = (output.match(/(\d+) failed/i) || [0, 0])[1];
return {
passed: parseInt(passed),
failed: parseInt(failed),
total: parseInt(passed) + parseInt(failed),
screenshots: [], // Would need to collect actual screenshots
duration_ms: 0
};
}
async calculateMetrics(changedFiles) {
const metrics = {
files_changed: changedFiles.length,
lines_added: 0,
lines_removed: 0,
complexity_score: 0,
quality_score: 0
};
// Calculate git diff stats
try {
const diffResult = await this.runCommand(['git', 'diff', '--numstat', 'HEAD']);
const lines = diffResult.stdout.trim().split('\n');
for (const line of lines) {
const [added, removed] = line.split('\t');
if (added !== '-') metrics.lines_added += parseInt(added);
if (removed !== '-') metrics.lines_removed += parseInt(removed);
}
} catch (error) {
// If git diff fails, use file count as approximation
metrics.lines_added = changedFiles.length * 10;
}
// Simple quality score based on successful tests and builds
metrics.quality_score = Math.min(100, (metrics.lines_added + metrics.lines_removed) / 10);
return metrics;
}
async commitAndPush() {
// Add changes
await this.runCommand(['git', 'add', '.']);
// Commit changes
const commitMessage = `feat: ${this.featureName}\n\nGenerated by ${this.agentId} agent`;
await this.runCommand(['git', 'commit', '-m', commitMessage]);
// Push to remote
await this.runCommand(['git', 'push', 'origin', this.branchName]);
}
async writeResult(result) {
const resultPath = join(this.resultsDir, 'result.json');
await writeFile(resultPath, JSON.stringify(result, null, 2));
}
}
// Run the agent
const agent = new AgentRunner();
agent.run().catch(console.error);