Skip to content

Instantly share code, notes, and snippets.

@pedrohenriquebr
Last active April 8, 2023 15:10
Show Gist options
  • Select an option

  • Save pedrohenriquebr/cb92ae45faaf4a70b8c8048a95c1f869 to your computer and use it in GitHub Desktop.

Select an option

Save pedrohenriquebr/cb92ae45faaf4a70b8c8048a95c1f869 to your computer and use it in GitHub Desktop.
JarvisGPT Extension preview
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import axios from 'axios';
import {prompts} from './prompts';
export const PROMPTS_ENUM : {[key:string] : string} = {
'jarvisgpt.refactor' : prompts.REFACTOR_PROMPT,
'jarvisgpt.explain': prompts.EXPLAIN_PROMPT,
'jarvisgpt.doc': prompts.DOC_PROMPT,
"jarvisgpt.cleancode": prompts.CLEAN_CODE_PROMPT,
"jarvisgpt.fixit": prompts.FIX_PROMPT,
}
const JARVISGPT_API_URL = 'http://localhost:3000/api';
const apiConnection = axios.create({
baseURL: JARVISGPT_API_URL,
})
export interface JarvisGPTReponse {
result: string
}
// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
const p = vscode.commands.registerTextEditorCommand('jarvisgpt.customprompt', (textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit) => {
// The code you place here will be executed every time your command is executed
// Display a message box to the user
const selectedCode = textEditor.document.getText(textEditor.selection);
const selection = textEditor.selection;
const { languageId, fileName } = textEditor.document;
vscode.window.showInputBox({
prompt: 'Your prompt:'
}).then(promptRequest => {
if(promptRequest == undefined)
return;
const payload = {
content: promptRequest+'.'
+ `using this programming language: '${languageId}'.\n`
+ `filename: '${fileName}'.\n`
+ `my code:\n${selectedCode}`
};
vscode.window.showInformationMessage('JarvisGPT: Processing the command...')
sendPromptAndReplace(payload, textEditor, selection);
})
});
context.subscriptions.push(p)
const unittestDisposable = vscode.commands.registerTextEditorCommand('jarvisgpt.unittest',
async (textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit) => {
// The code you place here will be executed every time your command is executed
// Display a message box to the user
const selectedCode = await vscode.env.clipboard.readText();
const { languageId, fileName } = textEditor.document;
const promptRequest = prompts.UNIT_TEST_PROMPT;
const payload = {
content: promptRequest+'.'
+ `using this programming language: '${languageId}'.\n`
+ `filename: '${fileName}'.\n`
+ `my code:\n${selectedCode}`
};
vscode.window.showInformationMessage('JarvisGPT: Processing the command...')
sendPromptOnly(payload, (result:string) => {
textEditor.edit(builder => {
builder.insert(textEditor.selection.active, result);
});
});
});
context.subscriptions.push(unittestDisposable)
Object.keys(PROMPTS_ENUM)
.map((command_id: string) =>
vscode.commands.registerTextEditorCommand(command_id, buildCommand(command_id)))
.forEach(item => {
context.subscriptions.push(item);
})
}
function buildCommand(command_id: string): (textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit, ...args: any[]) => void {
return (textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit) => {
// The code you place here will be executed every time your command is executed
// Display a message box to the user
const selectedCode = textEditor.document.getText(textEditor.selection);
const selection = textEditor.selection;
const { languageId, fileName } = textEditor.document;
const promptRequest = PROMPTS_ENUM[command_id] as string;
const payload = {
content: promptRequest
+ `using this programming language: ${languageId}.\n`
+ `filename: ${fileName}.\n`
+ `my code:\n${selectedCode}`
};
vscode.window.showInformationMessage('JarvisGPT: Processing the command...')
sendPromptAndReplace(payload, textEditor, selection);
};
}
function sendPromptAndReplace(payload: { content: string; }, textEditor: vscode.TextEditor, selection: vscode.Selection) {
apiConnection
.post<JarvisGPTReponse>('pseudo', payload)
.then(d => {
const result = d.data.result;
textEditor.edit(builder => {
builder.replace(selection, result);
});
vscode.window.showInformationMessage('JarvisGPT: Applying changes...');
});
}
function sendPromptOnly(payload: { content: string; }, callback: (result:string) => void) {
apiConnection
.post<JarvisGPTReponse>('pseudo', payload)
.then(d => {
const result = d.data.result;
vscode.window.showInformationMessage('JarvisGPT: Applying changes...');
callback(result);
});
}
// This method is called when your extension is deactivated
export function deactivate() {}
{
"name": "jarvisgpt",
"displayName": "JarvisGPT",
"description": "JarvisGPT - Connect to the ChatGPT via unoffical api",
"version": "0.0.1",
"engines": {
"vscode": "^1.77.0"
},
"categories": [
"Other"
],
"activationEvents": [],
"main": "./out/extension.js",
"contributes": {
"commands": [
{
"command": "jarvisgpt.refactor",
"title": "JarvisGPT: Refactor"
},
{
"command": "jarvisgpt.unittest",
"title": "JarvisGPT: Create Unit tests"
},
{
"command": "jarvisgpt.customprompt",
"title": "JarvisGPT: Custom Prompt..."
},
{
"command": "jarvisgpt.explain",
"title": "JarvisGPT: Explain Code"
},
{
"command": "jarvisgpt.doc",
"title": "JarvisGPT: Document Code"
},
{
"command": "jarvisgpt.cleancode",
"title": "JarvisGPT: Clean Code"
},
{
"command": "jarvisgpt.fixit",
"title": "JarvisGPT: Fix Code"
}
],
"menus": {
"editor/context": [
{
"command": "jarvisgpt.customprompt",
"when": "editorHasSelection",
"group": "1_modification"
},
{
"command": "jarvisgpt.fixit",
"when": "editorHasSelection",
"group": "1_modification"
},
{
"command": "jarvisgpt.unittest",
"when": "editorHasSelection",
"group": "1_modification"
},
{
"command": "jarvisgpt.refactor",
"when": "editorHasSelection",
"group": "1_modification"
},
{
"command": "jarvisgpt.explain",
"when": "editorHasSelection",
"group": "1_modification"
},
{
"command": "jarvisgpt.doc",
"when": "editorHasSelection",
"group": "1_modification"
},
{
"command": "jarvisgpt.cleancode",
"when": "editorHasSelection",
"group": "1_modification"
}
]
}
},
"scripts": {
"vscode:prepublish": "pnpm run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"pretest": "pnpm run compile && pnpm run lint",
"lint": "eslint src --ext ts",
"test": "node ./out/test/runTest.js"
},
"devDependencies": {
"@types/glob": "^8.1.0",
"@types/mocha": "^10.0.1",
"@types/node": "16.x",
"@types/vscode": "^1.77.0",
"@typescript-eslint/eslint-plugin": "^5.56.0",
"@typescript-eslint/parser": "^5.56.0",
"@vscode/test-electron": "^2.3.0",
"eslint": "^8.36.0",
"glob": "^8.1.0",
"mocha": "^10.2.0",
"typescript": "^4.9.5"
},
"dependencies": {
"axios": "^1.3.5"
}
}
export const prompts = {
REFACTOR_PROMPT: 'Refactor my code using the best practices of refactoring techniques. Suggestions include:'
+ 'extracting repeated values or expressions to local variables,'
+ 'extracting functionality into classes,'
+ 'and extracting complex logic into separate methods.',
EXPLAIN_PROMPT: 'Add comments to this code explaining each line.',
DOC_PROMPT: 'Create the proper documentation using comments.',
CLEAN_CODE_PROMPT: 'Apply clean code principles, improve the readbility and use the SOLID principles.',
UNIT_TEST_PROMPT: 'Create the unit tests given the code, '
+'consider best pratices and patterns for Unit Testing, like mocking, asserts, clean code,'
+'test data builders(create object mother builder as possible), also consider the programming paradigm from the code.',
FIX_PROMPT: 'Find syntax errors, logic errors, mistake using of the frameworks or '
+'libraries on my code and fix all them, also comment each line with error explaining each one.'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment