basic-ai/extension.js

128 lines
3.4 KiB
JavaScript

const vscode = require('vscode');
class ApiResponseViewProvider {
constructor() {
this._view = undefined;
this._response = '';
}
resolveWebviewView(webviewView) {
this._view = webviewView;
webviewView.webview.options = {
enableScripts: true
};
this.updateContent();
}
updateResponse(response) {
this._response = response;
this.updateContent();
}
getResponse() {
return this._response;
}
updateContent() {
if (this._view) {
this._view.webview.html = `
<!DOCTYPE html>
<html>
<body>
<pre>${this._response}</pre>
</body>
</html>
`;
}
}
}
function activate(context) {
const apiResponseProvider = new ApiResponseViewProvider();
context.subscriptions.push(
vscode.window.registerWebviewViewProvider('apiResponseView', apiResponseProvider)
);
// Comando para enviar código a la API
let sendToApiCommand = vscode.commands.registerCommand('code-api-integration.sendToApi', async () => {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showErrorMessage('No hay editor activo');
return;
}
const selection = editor.selection;
const selectedText = editor.document.getText(selection);
if (!selectedText) {
vscode.window.showErrorMessage('No hay texto seleccionado');
return;
}
try {
// Mostrar indicador de progreso
vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: "Enviando código a la API...",
cancellable: false
}, async (progress) => {
const response = await fetch('http://127.0.0.1:7869/api/v2/text/text', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(
{"server":"groq",
"model":"deepseek-r1-distill-llama-70B",
"system":"Te dare un codigo de python quiero que generes el docstring tomando el cuenta el codigo",
"content":selectedText,
"password": "1223Aer*",
"userid": "Anon"}
)
});
if (!response.ok) {
throw new Error(`Error en la API: ${response.statusText}`);
}
const data = await response.json();
apiResponseProvider.updateResponse(JSON.stringify(data, null, 2));
// Notificar que la respuesta está lista para usar
vscode.window.showInformationMessage('Respuesta de API guardada - usa el botón derecho para pegarla');
});
} catch (error) {
vscode.window.showErrorMessage(`Error: ${error.message}`);
}
});
// Comando para pegar la última respuesta
let pasteResponseCommand = vscode.commands.registerCommand('code-api-integration.pasteLastResponse', async () => {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showErrorMessage('No hay editor activo');
return;
}
const response = apiResponseProvider.getResponse();
if (!response) {
vscode.window.showWarningMessage('No hay respuesta guardada de la API');
return;
}
editor.edit(editBuilder => {
editBuilder.insert(editor.selection.active, response);
});
});
context.subscriptions.push(sendToApiCommand, pasteResponseCommand);
}
function deactivate() {}
module.exports = {
activate,
deactivate
}