-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·140 lines (115 loc) · 3.32 KB
/
index.js
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
#!/usr/bin/env node
const { OpenAI } = require("openai");
const path = require("path");
const actions = require("./actions");
const dotenv = require("dotenv");
const utils = require("./utils.js");
const scriptDir = __dirname;
const envPath = path.join(scriptDir, ".env");
dotenv.config({ path: envPath });
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
async function executePrompt(prompt) {
const response = await openai.chat.completions.create({
messages: [
{
role: "system",
content: "Answer in json format",
},
{ role: "user", content: prompt },
],
model: "gpt-4o",
temperature: 0,
response_format: { type: "json_object" },
});
const content = response.choices[0].message.content;
const parsedContent = JSON.parse(content).actions;
return Array.isArray(parsedContent) ? parsedContent : [parsedContent];
}
function extractContent(input) {
const startBracketIndex = input.indexOf("[");
const endBracketIndex = input.lastIndexOf("]");
if (
startBracketIndex === -1 ||
endBracketIndex === -1 ||
endBracketIndex <= startBracketIndex
) {
return null; // No valid JSON array found
}
const jsonArrayString = input.substring(
startBracketIndex,
endBracketIndex + 1,
);
return jsonArrayString.trim();
}
async function getPrompt(userInput, fileContents) {
const currentDirectory = utils.getCurrentDir();
const directoryTree = await utils.readDirectoryString(currentDirectory);
const actionDefinitions = Object.values(actions)
.map((action) => `${action.promptActionDefinition}\n`)
.join("");
const fileContentsString = fileContents
? fileContents
.map(
(file) =>
`//${file.fileNamePath}
${file.contents}
`,
)
.join("\n")
: "";
return `
Based on this query:
"${userInput}"
Action definitions (YOU MUST ALWAYS choose either read-file or create-file per fileNamePath)
${actionDefinitions}
Output this format:
{actions: [Actions here]}
This is the current directory structure:
${directoryTree}
${fileContentsString}
`;
}
async function executeActions(actionEvents) {
const files = [];
for (const actionEvent of actionEvents) {
switch (actionEvent.type) {
case "create-directory":
await actions.createDirectory.execute(actionEvent);
break;
case "create-file":
await actions.createFile.execute(actionEvent);
break;
case "read-file":
const fileContents = await actions.readFile.execute(actionEvent);
files.push({
fileNamePath: actionEvent.fileNamePath,
contents: fileContents,
});
break;
default:
console.error("Invalid action type:", actionEvent.type);
break;
}
}
return {
files,
};
}
async function run() {
const args = process.argv.slice(2);
const userInput = args.join(" ");
const prompt = await getPrompt(userInput);
const actionEvents = await executePrompt(prompt);
const actionReturns = await executeActions(actionEvents);
// Maybe make this into a loop later?
if (actionReturns.files.length > 0) {
const newPrompt = await getPrompt(userInput, actionReturns.files);
const newActionEvents = await executePrompt(newPrompt);
await executeActions(newActionEvents);
}
}
(async () => {
await run();
})();