mirror of
https://github.com/Iheuzio/gpt-contextfiles.git
synced 2025-07-18 14:00:48 +00:00
fix(redudant-code): Removed redundancy
This commit is contained in:
parent
b756de10e4
commit
0ec8dc8ac4
135
extension.js
135
extension.js
@ -73,6 +73,51 @@ const addFilesCommand = vscode.commands.registerCommand('extension.addFilesToGPT
|
|||||||
|
|
||||||
const fileDataProvider = new FileDataProvider();
|
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
|
// Command for displaying the webview panel
|
||||||
const openGPTContextPanelCommand = vscode.commands.registerCommand('extension.openGPTContextPanel', () => {
|
const openGPTContextPanelCommand = vscode.commands.registerCommand('extension.openGPTContextPanel', () => {
|
||||||
const panel = vscode.window.createWebviewPanel(
|
const panel = vscode.window.createWebviewPanel(
|
||||||
@ -88,50 +133,7 @@ const openGPTContextPanelCommand = vscode.commands.registerCommand('extension.op
|
|||||||
|
|
||||||
panel.webview.onDidReceiveMessage(async message => {
|
panel.webview.onDidReceiveMessage(async message => {
|
||||||
if (message.command === 'submitQuestion') {
|
if (message.command === 'submitQuestion') {
|
||||||
const question = message.text;
|
await handleQuestionSubmission(panel, message.text, message.selectedUris);
|
||||||
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);
|
|
||||||
}
|
|
||||||
} else if (message.command === 'toggleFileSelection') {
|
} else if (message.command === 'toggleFileSelection') {
|
||||||
const uri = message.uri;
|
const uri = message.uri;
|
||||||
const file = selectedFiles.find(file => file.uri.fsPath === uri);
|
const file = selectedFiles.find(file => file.uri.fsPath === uri);
|
||||||
@ -286,50 +288,7 @@ const provider = {
|
|||||||
fileDataProvider.refresh();
|
fileDataProvider.refresh();
|
||||||
webviewView.webview.html = getWebviewContent();
|
webviewView.webview.html = getWebviewContent();
|
||||||
} else if (message.command === 'submitQuestion') {
|
} else if (message.command === 'submitQuestion') {
|
||||||
const question = message.text;
|
await handleQuestionSubmission(webviewView, message.text, message.selectedUris);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user