@auto-engineer/server-implementer 0.8.12 ā 0.8.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -1
- package/.turbo/turbo-format.log +5 -0
- package/.turbo/turbo-lint.log +5 -0
- package/.turbo/turbo-test.log +13 -0
- package/.turbo/turbo-type-check.log +4 -0
- package/CHANGELOG.md +16 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +4 -4
- package/src/agent/runAllSlices.js +11 -0
- package/src/agent/runFlows.js +37 -0
- package/src/agent/runSlice.js +320 -0
- package/src/agent/runTests.js +100 -0
- package/src/commands/implement-server.js +100 -0
- package/src/commands/implement-slice.js +283 -0
- package/src/index.js +5 -0
- package/src/prompts/systemPrompt.js +43 -0
- package/src/utils/extractCodeBlock.js +6 -0
package/package.json
CHANGED
|
@@ -17,8 +17,8 @@
|
|
|
17
17
|
"debug": "^4.3.4",
|
|
18
18
|
"fast-glob": "^3.3.3",
|
|
19
19
|
"vite": "^5.4.1",
|
|
20
|
-
"@auto-engineer/
|
|
21
|
-
"@auto-engineer/
|
|
20
|
+
"@auto-engineer/ai-gateway": "0.8.14",
|
|
21
|
+
"@auto-engineer/message-bus": "0.8.14"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@types/fs-extra": "^11.0.4",
|
|
@@ -28,9 +28,9 @@
|
|
|
28
28
|
"glob": "^11.0.3",
|
|
29
29
|
"tsx": "^4.20.3",
|
|
30
30
|
"typescript": "^5.8.3",
|
|
31
|
-
"@auto-engineer/cli": "0.8.
|
|
31
|
+
"@auto-engineer/cli": "0.8.14"
|
|
32
32
|
},
|
|
33
|
-
"version": "0.8.
|
|
33
|
+
"version": "0.8.14",
|
|
34
34
|
"scripts": {
|
|
35
35
|
"build": "tsc && tsx ../../scripts/fix-esm-imports.ts",
|
|
36
36
|
"test": "vitest run --reporter=dot",
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import fg from 'fast-glob';
|
|
2
|
+
import { runSlice } from './runSlice';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
export async function runAllSlices(flowDir) {
|
|
5
|
+
const flowName = path.basename(flowDir);
|
|
6
|
+
const sliceDirs = await fg(`${flowDir}/**/*/`, { onlyDirectories: true });
|
|
7
|
+
for (const sliceDir of sliceDirs) {
|
|
8
|
+
await runSlice(sliceDir, flowName);
|
|
9
|
+
}
|
|
10
|
+
console.log('ā
All slices processed');
|
|
11
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import fg from 'fast-glob';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { runAllSlices } from './runAllSlices';
|
|
4
|
+
import createDebug from 'debug';
|
|
5
|
+
const debug = createDebug('server-impl:flows');
|
|
6
|
+
const debugFlow = createDebug('server-impl:flows:flow');
|
|
7
|
+
export async function runFlows(baseDir) {
|
|
8
|
+
debug('Running flows from base directory: %s', baseDir);
|
|
9
|
+
const flowDirs = await fg('*', {
|
|
10
|
+
cwd: baseDir,
|
|
11
|
+
onlyDirectories: true,
|
|
12
|
+
absolute: true,
|
|
13
|
+
});
|
|
14
|
+
debug('Found %d flow directories', flowDirs.length);
|
|
15
|
+
if (flowDirs.length > 0) {
|
|
16
|
+
debug(
|
|
17
|
+
'Flow directories: %o',
|
|
18
|
+
flowDirs.map((d) => path.basename(d)),
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
console.log(`š Found ${flowDirs.length} flows`);
|
|
22
|
+
for (const flowDir of flowDirs) {
|
|
23
|
+
const flowName = path.basename(flowDir);
|
|
24
|
+
debugFlow('Processing flow: %s', flowName);
|
|
25
|
+
debugFlow(' Path: %s', flowDir);
|
|
26
|
+
console.log(`š Processing flow: ${flowName}`);
|
|
27
|
+
try {
|
|
28
|
+
await runAllSlices(flowDir);
|
|
29
|
+
debugFlow('Flow %s completed successfully', flowName);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
debugFlow('ERROR: Flow %s failed: %O', flowName, error);
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
debug('All %d flows processed successfully', flowDirs.length);
|
|
36
|
+
console.log('ā
All flows processed');
|
|
37
|
+
}
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import fg from 'fast-glob';
|
|
3
|
+
import { generateTextWithAI } from '@auto-engineer/ai-gateway';
|
|
4
|
+
import { readFile, writeFile, access } from 'fs/promises';
|
|
5
|
+
import { execa } from 'execa';
|
|
6
|
+
import { SYSTEM_PROMPT } from '../prompts/systemPrompt';
|
|
7
|
+
import { extractCodeBlock } from '../utils/extractCodeBlock';
|
|
8
|
+
import { runTests } from './runTests';
|
|
9
|
+
export async function runSlice(sliceDir, flow) {
|
|
10
|
+
const sliceName = path.basename(sliceDir);
|
|
11
|
+
console.log(`āļø Implementing slice: ${sliceName} for flow: ${flow}`);
|
|
12
|
+
const contextFiles = await loadContextFiles(sliceDir);
|
|
13
|
+
const filesToImplement = findFilesToImplement(contextFiles);
|
|
14
|
+
for (const [targetFile] of filesToImplement) {
|
|
15
|
+
await implementFileFromAI(sliceDir, targetFile, contextFiles);
|
|
16
|
+
}
|
|
17
|
+
const result = await runTestsAndTypecheck(sliceDir);
|
|
18
|
+
reportTestAndTypecheckResults(sliceDir, flow, result);
|
|
19
|
+
if (result.success) {
|
|
20
|
+
console.log(`ā
All tests and checks passed on first attempt.`);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
await retryFailedFiles(sliceDir, flow, result);
|
|
24
|
+
if (result.failedTestFiles.length > 0) {
|
|
25
|
+
await retryFailedTests(sliceDir, flow, result);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
async function retryFailedFiles(sliceDir, flow, initialResult) {
|
|
29
|
+
let contextFiles = await loadContextFiles(sliceDir);
|
|
30
|
+
let result = initialResult;
|
|
31
|
+
for (let attempt = 1; attempt <= 5; attempt++) {
|
|
32
|
+
if (result.failedTypecheckFiles.length === 0) {
|
|
33
|
+
console.log(`ā
Typecheck issues resolved after attempt ${attempt - 1}`);
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
console.log(`š Typecheck retry attempt ${attempt} for ${result.failedTypecheckFiles.length} files...`);
|
|
37
|
+
contextFiles = await loadContextFiles(sliceDir);
|
|
38
|
+
for (const filePath of result.failedTypecheckFiles) {
|
|
39
|
+
const fileName = path.basename(filePath);
|
|
40
|
+
const retryPrompt = buildRetryPrompt(fileName, contextFiles, result.testErrors, result.typecheckErrors);
|
|
41
|
+
console.log(`š§ Retrying typecheck error in ${fileName} in flow ${flow}...`);
|
|
42
|
+
const aiOutput = await generateTextWithAI(retryPrompt);
|
|
43
|
+
const cleanedCode = extractCodeBlock(aiOutput);
|
|
44
|
+
await writeFile(path.join(sliceDir, fileName), cleanedCode, 'utf-8');
|
|
45
|
+
console.log(`ā»ļø Updated ${fileName} to fix typecheck errors`);
|
|
46
|
+
}
|
|
47
|
+
result = await runTestsAndTypecheck(sliceDir);
|
|
48
|
+
reportTestAndTypecheckResults(sliceDir, flow, result);
|
|
49
|
+
}
|
|
50
|
+
if (result.failedTypecheckFiles.length > 0) {
|
|
51
|
+
console.log(`ā ļø Fixing tests caused typecheck errors. Retrying typecheck fixes...`);
|
|
52
|
+
const typecheckOnlyResult = {
|
|
53
|
+
...result,
|
|
54
|
+
testErrors: '', // Clear test errors since we're only fixing typecheck
|
|
55
|
+
failedTestFiles: [], // Clear failed test files
|
|
56
|
+
};
|
|
57
|
+
result = await retryFailedFiles(sliceDir, path.basename(sliceDir), typecheckOnlyResult);
|
|
58
|
+
// After fixing typecheck, re-run everything to get fresh results
|
|
59
|
+
const freshResult = await runTestsAndTypecheck(sliceDir);
|
|
60
|
+
reportTestAndTypecheckResults(sliceDir, flow, freshResult);
|
|
61
|
+
result = freshResult;
|
|
62
|
+
if (result.failedTestFiles.length === 0) {
|
|
63
|
+
console.log(`ā
All test issues resolved after fixing type errors.`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
async function loadContextFiles(sliceDir) {
|
|
69
|
+
const files = await fg(['*.ts'], { cwd: sliceDir });
|
|
70
|
+
const context = {};
|
|
71
|
+
for (const file of files) {
|
|
72
|
+
const absPath = path.join(sliceDir, file);
|
|
73
|
+
context[file] = await readFile(absPath, 'utf-8');
|
|
74
|
+
}
|
|
75
|
+
return context;
|
|
76
|
+
}
|
|
77
|
+
function findFilesToImplement(contextFiles) {
|
|
78
|
+
return Object.entries(contextFiles).filter(
|
|
79
|
+
([, content]) => content.includes('TODO:') || content.includes('IMPLEMENTATION INSTRUCTIONS'),
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
function buildInitialPrompt(targetFile, context) {
|
|
83
|
+
return `
|
|
84
|
+
${SYSTEM_PROMPT}
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
š Target file to implement: ${targetFile}
|
|
88
|
+
|
|
89
|
+
${context[targetFile]}
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
š§ Other files in the same slice:
|
|
93
|
+
${Object.entries(context)
|
|
94
|
+
.filter(([name]) => name !== targetFile)
|
|
95
|
+
.map(([name, content]) => `// File: ${name}\n${content}`)
|
|
96
|
+
.join('\n\n')}
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
Return only the whole updated file of ${targetFile}. Do not remove existing imports or types that are still referenced or required in the file. The file returned has to be production ready.
|
|
100
|
+
`.trim();
|
|
101
|
+
}
|
|
102
|
+
function buildRetryPrompt(targetFile, context, testErrors, typeErrors) {
|
|
103
|
+
return `
|
|
104
|
+
${SYSTEM_PROMPT}
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
The previous implementation of ${targetFile} caused test or type-check failures.
|
|
108
|
+
|
|
109
|
+
š File to fix: ${targetFile}
|
|
110
|
+
|
|
111
|
+
${context[targetFile]}
|
|
112
|
+
|
|
113
|
+
š§ Other files in the same slice:
|
|
114
|
+
${Object.entries(context)
|
|
115
|
+
.filter(([name]) => name !== targetFile)
|
|
116
|
+
.map(([name, content]) => `// File: ${name}\n${content}`)
|
|
117
|
+
.join('\n\n')}
|
|
118
|
+
|
|
119
|
+
š§Ŗ Test errors:
|
|
120
|
+
${testErrors || 'None'}
|
|
121
|
+
|
|
122
|
+
š Typecheck errors:
|
|
123
|
+
${typeErrors || 'None'}
|
|
124
|
+
|
|
125
|
+
---
|
|
126
|
+
Return only the corrected full contents of ${targetFile}, no commentary, no markdown.
|
|
127
|
+
`.trim();
|
|
128
|
+
}
|
|
129
|
+
async function implementFileFromAI(sliceDir, targetFile, contextFiles) {
|
|
130
|
+
const filePath = path.join(sliceDir, targetFile);
|
|
131
|
+
const prompt = buildInitialPrompt(targetFile, contextFiles);
|
|
132
|
+
console.log(`š® Analysing and Implementing ${targetFile}`);
|
|
133
|
+
const aiOutput = await generateTextWithAI(prompt);
|
|
134
|
+
//console.log('AI output:', aiOutput);
|
|
135
|
+
const cleanedCode = extractCodeBlock(aiOutput);
|
|
136
|
+
await writeFile(filePath, cleanedCode, 'utf-8');
|
|
137
|
+
console.log(`ā» Implemented ${targetFile}`);
|
|
138
|
+
}
|
|
139
|
+
export async function runTestsAndTypecheck(sliceDir) {
|
|
140
|
+
const rootDir = await findProjectRoot(sliceDir);
|
|
141
|
+
const testResult = await runTests(sliceDir, rootDir);
|
|
142
|
+
const typecheckResult = await runTypecheck(sliceDir, rootDir);
|
|
143
|
+
return {
|
|
144
|
+
success: testResult.success && typecheckResult.success,
|
|
145
|
+
failedTestFiles: testResult.failedTestFiles,
|
|
146
|
+
failedTypecheckFiles: typecheckResult.failedTypecheckFiles,
|
|
147
|
+
testErrors: testResult.testErrors,
|
|
148
|
+
typecheckErrors: typecheckResult.typecheckErrors,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
async function retryFailedTests(sliceDir, flow, result) {
|
|
152
|
+
let contextFiles = await loadContextFiles(sliceDir);
|
|
153
|
+
for (let attempt = 1; attempt <= 5; attempt++) {
|
|
154
|
+
if (result.failedTestFiles.length === 0) {
|
|
155
|
+
console.log(`ā
Test failures resolved after attempt ${attempt - 1}`);
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
console.log(`š Test retry attempt ${attempt} for ${result.failedTestFiles.length} files...`);
|
|
159
|
+
const smartPrompt = `
|
|
160
|
+
${SYSTEM_PROMPT}
|
|
161
|
+
---
|
|
162
|
+
š§Ŗ The current implementation has test failures.
|
|
163
|
+
|
|
164
|
+
š Test errors:
|
|
165
|
+
${result.testErrors || 'None'}
|
|
166
|
+
|
|
167
|
+
š§ Full slice context:
|
|
168
|
+
${Object.entries(contextFiles)
|
|
169
|
+
.map(([name, content]) => `// File: ${name}\n${content}`)
|
|
170
|
+
.join('\n\n')}
|
|
171
|
+
|
|
172
|
+
---
|
|
173
|
+
Please return the full corrected content of a single file (not a test file) that should be updated to fix the failing tests.
|
|
174
|
+
|
|
175
|
+
Use this format:
|
|
176
|
+
\`\`\`ts
|
|
177
|
+
// File: <fileName>.ts
|
|
178
|
+
<corrected code>
|
|
179
|
+
\`\`\`
|
|
180
|
+
|
|
181
|
+
No commentary or markdown outside the code block.
|
|
182
|
+
`.trim();
|
|
183
|
+
console.log('š® Asking AI to suggest a fix for test failures...');
|
|
184
|
+
const aiOutput = await generateTextWithAI(smartPrompt);
|
|
185
|
+
const cleaned = extractCodeBlock(aiOutput);
|
|
186
|
+
const match = cleaned.match(/^\/\/ File: (.+?)\n([\s\S]*)/m);
|
|
187
|
+
if (!match) {
|
|
188
|
+
console.warn(`ā ļø Skipping retry. AI output didn't match expected format.`);
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
const [, fileName, code] = match;
|
|
192
|
+
const absPath = path.join(sliceDir, fileName.trim());
|
|
193
|
+
console.log('š§ Applying AI fix to:', absPath);
|
|
194
|
+
await writeFile(absPath, code.trim(), 'utf-8');
|
|
195
|
+
console.log(`ā»ļø Updated ${fileName.trim()} to fix tests`);
|
|
196
|
+
contextFiles = await loadContextFiles(sliceDir);
|
|
197
|
+
result = await runTestsAndTypecheck(sliceDir);
|
|
198
|
+
reportTestAndTypecheckResults(sliceDir, flow, result);
|
|
199
|
+
// If test fix introduced a new type error, handle it before continuing
|
|
200
|
+
if (result.failedTypecheckFiles.length > 0) {
|
|
201
|
+
console.log(`ā ļø Fixing tests caused typecheck errors. Retrying typecheck fixes...`);
|
|
202
|
+
result = await retryFailedFiles(sliceDir, flow, result);
|
|
203
|
+
if (result.failedTestFiles.length === 0) {
|
|
204
|
+
console.log(`ā
All test issues resolved after fixing type errors.`);
|
|
205
|
+
break;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
contextFiles = await loadContextFiles(sliceDir);
|
|
209
|
+
}
|
|
210
|
+
if (result.failedTestFiles.length > 0) {
|
|
211
|
+
console.error(`ā Some test failures remain after retry attempts.`);
|
|
212
|
+
for (const file of result.failedTestFiles) {
|
|
213
|
+
console.log(` - ${path.relative(sliceDir, file)}`);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
async function runTypecheck(sliceDir, rootDir) {
|
|
218
|
+
try {
|
|
219
|
+
const result = await execa('npx', ['tsc', '--noEmit'], {
|
|
220
|
+
cwd: rootDir,
|
|
221
|
+
stdio: 'pipe',
|
|
222
|
+
reject: false,
|
|
223
|
+
});
|
|
224
|
+
const output = (result.stdout ?? '') + (result.stderr ?? '');
|
|
225
|
+
if (result.exitCode !== 0 || output.includes('error')) {
|
|
226
|
+
return await processTypecheckOutput(output, sliceDir, rootDir);
|
|
227
|
+
}
|
|
228
|
+
return { success: true, typecheckErrors: '', failedTypecheckFiles: [] };
|
|
229
|
+
} catch (err) {
|
|
230
|
+
const execaErr = err;
|
|
231
|
+
const output = (execaErr.stdout ?? '') + (execaErr.stderr ?? '');
|
|
232
|
+
console.error('TypeScript execution error:', output);
|
|
233
|
+
const files = await fg(['*.ts'], { cwd: sliceDir, absolute: true });
|
|
234
|
+
return { success: false, typecheckErrors: output, failedTypecheckFiles: files };
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
function getTypecheckPatterns() {
|
|
238
|
+
return [
|
|
239
|
+
/^([^:]+\.ts)\(\d+,\d+\): error/gm,
|
|
240
|
+
/error TS\d+: (.+) '([^']+\.ts)'/gm,
|
|
241
|
+
/^([^:]+\.ts):\d+:\d+\s+-\s+error/gm,
|
|
242
|
+
];
|
|
243
|
+
}
|
|
244
|
+
function extractFailedFiles(output, patterns, rootDir, sliceDir) {
|
|
245
|
+
const failedFiles = new Set();
|
|
246
|
+
for (const pattern of patterns) {
|
|
247
|
+
for (const match of output.matchAll(pattern)) {
|
|
248
|
+
const filePath = match[1] ? path.resolve(rootDir, match[1]) : '';
|
|
249
|
+
const notNodeModules = !filePath.includes('node_modules');
|
|
250
|
+
const inSlice = sliceDir === undefined || filePath.startsWith(sliceDir);
|
|
251
|
+
if (notNodeModules && inSlice) {
|
|
252
|
+
failedFiles.add(filePath);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return Array.from(failedFiles);
|
|
257
|
+
}
|
|
258
|
+
async function processTypecheckOutput(output, sliceDir, rootDir) {
|
|
259
|
+
const relativePath = path.relative(rootDir, sliceDir);
|
|
260
|
+
const filtered = output
|
|
261
|
+
.split('\n')
|
|
262
|
+
.filter((line) => {
|
|
263
|
+
const hasError = line.includes('error TS') || line.includes('): error');
|
|
264
|
+
const notNodeModules = !line.includes('node_modules');
|
|
265
|
+
const hasSlicePath = line.includes(relativePath) || line.includes(sliceDir);
|
|
266
|
+
return hasError && notNodeModules && hasSlicePath;
|
|
267
|
+
})
|
|
268
|
+
.join('\n');
|
|
269
|
+
if (filtered.trim() === '') {
|
|
270
|
+
return { success: true, typecheckErrors: '', failedTypecheckFiles: [] };
|
|
271
|
+
}
|
|
272
|
+
const failedFiles = await processTypecheckFailure(filtered, rootDir, sliceDir);
|
|
273
|
+
return {
|
|
274
|
+
success: false,
|
|
275
|
+
typecheckErrors: filtered,
|
|
276
|
+
failedTypecheckFiles: failedFiles,
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
async function processTypecheckFailure(output, rootDir, sliceDir) {
|
|
280
|
+
const patterns = getTypecheckPatterns();
|
|
281
|
+
let failed = extractFailedFiles(output, patterns, rootDir, sliceDir);
|
|
282
|
+
if (failed.length === 0 && output.includes('error')) {
|
|
283
|
+
failed = await fg(['*.ts'], { cwd: sliceDir, absolute: true });
|
|
284
|
+
}
|
|
285
|
+
return failed;
|
|
286
|
+
}
|
|
287
|
+
async function findProjectRoot(startDir) {
|
|
288
|
+
let dir = startDir;
|
|
289
|
+
while (dir !== path.dirname(dir)) {
|
|
290
|
+
try {
|
|
291
|
+
await access(path.join(dir, 'package.json'));
|
|
292
|
+
return dir;
|
|
293
|
+
} catch {
|
|
294
|
+
dir = path.dirname(dir);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
throw new Error('ā Could not find project root');
|
|
298
|
+
}
|
|
299
|
+
function reportTestAndTypecheckResults(sliceDir, flow, result) {
|
|
300
|
+
const sliceName = path.basename(sliceDir);
|
|
301
|
+
if (result.success) {
|
|
302
|
+
console.log(`ā
All Tests and checks passed for: ${sliceName} in flow ${flow}`);
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
console.error(`ā ${sliceName} in floe ${flow} failed tests or type-checks.`);
|
|
306
|
+
if (result.failedTestFiles.length) {
|
|
307
|
+
const files = result.failedTestFiles.map((f) => path.relative(sliceDir, f));
|
|
308
|
+
console.log(`š§Ŗ Failed test files: ${files.join(', ')}`);
|
|
309
|
+
if (result.testErrors) {
|
|
310
|
+
console.log(`š Test errors:\n${result.testErrors}`);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
if (result.failedTypecheckFiles.length) {
|
|
314
|
+
const files = result.failedTypecheckFiles.map((f) => path.relative(sliceDir, f));
|
|
315
|
+
console.log(`š Failed typecheck files: ${files.join(', ')}`);
|
|
316
|
+
if (result.typecheckErrors) {
|
|
317
|
+
console.log(`š Typecheck errors:\n${result.typecheckErrors}`);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import fg from 'fast-glob';
|
|
3
|
+
import { execa } from 'execa';
|
|
4
|
+
import { existsSync } from 'fs';
|
|
5
|
+
import { unlink } from 'fs/promises';
|
|
6
|
+
import { readFile } from 'fs/promises';
|
|
7
|
+
// eslint-disable-next-line complexity
|
|
8
|
+
export async function runTests(sliceDir, rootDir) {
|
|
9
|
+
console.log(`š Running tests in ${path.basename(sliceDir)}...`);
|
|
10
|
+
const testFiles = await fg(['*.spec.ts', '*.specs.ts'], { cwd: sliceDir });
|
|
11
|
+
if (testFiles.length === 0) {
|
|
12
|
+
console.warn(`ā ļø No test files found in ${sliceDir}`);
|
|
13
|
+
return { success: true, testErrors: '', failedTestFiles: [] };
|
|
14
|
+
} else {
|
|
15
|
+
console.log(`š Found test files in ${path.basename(sliceDir)}:`, testFiles);
|
|
16
|
+
}
|
|
17
|
+
const relativePaths = testFiles.map((f) => path.join(sliceDir, f).replace(`${rootDir}/`, ''));
|
|
18
|
+
const reportPath = path.join(sliceDir, 'vitest-results.json');
|
|
19
|
+
try {
|
|
20
|
+
const { stdout, stderr } = await execa(
|
|
21
|
+
'npx',
|
|
22
|
+
['vitest', 'run', '--reporter=json', `--outputFile=${reportPath}`, ...relativePaths],
|
|
23
|
+
{
|
|
24
|
+
cwd: rootDir,
|
|
25
|
+
stdio: 'pipe',
|
|
26
|
+
reject: false,
|
|
27
|
+
},
|
|
28
|
+
);
|
|
29
|
+
// Wait a bit for the file to be written
|
|
30
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
31
|
+
// Check if the report file exists and read it
|
|
32
|
+
if (existsSync(reportPath)) {
|
|
33
|
+
try {
|
|
34
|
+
const reportRaw = await readFile(reportPath, 'utf-8');
|
|
35
|
+
const report = JSON.parse(reportRaw);
|
|
36
|
+
const testResults = report.testResults ?? [];
|
|
37
|
+
const failedFiles = testResults
|
|
38
|
+
.filter((r) => r.status === 'failed')
|
|
39
|
+
.map((r) => {
|
|
40
|
+
const fileName = r.name ?? r.file;
|
|
41
|
+
if (!fileName) {
|
|
42
|
+
console.warn('ā ļø Skipping test result with missing name or file:', r);
|
|
43
|
+
return path.join(sliceDir, 'unknown');
|
|
44
|
+
}
|
|
45
|
+
return path.join(sliceDir, path.basename(fileName));
|
|
46
|
+
});
|
|
47
|
+
const failedTestSummaries = testResults
|
|
48
|
+
.filter((r) => r.status === 'failed')
|
|
49
|
+
.map((r) => {
|
|
50
|
+
const fileName = path.basename(r.name ?? r.file ?? 'unknown');
|
|
51
|
+
if (r.assertionResults.length > 0) {
|
|
52
|
+
const lines = r.assertionResults
|
|
53
|
+
.filter((a) => a.status === 'failed')
|
|
54
|
+
.map((a) => `ā ${a.fullName}\n${a.failureMessages?.join('\n') ?? ''}`);
|
|
55
|
+
return `š ${fileName}\n${lines.join('\n')}`;
|
|
56
|
+
}
|
|
57
|
+
// fallback: use top-level message
|
|
58
|
+
if ('message' in r && typeof r.message === 'string' && r.message.trim() !== '') {
|
|
59
|
+
return `š ${fileName}\n${r.message}`;
|
|
60
|
+
}
|
|
61
|
+
return `š ${fileName}\nā ļø Test suite failed but no assertion or error message found.`;
|
|
62
|
+
})
|
|
63
|
+
.join('\n\n');
|
|
64
|
+
return {
|
|
65
|
+
success: failedFiles.length === 0,
|
|
66
|
+
testErrors: failedTestSummaries || stdout || stderr || 'Tests failed but no error details available',
|
|
67
|
+
failedTestFiles: failedFiles,
|
|
68
|
+
};
|
|
69
|
+
} catch (parseErr) {
|
|
70
|
+
console.error('Failed to parse test results:', parseErr);
|
|
71
|
+
return {
|
|
72
|
+
success: false,
|
|
73
|
+
testErrors: stdout || stderr || 'Failed to parse test results',
|
|
74
|
+
failedTestFiles: testFiles.map((f) => path.join(sliceDir, f)),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
} else {
|
|
78
|
+
// No report file, use stdout/stderr
|
|
79
|
+
return {
|
|
80
|
+
success: false,
|
|
81
|
+
testErrors: stdout || stderr || 'Test execution failed - no report generated',
|
|
82
|
+
failedTestFiles: testFiles.map((f) => path.join(sliceDir, f)),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
} catch (err) {
|
|
86
|
+
const execaErr = err;
|
|
87
|
+
const output = (execaErr.stdout ?? '') + (execaErr.stderr ?? '');
|
|
88
|
+
console.error('Test execution error:', output);
|
|
89
|
+
return {
|
|
90
|
+
success: false,
|
|
91
|
+
testErrors: output || 'Test execution failed with no output',
|
|
92
|
+
failedTestFiles: testFiles.map((f) => path.join(sliceDir, f)),
|
|
93
|
+
};
|
|
94
|
+
} finally {
|
|
95
|
+
// Clean up the report file
|
|
96
|
+
if (existsSync(reportPath)) {
|
|
97
|
+
await unlink(reportPath).catch(() => {});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { defineCommandHandler } from '@auto-engineer/message-bus';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { existsSync } from 'fs';
|
|
4
|
+
import { runFlows } from '../agent/runFlows';
|
|
5
|
+
import createDebug from 'debug';
|
|
6
|
+
const debug = createDebug('server-impl:command');
|
|
7
|
+
const debugHandler = createDebug('server-impl:command:handler');
|
|
8
|
+
const debugProcess = createDebug('server-impl:command:process');
|
|
9
|
+
const debugResult = createDebug('server-impl:command:result');
|
|
10
|
+
export const commandHandler = defineCommandHandler({
|
|
11
|
+
name: 'ImplementServer',
|
|
12
|
+
alias: 'implement:server',
|
|
13
|
+
description: 'AI implements server TODOs and tests',
|
|
14
|
+
category: 'implement',
|
|
15
|
+
fields: {
|
|
16
|
+
serverDirectory: {
|
|
17
|
+
description: 'Server directory path',
|
|
18
|
+
required: true,
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
examples: ['$ auto implement:server --server-directory=./server'],
|
|
22
|
+
handle: async (command) => {
|
|
23
|
+
debug('CommandHandler executing for ImplementServer');
|
|
24
|
+
const result = await handleImplementServerCommandInternal(command);
|
|
25
|
+
if (result.type === 'ServerImplemented') {
|
|
26
|
+
debug('Command handler completed: success');
|
|
27
|
+
debug('ā
Server implementation completed successfully');
|
|
28
|
+
if (result.data.flowsImplemented > 0) {
|
|
29
|
+
debug(' %d flows implemented', result.data.flowsImplemented);
|
|
30
|
+
}
|
|
31
|
+
} else {
|
|
32
|
+
debug('Command handler completed: failure - %s', result.data.error);
|
|
33
|
+
debug('ā Server implementation failed: %s', result.data.error);
|
|
34
|
+
}
|
|
35
|
+
return result;
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
async function handleImplementServerCommandInternal(command) {
|
|
39
|
+
const { serverDirectory } = command.data;
|
|
40
|
+
debug('Handling ImplementServerCommand');
|
|
41
|
+
debug(' Server directory: %s', serverDirectory);
|
|
42
|
+
debug(' Request ID: %s', command.requestId);
|
|
43
|
+
debug(' Correlation ID: %s', command.correlationId ?? 'none');
|
|
44
|
+
try {
|
|
45
|
+
const serverRoot = path.resolve(serverDirectory);
|
|
46
|
+
const flowsDir = path.join(serverRoot, 'src', 'domain', 'flows');
|
|
47
|
+
debugHandler('Resolved paths:');
|
|
48
|
+
debugHandler(' Server root: %s', serverRoot);
|
|
49
|
+
debugHandler(' Flows directory: %s', flowsDir);
|
|
50
|
+
if (!existsSync(flowsDir)) {
|
|
51
|
+
debugHandler('ERROR: Flows directory not found at %s', flowsDir);
|
|
52
|
+
return {
|
|
53
|
+
type: 'ServerImplementationFailed',
|
|
54
|
+
data: {
|
|
55
|
+
serverDirectory,
|
|
56
|
+
error: `Flows directory not found at: ${flowsDir}`,
|
|
57
|
+
},
|
|
58
|
+
timestamp: new Date(),
|
|
59
|
+
requestId: command.requestId,
|
|
60
|
+
correlationId: command.correlationId,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
debugProcess('Starting flow runner for directory: %s', flowsDir);
|
|
64
|
+
// Run the flows directly without spawning
|
|
65
|
+
await runFlows(flowsDir);
|
|
66
|
+
debugProcess('Flow runner completed successfully');
|
|
67
|
+
// For now, we don't have a way to count flows, so we'll use 0
|
|
68
|
+
// This could be enhanced by having runFlows return statistics
|
|
69
|
+
const flowsImplemented = 0;
|
|
70
|
+
debugResult('Process succeeded');
|
|
71
|
+
debugResult('Flows implemented: %d', flowsImplemented);
|
|
72
|
+
const successEvent = {
|
|
73
|
+
type: 'ServerImplemented',
|
|
74
|
+
data: {
|
|
75
|
+
serverDirectory,
|
|
76
|
+
flowsImplemented,
|
|
77
|
+
},
|
|
78
|
+
timestamp: new Date(),
|
|
79
|
+
requestId: command.requestId,
|
|
80
|
+
correlationId: command.correlationId,
|
|
81
|
+
};
|
|
82
|
+
debugResult('Returning success event: ServerImplemented');
|
|
83
|
+
return successEvent;
|
|
84
|
+
} catch (error) {
|
|
85
|
+
debug('ERROR: Exception caught: %O', error);
|
|
86
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
|
|
87
|
+
return {
|
|
88
|
+
type: 'ServerImplementationFailed',
|
|
89
|
+
data: {
|
|
90
|
+
serverDirectory,
|
|
91
|
+
error: errorMessage,
|
|
92
|
+
},
|
|
93
|
+
timestamp: new Date(),
|
|
94
|
+
requestId: command.requestId,
|
|
95
|
+
correlationId: command.correlationId,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
// Default export is the command handler
|
|
100
|
+
export default commandHandler;
|