From 572b4a39f763c86c1c1425edfe4eab2b5475a485 Mon Sep 17 00:00:00 2001 From: Iheuzio Date: Fri, 23 Jun 2023 14:27:57 -0400 Subject: [PATCH 1/3] feature(sidebar): Renders webpanel on the side --- extension.js | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ images/files.svg | 1 + package.json | 23 ++++++++++++-- 3 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 images/files.svg diff --git a/extension.js b/extension.js index 8a9c867..48021b3 100644 --- a/extension.js +++ b/extension.js @@ -260,6 +260,84 @@ function activate(context) { context.subscriptions.push(clearSelectedFilesCommand); context.subscriptions.push(refreshFilesCommand); vscode.window.registerTreeDataProvider('selectedFiles', fileDataProvider); + + const provider = { + resolveWebviewView(webviewView) { + webviewView.webview.options = { + enableScripts: true + }; + webviewView.webview.html = getWebviewContent(); + webviewView.webview.onDidReceiveMessage(async message => { + if (message.command === 'submitQuestion') { + const question = message.text; + const selectedUris = message.selectedUris; + + // Update the selectedFiles array based on the selectedUris + selectedFiles.forEach(file => { + file.selected = selectedUris.includes(file.uri.fsPath); + }); + + fileDataProvider.refresh(); + + const fileContents = selectedFiles + .filter(file => file.selected) + .map(file => { + const document = vscode.workspace.textDocuments.find(doc => doc.uri.fsPath === file.uri.fsPath); + if (document) { + const lines = document.getText().split('\n'); + const formattedLines = lines.map(line => `\t${line}`).join('\n'); + return `${file.uri.fsPath}:\n\`\`\`\n${formattedLines}\n\`\`\``; + } + return ''; + }) + .join('\n\n'); + + // Call OpenAI API with the question and file contents + try { + const chatCompletion = await openai.createChatCompletion({ + model: "gpt-3.5-turbo-16k", + messages: [ + { role: "system", content: "Answer the coding questions, only provide the code and documentation, explaining the solution after providing the code." }, + { role: "user", content: question }, + { role: "assistant", content: fileContents } + ], + }); + + // Extract the answer from the OpenAI response + const answer = chatCompletion.data.choices[0].message.content; + + // Update the webview content to display only the OpenAI response + webviewView.webview.html = getWebviewContent(answer, question); + } catch (error) { + // Handle any errors from the OpenAI API + console.error("Failed to get OpenAI response:", error); + webviewView.webview.html = getWebviewContent(`Failed to get response from OpenAI API. Error: ${error.message}`, question); + } + } else if (message.command === 'toggleFileSelection') { + const uri = message.uri; + const file = selectedFiles.find(file => file.uri.fsPath === uri); + if (file) { + file.toggleSelected(); + fileDataProvider.refresh(); + } + } else if (message.command === 'clearSelectedFiles') { + const clearedFiles = selectedFiles.filter(file => file.selected === false); + selectedFiles.length = 0; // Clear the array + clearedFiles.forEach(file => { + fileDataProvider.refresh(); + }); + webviewView.webview.html = getWebviewContent(); + } else if (message.command === 'refreshFiles') { + fileDataProvider.refresh(); + webviewView.webview.html = getWebviewContent(); + } + }); + } + }; + + context.subscriptions.push(vscode.window.registerWebviewViewProvider('gpt-context-sidebar', provider)); + } + exports.activate = activate; diff --git a/images/files.svg b/images/files.svg new file mode 100644 index 0000000..08261df --- /dev/null +++ b/images/files.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/package.json b/package.json index 6d7adb3..6880558 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,8 @@ ], "activationEvents": [ "onCommand:extension.addFilesToGPTContext", - "onCommand:extension.openGPTContextPanel" + "onCommand:extension.openGPTContextPanel", + "onCommand:extension.gpt-context-sidebar" ], "main": "./extension.js", "contributes": { @@ -54,6 +55,15 @@ "title": "Clear Selected Files" } ], + "viewsContainers" : { + "activitybar": [ + { + "id": "gpt-contextfiles-sidebar-view", + "title": "GPT Context", + "icon": "images/files.svg" + } + ] + }, "menus": { "explorer/context": [ { @@ -70,8 +80,17 @@ "name": "Selected Files", "when": "explorerResourceIsFolder && explorerViewletVisible" } + ], + "gpt-contextfiles-sidebar-view": [ + { + "type": "webview", + "id": "gpt-context-sidebar", + "name": "GPTContextFiles", + "icon": "images/files.svg", + "contextualTitle": "GPTContext" + } ] - } + } }, "scripts": { "lint": "eslint .", From b756de10e470f8abb453569afa93bd6f46bc0211 Mon Sep 17 00:00:00 2001 From: Iheuzio Date: Fri, 23 Jun 2023 14:43:58 -0400 Subject: [PATCH 2/3] fix(redundant-code): Fixing redundant code --- extension.js | 148 +++++++++++++++++++++++++-------------------------- 1 file changed, 74 insertions(+), 74 deletions(-) diff --git a/extension.js b/extension.js index 48021b3..1d69106 100644 --- a/extension.js +++ b/extension.js @@ -261,82 +261,82 @@ function activate(context) { context.subscriptions.push(refreshFilesCommand); vscode.window.registerTreeDataProvider('selectedFiles', fileDataProvider); - const provider = { - resolveWebviewView(webviewView) { - webviewView.webview.options = { - enableScripts: true - }; - webviewView.webview.html = getWebviewContent(); - webviewView.webview.onDidReceiveMessage(async message => { - if (message.command === 'submitQuestion') { - const question = message.text; - const selectedUris = message.selectedUris; - - // Update the selectedFiles array based on the selectedUris - selectedFiles.forEach(file => { - file.selected = selectedUris.includes(file.uri.fsPath); - }); - +const provider = { + resolveWebviewView(webviewView) { + webviewView.webview.options = { + enableScripts: true + }; + webviewView.webview.html = getWebviewContent(); + webviewView.webview.onDidReceiveMessage(async message => { + if (message.command === 'toggleFileSelection') { + const uri = message.uri; + const file = selectedFiles.find(file => file.uri.fsPath === uri); + if (file) { + file.toggleSelected(); fileDataProvider.refresh(); - - const fileContents = selectedFiles - .filter(file => file.selected) - .map(file => { - const document = vscode.workspace.textDocuments.find(doc => doc.uri.fsPath === file.uri.fsPath); - if (document) { - const lines = document.getText().split('\n'); - const formattedLines = lines.map(line => `\t${line}`).join('\n'); - return `${file.uri.fsPath}:\n\`\`\`\n${formattedLines}\n\`\`\``; - } - return ''; - }) - .join('\n\n'); - - // Call OpenAI API with the question and file contents - try { - const chatCompletion = await openai.createChatCompletion({ - model: "gpt-3.5-turbo-16k", - messages: [ - { role: "system", content: "Answer the coding questions, only provide the code and documentation, explaining the solution after providing the code." }, - { role: "user", content: question }, - { role: "assistant", content: fileContents } - ], - }); - - // Extract the answer from the OpenAI response - const answer = chatCompletion.data.choices[0].message.content; - - // Update the webview content to display only the OpenAI response - webviewView.webview.html = getWebviewContent(answer, question); - } catch (error) { - // Handle any errors from the OpenAI API - console.error("Failed to get OpenAI response:", error); - webviewView.webview.html = getWebviewContent(`Failed to get response from OpenAI API. Error: ${error.message}`, question); - } - } else if (message.command === 'toggleFileSelection') { - const uri = message.uri; - const file = selectedFiles.find(file => file.uri.fsPath === uri); - if (file) { - file.toggleSelected(); - fileDataProvider.refresh(); - } - } else if (message.command === 'clearSelectedFiles') { - const clearedFiles = selectedFiles.filter(file => file.selected === false); - selectedFiles.length = 0; // Clear the array - clearedFiles.forEach(file => { - fileDataProvider.refresh(); - }); - webviewView.webview.html = getWebviewContent(); - } else if (message.command === 'refreshFiles') { - fileDataProvider.refresh(); - webviewView.webview.html = getWebviewContent(); } - }); - } - }; - - context.subscriptions.push(vscode.window.registerWebviewViewProvider('gpt-context-sidebar', provider)); - + } else if (message.command === 'clearSelectedFiles') { + const clearedFiles = selectedFiles.filter(file => file.selected === false); + selectedFiles.length = 0; // Clear the array + clearedFiles.forEach(file => { + fileDataProvider.refresh(); + }); + webviewView.webview.html = getWebviewContent(); + } else if (message.command === 'refreshFiles') { + fileDataProvider.refresh(); + webviewView.webview.html = getWebviewContent(); + } else if (message.command === 'submitQuestion') { + const question = message.text; + const selectedUris = message.selectedUris; + + // Update the selectedFiles array based on the selectedUris + selectedFiles.forEach(file => { + file.selected = selectedUris.includes(file.uri.fsPath); + }); + + fileDataProvider.refresh(); + + const fileContents = selectedFiles + .filter(file => file.selected) + .map(file => { + const document = vscode.workspace.textDocuments.find(doc => doc.uri.fsPath === file.uri.fsPath); + if (document) { + const lines = document.getText().split('\n'); + const formattedLines = lines.map(line => `\t${line}`).join('\n'); + return `${file.uri.fsPath}:\n\`\`\`\n${formattedLines}\n\`\`\``; + } + return ''; + }) + .join('\n\n'); + + // Call OpenAI API with the question and file contents + try { + const chatCompletion = await openai.createChatCompletion({ + model: "gpt-3.5-turbo-16k", + messages: [ + { role: "system", content: "Answer the coding questions, only provide the code and documentation, explaining the solution after providing the code." }, + { role: "user", content: question }, + { role: "assistant", content: fileContents } + ], + }); + + // Extract the answer from the OpenAI response + const answer = chatCompletion.data.choices[0].message.content; + + // Update the webview content to display only the OpenAI response + webviewView.webview.html = getWebviewContent(answer, question); + } catch (error) { + // Handle any errors from the OpenAI API + console.error("Failed to get OpenAI response:", error); + webviewView.webview.html = getWebviewContent(`Failed to get response from OpenAI API. Error: ${error.message}`, question); + } + } + }); + } +}; + +context.subscriptions.push(vscode.window.registerWebviewViewProvider('gpt-context-sidebar', provider)); + } From 0ec8dc8ac40db011f4f7ece6748ffd8cfe1cd93f Mon Sep 17 00:00:00 2001 From: Iheuzio Date: Fri, 23 Jun 2023 15:05:45 -0400 Subject: [PATCH 3/3] fix(redudant-code): Removed redundancy --- extension.js | 135 ++++++++++++++++++--------------------------------- 1 file changed, 47 insertions(+), 88 deletions(-) diff --git a/extension.js b/extension.js index 1d69106..2deab94 100644 --- a/extension.js +++ b/extension.js @@ -73,6 +73,51 @@ const addFilesCommand = vscode.commands.registerCommand('extension.addFilesToGPT const fileDataProvider = new FileDataProvider(); +// Function to handle question submission +async function handleQuestionSubmission(panel, question, selectedUris) { + // Update the selectedFiles array based on the selectedUris + selectedFiles.forEach(file => { + file.selected = selectedUris.includes(file.uri.fsPath); + }); + + fileDataProvider.refresh(); + + const fileContents = selectedFiles + .filter(file => file.selected) + .map(file => { + const document = vscode.workspace.textDocuments.find(doc => doc.uri.fsPath === file.uri.fsPath); + if (document) { + const lines = document.getText().split('\n'); + const formattedLines = lines.map(line => `\t${line}`).join('\n'); + return `${file.uri.fsPath}:\n\`\`\`\n${formattedLines}\n\`\`\``; + } + return ''; + }) + .join('\n\n'); + + // Call OpenAI API with the question and file contents + try { + const chatCompletion = await openai.createChatCompletion({ + model: "gpt-3.5-turbo-16k", + messages: [ + { role: "system", content: "Answer the coding questions, only provide the code and documentation, explaining the solution after providing the code." }, + { role: "user", content: question }, + { role: "assistant", content: fileContents } + ], + }); + + // Extract the answer from the OpenAI response + const answer = chatCompletion.data.choices[0].message.content; + + // Update the webview content to display only the OpenAI response + panel.webview.html = getWebviewContent(answer, question); + } catch (error) { + // Handle any errors from the OpenAI API + console.error("Failed to get OpenAI response:", error); + panel.webview.html = getWebviewContent(`Failed to get response from OpenAI API. Error: ${error.message}`, question); + } +} + // Command for displaying the webview panel const openGPTContextPanelCommand = vscode.commands.registerCommand('extension.openGPTContextPanel', () => { const panel = vscode.window.createWebviewPanel( @@ -88,50 +133,7 @@ const openGPTContextPanelCommand = vscode.commands.registerCommand('extension.op panel.webview.onDidReceiveMessage(async message => { if (message.command === 'submitQuestion') { - const question = message.text; - const selectedUris = message.selectedUris; - - // Update the selectedFiles array based on the selectedUris - selectedFiles.forEach(file => { - file.selected = selectedUris.includes(file.uri.fsPath); - }); - - fileDataProvider.refresh(); - - const fileContents = selectedFiles - .filter(file => file.selected) - .map(file => { - const document = vscode.workspace.textDocuments.find(doc => doc.uri.fsPath === file.uri.fsPath); - if (document) { - const lines = document.getText().split('\n'); - const formattedLines = lines.map(line => `\t${line}`).join('\n'); - return `${file.uri.fsPath}:\n\`\`\`\n${formattedLines}\n\`\`\``; - } - return ''; - }) - .join('\n\n'); - - // Call OpenAI API with the question and file contents - try { - const chatCompletion = await openai.createChatCompletion({ - model: "gpt-3.5-turbo-16k", - messages: [ - { role: "system", content: "Answer the coding questions, only provide the code and documentation, explaining the solution after providing the code." }, - { role: "user", content: question }, - { role: "assistant", content: fileContents } - ], - }); - - // Extract the answer from the OpenAI response - const answer = chatCompletion.data.choices[0].message.content; - - // Update the webview content to display only the OpenAI response - panel.webview.html = getWebviewContent(answer, question); - } catch (error) { - // Handle any errors from the OpenAI API - console.error("Failed to get OpenAI response:", error); - panel.webview.html = getWebviewContent(`Failed to get response from OpenAI API. Error: ${error.message}`, question); - } + await handleQuestionSubmission(panel, message.text, message.selectedUris); } else if (message.command === 'toggleFileSelection') { const uri = message.uri; const file = selectedFiles.find(file => file.uri.fsPath === uri); @@ -286,50 +288,7 @@ const provider = { fileDataProvider.refresh(); webviewView.webview.html = getWebviewContent(); } else if (message.command === 'submitQuestion') { - const question = message.text; - const selectedUris = message.selectedUris; - - // Update the selectedFiles array based on the selectedUris - selectedFiles.forEach(file => { - file.selected = selectedUris.includes(file.uri.fsPath); - }); - - fileDataProvider.refresh(); - - const fileContents = selectedFiles - .filter(file => file.selected) - .map(file => { - const document = vscode.workspace.textDocuments.find(doc => doc.uri.fsPath === file.uri.fsPath); - if (document) { - const lines = document.getText().split('\n'); - const formattedLines = lines.map(line => `\t${line}`).join('\n'); - return `${file.uri.fsPath}:\n\`\`\`\n${formattedLines}\n\`\`\``; - } - return ''; - }) - .join('\n\n'); - - // Call OpenAI API with the question and file contents - try { - const chatCompletion = await openai.createChatCompletion({ - model: "gpt-3.5-turbo-16k", - messages: [ - { role: "system", content: "Answer the coding questions, only provide the code and documentation, explaining the solution after providing the code." }, - { role: "user", content: question }, - { role: "assistant", content: fileContents } - ], - }); - - // Extract the answer from the OpenAI response - const answer = chatCompletion.data.choices[0].message.content; - - // Update the webview content to display only the OpenAI response - webviewView.webview.html = getWebviewContent(answer, question); - } catch (error) { - // Handle any errors from the OpenAI API - console.error("Failed to get OpenAI response:", error); - webviewView.webview.html = getWebviewContent(`Failed to get response from OpenAI API. Error: ${error.message}`, question); - } + await handleQuestionSubmission(webviewView, message.text, message.selectedUris); } }); }