Compare commits

..

No commits in common. "main" and "0.2.0" have entirely different histories.
main ... 0.2.0

18 changed files with 330 additions and 409 deletions

View File

@ -4,52 +4,6 @@ Version History
## [Unreleased] ## [Unreleased]
## [Release 0.2.3]
## What's Changed
* fix(display-html): html is now properly displayed by @Iheuzio in https://github.com/Iheuzio/gpt-contextfiles/pull/38
* Release 0.2.3 by @Iheuzio in https://github.com/Iheuzio/gpt-contextfiles/pull/39
**Full Changelog**: https://github.com/Iheuzio/gpt-contextfiles/compare/0.2.2...0.2.3
## [Release 0.2.2]
## What's Changed
* Fix/organize files by @Iheuzio in https://github.com/Iheuzio/gpt-contextfiles/pull/32
* Fix/style rendered by @Iheuzio in https://github.com/Iheuzio/gpt-contextfiles/pull/33
* fix(style-rendered): padding around response by @Iheuzio in https://github.com/Iheuzio/gpt-contextfiles/pull/34
* update(readme-fix): added new features to readme by @Iheuzio in https://github.com/Iheuzio/gpt-contextfiles/pull/35
* Release 0.2.2 by @Iheuzio in https://github.com/Iheuzio/gpt-contextfiles/pull/36
**Full Changelog**: https://github.com/Iheuzio/gpt-contextfiles/compare/0.2.1...0.2.2
## [Release 0.2.1]
## What's Changed
* Update/logo by @Iheuzio in https://github.com/Iheuzio/gpt-contextfiles/pull/28
* feature(copy-code): add a copy, button improved ui by @Iheuzio in https://github.com/Iheuzio/gpt-contextfiles/pull/29
* Release 0.2.1 by @Iheuzio in https://github.com/Iheuzio/gpt-contextfiles/pull/30
**Full Changelog**: https://github.com/Iheuzio/gpt-contextfiles/compare/0.2.0...0.2.1
## [Release 0.2.0]
- Copy code
- Auto response
- Formatting
## What's Changed
* Feature/format code by @Iheuzio in https://github.com/Iheuzio/gpt-contextfiles/pull/23
* update(gif): Added new gif to reflect program by @Iheuzio in https://github.com/Iheuzio/gpt-contextfiles/pull/24
* feature(code-box): Can copy code and load responses by @Iheuzio in https://github.com/Iheuzio/gpt-contextfiles/pull/25
* Release 0.2.0 by @Iheuzio in https://github.com/Iheuzio/gpt-contextfiles/pull/26
**Full Changelog**: https://github.com/Iheuzio/gpt-contextfiles/compare/0.1.3...0.2.0
## [Release 0.1.3] ## [Release 0.1.3]
## What's Changed ## What's Changed

View File

