-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.ts
87 lines (77 loc) · 3.1 KB
/
main.ts
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
import { createAgent, MessageEvent } from "./agent";
import * as fs from "fs/promises";
import * as path from "path";
import { PackageResult } from "./packager";
const apps = [
{
name: "Python QA Database",
spec: `Python app that takes user questions and looks them up in a
database where they are mapped to answers. If there is a close match, it retrieves
the matched answer. If there isn't, it asks the user to provide an answer and
stores the question/answer pair in the database.`,
models: ["gpt-4o-mini", "gpt-4o", "o1-mini", "o1-preview"],
},
{
name: "JavaScript Todo App",
spec: `Create a full-stack NextJS todo app with a TailwindCSS frontend, that allows users to add,
remove, and mark tasks as complete. Use a Postgres database to persist the tasks.`,
models: ["gpt-4o-mini", "gpt-4o", "o1-mini"], // didn't complete with o1-preview
},
{
name: "Iphone calculator",
spec: `Iphone style scientific calculator in one html file, using tailwind css and javascript.`,
models: ["gpt-4o-mini", "gpt-4o", "o1-mini", "o1-preview"],
},
// Add more specifications as needed
];
async function outputResult(
name: string,
model: string,
packageResult: PackageResult,
) {
// Generate timestamp
const timestamp = new Date().toISOString().slice(0, 10);
// Create output directory if it doesn't exist
const outputDir = path.join("output", timestamp, name, model);
await fs.mkdir(outputDir, { recursive: true });
try {
// Iterate over all files in the packageResult and store each file with their correct path
for (const file of packageResult.files) {
const filePath = path.join(outputDir, file.path);
const fileDir = path.dirname(filePath);
// Create directory if it doesn't exist
await fs.mkdir(fileDir, { recursive: true });
// Write file content
await fs.writeFile(filePath, file.content);
console.log(`File written to: ${filePath}`);
}
} catch (error) {
console.error("Error in outputResult:", error);
const errorLogPath = path.join(outputDir, "error.log");
const errorContent = `Error: ${error}\n\nPackageResult structure:\n${JSON.stringify(packageResult, null, 2)}`;
await fs.writeFile(errorLogPath, errorContent);
console.log(`Error details written to: ${errorLogPath}`);
}
}
async function runGeneration(name: string, spec: string, model: string) {
console.log(`Running generation with model: ${model}`);
const codeAgent = createAgent(model);
const run = codeAgent.run(spec);
for await (const event of codeAgent.streamEvents()) {
const msg = (event as MessageEvent).data.msg;
console.log(`${msg}\n`);
}
const result = await run;
const packageResult = result.data.result as unknown as PackageResult;
await outputResult(name, model, packageResult);
}
async function main() {
for (const specItem of apps) {
console.log(`\n--- Starting generation: ${specItem.name} ---\n`);
for (const model of specItem.models) {
await runGeneration(specItem.name, specItem.spec, model);
}
console.log(`\n--- Finished generation: ${specItem.name} ---\n`);
}
}
main().catch(console.error);