forked from e2b-dev/ai-developer-with-sandbox
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
226 lines (192 loc) · 6.25 KB
/
index.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
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
import 'dotenv/config'
import OpenAI from 'openai'
import prompts from 'prompts'
import ora from 'ora'
import { MessageContentText } from 'openai/resources/beta/threads'
import { Sandbox } from '@e2b/sdk'
import { onLog } from './log'
import { cloneRepo, loginWithGH, listLastEditedRepos, createRepo } from './gh'
import { sleep } from './sleep'
import { listFiles, makeCommit, makeDir, makePullRequest, readFile, saveCodeToFile, runCode, runPowershellCommand } from './actions'
import { listAIDeveloper } from './aiAssistant-list'
const openai = new OpenAI()
//const AI_ASSISTANT_ID = process.env.AI_ASSISTANT_ID!
async function initChat(): Promise<{ repoName: string; task: string; mode: string; AI_ASSISTANT_ID: string }> {
let { mode } = (await prompts({
type: 'select',
name: 'mode',
message: 'Choose mode:',
choices: [
{ title: 'Work in a repo', value: 'repo' },
{ title: 'Just run code or commands', value: 'chit-chat' },
],
})) as any
// Function to prompt the user to select an AI assistant and return the selected assistant's ID
async function selectAIAssistant(): Promise<string> {
// Fetch the list of AI assistants
const aiAssistants = await listAIDeveloper();
// Create a list of choices for the prompt
const aiChoices = aiAssistants.map((assistant, index) => ({
title: assistant.name,
value: index,
}));
let { selectedAI } = (await prompts({
type: 'select',
name: 'selectedAI',
message: 'Choose an AI assistant:',
choices: aiChoices,
})) as any;
// Return the ID of the selected AI assistant
return aiAssistants[selectedAI].id;
}
let repoName = '';
let task = '';
// Set the selected AI assistant
let AI_ASSISTANT_ID = await selectAIAssistant();
if (mode === 'repo') {
await loginWithGH(sandbox)
let { repoOption } = (await prompts({
type: 'select',
name: 'repoOption',
message: 'Choose an option for setting the repo:',
choices: [
{ title: 'Select a repo from last 10', value: 'select' },
{ title: 'Create a new repo', value: 'create' },
{ title: 'Enter a repo name manually', value: 'manual' },
],
})) as any;
switch (repoOption) {
case 'create':
let { newRepoName } = (await prompts({
type: 'text',
name: 'newRepoName',
message: 'Enter new repo name:',
})) as any;
let username = process.env.GIT_USERNAME;
await createRepo(sandbox, newRepoName);
repoName = `${username}/${newRepoName}`;
break;
case 'select':
let repos = await listLastEditedRepos(sandbox);
let repoChoices = repos.map((repo, index) => ({
title: repo,
value: index,
}));
let { selectedRepo } = (await prompts({
type: 'select',
name: 'selectedRepo',
message: 'Select a repo:',
choices: repoChoices,
})) as any;
repoName = repos[selectedRepo];
break;
case 'manual':
let { manualRepoName } = (await prompts({
type: 'text',
name: 'manualRepoName',
message: 'Enter repo name (eg: username/repo):',
})) as any;
repoName = manualRepoName.replace(/\\/g, '/');
break;
}
await cloneRepo(sandbox, repoName)
let { task } = (await prompts({
type: 'text',
name: 'task',
message: 'Enter the task you want the AI developer to work on:',
})) as any;
}
return { repoName, task, mode, AI_ASSISTANT_ID }
}
function createThread(repoURL: string, task: string, mode: string) {
let content;
if (mode === 'repo') {
content = `Pull this repo: '${repoURL}'. Then carefully plan this task and start working on it: ${task}`;
} else if (mode === 'chit-chat') {
content = `Enter your first command or code:`;
} else {
content = 'Enter your first command or code:';
}
return openai.beta.threads.create({
messages: [
{
'role': 'user',
'content': content,
}
]
})
}
const sandbox = await Sandbox.create({
id: 'ai-developer-sandbox',
onStdout: onLog,
onStderr: onLog,
})
sandbox
.addAction(readFile)
.addAction(makeCommit)
.addAction(makePullRequest)
.addAction(saveCodeToFile)
.addAction(makeDir)
.addAction(listFiles)
.addAction(runCode)
.addAction(runPowershellCommand)
const { repoName, task, mode, AI_ASSISTANT_ID } = await initChat()
const spinner = ora('Waiting for assistant')
spinner.start()
const thread = await createThread(repoName, task, mode)
const assistant = await openai.beta.assistants.retrieve(AI_ASSISTANT_ID)
let run = await openai.beta.threads.runs.create(thread.id, {
assistant_id: assistant.id,
})
assistantLoop: while (true) {
await sleep(1000)
switch (run.status) {
case 'requires_action': {
spinner.stop()
const outputs = await sandbox.openai.actions.run(run)
spinner.start()
if (outputs.length > 0) {
await openai.beta.threads.runs.submitToolOutputs(thread.id, run.id, {
tool_outputs: outputs,
})
}
break
}
case 'completed': {
spinner.stop()
const messages = await openai.beta.threads.messages.list(thread.id)
const textMessages = messages.data[0].content.filter(message => message.type === 'text') as MessageContentText[]
const { userResponse }: { userResponse: string } = await prompts({
type: 'text',
name: 'userResponse',
message: `${textMessages[0].text.value}\nIf you want to exit write 'exit', otherwise write your response:\n`,
})
if (userResponse === 'exit') {
break assistantLoop
}
spinner.start()
await openai.beta.threads.messages.create(thread.id, {
role: 'user',
content: userResponse as string,
})
run = await openai.beta.threads.runs.create(thread.id, {
assistant_id: assistant.id,
})
break
}
case 'queued':
case 'in_progress':
break
case 'cancelled':
case 'cancelling':
case 'expired':
case 'failed':
break assistantLoop
default:
console.error(`Unknown status: ${run.status}`)
break assistantLoop
}
run = await openai.beta.threads.runs.retrieve(thread.id, run.id)
}
spinner.stop()
await sandbox.close()