@ -1,7 +1,5 @@
# gpt-contextfiles # gpt-contextfiles
![gpt-contextfiles viewport](./images/extension.png)
> if you'll like to contribute or provide any feedback check out the [link](https://github.com/Iheuzio/gpt-contextfiles/issues) > if you'll like to contribute or provide any feedback check out the [link](https://github.com/Iheuzio/gpt-contextfiles/issues)
This extension uses the openai api, there are many models avaliable: This extension uses the openai api, there are many models avaliable:
@ -10,7 +8,7 @@ https://openai.com/pricing
> However, being orientated with managing files this project defaults to the 16k context with GPT-3.5-turbo-16k > However, being orientated with managing files this project defaults to the 16k context with GPT-3.5-turbo-16k
If you wish to change the model, you must change the model in the `./src/gptContext.js` file If you wish to change the model, you must change the model in the extension.js file
# Examples # Examples
@ -34,12 +32,7 @@ Refresh -> refreshes the window so that all new files will be available for that
- Click on the extension addon to open the context window, refresh to update the files to check. - Click on the extension addon to open the context window, refresh to update the files to check.
- Select the files uses checkboxes - Select the files uses checkboxes
- Wait for response to be returned - Wait for response to be returned
- Copy the code (orange portion) and paste it into your code editor
Other features:
![Copy button demonstration](./images/copy-image.png)
- Click copy to to copy the snippet of code into your file for fast coding
![Copy individual code](./images/demo-copying-quotes.gif)
# How it works # How it works

View File

@ -1,6 +1,179 @@
const vscode = require('vscode'); const vscode = require('vscode');
const { selectedFiles } = require('./fileDataProvider'); const { Configuration, OpenAIApi } = require("openai");
// move these into the script so that instead of echoing the question and the contents,
// it will echo the question, followed by the answer from the response when the submit button is pressed.
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
// Represents a file item in the file explorer
class FileItem {
constructor(uri, selected = false) {
this.uri = uri;
this.selected = selected;
}
toggleSelected() {
this.selected = !this.selected;
}
}
// Represents the selected files in the file explorer
const selectedFiles = [];
// Tree data provider for the selected files
class FileDataProvider {
constructor() {
this._onDidChangeTreeData = new vscode.EventEmitter();
this.onDidChangeTreeData = this._onDidChangeTreeData.event;
}
refresh() {
this._onDidChangeTreeData.fire();
}
getTreeItem(element) {
return {
label: element.uri.fsPath,
collapsibleState: vscode.TreeItemCollapsibleState.None
};
}
getChildren(element) {
if (element) {
return [];
}
// Return only the selected files
return selectedFiles.filter(file => file.selected);
}
}
// Command for adding files to gpt-contextfiles
const addFilesCommand = vscode.commands.registerCommand('extension.addFilesToGPTContext', () => {
const editor = vscode.window.activeTextEditor;
if (editor) {
const uri = editor.document.uri;
const existingFileIndex = selectedFiles.findIndex(file => file.uri.fsPath === uri.fsPath);
if (existingFileIndex !== -1) {
// File already exists, remove it from the list
selectedFiles.splice(existingFileIndex, 1);
} else {
// Add the file to the list with selected state
selectedFiles.push(new FileItem(uri, true));
}
fileDataProvider.refresh();
}
});
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 + "\n" + 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(
'gptContextPanel',
'GPT Context',
vscode.ViewColumn.One,
{
enableScripts: true
}
);
panel.webview.html = getWebviewContent();
panel.webview.onDidReceiveMessage(async message => {
if (message.command === 'submitQuestion') {
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);
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();
});
panel.webview.html = getWebviewContent();
} else if (message.command === 'refreshFiles') {
fileDataProvider.refresh();
panel.webview.html = getWebviewContent();
}
});
});
// Command for refreshing the selected files
const refreshSelectedFilesCommand = vscode.commands.registerCommand('extension.refreshSelectedFiles', () => {
fileDataProvider.refresh();
});
// Command for clearing the selected files
const clearSelectedFilesCommand = vscode.commands.registerCommand('extension.clearSelectedFiles', () => {
selectedFiles.forEach(file => {
file.selected = false;
});
fileDataProvider.refresh();
});
// Command for refreshing all files
const refreshFilesCommand = vscode.commands.registerCommand('extension.refreshFiles', () => {
fileDataProvider.refresh();
});
// Helper function to generate the HTML content for the webview panel
function getWebviewContent(apiResponse = '', question = '') { function getWebviewContent(apiResponse = '', question = '') {
const fileList = selectedFiles const fileList = selectedFiles
.map( .map(
@ -20,7 +193,7 @@ function getWebviewContent(apiResponse = '', question = '') {
flex-direction: column; flex-direction: column;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
width: 90%; width: 50%;
height: 100%; height: 100%;
margin: 0 auto; margin: 0 auto;
background-color: #1e1e1e; background-color: #1e1e1e;
@ -55,13 +228,11 @@ function getWebviewContent(apiResponse = '', question = '') {
#response { #response {
white-space: pre-wrap; white-space: pre-wrap;
background: #343434;
border-radius: 5px;
padding: 10px;
} }
#file-list { #file-list {
margin-top: 20px; margin-top: 20px;
border
} }
.form-group { .form-group {
@ -164,7 +335,7 @@ function getWebviewContent(apiResponse = '', question = '') {
color: #d4d4d4; color: #d4d4d4;
cursor: pointer; cursor: pointer;
padding: 10px; padding: 10px;
width: 98%; width: 100%;
border: none; border: none;
outline: none; outline: none;
text-align: left; text-align: left;
@ -188,12 +359,12 @@ function getWebviewContent(apiResponse = '', question = '') {
display: none; display: none;
overflow: hidden; overflow: hidden;
background-color: #f1f1f1; background-color: #f1f1f1;
width: 98%; width: 100%;
} }
.content p { .content p {
margin-top: 0;
font-size: 14px; font-size: 14px;
margin: 0;
} }
.active, .active,
@ -232,7 +403,6 @@ function getWebviewContent(apiResponse = '', question = '') {
border: 1px solid white; border: 1px solid white;
border-radius: 5px; border-radius: 5px;
padding: 10px; padding: 10px;
color: white;
} }
#question-rep { #question-rep {
@ -241,8 +411,6 @@ function getWebviewContent(apiResponse = '', question = '') {
word-wrap: wrap; word-wrap: wrap;
border: 1px solid white; border: 1px solid white;
border-radius: 5px; border-radius: 5px;
padding: 10px;
margin-top: 20px;
} }
div#api-response.content.active { div#api-response.content.active {
@ -250,40 +418,17 @@ function getWebviewContent(apiResponse = '', question = '') {
} }
#code-block { #code-block {
padding: 10px 0 10px 10px; padding: 0;
border-radius: 5px; background: none;
background-color: black;
border: none; border: none;
font: inherit; font: inherit;
color: inherit; color: inherit;
cursor: pointer; cursor: pointer;
outline: inherit; outline: inherit;
width: 98%; margin: 0;
width: 100%;
text-align: left; text-align: left;
display: inline-block;
position: relative;
} }
#copy-button {
padding: 5px;
border-radius: 5px;
background-color: #007acc;
border: none;
font: inherit;
color: #fff;
cursor: pointer;
outline: inherit;
margin: 5px;
position: absolute;
top: 0;
right: 0;
}
#copy-button:hover {
background-color: #fff;
color: #000;
}
</style> </style>
</head> </head>
@ -304,19 +449,13 @@ function getWebviewContent(apiResponse = '', question = '') {
</div> </div>
<div class="content" id="api-response"> <div class="content" id="api-response">
<div id="question-rep"> <div id="question-rep">
<p>${ <p>${question ? '> ' + question : null}</p>
question = question.replace(/</g, '&lt;').replace(/>/g, '&gt;'),
question ? '> ' + question : null
}</p>
</div> </div>
${ ${
apiResponse ? ` apiResponse ? `
<div id="rendered"> <div id="rendered">
<p id="responses"> <p id="responses">
<pre id="response">${ <pre id="response">${apiResponse.replace(/```([^```]+)```/g, '<button onclick="copyCode()" id="code-block"><code>$1</code></button>')}</pre>
apiResponse = apiResponse.replace(/</g, '&lt;').replace(/>/g, '&gt;'),
apiResponse.replace(/```([^```]+)```/g, '<div id="code-block"><code>$1</code><button onclick="copyCode(event)" id="copy-button">copy</button></div>')
}</pre>
</p> </p>
</div> </div>
` : null ` : null
@ -337,12 +476,13 @@ function getWebviewContent(apiResponse = '', question = '') {
toggleApiResponse(); toggleApiResponse();
} }
function copyCode(event) { function copyCode() {
event.preventDefault(); event.preventDefault();
const codeBlock = event.target.parentNode.querySelector('code'); const codeBlocks = document.getElementsByTagName('code');
const selectedCodeBlock = event.target.closest('code');
if (codeBlock) { if (selectedCodeBlock) {
const codeText = codeBlock.innerText; const codeText = selectedCodeBlock.innerText;
const dummyTextArea = document.createElement('textarea'); const dummyTextArea = document.createElement('textarea');
dummyTextArea.value = codeText; dummyTextArea.value = codeText;
document.body.appendChild(dummyTextArea); document.body.appendChild(dummyTextArea);
@ -421,6 +561,53 @@ function getWebviewContent(apiResponse = '', question = '') {
`; `;
} }
module.exports = {
getWebviewContent
}; // Activates the extension
function activate(context) {
context.subscriptions.push(addFilesCommand);
context.subscriptions.push(openGPTContextPanelCommand);
context.subscriptions.push(refreshSelectedFilesCommand);
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 === '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();
} else if (message.command === 'submitQuestion') {
await handleQuestionSubmission(webviewView, message.text, message.selectedUris);
} else if (message.command === 'codeCopied') {
vscode.window.showInformationMessage('Code copied to clipboard');
}
});
}
};
context.subscriptions.push(vscode.window.registerWebviewViewProvider('gpt-context-sidebar', provider));
}
exports.activate = activate;

Binary file not shown.

Before

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 MiB

After

Width:  |  Height:  |  Size: 4.6 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 36 KiB

After

Width:  |  Height:  |  Size: 4.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 97 KiB

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "gpt-contextfiles", "name": "gpt-contextfiles",
"version": "0.2.3", "version": "0.0.1",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "gpt-contextfiles", "name": "gpt-contextfiles",
"version": "0.2.3", "version": "0.0.1",
"dependencies": { "dependencies": {
"openai": "^3.3.0" "openai": "^3.3.0"
}, },

View File

@ -1,8 +1,8 @@
{ {
"name": "gpt-contextfiles", "name": "gpt-contextfiles",
"displayName": "GPT-ContextFiles", "displayName": "GPT-ContextFiles",
"description": "Choose the files to pass into GPT to provide a question with multiple files", "description": "Choose the files to pass into GPT to provide a question with multiple files (doesn't check context)",
"version": "0.2.3", "version": "0.2.0",
"engines": { "engines": {
"vscode": "^1.79.0" "vscode": "^1.79.0"
}, },
@ -22,12 +22,7 @@
"testing", "testing",
"debugging", "debugging",
"files", "files",
"api", "api"
"snippets",
"openai",
"vscode",
"extension",
"chatbot"
], ],
"relatedTags": [ "relatedTags": [
"Artificial Intelligence", "Artificial Intelligence",
@ -39,7 +34,7 @@
"onCommand:extension.openGPTContextPanel", "onCommand:extension.openGPTContextPanel",
"onCommand:extension.gpt-context-sidebar" "onCommand:extension.gpt-context-sidebar"
], ],
"main": "./src/extension.js", "main": "./extension.js",
"contributes": { "contributes": {
"commands": [ "commands": [
{ {
@ -120,6 +115,6 @@
"type": "git", "type": "git",
"url": "https://github.com/Iheuzio/gpt-contextfiles/" "url": "https://github.com/Iheuzio/gpt-contextfiles/"
}, },
"icon": "images/gpt-icon.png", "icon": "images/gpt-icon.jpg",
"publisher": "Iheuzio" "publisher": "Iheuzio"
} }

View File

@ -1,84 +0,0 @@
const vscode = require('vscode');
const { selectedFiles, fileDataProvider, handleQuestionSubmission } = require('./gptContext');
const FileItem = require('./fileItem');
const { getWebviewContent } = require('./webviewPanel');
const addFilesCommand = vscode.commands.registerCommand('extension.addFilesToGPTContext', () => {
const editor = vscode.window.activeTextEditor;
if (editor) {
const uri = editor.document.uri;
const existingFileIndex = selectedFiles.findIndex(file => file.uri.fsPath === uri.fsPath);
if (existingFileIndex !== -1) {
// File already exists, remove it from the list
selectedFiles.splice(existingFileIndex, 1);
} else {
// Add the file to the list with selected state
selectedFiles.push(new FileItem(uri, true));
}
fileDataProvider.refresh();
}
});
const openGPTContextPanelCommand = vscode.commands.registerCommand('extension.openGPTContextPanel', () => {
const panel = vscode.window.createWebviewPanel(
'gptContextPanel',
'GPT Context',
vscode.ViewColumn.One,
{
enableScripts: true
}
);
panel.webview.html = getWebviewContent();
panel.webview.onDidReceiveMessage(async message => {
if (message.command === 'submitQuestion') {
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);
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();
});
panel.webview.html = getWebviewContent();
} else if (message.command === 'refreshFiles') {
fileDataProvider.refresh();
panel.webview.html = getWebviewContent();
}
});
});
const refreshSelectedFilesCommand = vscode.commands.registerCommand('extension.refreshSelectedFiles', () => {
fileDataProvider.refresh();
});
// Command for clearing the selected files
const clearSelectedFilesCommand = vscode.commands.registerCommand('extension.clearSelectedFiles', () => {
selectedFiles.forEach(file => {
file.selected = false;
});
fileDataProvider.refresh();
});
// Command for refreshing all files
const refreshFilesCommand = vscode.commands.registerCommand('extension.refreshFiles', () => {
fileDataProvider.refresh();
});
module.exports = {
addFilesCommand,
openGPTContextPanelCommand,
refreshSelectedFilesCommand,
clearSelectedFilesCommand,
refreshFilesCommand
};

