Skip to content

Commit

Permalink
Add clearing logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay Barnes committed Dec 6, 2022
1 parent 5bec243 commit 6805177
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 31 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -94,4 +98,4 @@
"dependencies": {
"chatgpt": "^1.0.0"
}
}
}
76 changes: 46 additions & 30 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand All @@ -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');
Expand All @@ -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 = ` <body>
<h1 style="text-align: center; font-family: sans-serif;">Please wait while we fetch your request</h1>
<div style="text-align: center;">
Expand Down

0 comments on commit 6805177

Please sign in to comment.