From 68051774aa3e468dfb327fd437a34fe48c94bd02 Mon Sep 17 00:00:00 2001 From: Jay Barnes Date: Tue, 6 Dec 2022 20:54:59 +0000 Subject: [PATCH] Add clearing logs --- README.md | 2 ++ package.json | 6 +++- src/extension.ts | 76 +++++++++++++++++++++++++++++------------------- 3 files changed, 53 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 7dd0acc..4814a61 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ The `README.md` file for this extension was generated by ChatGPT. - Ask ChatGPT to refactor your code and explain the changes - Ask ChatGPT for help explaining code - Ask ChatGPT for help troubleshooting code +- Clear logs/output from ChatGPT ![Small Demo](/images/demo.gif "Demo") @@ -37,6 +38,7 @@ The commands are: - `ChatGPT: Why is my code broken?` - `ChatGPT: Explain this code` - `ChatGPT: Refactor this code` +- `ChatGPT: Clear ChatGPT Logs` (clears down responses stored in MD file) ## Installation diff --git a/package.json b/package.json index 194c2e0..452aa82 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,10 @@ { "command": "chatgpt-vscode-plugin.refactor", "title": "ChatGPT: Refactor this code" + }, + { + "command": "chatgpt-vscode-plugin.clearLogs", + "title": "ChatGPT: Clear ChatGPT Logs" } ], "viewsContainers": { @@ -94,4 +98,4 @@ "dependencies": { "chatgpt": "^1.0.0" } -} +} \ No newline at end of file diff --git a/src/extension.ts b/src/extension.ts index 1b73e92..455c3b6 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -3,31 +3,6 @@ import { ChatGPTAPI } from 'chatgpt'; const sessionTokenName = 'sessionToken'; -const queryChatGPT = async (userInput: string, api: ChatGPTAPI) => { - await textToMarkdownPreview(spinner); - const languageId = vscode.window.activeTextEditor?.document.languageId; - const selectedCode = vscode.window.activeTextEditor?.document.getText(vscode.window.activeTextEditor?.selection); - const entireFileContents = vscode.window.activeTextEditor?.document.getText(); - - const query = selectedCode - ? selectedCode + "\n" + userInput - : `This is the ${languageId} file I'm working on \n` + entireFileContents + "\n" + userInput; - - const response = await api.sendMessage(query); - await textToMarkdownPreview(response); -}; - -const textToMarkdownPreview = async (userInput: string) => { - // Create a new temporary file for the Markdown content - const tempFile = vscode.Uri.file('/tmp/ChatGP.md'); - - // Write the user's input to the temporary file - await vscode.workspace.fs.writeFile(tempFile, Buffer.from(userInput)); - - // Open the Markdown preview and show the temporary file - await vscode.commands.executeCommand('markdown.showPreview', tempFile); -}; - export async function activate(context: vscode.ExtensionContext) { context.globalState.setKeysForSync([sessionTokenName]); @@ -39,22 +14,28 @@ export async function activate(context: vscode.ExtensionContext) { context.subscriptions.push( vscode.commands.registerCommand('chatgpt-vscode-plugin.setUpGPT', setUpGPT), - vscode.commands.registerCommand('chatgpt-vscode-plugin.askGPT', askGPT), + vscode.commands.registerCommand('chatgpt-vscode-plugin.askGPT', askChatGPT), vscode.commands.registerCommand('chatgpt-vscode-plugin.whyBroken', askGPTWhyBroken), vscode.commands.registerCommand('chatgpt-vscode-plugin.explainPls', askGPTToExplain), vscode.commands.registerCommand('chatgpt-vscode-plugin.refactor', askGPTToRefactor), + vscode.commands.registerCommand('chatgpt-vscode-plugin.clearLogs', clearChatGPTLogs), ); + async function clearChatGPTLogs() { + await vscode.workspace.fs.writeFile(vscode.Uri.file('/tmp/ChatGPT'), Buffer.from('')); + vscode.window.showInformationMessage('ChatGPT logs cleared'); + } + async function setUpGPT() { let res = await vscode.window.showInputBox({ prompt: 'Please enter your access token, this can be retrieved using the guide on the README' }); context.globalState.update(sessionTokenName, res); } - async function askGPTToExplain() { await askGPT('Can you explain what this code does?'); } - async function askGPTWhyBroken() { await askGPT('Why is this code broken?'); } - async function askGPTToRefactor() { await askGPT('Can you refactor this code and explain what\'s changed?'); } + async function askGPTToExplain() { await askChatGPT('Can you explain what this code does?'); } + async function askGPTWhyBroken() { await askChatGPT('Why is this code broken?'); } + async function askGPTToRefactor() { await askChatGPT('Can you refactor this code and explain what\'s changed?'); } - async function askGPT(queryOverride?: string) { + async function askChatGPT(queryOverride?: string) { let stateSessionToken: string | undefined = context.globalState.get(sessionTokenName); if (!stateSessionToken) { return vscode.window.showErrorMessage('You need to set up your access token first, run ChatGPT: Login'); @@ -72,6 +53,41 @@ export async function activate(context: vscode.ExtensionContext) { } } +const queryChatGPT = async (userInput: string, api: ChatGPTAPI) => { + await textToMarkdownPreview(spinner, "Thinking...", false); + const languageId = vscode.window.activeTextEditor?.document.languageId; + const selectedCode = vscode.window.activeTextEditor?.document.getText(vscode.window.activeTextEditor?.selection); + const entireFileContents = vscode.window.activeTextEditor?.document.getText(); + + const query = selectedCode + ? selectedCode + "\n\n" + userInput + : `This is the ${languageId} file I'm working on: \n\n` + entireFileContents + "\n\n" + userInput; + + console.log('Querying ChatGPT...', { query }); + + const response = await api.sendMessage(query); + console.log('ChatGPT response:', { response }); + await textToMarkdownPreview(response, userInput); +}; + +const textToMarkdownPreview = async (textResponse: string, query: string, shouldAppend: boolean = true) => { + if (shouldAppend) { + const chatLog = vscode.Uri.file('/tmp/ChatGPT'); + let tempFileContents: string = ''; + + try { + tempFileContents = (await vscode.workspace.fs.readFile(chatLog)).toString(); + } catch (EntryNotFound) { } + + await vscode.workspace.fs.writeFile(chatLog, Buffer.from(`${tempFileContents}\n\n# ${query}\n\n${textResponse}`)); + await vscode.commands.executeCommand('markdown.showPreview', chatLog); + } else { + const tempFile = vscode.Uri.file('/tmp/ChatGPT-temp'); + await vscode.workspace.fs.writeFile(tempFile, Buffer.from(textResponse)); + await vscode.commands.executeCommand('markdown.showPreview', tempFile); + } +}; + const spinner = `

Please wait while we fetch your request