View File

@ -1,52 +0,0 @@
const vscode = require('vscode');
const { addFilesCommand, openGPTContextPanelCommand, refreshSelectedFilesCommand, clearSelectedFilesCommand, refreshFilesCommand } = require('./commands');
const { getWebviewContent } = require('./webviewPanel');
const { selectedFiles, fileDataProvider, handleQuestionSubmission } = require('./gptContext');
function activate(context) {
context.subscriptions.push(addFilesCommand);
context.subscriptions.push(openGPTContextPanelCommand);
context.subscriptions.push(refreshSelectedFilesCommand);
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 === '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();
} else if (message.command === 'submitQuestion') {
await handleQuestionSubmission(webviewView, message.text, message.selectedUris);
} else if (message.command === 'codeCopied') {
vscode.window.showInformationMessage('Code copied to clipboard');
}
});
}
};
context.subscriptions.push(vscode.window.registerWebviewViewProvider('gpt-context-sidebar', provider));
}
exports.activate = activate;

View File

@ -1,38 +0,0 @@
const vscode = require('vscode');
const FileItem = require('./fileItem.js');
// Represents the selected files in the file explorer
const selectedFiles = [];
// Tree data provider for the selected files
class FileDataProvider {
constructor() {
this._onDidChangeTreeData = new vscode.EventEmitter();
this.onDidChangeTreeData = this._onDidChangeTreeData.event;
}
refresh() {
this._onDidChangeTreeData.fire();
}
getTreeItem(element) {
return {
label: element.uri.fsPath,
collapsibleState: vscode.TreeItemCollapsibleState.None
};
}
getChildren(element) {
if (element) {
return [];
}
// Return only the selected files
return selectedFiles.filter(file => file.selected);
}
}
module.exports = {
FileDataProvider,
selectedFiles
};

