-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.js
195 lines (166 loc) · 6.51 KB
/
extension.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
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
const {runCommandsInSandbox, restartSandbox} = require('./runInSandbox.js');
const {createPanel} = require('./frontend.js');
const {getAdditionalContext} = require('./utils.js');
const {performTask} = require('./performTask.js');
// interface Range {
// start: {
// line: number,
// character: number
// },
// end: {
// line: number,
// character: number
// }
// }
// interface FileDiff {
// range: Range,
// code_before: string,
// code_after: string,
// filename: string
// request: string,
// message: string
// }
// interface RequiredContent {
// range: Range,
// code_before: string,
// filename: string
// request: string,
// message: string
// }
const getImplementPrompt = (featureDescription, currentFilePath) => {
let prefix = "";
if (currentFilePath) {
prefix = `In the file ${currentFilePath}:\n`;
}
return `${prefix}I want to implement the following feature: ${featureDescription}.`;
};
const getEditPrompt = (selection, featureDescription) => {
return `In this section:\n${selection}\nI want you to do this: ${featureDescription}`
}
const getDebugPrompt = (command, output) => {
return `I need your help debugging this command:\n\`\`\`${command}\`\`\`\nCurrently, I get:\n\`\`\`${output}\`\`\`\n`;
}
const applyDiffs = async (fileDiffs) => {
console.log("applying: ", fileDiffs);
const sortedFileDiffs = fileDiffs.sort((a, b) => b.range.start.line - a.range.start.line);
for (const diff of sortedFileDiffs) {
const document = await vscode.workspace.openTextDocument(diff.filepath);
const editRange = new vscode.Range(
new vscode.Position(parseInt(diff.range.start.line) - 1, parseInt(diff.range.start.character)),
new vscode.Position(parseInt(diff.range.end.line), parseInt(diff.range.end.character))
);
// remove the line numbers from the code if they exist
const codeAfter = diff.code_after.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);
}
}
const editSelection = async (context) => {
const editor = vscode.window.activeTextEditor;
const selection = editor.selection;
const featureDescription = await vscode.window.showInputBox({
prompt: 'What should be done with the selected code?',
placeHolder: 'e.g. "refactor"'
});
if (!featureDescription) {
return;
}
const filepath = editor.document.uri.path;
// context: e.g. {"selection": "code", "currentFile": "path/to/file", "start": 1, "end": 10}
const initialAssistantMessage = {
"action": "view section",
"path": filepath,
"start": selection.start.line + 1,
"end": selection.end.line + 1
};
const prompt = `Edit the selected code: ${featureDescription}`;
await createPanel(context);
const finalMessage = await performTask(prompt, {
'path': filepath,
'start': selection.start.line + 1,
'end': selection.end.line + 1
}, initialAssistantMessage);
vscode.window.showInformationMessage(`Selection edited! ${finalMessage}`);
};
// debug asks for a command to debug, then runs it in the sandbox and sends the output back to chatgpt.
// we need to make a loop similar to performTask, but also parse the `# Shell` section, run the commands, and resond with a `# Output` section
// we ask ChatGPT also if we are done debugging, and if so, we exit the loop
async function debugCommand(context) {
const commandPrompt = await vscode.window.showInputBox({
prompt: 'What command would you like to debug?',
placeHolder: 'e.g. "pytest test'
});
if (!commandPrompt) {
return;
}
await createPanel(context);
const task = `Debug the following command: \`${commandPrompt}\``;
const finalMessage = await performTask(task, {}, {"action": "run command", "command": commandPrompt});
vscode.window.showInformationMessage(`Command debugged! ${finalMessage}`);
}
const implementFeature = async (context) => {
const editor = vscode.window.activeTextEditor;
// The code you place here will be executed every time your command is executed
// Display a message box to the user
// const selection = editor?.selection;
const featureDescription = await vscode.window.showInputBox({
prompt: 'Enter a feature description',
placeHolder: 'e.g. "add a menu bar to the top of the page using a new component"'
});
console.log("feature description: ", featureDescription);
if (!featureDescription) {
return;
}
let currentFilePath;
try {
currentFilePath = editor.document.uri.path;
} catch (e) {
console.log(e);
}
await createPanel(context);
console.log("created panel");
const finalMessage = await performTask(featureDescription,
{"currentFile": currentFilePath},
{
"action": "show file summary",
"path": currentFilePath
}
);
// todo: sanity check that the file diffs are what chadgpt wanted
// const validatePrompt = `You were just tasked to do the following: ${featureDescription}.You proposed the following changes: ${fileDiffs.map(renderDiffForMessage)}. Are these changes correct? If yes, answer with 'yes' - otherwise suggest a new edit.`;
vscode.window.showInformationMessage(`Feature implemented! ${finalMessage}`);
}
function activate(context) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
context.subscriptions.push(
vscode.commands.registerCommand('chadgpt.implementFeature', async () => await implementFeature(context))
);
context.subscriptions.push(
vscode.commands.registerCommand('chadgpt.showChadGPT', async () => await createPanel(context))
);
context.subscriptions.push(
vscode.commands.registerCommand('chadgpt.editSelection', async () => await editSelection(context))
);
context.subscriptions.push(
vscode.commands.registerCommand('chadgpt.debug', async () => await debugCommand(context))
);
context.subscriptions.push(
vscode.commands.registerCommand('chadgpt.restartSandbox', async () => await restartSandbox())
);
}
// This method is called when your extension is deactivated
function deactivate() { }
module.exports = {
activate,
deactivate
}