-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
440 lines (391 loc) · 13.5 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
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
const vscode = require("vscode");
const { ChatGroq } = require("@langchain/groq");
const { HumanMessage, SystemMessage } = require("@langchain/core/messages");
const fs = require("fs");
const path = require("path");
let groqApiKey = null;
/**
* @param {vscode.ExtensionContext} context
*/
async function activate(context) {
let disposable = vscode.commands.registerCommand(
"extension.showCodeSuggestions",
async function () {
if (!groqApiKey) {
await promptForApiKey();
}
const editor = vscode.window.activeTextEditor;
if (editor) {
const document = editor.document;
const selection = editor.selection;
const text = document.getText(selection);
if (text) {
const workspaceFolders = vscode.workspace.workspaceFolders;
let contextFiles = "";
if (workspaceFolders && workspaceFolders.length > 0) {
const workspaceFolder = workspaceFolders[0].uri.fsPath;
contextFiles = await getContextFiles(workspaceFolder);
} else {
vscode.window.showInformationMessage(
"No workspace folder is open. Limited context will be used."
);
}
const model = new ChatGroq({
model: "llama3-8b-8192",
temperature: 0,
apiKey: groqApiKey,
});
const languageId = document.languageId;
const messages = [
new SystemMessage(
`You're a copilot. Just correct the following code in ${languageId} and rewrite it without any explanation.Do not write for exmaple, Here's corrected code or anything or do not type in any back text`
),
new HumanMessage(`Context from other files:\n${contextFiles}`),
new HumanMessage(text),
];
try {
const response = await model.invoke(messages);
const suggestion = response.content || "No suggestion available";
const panel = vscode.window.createWebviewPanel(
"codeSuggestions",
"Code Suggestions",
vscode.ViewColumn.Beside,
{
enableScripts: true,
}
);
panel.webview.html = getWebviewContent(suggestion);
panel.webview.onDidReceiveMessage(
async (message) => {
if (message.command === "askQuestion") {
const question = message.text;
const queryMessages = [
new SystemMessage(
`You are a code assistant. Provide answers or clarifications for the following code suggestion.`
),
new HumanMessage(
`Code suggestion:\n${suggestion}\n\nQuestion:\n${question}`
),
];
try {
const answerResponse = await model.invoke(queryMessages);
const answer =
answerResponse.content || "No answer available";
panel.webview.postMessage({
command: "displayAnswer",
text: answer,
});
} catch (error) {
console.error("Error querying Groq API:", error);
panel.webview.postMessage({
command: "displayAnswer",
text: `Error: ${error.message}`,
});
}
} else if (message.command === "replaceCode") {
const edit = new vscode.WorkspaceEdit();
edit.replace(document.uri, selection, suggestion.toString());
await vscode.workspace.applyEdit(edit);
vscode.window.showInformationMessage(
"Code replaced with suggestion."
);
} else if (message.command === "toggleDarkMode") {
const isDarkMode = message.isDarkMode;
panel.webview.postMessage({
command: "updateTheme",
isDarkMode,
});
}
},
undefined,
context.subscriptions
);
} catch (error) {
console.error("Error calling Groq API:", error);
vscode.window.showErrorMessage(
`Error fetching suggestion: ${error.message}`
);
}
} else {
vscode.window.showInformationMessage("No code selected.");
}
} else {
vscode.window.showInformationMessage("No active text editor.");
}
}
);
context.subscriptions.push(disposable);
}
async function promptForApiKey() {
const apiKey = await vscode.window.showInputBox({
placeHolder: "Enter your Groq API Key",
prompt: "API Key required for LangChain model",
ignoreFocusOut: true,
});
if (apiKey) {
groqApiKey = apiKey;
} else {
vscode.window.showErrorMessage(
"API Key is required for fetching suggestions."
);
}
}
async function getContextFiles(folderPath) {
const contextFiles = [];
const fileExtensions = [".py", ".js", ".ts"];
const readFiles = async (dir) => {
const files = await fs.promises.readdir(dir);
for (const file of files) {
const filePath = path.join(dir, file);
const stat = await fs.promises.stat(filePath);
if (stat.isDirectory() && file !== "node_modules") {
await readFiles(filePath);
} else if (fileExtensions.includes(path.extname(file))) {
const content = await fs.promises.readFile(filePath, "utf8");
contextFiles.push(`File: ${file}\n${content}`);
}
}
};
await readFiles(folderPath);
return contextFiles.join("\n\n");
}
function getWebviewContent(suggestion) {
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Groq Copilot</title>
<style>
@import url("https://fonts.googleapis.com/css2?family=Montserrat&display=swap");
* {box-sizing: border-box;}
body {
font-family: "Montserrat", sans-serif;
background-color: #fff;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
text-align: center;
min-height: 100vh;
margin: 0;
transition: background 0.2s linear;
}
body.dark {
background-color: #292c35;
}
body.dark h1, body.dark #suggestion, body.dark #question, body.dark .Btn-Container, body.dark #answer {
color: #fff;
}
h1 {
margin: 20px 0;
font-weight: bold;
display: flex;
justify-content: center;
width: 100%;
}
#container {
width: 100%;
max-width: 800px;
margin: 20px;
}
#suggestion {
border: 1px solid #e0e0e0;
padding: 15px;
white-space: pre-wrap;
overflow-wrap: break-word;
text-align: left;
background-color: #f7f7f7;
margin-bottom: 20px;
color: #e06c75; /* Red color similar to ChatGPT code highlights */
font-family: 'Courier New', Courier, monospace;
font-size: 14px;
border-radius: 5px;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
}
body.dark #suggestion {
border-color: #444;
background-color: #2e2e2e;
color: #e06c75; /* Red color similar to ChatGPT code highlights in dark mode */
}
#question {
border: 1px solid #e0e0e0;
padding: 10px;
background-color: #fff;
color: #333;
margin-bottom: 20px;
font-weight: bold;
}
body.dark #question {
border-color: #444;
background-color: #333;
color: #ddd;
}
#answer {
border: 1px solid #e0e0e0;
padding: 10px;
background-color: #fff;
color: #000;
margin-top: 20px;
font-family: Courier New, Courier, monospace;
}
body.dark #answer {
border-color: #444;
background-color: #333;
color: #ddd;
}
.Btn-Container {
display: flex;
width: 170px;
height: fit-content;
background-color: #1d2129;
border-radius: 40px;
box-shadow: 0px 5px 10px #bebebe;
justify-content: space-between;
align-items: center;
border: none;
cursor: pointer;
margin: 10px;
}
.icon-Container {
width: 45px;
height: 45px;
background-color: #f59aff;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
border: 3px solid #1d2129;
}
.text {
width: calc(170px - 45px);
height: 100%;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-size: 1.1em;
letter-spacing: 1.2px;
}
.icon-Container svg {
transition-duration: 1.5s;
}
.Btn-Container:hover .icon-Container svg {
transition-duration: 1.5s;
animation: arrow 1s linear infinite;
}
@keyframes arrow {
0% {
opacity: 0;
margin-left: 0px;
}
100% {
opacity: 1;
margin-left: 10px;
}
}
.checkbox {
opacity: 0;
position: absolute;
}
.checkbox-label {
background-color: #111;
width: 50px;
height: 26px;
border-radius: 50px;
position: relative;
padding: 5px;
cursor: pointer;
display: flex;
justify-content: space-between;
align-items: center;
}
.fa-moon {
color: #f1c40f;
}
.fa-sun {
color: #f39c12;
}
.checkbox-label .ball {
background-color: #fff;
width: 22px;
height: 22px;
position: absolute;
left: 2px;
top: 2px;
border-radius: 50%;
transition: transform 0.2s linear;
}
.checkbox:checked + .checkbox-label .ball {
transform: translateX(24px);
}
</style>
</head>
<body>
<div id="container">
<h1>Groq Copilot</h1>
<div>
<input type="checkbox" class="checkbox" id="checkbox">
<label for="checkbox" class="checkbox-label">
<i class="fas fa-moon"></i>
<i class="fas fa-sun"></i>
<span class="ball"></span>
</label>
</div>
<div id="suggestion">
<pre>${suggestion}</pre>
</div>
<textarea id="question" rows="5" placeholder="Ask a question about the suggestion..."></textarea>
<div style="display: flex; justify-content: center; gap: 10px; line-height: 1.5;">
<div class="Btn-Container" id="askButton">
<div class="icon-Container">
<!-- Optional: Include icons here -->
</div>
<div class="text">Clarify</div>
</div>
<div class="Btn-Container" id="replaceButton">
<div class="icon-Container">
<!-- Optional: Include icons here -->
</div>
<div class="text">Replace</div>
</div>
</div>
<div id="answer"></div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/js/all.min.js"></script>
<script>
const vscode = acquireVsCodeApi();
document.getElementById('askButton').addEventListener('click', () => {
const question = document.getElementById('question').value;
vscode.postMessage({
command: 'askQuestion',
text: question
});
});
document.getElementById('replaceButton').addEventListener('click', () => {
vscode.postMessage({
command: 'replaceCode'
});
});
document.getElementById('checkbox').addEventListener('change', () => {
const isDarkMode = document.getElementById('checkbox').checked;
document.body.classList.toggle('dark', isDarkMode);
// Update answer text color based on mode
document.getElementById('answer').style.color = isDarkMode ? '#ddd' : '#000';
});
window.addEventListener('message', event => {
const message = event.data;
if (message.command === 'displayAnswer') {
document.getElementById('answer').innerText = message.text;
document.getElementById('answer').style.color = document.body.classList.contains('dark') ? '#ddd' : '#000'; // Ensure the text color matches the mode
document.getElementById('answer').style.fontFamily = 'Courier New, Courier, monospace';
}
});
</script>
</body>
</html>`;
}
module.exports = {
activate,
deactivate: function () {},
};