View File

@ -1,12 +0,0 @@
class FileItem {
constructor(uri, selected = false) {
this.uri = uri;
this.selected = selected;
}
toggleSelected() {
this.selected = !this.selected;
}
}
module.exports = FileItem;

View File

@ -1,63 +0,0 @@
const vscode = require('vscode');
const { Configuration, OpenAIApi } = require("openai");
const FileDataProvider = require('./fileDataProvider');
const { getWebviewContent } = require('./webviewPanel');
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const selectedFiles = FileDataProvider.selectedFiles;
const fileDataProvider = new FileDataProvider.FileDataProvider();
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. Put codeblocks inside ``` code ``` with file names above each snippet." },
{ role: "user", content: question + "\n" + 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);
}
}
module.exports = {
handleQuestionSubmission,
fileDataProvider,
selectedFiles
};

View File

@ -0,0 +1,41 @@
# Welcome to your VS Code Extension
## What's in the folder
* This folder contains all of the files necessary for your extension.
* `package.json` - this is the manifest file in which you declare your extension and command.
* The sample plugin registers a command and defines its title and command name. With this information VS Code can show the command in the command palette. It doesnt yet need to load the plugin.
* `extension.js` - this is the main file where you will provide the implementation of your command.
* The file exports one function, `activate`, which is called the very first time your extension is activated (in this case by executing the command). Inside the `activate` function we call `registerCommand`.
* We pass the function containing the implementation of the command as the second parameter to `registerCommand`.
## Get up and running straight away
* Press `F5` to open a new window with your extension loaded.
* Run your command from the command palette by pressing (`Ctrl+Shift+P` or `Cmd+Shift+P` on Mac) and typing `Hello World`.
* Set breakpoints in your code inside `extension.js` to debug your extension.
* Find output from your extension in the debug console.
## Make changes
* You can relaunch the extension from the debug toolbar after changing code in `extension.js`.
* You can also reload (`Ctrl+R` or `Cmd+R` on Mac) the VS Code window with your extension to load your changes.
## Explore the API
* You can open the full set of our API when you open the file `node_modules/@types/vscode/index.d.ts`.
## Run tests
* Open the debug viewlet (`Ctrl+Shift+D` or `Cmd+Shift+D` on Mac) and from the launch configuration dropdown pick `Extension Tests`.
* Press `F5` to run the tests in a new window with your extension loaded.
* See the output of the test result in the debug console.
* Make changes to `src/test/suite/extension.test.js` or create new test files inside the `test/suite` folder.
* The provided test runner will only consider files matching the name pattern `**.test.ts`.
* You can create folders inside the `test` folder to structure your tests any way you want.
## Go further
* [Follow UX guidelines](https://code.visualstudio.com/api/ux-guidelines/overview) to create extensions that seamlessly integrate with VS Code's native interface and patterns.
* [Publish your extension](https://code.visualstudio.com/api/working-with-extensions/publishing-extension) on the VS Code extension marketplace.
* Automate builds by setting up [Continuous Integration](https://code.visualstudio.com/api/working-with-extensions/continuous-integration).