diff --git a/README.md b/README.md index 456c695..217ec50 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ You simply right click each file you want to pass through, check or uncheck the # Installation -Not done yet +Add your api key to `OPENAI_API_KEY` for your windows/linux environment variable (tested with system variable) # Features @@ -18,7 +18,7 @@ Submit -> Submits the query to the api Refresh -> refreshes the window so that all new files will be available for that session. -User must ctrl+p and click on the `Open GPT Context Window` option and then add files (before or after), then input the question. +User must ctrl+shift+p and click on the `Open GPT Context Panel` option and then add files (before or after), then input the question. # Examples @@ -38,7 +38,6 @@ Selected Files: Expected Ouput: -``` +` The window.alert() method is a built-in JavaScript function that displays an alert box with a specified message and an OK button. In this case, the message is "Hello World!". -``` - +` diff --git a/extension.js b/extension.js index 11c844e..366328b 100644 --- a/extension.js +++ b/extension.js @@ -1,11 +1,23 @@ const vscode = require('vscode'); -const path = require('path'); +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, checked) { + constructor(uri, selected = false) { this.uri = uri; - this.checked = checked || false; + this.selected = selected; + } + + toggleSelected() { + this.selected = !this.selected; } } @@ -17,7 +29,6 @@ class FileDataProvider { constructor() { this._onDidChangeTreeData = new vscode.EventEmitter(); this.onDidChangeTreeData = this._onDidChangeTreeData.event; - this.filterPatterns = ['*.*']; // Default filter pattern } refresh() { @@ -27,8 +38,7 @@ class FileDataProvider { getTreeItem(element) { return { label: element.uri.fsPath, - collapsibleState: vscode.TreeItemCollapsibleState.None, - checked: element.checked + collapsibleState: vscode.TreeItemCollapsibleState.None }; } @@ -36,57 +46,31 @@ class FileDataProvider { if (element) { return []; } - return selectedFiles; - } - setFilter(filter) { - this.filterPatterns = filter.split(',').map(pattern => pattern.trim()); - this.refresh(); - } - - filterFiles(files) { - return files.filter(file => { - const extension = path.extname(file.uri.fsPath); - return this.filterPatterns.some(pattern => { - const regex = new RegExp(pattern.replace(/\./g, '\\.').replace(/\*/g, '.*')); - return regex.test(extension); - }); - }); + // 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 workspaceFolders = vscode.workspace.workspaceFolders; - if (workspaceFolders && workspaceFolders.length > 0) { - const workspacePath = workspaceFolders[0].uri.fsPath; - vscode.workspace.findFiles('**/*', '', 1000).then(files => { - const fileItems = files.map(file => new FileItem(file)); - selectedFiles.splice(0, selectedFiles.length, ...fileItems); - fileDataProvider.refresh(); - }); - } + 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(); + } }); -// Refresh the file list when workspace changes (file creation, deletion, renaming) -vscode.workspace.onDidChangeWorkspaceFolders(() => { - vscode.commands.executeCommand('extension.addFilesToGPTContext'); -}); - -vscode.workspace.onDidCreateFiles(() => { - vscode.commands.executeCommand('extension.addFilesToGPTContext'); -}); - -vscode.workspace.onDidDeleteFiles(() => { - vscode.commands.executeCommand('extension.addFilesToGPTContext'); -}); - -vscode.workspace.onDidRenameFiles(() => { - vscode.commands.executeCommand('extension.addFilesToGPTContext'); -}); - - - const fileDataProvider = new FileDataProvider(); // Command for displaying the webview panel @@ -102,133 +86,180 @@ const openGPTContextPanelCommand = vscode.commands.registerCommand('extension.op panel.webview.html = getWebviewContent(); - panel.webview.onDidReceiveMessage(message => { + panel.webview.onDidReceiveMessage(async message => { if (message.command === 'submitQuestion') { const question = message.text; - const selectedFilePaths = selectedFiles - .filter(file => file.checked) - .map(file => file.uri.fsPath); + const selectedUris = message.selectedUris; - const fileContents = selectedFilePaths - .map(filePath => { - const document = vscode.workspace.textDocuments.find(doc => doc.uri.fsPath === filePath); + // 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'); - return `${filePath}\n${lines.join('\n')}`; + const formattedLines = lines.map(line => `\t${line}`).join('\n'); + return `${file.uri.fsPath}:\n\`\`\`\n${formattedLines}\n\`\`\``; } return ''; }) .join('\n\n'); - panel.webview.html = getWebviewContent(fileContents, question); - } else if (message.command === 'fileSelectionChanged') { - const { filePath, checked } = message; - const file = selectedFiles.find(file => file.uri.fsPath === filePath); - if (file) { - file.checked = checked; + // Call OpenAI API with the question and file contents + try { + const chatCompletion = await openai.createChatCompletion({ + model: "gpt-3.5-turbo", + 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); } - } else if (message.command === 'filterFiles') { - const { filter } = message; - fileDataProvider.setFilter(filter); + } 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(fileContents, question) { - const fileItems = fileDataProvider - .filterFiles(selectedFiles) - .map(file => ` -
${fileContents}
${question ? question : ''}