Repository navigation
Expand file tree
/
Copy pathcodeGenerator.js
More file actions
585 lines (460 loc) · 22.4 KB
/
Copy pathcodeGenerator.js
File metadata and controls
585 lines (460 loc) · 22.4 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
import { CONFIG } from "./config.js";
import chalk from "chalk";
import inquirer from "inquirer";
import path from "path";
import FileManager from "./fileManager.js";
import ora from "ora";
import CodeAnalyzer from "./codeAnalyzer.js";
import DocumentationGenerator from "./documentationGenerator.js";
import UserInterface from "./userInterface.js";
import fs from "fs/promises";
import { getResponse } from "./model.js";
const CodeGenerator = {
async generate(readme, currentCode, fileName, projectStructure, allFileContents, model, apiKey) {
const fileExtension = path.extname(fileName);
const language = this.getLanguageFromExtension(fileExtension);
const languageConfig = CONFIG.languageConfigs[language];
const prompt = `
You are AutoCode, an automatic coding tool. Your task is to generate or update the ${fileName} file based on the README.md instructions, the current ${fileName} content (if any), the project structure, and the content of all other files.
README.md content:
${readme}
Current ${fileName} content (if exists):
${currentCode || "No existing code"}
Project structure:
${JSON.stringify(projectStructure, null, 2)}
Content of other selected files:
${Object.entries(allFileContents)
.filter(([key]) => key !== fileName)
.map(([key, value]) => `${key}:\n${value}`)
.join("\n\n")}
Language: ${language}
File extension: ${fileExtension}
Linter: ${languageConfig.linter}
Formatter: ${languageConfig.formatter}
Package manager: ${languageConfig.packageManager}
Please generate or update the ${fileName} file to implement the features described in the README. Ensure the code is complete, functional, and follows best practices for ${language}. Consider the project structure and the content of other selected files when making changes or adding new features. Reuse functionality from other modules and avoid duplicating code. Do not include any explanations or comments in your response, just provide the code.
`;
const spinner = ora("Generating code...").start();
try {
const response = await getResponse(prompt, model, apiKey);
spinner.succeed("Code generated successfully");
await this.calculateTokenStats(response.usage?.input_tokens, response.usage?.output_tokens);
return this.cleanGeneratedCode(response.content[0].text);
} catch (error) {
spinner.fail("Error generating code");
throw error;
}
},
cleanGeneratedCode(code) {
const codeBlockRegex =
/`{3,4}(?:javascript|js|jsx|css|yaml|ts|tsx|markdown|json|html|python|csharp|java|ruby|go|rust|php|swift|kotlin|dart)?\n([\s\S]*?)\n`{3,4}/;
const match = code.match(codeBlockRegex);
return match ? match[1] : code;
},
async updateReadme(readme, projectStructure, model, apiKey) {
const prompt = `
You are AutoCode, an automatic coding tool. Your task is to update the README.md file with new design ideas and considerations based on the current content and project structure.
Current README.md content:
${readme}
Project structure:
${JSON.stringify(projectStructure, null, 2)}
Please update the README.md file with new design ideas and considerations. Ensure the content is well-structured and follows best practices. Consider the current project structure when suggesting improvements or new features. Do not include any explanations or comments in your response, just provide the updated README.md content.
`;
const spinner = ora("Updating README...").start();
try {
const response = await getResponse(prompt, model, apiKey);
spinner.succeed("README updated successfully");
const updatedReadme = this.cleanGeneratedCode(response.content[0].text);
await this.calculateTokenStats(response.usage?.input_tokens, response.usage?.output_tokens);
return updatedReadme;
} catch (error) {
spinner.fail("Error updating README");
throw error;
}
},
async splitLargeFile(filePath, content, projectStructure) {
console.log(chalk.yellow(`📂 File ${filePath} exceeds ${CONFIG.maxFileLines} lines. Splitting...`));
const fileExtension = path.extname(filePath);
const language = this.getLanguageFromExtension(fileExtension);
const prompt = `
The file ${filePath} exceeds ${
CONFIG.maxFileLines
} lines. Please suggest how to split this file into smaller, more manageable parts. Consider the following:
1. Identify logical components or functionalities that can be separated.
2. Suggest new file names for the extracted parts.
3. Provide the content for each new file, including the updated content for the original file.
4. Ensure that the split maintains the overall functionality and doesn't break any dependencies.
5. Use appropriate language-specific conventions for ${language}.
Current file content:
${content}
Project structure:
${JSON.stringify(projectStructure, null, 2)}
Please provide your suggestions in the following Markdown format:
# Original File: [original_file_name]
[content for the original file]
# New File: [new_file_name_1]
[content for new_file_1]
# New File: [new_file_name_2]
[content for new_file_2]
... (repeat for all new files)
`;
const spinner = ora("Generating file split suggestion...").start();
try {
const response = await getResponse(prompt);
spinner.succeed("File split suggestion generated");
const splitSuggestion = response.content[0].text;
console.log(chalk.cyan("📋 File splitting suggestion:"));
console.log(splitSuggestion);
const { confirmSplit } = await inquirer.prompt({
type: "confirm",
name: "confirmSplit",
message: "Do you want to proceed with the suggested file split?",
default: true,
});
if (confirmSplit) {
const files = this.parseSplitSuggestion(splitSuggestion);
await this.saveFiles(filePath, files);
console.log(chalk.green("✅ File split completed."));
} else {
console.log(chalk.yellow("⏹️ File split cancelled."));
}
await this.calculateTokenStats(response.usage?.input_tokens, response.usage?.output_tokens);
} catch (error) {
spinner.fail("Error generating file split suggestion");
throw error;
}
},
parseSplitSuggestion(suggestion) {
const files = {};
const fileRegex = /# (?:Original File|New File): (.+)\n([\s\S]+?)(?=\n# (?:Original File|New File)|$)/g;
let match;
while ((match = fileRegex.exec(suggestion)) !== null) {
const [, fileName, content] = match;
files[fileName.trim()] = content.trim();
}
return files;
},
async saveFiles(originalFilePath, files) {
const originalDir = path.dirname(originalFilePath);
for (const [fileName, content] of Object.entries(files)) {
const filePath =
fileName === path.basename(originalFilePath) ? originalFilePath : path.join(originalDir, fileName);
await FileManager.write(filePath, this.cleanGeneratedCode(content));
console.log(chalk.green(`✅ Saved file: ${filePath}`));
}
},
async optimizeAndRefactorFile(filePath, projectStructure) {
console.log(chalk.cyan(`🔄 Optimizing and refactoring ${filePath}...`));
const fileContent = await FileManager.read(filePath);
const fileExtension = path.extname(filePath);
const language = this.getLanguageFromExtension(fileExtension);
const languageConfig = CONFIG.languageConfigs[language];
const prompt = `
Please optimize and refactor the following code from ${filePath}:
${fileContent}
Project structure:
${JSON.stringify(projectStructure, null, 2)}
Language: ${language}
File extension: ${fileExtension}
Linter: ${languageConfig.linter}
Formatter: ${languageConfig.formatter}
Package manager: ${languageConfig.packageManager}
Focus on:
1. Improving code efficiency
2. Enhancing readability
3. Applying design patterns where appropriate
4. Reducing code duplication
5. Improving overall code structure
6. Ensuring consistency with the project structure
7. Reusing functionality from other modules
8. Following best practices and conventions for ${language}
Return the optimized and refactored code ONLY!! without explanations or comments or md formatting. Do not include any explanations or comments in your response, just provide the code.
`;
const spinner = ora("Optimizing and refactoring...").start();
try {
const response = await getResponse(prompt);
spinner.succeed("Optimization and refactoring completed");
const optimizedCode = this.cleanGeneratedCode(response.content[0].text);
await FileManager.write(filePath, optimizedCode);
console.log(chalk.green(`✅ ${filePath} has been optimized and refactored.`));
await this.calculateTokenStats(response.usage?.input_tokens, response.usage?.output_tokens);
} catch (error) {
spinner.fail("Error optimizing and refactoring");
throw error;
}
},
getLanguageFromExtension(fileExtension) {
for (const [language, config] of Object.entries(CONFIG.languageConfigs)) {
if (config.fileExtensions.includes(fileExtension)) {
return language;
}
}
return "javascript";
},
async generateDependencyFile(language, projectStructure, readme) {
const languageConfig = CONFIG.languageConfigs[language];
let dependencyFileName;
switch (languageConfig.packageManager) {
case "npm":
dependencyFileName = "package.json";
break;
case "pip":
dependencyFileName = "requirements.txt";
break;
case "nuget":
dependencyFileName = "ProjectName.csproj";
break;
case "maven":
dependencyFileName = "pom.xml";
break;
case "bundler":
dependencyFileName = "Gemfile";
break;
case "go mod":
dependencyFileName = "go.mod";
break;
case "cargo":
dependencyFileName = "Cargo.toml";
break;
case "composer":
dependencyFileName = "composer.json";
break;
case "swift package manager":
dependencyFileName = "Package.swift";
break;
case "gradle":
dependencyFileName = "build.gradle";
break;
case "pub":
dependencyFileName = "pubspec.yaml";
break;
default:
console.log(chalk.red(`Unsupported package manager: ${languageConfig.packageManager}`));
return;
}
console.log(chalk.cyan(`📦 Generating ${dependencyFileName} for ${language}...`));
const prompt = `
Please generate a ${dependencyFileName} file for a ${language} project with the following structure:
${JSON.stringify(projectStructure, null, 2)}
README.md content:
${readme}
Include all necessary dependencies based on the project structure and features described in the README.md. Ensure the file is properly formatted and follows best practices for ${language} projects.
Return the content of the ${dependencyFileName} file without explanations or comments. Do not include any explanations or comments in your response, just provide the code.
`;
const spinner = ora(`Generating ${dependencyFileName}...`).start();
try {
const response = await getResponse(prompt);
spinner.succeed(`${dependencyFileName} generated successfully`);
const dependencyFileContent = this.cleanGeneratedCode(response.content[0].text);
await FileManager.write(dependencyFileName, dependencyFileContent);
console.log(chalk.green(`✅ Generated ${dependencyFileName}`));
await this.calculateTokenStats(response.usage?.input_tokens, response.usage?.output_tokens);
} catch (error) {
spinner.fail(`Error generating ${dependencyFileName}`);
throw error;
}
},
async generateAIAgentCode(agentType, agentDescription, projectStructure, readme) {
console.log(chalk.cyan(`🤖 Generating AI agent code for ${agentType}...`));
const fileManagerContent = await FileManager.read("fileManager.js");
const userInterfaceContent = await FileManager.read("userInterface.js");
const configContent = await FileManager.read("config.js");
const prompt = `
Please generate code for the ${agentType} AI agent based on the project structure and features described in the README.md. The agent should be able to perform its specific tasks as outlined in the README.
Agent description: ${agentDescription}
README.md content:
${readme}
Project structure:
${JSON.stringify(projectStructure, null, 2)}
fileManager.js content:
${fileManagerContent}
userInterface.js content:
${userInterfaceContent}
config.js content:
${configContent}
Ensure the code is complete, functional, and follows best practices for JavaScript. Consider the project structure when implementing the agent's functionality. Reuse existing modules and avoid duplicating code.
Return the generated code for the ${agentType} AI agent without explanations or comments. Do not include any explanations or comments in your response, just provide the code.
`;
const spinner = ora(`Generating ${agentType} AI agent code...`).start();
try {
const response = await getResponse(prompt);
spinner.succeed(`${agentType} AI agent code generated successfully`);
const agentCode = this.cleanGeneratedCode(response.content[0].text);
const fileName = `./${agentType.replace(/\s+/g, "")}.js`;
await FileManager.write(fileName, agentCode);
console.log(chalk.green(`✅ Generated ${fileName}`));
await this.calculateTokenStats(response.usage?.input_tokens, response.usage?.output_tokens);
} catch (error) {
spinner.fail(`Error generating ${agentType} AI agent code`);
throw error;
}
},
async generateLandingPage(projectStructure, readme) {
console.log(chalk.cyan("🌐 Generating landing page..."));
const prompt = `
Please generate an HTML file for a landing page based on the project structure and features described in the README.md. The landing page should showcase the key features of the project and provide a visually appealing introduction.
Project structure:
${JSON.stringify(projectStructure, null, 2)}
README.md content:
${readme}
Use the following design guidelines:
- Responsive and mobile-friendly design
- Highlight key features and project information
- Include sections for pricing tiers
- Use a sleek design and creativity
Return the generated HTML code for the landing page without explanations or comments.
`;
const spinner = ora("Generating landing page...").start();
try {
const response = await getResponse(prompt);
spinner.succeed("Landing page generated successfully");
const landingPageCode = this.cleanGeneratedCode(response.content[0].text);
const fileName = "landing.html";
await FileManager.write(fileName, landingPageCode);
console.log(chalk.green(`✅ Generated ${fileName}`));
await this.calculateTokenStats(response.usage?.input_tokens, response.usage?.output_tokens);
} catch (error) {
spinner.fail("Error generating landing page");
throw error;
}
},
async generateFullProject(projectStructure, readme) {
console.log(chalk.cyan("🚀 Generating full project..."));
const { language } = await UserInterface.promptForLanguage();
await this.generateDependencyFile(language, projectStructure, readme);
const files = Object.keys(projectStructure).filter(
(file) => !file.includes(CONFIG.languageConfigs[language].dependencyFile)
);
for (const file of files) {
const fileExtension = path.extname(file);
const fileLanguage = this.getLanguageFromExtension(fileExtension);
const languageConfig = CONFIG.languageConfigs[fileLanguage];
if (languageConfig) {
const content = await this.generate(readme, "", file, projectStructure, {});
await FileManager.write(file, content);
console.log(chalk.green(`✅ Generated ${file}`));
await CodeAnalyzer.runLintChecks(file);
await DocumentationGenerator.generate(file, content, projectStructure);
}
}
await this.createMissingSourceFile(projectStructure, readme);
await this.generateLandingPage(projectStructure, readme);
await DocumentationGenerator.generateProjectDocumentation(projectStructure);
console.log(chalk.green("✅ Full project generated successfully"));
},
async createMissingSourceFile(projectStructure, readme) {
console.log(chalk.cyan("🔍 Checking for missing source files..."));
const sourceFiles = Object.keys(projectStructure).filter(
(file) => file.endsWith(".js") && !file.startsWith("server/") && file !== "index.js"
);
if (sourceFiles.length === 0) {
console.log(chalk.yellow("No source files found. Creating a new one..."));
const fileName = "app.js";
const prompt = `
Please generate code for a new source file named ${fileName} based on the project structure and features described in the README.md. This file should serve as the main application logic for the project.
README.md content:
${readme}
Project structure:
${JSON.stringify(projectStructure, null, 2)}
Ensure the code is complete, functional, and follows best practices for JavaScript. Consider the project structure when implementing the main application logic. Reuse existing modules and avoid duplicating code.
Return the generated code for ${fileName} without explanations or comments.
`;
const spinner = ora(`Generating ${fileName}...`).start();
try {
const response = await getResponse(prompt);
spinner.succeed(`${fileName} generated successfully`);
const sourceFileContent = this.cleanGeneratedCode(response.content[0].text);
await FileManager.write(fileName, sourceFileContent);
console.log(chalk.green(`✅ Generated ${fileName}`));
projectStructure[fileName] = null;
await this.calculateTokenStats(response.usage?.input_tokens, response.usage?.output_tokens);
} catch (error) {
spinner.fail(`Error generating ${fileName}`);
throw error;
}
} else {
console.log(chalk.green("✅ Source files already exist. No need to create a new one."));
}
},
async calculateTokenStats(inputTokens, outputTokens) {
if (!inputTokens || !outputTokens) {
return;
}
const inputCost = (inputTokens / 1000000) * 3;
const outputCost = (outputTokens / 1000000) * 15;
const totalCost = inputCost + outputCost;
console.log(
chalk.cyan(
`📊 Token Statistics: Input: ${inputTokens}, Output: ${outputTokens}, Cost: ${chalk.yellow(
`$${totalCost.toFixed(2)}`
)}`
)
);
},
async updateChangelog(changes) {
const changelogPath = "CHANGELOG.md";
let changelog = "";
try {
changelog = await fs.readFile(changelogPath, "utf-8");
} catch {
console.log(chalk.yellow("CHANGELOG.md not found. Creating a new one."));
}
const currentDate = new Date().toISOString().split("T")[0];
const newEntry = `
## [Unreleased] - ${currentDate}
${changes.map((change) => `- ${change}`).join("\n")}
`;
changelog = newEntry + changelog;
await fs.writeFile(changelogPath, changelog);
console.log(chalk.green("✅ CHANGELOG.md updated successfully"));
},
async createAppDescriptionFiles(projectStructure, readme) {
console.log(chalk.cyan("📝 Creating app description and metadata files..."));
const prompt = `
Please generate the following app description and metadata files for both Google Play Store and Apple App Store:
1. app_description.txt (max 4000 characters)
2. short_description.txt (max 80 characters)
3. keywords.txt (comma-separated, max 100 characters)
4. title.txt (max 30 characters)
5. subtitle.txt (max 30 characters)
6. privacy_policy.html
7. release_notes.txt
Base the content on the project's README.md and existing features. Ensure the descriptions are engaging and highlight the key features of the app.
README.md content:
${readme}
Project structure:
${JSON.stringify(projectStructure, null, 2)}
Return the content for each file in the following format:
# File: [filename]
[content]
# File: [filename]
[content]
...and so on for all files.
`;
const spinner = ora("Generating app description and metadata files...").start();
try {
const response = await getResponse(prompt);
spinner.succeed("App description and metadata files generated successfully");
const files = this.parseGeneratedFiles(response.content[0].text);
for (const [fileName, content] of Object.entries(files)) {
await FileManager.write(`docs/${fileName}`, content);
console.log(chalk.green(`✅ Generated docs/${fileName}`));
}
await this.calculateTokenStats(response.usage?.input_tokens, response.usage?.output_tokens);
} catch (error) {
spinner.fail("Error generating app description and metadata files");
throw error;
}
},
parseGeneratedFiles(content) {
const files = {};
const fileRegex = /# File: (.+)\n([\s\S]+?)(?=\n# File:|$)/g;
let match;
while ((match = fileRegex.exec(content)) !== null) {
const [, fileName, fileContent] = match;
files[fileName.trim()] = fileContent.trim();
}
return files;
},
};
export default CodeGenerator;