-
Notifications
You must be signed in to change notification settings - Fork 1
/
performTask.js
444 lines (393 loc) · 16 KB
/
performTask.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
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
let vscode;
vscode = require('vscode');
const { sendChatMessage } = require('./frontend.js');
const { runCommandsInSandbox } = require('./runInSandbox.js');
const { createChatCompletion } = require('./createChatCompletion');
const { get } = require('http');
const initialPrompt = {
"task": "Code assistant",
"system_msg": "You are a helpful coding assistant. You only speak JSON. You help the user implement a feature or debug code in multiple messages. You perform tasks to gather information and then use that information to perform actions. After you have edited a file, you review the new code to check if the edits are correct. When you are done, you perform the 'task completed' action.",
"response_format": {
"format": "json",
"info": "Respond in one of the formats specified in the options. Use actions 'run command' to create or move files, etc. You can take one action per response, and continue to perform actions until the task is done. Respond in pure JSON, with no prose text before or after the JSON, and exactly one JSON object optionally followed by a code block. One exception to the JSON format is that the payload for the 'edit file' action (the new code) are sent directly after the JSON in a ```block```. Do not put JSON after code.",
"options": [
{
"action": "show file summary",
"path": "<path/to/file>"
},
{
"action": "view section",
"path": "<path/to/file>",
"start": "<start line>",
"end": "<end line>"
},
{
"action": "run command",
"command": "<bash command to run - you will see the output in the next message. Examples: 'tree', 'pip install torch', 'mkdir new-dir', 'grep' ...>"
},
{
"action": "edit file",
"path": "<path/to/file>",
"start": "<start line>",
"end": "<end line>",
},
{
"action": "validate edit",
},
{
"action": "validate and apply",
},
// {
// "action": "search workspace",
// "term": "<search term>",
// "include": "<regex to include files, optional>",
// "exclude": "<regex to exclude files, optional>"
// },
{
"action": "task completed",
"finalMessage": "<message to show when task is completed>"
}
]
}
}
const initialUserPrompt = {
"request": "<insert>",
"context": null,
}
const performTask = async (task, context, initialAssistantMessage) => {
// context: e.g. {"selection": "code", "currentFile": "path/to/file", "start": 1, "end": 10}
// currentFile: e.g. 'path/to/file'
let systemMsg = { ...initialPrompt };
let userMsg = { ...initialUserPrompt };
userMsg.request = task;
userMsg.context = context;
return performTasksUntilDone(systemMsg, userMsg, initialAssistantMessage);
}
const parseResponse = (responseMsg) => {
responseMsg = responseMsg.trim().replace('```python', '```')
.replace('```javascript', '```')
.replace('```bash', '```')
.replace('```json', '```')
.replace('```js', '```')
.replace('```py', '```')
.replace('```sh', '```')
.replace('```ts', '```')
.replace('```typescript', '```')
.replace('```html', '```')
.replace('```css', '```')
.replace('```scss', '```')
.replace('```yaml', '```')
.replace('```yml', '```')
.replace('```xml', '```')
.replace('```c', '```')
.replace('```cpp', '```');
if (responseMsg.includes('```')) {
const responseParts = responseMsg.split('```');
const response = JSON.parse(responseParts[0]);
// remove the final ``` from the response
if (responseParts[1].endsWith('```')) {
responseParts[1] = responseParts[1].substring(0, responseParts[1].length - 3);
}
// filter only lines that are in the new range
const codeLines = responseParts[1].trim().split('\n');
const newLines = codeLines.map(line => {
console.log('checkinf if we should use', line);
const lineNum = parseInt(line.split(':')[0]);
if (lineNum >= response.start)
// remove line numbers (e.g. '10:') from the response if they exist
return line.split(': ').slice(1).join(':');
if (isNaN(lineNum))
return line;
return null;
}).filter(line => line !== null);
response.content = newLines.join('\n');
console.log({codeLines, newLines, response})
return response;
} else {
return JSON.parse(responseMsg);
}
}
const formatAsJsonWithCode = (response) => {
const content = response.content;
delete response.content;
const output = response.output;
delete response.output;
const responseString = JSON.stringify(response, null, 2);
if (!content && !output) return responseString;
return `${responseString}\n\`\`\`\n${content || ''}${output || ''}\n\`\`\``;
}
const performTasksUntilDone = async (systemMsg, userMsg, initialAssistant) => {
let messages = [
{
"role": "system",
"content": JSON.stringify(systemMsg, null, 2)
},
{
"role": "user",
"content": JSON.stringify(userMsg, null, 2)
}
];
if (initialAssistant) {
initialAssistant = { "gptResponse": initialAssistant, "responseRaw": formatAsJsonWithCode(initialAssistant) };
}
let currentMsgId = new Date().getTime().toString();
await sendChatMessage(messages[0].role, messages[0].content, currentMsgId);
currentMsgId = `${currentMsgId}.${new Date().getTime().toString()}`;
await sendChatMessage(messages[messages.length - 1].role, messages[messages.length - 1].content, currentMsgId);
let fileEdits = [];
while (messages.length < 40) {
let { gptResponse, responseRaw } = initialAssistant || await askForNextAction(messages);
initialAssistant = null;
console.log('gptResponse', gptResponse, responseRaw);
messages.push(
{
"role": "assistant",
"content": responseRaw
}
);
currentMsgId = `${currentMsgId}.${new Date().getTime().toString()}`;
await sendChatMessage(messages[messages.length - 1].role, messages[messages.length - 1].content, currentMsgId);
// await sendChatMessage(JSON.stringify(gptResponse, null, 2), currentMsgId);
if (gptResponse.action === 'task completed') {
await sortAndApplyFileEdits(fileEdits);
return gptResponse.finalMessage;
}
let userResponse;
[userResponse, fileEdits] = await executeTask(gptResponse, `${currentMsgId}.stream`, fileEdits);
messages.push({
"role": "user",
"content": formatAsJsonWithCode(userResponse)
});
currentMsgId = `${currentMsgId}.${new Date().getTime().toString()}`;
await sendChatMessage(messages[messages.length - 1].role, messages[messages.length - 1].content, currentMsgId);
}
}
const sortAndApplyFileEdits = async (fileEdits, save = false) => {
fileEdits = fileEdits.filter(fileEdit => fileEdit.validated);
fileEdits = fileEdits.sort((a, b) => {
if (a.start < b.start) return 1;
if (a.start > b.start) return -1;
return 0;
});
for (let fileEdit of fileEdits) {
await applyDiffs(fileEdit, save);
}
}
const askForNextAction = async (messages, retry = 4) => {
// send messages to GPT
// add the surrounding JSON with role: assistant / user etc
let responseRaw = await createChatCompletion(messages);
console.log('askForNextAction', responseRaw);
try {
let gptResponse = parseResponse(responseRaw);
console.log('prased', gptResponse);
return { gptResponse, responseRaw };
} catch (e) {
if (retry > 0) {
console.log('retrying', retry);
return askForNextAction(messages, retry - 1);
}
console.log('error parsing', e);
return { gptResponse: null, responseRaw };
}
}
// tasks
const runCommand = async ({ command }, streamId) => {
let output = await runCommandsInSandbox([command], streamId);
console.log('runCommand', output);
return {
"action": "run command",
"command": command,
"output": output
}
}
const createFile = async ({ path, content }) => {
let output = await runCommandsInSandbox(`echo "${content}" > ${path}`);
return {
"action": "create file",
"path": path,
"output": 'ok'
}
}
const isIndented = (numberedLine) => {
const line = numberedLine.split(': ').slice(1).join(': ');
return line.startsWith(' ') || line.startsWith('\t') || line === '';
};
const getShortContent = async (file) => {
file = getAbsolutePath(file);
let fileContents = 'File does not exist';
try {
const document = await vscode.workspace.openTextDocument(file);
fileContents = addLineNumbers(document.getText());
} catch (e) { }
// include only lines that are not indented. Note that the line numbers are already added. Insert a line with '...' where the code is removed
const f = [];
for (let i = 0; i < fileContents.length; i++) {
if (isIndented(fileContents[i])) {
if (f[f.length - 1] !== '...') {
f.push('...');
}
} else {
f.push(fileContents[i]);
}
}
return f.join('\n');
}
const showFileSummary = async ({ path }) => {
const output = await getShortContent(path);
console.log('getShortContent', output);
return {
"action": "show file summary",
"path": path,
"output": output
}
}
const addLineNumbers = (fileContent) => {
const lines = fileContent.split('\n');
const numberedLines = lines.map((line, index) => `${index + 1}: ${line}`);
return numberedLines;
};
const getSectionContent = async (path, start, end) => {
const document = await vscode.workspace.openTextDocument(path);
const lines = addLineNumbers(document.getText());
const section = lines.slice(parseInt(start) - 1, parseInt(end)).join('\n');
return section;
}
const getAbsolutePath = (path) => {
console.log('getAbsolutePath', path);
if (!path.startsWith('/')) {
const rootDir = vscode.workspace.workspaceFolders[0].uri.path;
path = `${rootDir}/${path}`;
}
console.log('getAbsolutePath', path);
return path;
}
const viewSection = async ({ path, start, end }) => {
// make path absolute
path = getAbsolutePath(path);
const output = await getSectionContent(path, start, end);
return {
"action": "view section",
"path": path,
"start": start,
"end": end,
"output": output
}
}
const applyDiffs = async (diff, save) => {
console.log('applyDiffs', diff);
const document = await vscode.workspace.openTextDocument(diff.path);
const editRange = new vscode.Range(
new vscode.Position(parseInt(diff.start) - 1, 0),
new vscode.Position(parseInt(diff.end), 0)
);
// remove the line numbers from the code if they exist
const codeAfter = diff.content.replace(/^[0-9]+: /gm, '');
const edit = new vscode.TextEdit(editRange, codeAfter + '\n');
const workspaceEdit = new vscode.WorkspaceEdit();
workspaceEdit.set(document.uri, [edit]);
await vscode.workspace.applyEdit(workspaceEdit);
await vscode.commands.executeCommand('editor.action.formatDocument', document.uri);
if (save) {
await document.save();
}
}
const previewEditFile = async ({ path, start, end, content }, fileEdits) => {
path = getAbsolutePath(path);
const document = await vscode.workspace.openTextDocument(path);
const lines = document.getText().split('\n');
const newLines = lines.slice(0, parseInt(start) - 1).concat(content.split('\n')).concat(lines.slice(parseInt(end)));
const newDocument = newLines.join('\n');
const newLinesWithNumbers = addLineNumbers(newDocument);
const startLine = Math.max(parseInt(start - 4), 1);
const newEnd = start + content.split('\n').length + 4;
const endLine = Math.min(newEnd, newLinesWithNumbers.length);
const newContent = newLinesWithNumbers.slice(startLine - 1, endLine).join('\n');
console.log({content, newContent})
fileEdits.push({
path,
start,
end,
content,
validated: false
});
return [{
"action": "edit file",
"path": path,
"start": startLine,
"end": endLine,
"content": newContent,
"info": "This is a preview. Check if the appended new content is correct - in particular, check if the edit specified the correct line range (i.e. the first and last lines of the edit are not duplicated, no line original line is missing). If it looks correct, respond with the 'validate edit'. If you 'validate and apply', the file is saved and future action. If you want to discard this edit, simply respond with a different action (e.g. a new file edit)."
}, fileEdits];
}
const applyEditFile = async (message, fileEdits) => {
fileEdits[fileEdits.length - 1].validated = true;
return [{
"action": "validate edit",
"info": "The file edit is saved and will be applied when you finish the task. In future file edits, refer to the lines by their old numbers, as all diffs are applied in the end.",
}, fileEdits];
}
const validateAndApplyEditFile = async (message, fileEdits) => {
fileEdits[fileEdits.length - 1].validated = true;
await sortAndApplyFileEdits(fileEdits, true);
const fileSummary = (await Promise.all(
fileEdits.map(async (fileEdit) => {
const summary = await getShortContent(fileEdit.path);
return `# path: ${fileEdit.path}\n${summary}\n`;
}))).join('\n');
return [{
"action": "validate and apply",
"info": "The file is updated. Future edits will potentially need to refer to updated line numbers.",
"output": fileSummary
}, []];
}
// const searchFolder = async ({searchTerm, includeRegex, excludeRegex}) => {
// // TODO only search those files that match the includeRegex and don't match the excludeRegex
// let output =
// return {
// "action": "search folder",
// "search_term": searchTerm,
// "include_regex": includeRegex,
// "exclude_regex": excludeRegex,
// "output": output
// }
// }
const executeTask = async (message, streamId, fileEdits) => {
console.log('executeTask', message);
try {
switch (message.action) {
case 'run command':
return [await runCommand(message, streamId), fileEdits];
case 'create file':
return [await createFile(message), fileEdits];
case 'show file summary':
return [await showFileSummary(message), fileEdits];
case 'view section':
return [await viewSection(message), fileEdits];
case 'edit file':
return await previewEditFile(message, fileEdits);
case 'validate edit':
return await applyEditFile(message, fileEdits);
case 'validate and apply':
return await validateAndApplyEditFile(message, fileEdits);
// case 'search folder':
// return await searchFolder(message);
case 'task completed':
return [message, fileEdits];
default:
// retry
return [{
"error": "unknown action",
}, fileEdits];
}
} catch (error) {
console.log('error', error.message, error.stack);
return [{
"action": message.action,
"error": `error while performing action: ${error} - please try again.`,
}, fileEdits];
}
}
module.exports = {
performTask
}