-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnodejs-project-example.ts
More file actions
112 lines (95 loc) · 3.29 KB
/
nodejs-project-example.ts
File metadata and controls
112 lines (95 loc) · 3.29 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
import { generateText } from 'ai';
import { openai } from "@ai-sdk/openai";
import { createCodeExecutionTool } from '../src/code-execution-tool';
import * as fs from 'fs';
import * as path from 'path';
import { v4 as uuidv4 } from 'uuid';
import { exec } from 'child_process';
async function main() {
try {
// First, ask AI to generate a complete Node.js project structure
console.log('Generating project structure...');
const result = await generateText({
model: openai('gpt-4'),
maxSteps: 10,
messages: [
{
role: 'user',
content: `Create a simple Node.js project with the following structure:
1. A main server file (server.js) that creates an Express server
2. A package.json with necessary dependencies
3. A README.md with setup instructions
4. A simple route that returns "Hello World"
5. Include a console log message "Listening on port 3000"
Please format your response as follows:
FILE: filename
\`\`\`
file contents
\`\`\`
Separate each file with a blank line.`
}
],
tools: { codeExecutionTool: createCodeExecutionTool().codeExecutionTool },
toolChoice: 'auto'
});
console.log('AI Response:', result.text);
// Create a temporary directory for the project
const projectDir = path.join('/tmp', uuidv4());
fs.mkdirSync(projectDir, { recursive: true });
console.log('** Project Directory:', projectDir);
// Parse the response and create files
const fileRegex = /FILE: ([^\n]+)\n```(?:[^\n]*)\n([\s\S]*?)```/g;
let match;
const files: { name: string; content: string }[] = [];
while ((match = fileRegex.exec(result.text)) !== null) {
const fileName = match[1].trim();
const content = match[2].trim();
files.push({ name: fileName, content });
// Write file to temp directory
const filePath = path.join(projectDir, fileName);
fs.writeFileSync(filePath, content);
console.log(`** Created file: ${fileName}`);
}
console.log('** Created files:', files.map(f => f.name).join(', '));
// Create a code execution tool with the project directory mounted
const { codeExecutionTool, executionEngine } = createCodeExecutionTool({
mounts: [{
type: 'directory',
source: projectDir,
target: '/project'
}]
});
executionEngine.setVerbosity('info');
console.log('Executing project...');
// Execute the project using codeExecutionTool
const executionResult = await codeExecutionTool.execute({
language: 'javascript',
code: '',
runApp: {
entryFile: 'server.js',
cwd: '/project'
},
dependencies: ['express'],
streamOutput: {
dependencyStdout: (data) => {
console.log('Dependency stdout:', data);
},
dependencyStderr: (data) => {
console.error('Dependency stderr:', data);
},
stdout: (data) => {
console.log('Container stdout:', data);
},
stderr: (data) => {
console.error('Container stderr:', data);
}
}
});
console.log('Project Execution Result:');
console.log('Exit Code:', executionResult.exitCode);
console.log('Execution Time:', executionResult.executionTime, 'ms');
} catch (error) {
console.error('Error:', error);
}
}
main();