-
Notifications
You must be signed in to change notification settings - Fork 364
/
Copy pathextension.ts
336 lines (272 loc) · 10.5 KB
/
extension.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
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
import * as vscode from 'vscode';
import { ChatGPTAPI, ChatGPTConversation } from 'chatgpt';
export function activate(context: vscode.ExtensionContext) {
// Get the API session token from the extension's configuration
const config = vscode.workspace.getConfiguration('chatgpt');
const sessionToken = config.get('sessionToken') as string|undefined;
// Create a new ChatGPTViewProvider instance and register it with the extension's context
const provider = new ChatGPTViewProvider(context.extensionUri);
provider.setSessionToken(sessionToken);
// Put configuration settings into the provider
provider.selectedInsideCodeblock = config.get('selectedInsideCodeblock') || false;
provider.pasteOnClick = config.get('pasteOnClick') || false;
provider.keepConversation = config.get('keepConversation') || false;
provider.timeoutLength = config.get('timeoutLength') || 60;
context.subscriptions.push(
vscode.window.registerWebviewViewProvider(ChatGPTViewProvider.viewType, provider, {
webviewOptions: { retainContextWhenHidden: true }
})
);
// Register the commands that can be called from the extension's package.json
const commandHandler = (command:string) => {
const config = vscode.workspace.getConfiguration('chatgpt');
const prompt = config.get(command) as string;
provider.search(prompt);
};
const commandAsk = vscode.commands.registerCommand('chatgpt.ask', () => {
vscode.window.showInputBox({ prompt: 'What do you want to do?' }).then((value) => {
provider.search(value);
});
});
const commandConversationId = vscode.commands.registerCommand('chatgpt.conversationId', () => {
vscode.window.showInputBox({
prompt: 'Set Conversation ID or delete it to reset the conversation',
placeHolder: 'conversationId (leave empty to reset)',
value: provider.getConversationId()
}).then((conversationId) => {
if (!conversationId) {
provider.setConversationId();
} else {
vscode.window.showInputBox({
prompt: 'Set Parent Message ID',
placeHolder: 'messageId (leave empty to reset)',
value: provider.getParentMessageId()
}).then((messageId) => {
provider.setConversationId(conversationId, messageId);
});
}
});
});
const commandExplain = vscode.commands.registerCommand('chatgpt.explain', () => {
commandHandler('promptPrefix.explain');
});
const commandRefactor = vscode.commands.registerCommand('chatgpt.refactor', () => {
commandHandler('promptPrefix.refactor');
});
const commandOptimize = vscode.commands.registerCommand('chatgpt.optimize', () => {
commandHandler('promptPrefix.optimize');
});
const commandProblems = vscode.commands.registerCommand('chatgpt.findProblems', () => {
commandHandler('promptPrefix.findProblems');
});
let commandResetConversation = vscode.commands.registerCommand('chatgpt.resetConversation', () => {
provider.setConversationId();
});
context.subscriptions.push(commandAsk, commandConversationId, commandExplain, commandRefactor, commandOptimize, commandProblems, commandResetConversation);
// Change the extension's session token when configuration is changed
vscode.workspace.onDidChangeConfiguration((event: vscode.ConfigurationChangeEvent) => {
if (event.affectsConfiguration('chatgpt.sessionToken')) {
// Get the extension's configuration
const config = vscode.workspace.getConfiguration('chatgpt');
const sessionToken = config.get('sessionToken') as string|undefined;
// add the new token to the provider
provider.setSessionToken(sessionToken);
} else if (event.affectsConfiguration('chatgpt.selectedInsideCodeblock')) {
const config = vscode.workspace.getConfiguration('chatgpt');
provider.selectedInsideCodeblock = config.get('selectedInsideCodeblock') || false;
} else if (event.affectsConfiguration('chatgpt.pasteOnClick')) {
const config = vscode.workspace.getConfiguration('chatgpt');
provider.pasteOnClick = config.get('pasteOnClick') || false;
} else if (event.affectsConfiguration('chatgpt.keepConversation')) {
const config = vscode.workspace.getConfiguration('chatgpt');
provider.keepConversation = config.get('keepConversation') || false;
}else if (event.affectsConfiguration('chatgpt.timeoutLength')) {
const config = vscode.workspace.getConfiguration('chatgpt');
provider.timeoutLength = config.get('timeoutLength') || 60;
}
});
}
class ChatGPTViewProvider implements vscode.WebviewViewProvider {
public static readonly viewType = 'chatgpt.chatView';
private _view?: vscode.WebviewView;
// This variable holds a reference to the ChatGPTAPI instance
private _chatGPTAPI?: ChatGPTAPI;
private _conversation?: ChatGPTConversation;
private _response?: string;
private _prompt?: string;
private _fullPrompt?: string;
public selectedInsideCodeblock = false;
public pasteOnClick = true;
public keepConversation = true;
public timeoutLength = 60;
private _sessionToken?: string;
// In the constructor, we store the URI of the extension
constructor(private readonly _extensionUri: vscode.Uri) {
}
// Set the session token and create a new API instance based on this token
public setSessionToken(sessionToken?: string) {
this._sessionToken = sessionToken;
this._newAPI();
}
public setConversationId(conversationId?: string, parentMessageId?: string) {
if (!conversationId || !parentMessageId) {
this._conversation = this._chatGPTAPI?.getConversation();
} else if (conversationId && parentMessageId) {
this._conversation = this._chatGPTAPI?.getConversation({conversationId: conversationId, parentMessageId: parentMessageId});
}
}
public getConversationId() {
return this._conversation?.conversationId;
}
public getParentMessageId() {
return this._conversation?.parentMessageId;
}
// This private method initializes a new ChatGPTAPI instance, using the session token if it is set
private _newAPI() {
if (!this._sessionToken) {
console.warn("Session token not set");
}else{
this._chatGPTAPI = new ChatGPTAPI({
sessionToken: this._sessionToken
});
this._conversation = this._chatGPTAPI.getConversation();
}
}
public resolveWebviewView(
webviewView: vscode.WebviewView,
context: vscode.WebviewViewResolveContext,
_token: vscode.CancellationToken,
) {
this._view = webviewView;
// set options for the webview
webviewView.webview.options = {
// Allow scripts in the webview
enableScripts: true,
localResourceRoots: [
this._extensionUri
]
};
// set the HTML for the webview
webviewView.webview.html = this._getHtmlForWebview(webviewView.webview);
// add an event listener for messages received by the webview
webviewView.webview.onDidReceiveMessage(data => {
switch (data.type) {
case 'codeSelected':
{
// do nothing if the pasteOnClick option is disabled
if (!this.pasteOnClick) {
break;
}
let code = data.value;
code = code.replace(/([^\\])(\$)([^{0-9])/g, "$1\\$$$3");
// insert the code as a snippet into the active text editor
vscode.window.activeTextEditor?.insertSnippet(new vscode.SnippetString(code));
break;
}
case 'prompt':
{
this.search(data.value);
}
}
});
}
public async search(prompt?:string) {
this._prompt = prompt;
if (!prompt) {
prompt = '';
};
// Check if the ChatGPTAPI instance is defined
if (!this._chatGPTAPI) {
this._newAPI();
}
// focus gpt activity from activity bar
if (!this._view) {
await vscode.commands.executeCommand('chatgpt.chatView.focus');
} else {
this._view?.show?.(true);
}
let response = '';
// Get the selected text of the active editor
const selection = vscode.window.activeTextEditor?.selection;
const selectedText = vscode.window.activeTextEditor?.document.getText(selection);
let searchPrompt = '';
if (selection && selectedText) {
// If there is a selection, add the prompt and the selected text to the search prompt
if (this.selectedInsideCodeblock) {
searchPrompt = `${prompt}\n\`\`\`\n${selectedText}\n\`\`\``;
} else {
searchPrompt = `${prompt}\n${selectedText}\n`;
}
} else {
// Otherwise, just use the prompt if user typed it
searchPrompt = prompt;
}
this._fullPrompt = searchPrompt;
if (!this._chatGPTAPI || !this._conversation) {
response = '[ERROR] Please enter an API key in the extension settings';
} else {
// If successfully signed in
console.log("sendMessage");
// Make sure the prompt is shown
this._view?.webview.postMessage({ type: 'setPrompt', value: this._prompt });
if (this._view) {
this._view.webview.postMessage({ type: 'addResponse', value: '...' });
}
let agent;
if (this.keepConversation) {
agent = this._conversation;
} else {
agent = this._chatGPTAPI;
}
try {
// Send the search prompt to the ChatGPTAPI instance and store the response
response = await agent.sendMessage(searchPrompt, {
onProgress: (partialResponse) => {
if (this._view && this._view.visible) {
this._view.webview.postMessage({ type: 'addResponse', value: partialResponse });
}
},
timeoutMs: this.timeoutLength * 1000
});
} catch (e) {
console.error(e);
response = `[ERROR] ${e}`;
}
}
// Saves the response
this._response = response;
// Show the view and send a message to the webview with the response
if (this._view) {
this._view.show?.(true);
this._view.webview.postMessage({ type: 'addResponse', value: response });
}
}
private _getHtmlForWebview(webview: vscode.Webview) {
const scriptUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, 'media', 'main.js'));
const microlightUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, 'media', 'scripts', 'microlight.min.js'));
const tailwindUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, 'media', 'scripts', 'showdown.min.js'));
const showdownUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, 'media', 'scripts', 'tailwind.min.js'));
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="${tailwindUri}"></script>
<script src="${showdownUri}"></script>
<script src="${microlightUri}"></script>
<style>
.code {
white-space : pre;
</style>
</head>
<body>
<input class="h-10 w-full text-white bg-stone-700 p-4 text-sm" type="text" id="prompt-input" />
<div id="response" class="pt-6 text-sm">
</div>
<script src="${scriptUri}"></script>
</body>
</html>`;
}
}
// This method is called when your extension is deactivated
export function deactivate() {}