@auto-engineer/server-implementer 0.4.2 → 0.4.3
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 +22 -14
- package/.turbo/turbo-lint.log +5 -4
- package/.turbo/turbo-test.log +12 -14
- package/.turbo/turbo-type-check.log +4 -5
- package/CHANGELOG.md +8 -0
- package/package.json +2 -2
- package/src/agent/runAllSlices.js +11 -0
- package/src/agent/runFlows.js +16 -0
- package/src/agent/runSlice.js +328 -0
- package/src/agent/runTests.js +100 -0
- package/src/cli/index.js +21 -0
- package/src/prompts/systemPrompt.js +43 -0
- package/src/utils/extractCodeBlock.js +6 -0
package/.turbo/turbo-build.log
CHANGED
package/.turbo/turbo-format.log
CHANGED
|
@@ -1,14 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
>
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
src/agent/
|
|
9
|
-
src/agent/
|
|
10
|
-
src/agent/
|
|
11
|
-
src/
|
|
12
|
-
src/
|
|
13
|
-
src/
|
|
14
|
-
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
> @auto-engineer/server-implementer@0.4.2 format /Users/sam/WebstormProjects/top/auto-engineer/packages/server-implementer
|
|
4
|
+
> prettier --write "**/*.{js,ts,json,md,yml,yaml}" --ignore-path ../../.prettierignore
|
|
5
|
+
|
|
6
|
+
CHANGELOG.md[2K[1G[90mCHANGELOG.md[39m 22ms (unchanged)
|
|
7
|
+
package.json[2K[1G[90mpackage.json[39m 16ms (unchanged)
|
|
8
|
+
src/agent/runAllSlices.js[2K[1G[90msrc/agent/runAllSlices.js[39m 30ms (unchanged)
|
|
9
|
+
src/agent/runAllSlices.ts[2K[1G[90msrc/agent/runAllSlices.ts[39m 39ms (unchanged)
|
|
10
|
+
src/agent/runFlows.js[2K[1G[90msrc/agent/runFlows.js[39m 3ms (unchanged)
|
|
11
|
+
src/agent/runFlows.ts[2K[1G[90msrc/agent/runFlows.ts[39m 3ms (unchanged)
|
|
12
|
+
src/agent/runSlice.js[2K[1G[90msrc/agent/runSlice.js[39m 29ms (unchanged)
|
|
13
|
+
src/agent/runSlice.ts[2K[1G[90msrc/agent/runSlice.ts[39m 30ms (unchanged)
|
|
14
|
+
src/agent/runTests.js[2K[1G[90msrc/agent/runTests.js[39m 6ms (unchanged)
|
|
15
|
+
src/agent/runTests.ts[2K[1G[90msrc/agent/runTests.ts[39m 7ms (unchanged)
|
|
16
|
+
src/cli/index.js[2K[1G[90msrc/cli/index.js[39m 1ms (unchanged)
|
|
17
|
+
src/cli/index.ts[2K[1G[90msrc/cli/index.ts[39m 2ms (unchanged)
|
|
18
|
+
src/prompts/systemPrompt.js[2K[1G[90msrc/prompts/systemPrompt.js[39m 1ms (unchanged)
|
|
19
|
+
src/prompts/systemPrompt.ts[2K[1G[90msrc/prompts/systemPrompt.ts[39m 2ms (unchanged)
|
|
20
|
+
src/utils/extractCodeBlock.js[2K[1G[90msrc/utils/extractCodeBlock.js[39m 1ms (unchanged)
|
|
21
|
+
src/utils/extractCodeBlock.ts[2K[1G[90msrc/utils/extractCodeBlock.ts[39m 1ms (unchanged)
|
|
22
|
+
tsconfig.json[2K[1G[90mtsconfig.json[39m 1ms (unchanged)
|
package/.turbo/turbo-lint.log
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
>
|
|
4
|
-
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
> @auto-engineer/server-implementer@0.4.2 lint /Users/sam/WebstormProjects/top/auto-engineer/packages/server-implementer
|
|
4
|
+
> eslint 'src/**/*.ts' --max-warnings 0 --config ../../eslint.config.ts
|
|
5
|
+
|
package/.turbo/turbo-test.log
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
>
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
[31m[39m
|
|
14
|
-
[?25h
|
|
1
|
+
|
|
2
|
+
> @auto-engineer/server-implementer@0.4.3 test /Users/sam/WebstormProjects/top/auto-engineer/packages/server-implementer
|
|
3
|
+
> vitest run
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
RUN v3.2.4 /Users/sam/WebstormProjects/top/auto-engineer/packages/server-implementer
|
|
7
|
+
|
|
8
|
+
No test files found, exiting with code 0
|
|
9
|
+
|
|
10
|
+
include: **/*.specs.{js,ts}
|
|
11
|
+
exclude: **/.tmp/**, **/node_modules/**, **/dist/**
|
|
12
|
+
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
>
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
|
|
2
|
+
> @auto-engineer/server-implementer@0.4.3 type-check /Users/sam/WebstormProjects/top/auto-engineer/packages/server-implementer
|
|
3
|
+
> tsc --noEmit
|
|
4
|
+
|
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@auto-engineer/server-implementer",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"access": "public"
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"@auto-engineer/ai-gateway": "^0.4.
|
|
11
|
+
"@auto-engineer/ai-gateway": "^0.4.3",
|
|
12
12
|
"@modelcontextprotocol/sdk": "^1.3.0",
|
|
13
13
|
"fast-glob": "^3.3.3",
|
|
14
14
|
"vite": "^5.4.1"
|
|
@@ -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,16 @@
|
|
|
1
|
+
import fg from 'fast-glob';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { runAllSlices } from './runAllSlices';
|
|
4
|
+
export async function runFlows(baseDir) {
|
|
5
|
+
const flowDirs = await fg('*', {
|
|
6
|
+
cwd: baseDir,
|
|
7
|
+
onlyDirectories: true,
|
|
8
|
+
absolute: true,
|
|
9
|
+
});
|
|
10
|
+
console.log(`🚀 Found ${flowDirs.length} flows`);
|
|
11
|
+
for (const flowDir of flowDirs) {
|
|
12
|
+
console.log(`📂 Processing flow: ${path.basename(flowDir)}`);
|
|
13
|
+
await runAllSlices(flowDir);
|
|
14
|
+
}
|
|
15
|
+
console.log('✅ All flows processed');
|
|
16
|
+
}
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import fg from 'fast-glob';
|
|
3
|
+
import { generateTextWithAI, getAvailableProviders } 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
|
+
const availableProviders = getAvailableProviders();
|
|
10
|
+
const AI_PROVIDER = availableProviders[0];
|
|
11
|
+
if (availableProviders.length === 0) {
|
|
12
|
+
console.error(
|
|
13
|
+
'❌ No AI providers configured. Please set one of: OPENAI_API_KEY, ANTHROPIC_API_KEY, GOOGLE_GENERATIVE_AI_API_KEY, or XAI_API_KEY',
|
|
14
|
+
);
|
|
15
|
+
process.exit(1);
|
|
16
|
+
}
|
|
17
|
+
export async function runSlice(sliceDir, flow) {
|
|
18
|
+
const sliceName = path.basename(sliceDir);
|
|
19
|
+
console.log(`✏️ Implementing slice: ${sliceName} for flow: ${flow}`);
|
|
20
|
+
const contextFiles = await loadContextFiles(sliceDir);
|
|
21
|
+
const filesToImplement = findFilesToImplement(contextFiles);
|
|
22
|
+
for (const [targetFile] of filesToImplement) {
|
|
23
|
+
await implementFileFromAI(sliceDir, targetFile, contextFiles);
|
|
24
|
+
}
|
|
25
|
+
const result = await runTestsAndTypecheck(sliceDir);
|
|
26
|
+
reportTestAndTypecheckResults(sliceDir, flow, result);
|
|
27
|
+
if (result.success) {
|
|
28
|
+
console.log(`✅ All tests and checks passed on first attempt.`);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
await retryFailedFiles(sliceDir, flow, result);
|
|
32
|
+
if (result.failedTestFiles.length > 0) {
|
|
33
|
+
await retryFailedTests(sliceDir, flow, result);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
async function retryFailedFiles(sliceDir, flow, initialResult) {
|
|
37
|
+
let contextFiles = await loadContextFiles(sliceDir);
|
|
38
|
+
let result = initialResult;
|
|
39
|
+
for (let attempt = 1; attempt <= 5; attempt++) {
|
|
40
|
+
if (result.failedTypecheckFiles.length === 0) {
|
|
41
|
+
console.log(`✅ Typecheck issues resolved after attempt ${attempt - 1}`);
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
console.log(`🔁 Typecheck retry attempt ${attempt} for ${result.failedTypecheckFiles.length} files...`);
|
|
45
|
+
contextFiles = await loadContextFiles(sliceDir);
|
|
46
|
+
for (const filePath of result.failedTypecheckFiles) {
|
|
47
|
+
const fileName = path.basename(filePath);
|
|
48
|
+
const retryPrompt = buildRetryPrompt(fileName, contextFiles, result.testErrors, result.typecheckErrors);
|
|
49
|
+
console.log(`🔧 Retrying typecheck error in ${fileName} in flow ${flow}...`);
|
|
50
|
+
const aiOutput = await generateTextWithAI(retryPrompt, AI_PROVIDER);
|
|
51
|
+
const cleanedCode = extractCodeBlock(aiOutput);
|
|
52
|
+
await writeFile(path.join(sliceDir, fileName), cleanedCode, 'utf-8');
|
|
53
|
+
console.log(`♻️ Updated ${fileName} to fix typecheck errors`);
|
|
54
|
+
}
|
|
55
|
+
result = await runTestsAndTypecheck(sliceDir);
|
|
56
|
+
reportTestAndTypecheckResults(sliceDir, flow, result);
|
|
57
|
+
}
|
|
58
|
+
if (result.failedTypecheckFiles.length > 0) {
|
|
59
|
+
console.log(`⚠️ Fixing tests caused typecheck errors. Retrying typecheck fixes...`);
|
|
60
|
+
const typecheckOnlyResult = {
|
|
61
|
+
...result,
|
|
62
|
+
testErrors: '', // Clear test errors since we're only fixing typecheck
|
|
63
|
+
failedTestFiles: [], // Clear failed test files
|
|
64
|
+
};
|
|
65
|
+
result = await retryFailedFiles(sliceDir, path.basename(sliceDir), typecheckOnlyResult);
|
|
66
|
+
// After fixing typecheck, re-run everything to get fresh results
|
|
67
|
+
const freshResult = await runTestsAndTypecheck(sliceDir);
|
|
68
|
+
reportTestAndTypecheckResults(sliceDir, flow, freshResult);
|
|
69
|
+
result = freshResult;
|
|
70
|
+
if (result.failedTestFiles.length === 0) {
|
|
71
|
+
console.log(`✅ All test issues resolved after fixing type errors.`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
async function loadContextFiles(sliceDir) {
|
|
77
|
+
const files = await fg(['*.ts'], { cwd: sliceDir });
|
|
78
|
+
const context = {};
|
|
79
|
+
for (const file of files) {
|
|
80
|
+
const absPath = path.join(sliceDir, file);
|
|
81
|
+
context[file] = await readFile(absPath, 'utf-8');
|
|
82
|
+
}
|
|
83
|
+
return context;
|
|
84
|
+
}
|
|
85
|
+
function findFilesToImplement(contextFiles) {
|
|
86
|
+
return Object.entries(contextFiles).filter(
|
|
87
|
+
([, content]) => content.includes('TODO:') || content.includes('IMPLEMENTATION INSTRUCTIONS'),
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
function buildInitialPrompt(targetFile, context) {
|
|
91
|
+
return `
|
|
92
|
+
${SYSTEM_PROMPT}
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
📄 Target file to implement: ${targetFile}
|
|
96
|
+
|
|
97
|
+
${context[targetFile]}
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
🧠 Other files in the same slice:
|
|
101
|
+
${Object.entries(context)
|
|
102
|
+
.filter(([name]) => name !== targetFile)
|
|
103
|
+
.map(([name, content]) => `// File: ${name}\n${content}`)
|
|
104
|
+
.join('\n\n')}
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
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.
|
|
108
|
+
`.trim();
|
|
109
|
+
}
|
|
110
|
+
function buildRetryPrompt(targetFile, context, testErrors, typeErrors) {
|
|
111
|
+
return `
|
|
112
|
+
${SYSTEM_PROMPT}
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
The previous implementation of ${targetFile} caused test or type-check failures.
|
|
116
|
+
|
|
117
|
+
📄 File to fix: ${targetFile}
|
|
118
|
+
|
|
119
|
+
${context[targetFile]}
|
|
120
|
+
|
|
121
|
+
🧠 Other files in the same slice:
|
|
122
|
+
${Object.entries(context)
|
|
123
|
+
.filter(([name]) => name !== targetFile)
|
|
124
|
+
.map(([name, content]) => `// File: ${name}\n${content}`)
|
|
125
|
+
.join('\n\n')}
|
|
126
|
+
|
|
127
|
+
🧪 Test errors:
|
|
128
|
+
${testErrors || 'None'}
|
|
129
|
+
|
|
130
|
+
📐 Typecheck errors:
|
|
131
|
+
${typeErrors || 'None'}
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
Return only the corrected full contents of ${targetFile}, no commentary, no markdown.
|
|
135
|
+
`.trim();
|
|
136
|
+
}
|
|
137
|
+
async function implementFileFromAI(sliceDir, targetFile, contextFiles) {
|
|
138
|
+
const filePath = path.join(sliceDir, targetFile);
|
|
139
|
+
const prompt = buildInitialPrompt(targetFile, contextFiles);
|
|
140
|
+
console.log(`🔮 Analysing and Implementing ${targetFile}`);
|
|
141
|
+
const aiOutput = await generateTextWithAI(prompt, AI_PROVIDER);
|
|
142
|
+
//console.log('AI output:', aiOutput);
|
|
143
|
+
const cleanedCode = extractCodeBlock(aiOutput);
|
|
144
|
+
await writeFile(filePath, cleanedCode, 'utf-8');
|
|
145
|
+
console.log(`♻ Implemented ${targetFile}`);
|
|
146
|
+
}
|
|
147
|
+
export async function runTestsAndTypecheck(sliceDir) {
|
|
148
|
+
const rootDir = await findProjectRoot(sliceDir);
|
|
149
|
+
const testResult = await runTests(sliceDir, rootDir);
|
|
150
|
+
const typecheckResult = await runTypecheck(sliceDir, rootDir);
|
|
151
|
+
return {
|
|
152
|
+
success: testResult.success && typecheckResult.success,
|
|
153
|
+
failedTestFiles: testResult.failedTestFiles,
|
|
154
|
+
failedTypecheckFiles: typecheckResult.failedTypecheckFiles,
|
|
155
|
+
testErrors: testResult.testErrors,
|
|
156
|
+
typecheckErrors: typecheckResult.typecheckErrors,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
async function retryFailedTests(sliceDir, flow, result) {
|
|
160
|
+
let contextFiles = await loadContextFiles(sliceDir);
|
|
161
|
+
for (let attempt = 1; attempt <= 5; attempt++) {
|
|
162
|
+
if (result.failedTestFiles.length === 0) {
|
|
163
|
+
console.log(`✅ Test failures resolved after attempt ${attempt - 1}`);
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
console.log(`🔁 Test retry attempt ${attempt} for ${result.failedTestFiles.length} files...`);
|
|
167
|
+
const smartPrompt = `
|
|
168
|
+
${SYSTEM_PROMPT}
|
|
169
|
+
---
|
|
170
|
+
🧪 The current implementation has test failures.
|
|
171
|
+
|
|
172
|
+
📄 Test errors:
|
|
173
|
+
${result.testErrors || 'None'}
|
|
174
|
+
|
|
175
|
+
🧠 Full slice context:
|
|
176
|
+
${Object.entries(contextFiles)
|
|
177
|
+
.map(([name, content]) => `// File: ${name}\n${content}`)
|
|
178
|
+
.join('\n\n')}
|
|
179
|
+
|
|
180
|
+
---
|
|
181
|
+
Please return the full corrected content of a single file (not a test file) that should be updated to fix the failing tests.
|
|
182
|
+
|
|
183
|
+
Use this format:
|
|
184
|
+
\`\`\`ts
|
|
185
|
+
// File: <fileName>.ts
|
|
186
|
+
<corrected code>
|
|
187
|
+
\`\`\`
|
|
188
|
+
|
|
189
|
+
No commentary or markdown outside the code block.
|
|
190
|
+
`.trim();
|
|
191
|
+
console.log('🔮 Asking AI to suggest a fix for test failures...');
|
|
192
|
+
const aiOutput = await generateTextWithAI(smartPrompt, AI_PROVIDER);
|
|
193
|
+
const cleaned = extractCodeBlock(aiOutput);
|
|
194
|
+
const match = cleaned.match(/^\/\/ File: (.+?)\n([\s\S]*)/m);
|
|
195
|
+
if (!match) {
|
|
196
|
+
console.warn(`⚠️ Skipping retry. AI output didn't match expected format.`);
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
const [, fileName, code] = match;
|
|
200
|
+
const absPath = path.join(sliceDir, fileName.trim());
|
|
201
|
+
console.log('🔧 Applying AI fix to:', absPath);
|
|
202
|
+
await writeFile(absPath, code.trim(), 'utf-8');
|
|
203
|
+
console.log(`♻️ Updated ${fileName.trim()} to fix tests`);
|
|
204
|
+
contextFiles = await loadContextFiles(sliceDir);
|
|
205
|
+
result = await runTestsAndTypecheck(sliceDir);
|
|
206
|
+
reportTestAndTypecheckResults(sliceDir, flow, result);
|
|
207
|
+
// If test fix introduced a new type error, handle it before continuing
|
|
208
|
+
if (result.failedTypecheckFiles.length > 0) {
|
|
209
|
+
console.log(`⚠️ Fixing tests caused typecheck errors. Retrying typecheck fixes...`);
|
|
210
|
+
result = await retryFailedFiles(sliceDir, flow, result);
|
|
211
|
+
if (result.failedTestFiles.length === 0) {
|
|
212
|
+
console.log(`✅ All test issues resolved after fixing type errors.`);
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
contextFiles = await loadContextFiles(sliceDir);
|
|
217
|
+
}
|
|
218
|
+
if (result.failedTestFiles.length > 0) {
|
|
219
|
+
console.error(`❌ Some test failures remain after retry attempts.`);
|
|
220
|
+
for (const file of result.failedTestFiles) {
|
|
221
|
+
console.log(` - ${path.relative(sliceDir, file)}`);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
async function runTypecheck(sliceDir, rootDir) {
|
|
226
|
+
try {
|
|
227
|
+
const result = await execa('npx', ['tsc', '--noEmit'], {
|
|
228
|
+
cwd: rootDir,
|
|
229
|
+
stdio: 'pipe',
|
|
230
|
+
reject: false,
|
|
231
|
+
});
|
|
232
|
+
const output = (result.stdout ?? '') + (result.stderr ?? '');
|
|
233
|
+
if (result.exitCode !== 0 || output.includes('error')) {
|
|
234
|
+
return await processTypecheckOutput(output, sliceDir, rootDir);
|
|
235
|
+
}
|
|
236
|
+
return { success: true, typecheckErrors: '', failedTypecheckFiles: [] };
|
|
237
|
+
} catch (err) {
|
|
238
|
+
const execaErr = err;
|
|
239
|
+
const output = (execaErr.stdout ?? '') + (execaErr.stderr ?? '');
|
|
240
|
+
console.error('TypeScript execution error:', output);
|
|
241
|
+
const files = await fg(['*.ts'], { cwd: sliceDir, absolute: true });
|
|
242
|
+
return { success: false, typecheckErrors: output, failedTypecheckFiles: files };
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
function getTypecheckPatterns() {
|
|
246
|
+
return [
|
|
247
|
+
/^([^:]+\.ts)\(\d+,\d+\): error/gm,
|
|
248
|
+
/error TS\d+: (.+) '([^']+\.ts)'/gm,
|
|
249
|
+
/^([^:]+\.ts):\d+:\d+\s+-\s+error/gm,
|
|
250
|
+
];
|
|
251
|
+
}
|
|
252
|
+
function extractFailedFiles(output, patterns, rootDir, sliceDir) {
|
|
253
|
+
const failedFiles = new Set();
|
|
254
|
+
for (const pattern of patterns) {
|
|
255
|
+
for (const match of output.matchAll(pattern)) {
|
|
256
|
+
const filePath = match[1] ? path.resolve(rootDir, match[1]) : '';
|
|
257
|
+
const notNodeModules = !filePath.includes('node_modules');
|
|
258
|
+
const inSlice = sliceDir === undefined || filePath.startsWith(sliceDir);
|
|
259
|
+
if (notNodeModules && inSlice) {
|
|
260
|
+
failedFiles.add(filePath);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return Array.from(failedFiles);
|
|
265
|
+
}
|
|
266
|
+
async function processTypecheckOutput(output, sliceDir, rootDir) {
|
|
267
|
+
const relativePath = path.relative(rootDir, sliceDir);
|
|
268
|
+
const filtered = output
|
|
269
|
+
.split('\n')
|
|
270
|
+
.filter((line) => {
|
|
271
|
+
const hasError = line.includes('error TS') || line.includes('): error');
|
|
272
|
+
const notNodeModules = !line.includes('node_modules');
|
|
273
|
+
const hasSlicePath = line.includes(relativePath) || line.includes(sliceDir);
|
|
274
|
+
return hasError && notNodeModules && hasSlicePath;
|
|
275
|
+
})
|
|
276
|
+
.join('\n');
|
|
277
|
+
if (filtered.trim() === '') {
|
|
278
|
+
return { success: true, typecheckErrors: '', failedTypecheckFiles: [] };
|
|
279
|
+
}
|
|
280
|
+
const failedFiles = await processTypecheckFailure(filtered, rootDir, sliceDir);
|
|
281
|
+
return {
|
|
282
|
+
success: false,
|
|
283
|
+
typecheckErrors: filtered,
|
|
284
|
+
failedTypecheckFiles: failedFiles,
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
async function processTypecheckFailure(output, rootDir, sliceDir) {
|
|
288
|
+
const patterns = getTypecheckPatterns();
|
|
289
|
+
let failed = extractFailedFiles(output, patterns, rootDir, sliceDir);
|
|
290
|
+
if (failed.length === 0 && output.includes('error')) {
|
|
291
|
+
failed = await fg(['*.ts'], { cwd: sliceDir, absolute: true });
|
|
292
|
+
}
|
|
293
|
+
return failed;
|
|
294
|
+
}
|
|
295
|
+
async function findProjectRoot(startDir) {
|
|
296
|
+
let dir = startDir;
|
|
297
|
+
while (dir !== path.dirname(dir)) {
|
|
298
|
+
try {
|
|
299
|
+
await access(path.join(dir, 'package.json'));
|
|
300
|
+
return dir;
|
|
301
|
+
} catch {
|
|
302
|
+
dir = path.dirname(dir);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
throw new Error('❌ Could not find project root');
|
|
306
|
+
}
|
|
307
|
+
function reportTestAndTypecheckResults(sliceDir, flow, result) {
|
|
308
|
+
const sliceName = path.basename(sliceDir);
|
|
309
|
+
if (result.success) {
|
|
310
|
+
console.log(`✅ All Tests and checks passed for: ${sliceName} in flow ${flow}`);
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
console.error(`❌ ${sliceName} in floe ${flow} failed tests or type-checks.`);
|
|
314
|
+
if (result.failedTestFiles.length) {
|
|
315
|
+
const files = result.failedTestFiles.map((f) => path.relative(sliceDir, f));
|
|
316
|
+
console.log(`🧪 Failed test files: ${files.join(', ')}`);
|
|
317
|
+
if (result.testErrors) {
|
|
318
|
+
console.log(`📝 Test errors:\n${result.testErrors}`);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
if (result.failedTypecheckFiles.length) {
|
|
322
|
+
const files = result.failedTypecheckFiles.map((f) => path.relative(sliceDir, f));
|
|
323
|
+
console.log(`📐 Failed typecheck files: ${files.join(', ')}`);
|
|
324
|
+
if (result.typecheckErrors) {
|
|
325
|
+
console.log(`📝 Typecheck errors:\n${result.typecheckErrors}`);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
@@ -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
|
+
}
|
package/src/cli/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import 'dotenv/config';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
import { existsSync } from 'fs';
|
|
5
|
+
import { runFlows } from '../agent/runFlows';
|
|
6
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
7
|
+
const isMain = process.argv[1] === __filename;
|
|
8
|
+
if (isMain) {
|
|
9
|
+
const base = process.argv[2];
|
|
10
|
+
if (!base) {
|
|
11
|
+
console.error('Usage: tsx src/cli/index.ts path/to/server/root');
|
|
12
|
+
process.exit(1);
|
|
13
|
+
}
|
|
14
|
+
const serverRoot = path.resolve(base);
|
|
15
|
+
const flowsDir = path.join(serverRoot, 'src', 'domain', 'flows');
|
|
16
|
+
if (!existsSync(flowsDir)) {
|
|
17
|
+
console.error(`❌ Flows directory not found at: ${flowsDir}`);
|
|
18
|
+
process.exit(1);
|
|
19
|
+
}
|
|
20
|
+
void runFlows(flowsDir);
|
|
21
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export const SYSTEM_PROMPT = `
|
|
2
|
+
You are a software engineer implementing missing logic in a sliced event-driven TypeScript backend. Each slice contains partially scaffolded code, and your task is to complete the logic following implementation instructions embedded in each file.
|
|
3
|
+
|
|
4
|
+
Project Characteristics:
|
|
5
|
+
- Architecture: sliced event-sourced CQRS (Command, Query, Reaction slices)
|
|
6
|
+
- Language: TypeScript with type-graphql and Emmett
|
|
7
|
+
- Each slice has scaffolded files with implementation instructions clearly marked with comments (e.g., '## IMPLEMENTATION INSTRUCTIONS ##') or TODOs.
|
|
8
|
+
- Tests (e.g., *.specs.ts) must pass.
|
|
9
|
+
- Type errors are not allowed.
|
|
10
|
+
|
|
11
|
+
Your Goal:
|
|
12
|
+
- Read the implementation instructions from the provided file.
|
|
13
|
+
- Generate only the code needed to fulfill the instructions, nothing extra and provide back the whole file without the instructions.
|
|
14
|
+
- Maintain immutability and adhere to functional best practices.
|
|
15
|
+
- Use only the types and domain constructs already present in the slice.
|
|
16
|
+
- Do not remove existing imports or types that are still referenced or required in the file.
|
|
17
|
+
- Return the entire updated file, not just the modified parts and remove any TODO comments or instructions after implementing the logic
|
|
18
|
+
|
|
19
|
+
Key rules:
|
|
20
|
+
- Never modify code outside the TODO or instruction areas.
|
|
21
|
+
- Ensure the code is production-ready and type-safe.
|
|
22
|
+
- Follow the slice type conventions:
|
|
23
|
+
- **Command slice**: validate command, inspect state, emit events, never mutate state. Uses graphql mutations.
|
|
24
|
+
- **Reaction slice**: respond to events with commands.
|
|
25
|
+
- **Query slice**: maintain projections based on events, do not emit or throw. Uses graphql queries.
|
|
26
|
+
- All code must be TypeScript compliant and follow functional patterns.
|
|
27
|
+
- If a test exists, make it pass.
|
|
28
|
+
- Keep implementations minimal and idiomatic.
|
|
29
|
+
|
|
30
|
+
Avoid:
|
|
31
|
+
- Adding new dependencies.
|
|
32
|
+
- Refactoring unrelated code.
|
|
33
|
+
- Changing the structure of already scaffolded files unless instructed.
|
|
34
|
+
|
|
35
|
+
You will receive:
|
|
36
|
+
- The path of the file to implement.
|
|
37
|
+
- The current contents of the file, with instruction comments.
|
|
38
|
+
- Other relevant files from the same slice (e.g., types, test, state, etc.).
|
|
39
|
+
|
|
40
|
+
You must:
|
|
41
|
+
- Return the entire updated file (no commentary and remove all implementation instructions).
|
|
42
|
+
- Ensure the output is valid TypeScript.
|
|
43
|
+
`;
|