neo1 / neo-api-client.js
bentosmau
Implement API key authentication for enhanced NeoAPI security
bc2ac28
/**
* NeoAPI Client β€” mdfjbots-neo-1
* Úsalo en tu pÑgina web para conectarte con la NeoAPI.
*
* Uso bΓ‘sico:
* const neo = new NeoAPI("neo1-tu_api_key_aqui");
* const respuesta = await neo.chat("Hola, ΒΏquiΓ©n eres?");
* console.log(respuesta);
*/
class NeoAPI {
/**
* @param {string} apiKey - Tu API key (formato: neo1-...)
* @param {string} [baseUrl] - URL base de la NeoAPI (opcional, por defecto la URL de producciΓ³n)
*/
constructor(apiKey, baseUrl = "https://TU_DOMINIO_AQUI/api") {
if (!apiKey) throw new Error("NeoAPI: se requiere una API key.");
this.apiKey = apiKey;
this.baseUrl = baseUrl.replace(/\/$/, "");
this.history = [];
}
/**
* EnvΓ­a un mensaje al modelo NEO-1 y obtiene la respuesta.
* El historial de conversaciΓ³n se mantiene automΓ‘ticamente.
*
* @param {string} message - El mensaje del usuario
* @returns {Promise<string>} - La respuesta del modelo
*/
async chat(message) {
const response = await fetch(`${this.baseUrl}/neo/chat`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.apiKey}`,
},
body: JSON.stringify({
message,
history: this.history,
}),
});
if (!response.ok) {
const err = await response.json().catch(() => ({ error: response.statusText }));
throw new Error(`NeoAPI error ${response.status}: ${err.error ?? response.statusText}`);
}
const data = await response.json();
const botReply = data.response;
this.history.push(
{ role: "user", content: message },
{ role: "assistant", content: botReply }
);
return botReply;
}
/**
* EnvΓ­a un mensaje sin mantener historial (cada llamada es independiente).
*
* @param {string} message - El mensaje del usuario
* @param {Array} [history] - Historial opcional a enviar
* @returns {Promise<string>}
*/
async chatOnce(message, history = []) {
const response = await fetch(`${this.baseUrl}/neo/chat`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.apiKey}`,
},
body: JSON.stringify({ message, history }),
});
if (!response.ok) {
const err = await response.json().catch(() => ({ error: response.statusText }));
throw new Error(`NeoAPI error ${response.status}: ${err.error ?? response.statusText}`);
}
const data = await response.json();
return data.response;
}
/** Limpia el historial de conversaciΓ³n. */
clearHistory() {
this.history = [];
}
/** Obtiene los modelos disponibles. */
async getModels() {
const response = await fetch(`${this.baseUrl}/neo/models`, {
headers: { Authorization: `Bearer ${this.apiKey}` },
});
if (!response.ok) throw new Error("NeoAPI: error al obtener modelos.");
return response.json();
}
}
/**
* ─────────────────────────────────────────────
* EJEMPLOS DE USO
* ─────────────────────────────────────────────
*
* 1) ConversaciΓ³n simple:
*
* const neo = new NeoAPI("neo1-tu_api_key");
* const res = await neo.chat("ΒΏQuΓ© es Linux?");
* document.getElementById("respuesta").textContent = res;
*
*
* 2) Chatbot con historial en la pΓ‘gina:
*
* const neo = new NeoAPI("neo1-tu_api_key");
*
* async function enviar() {
* const input = document.getElementById("input").value;
* const res = await neo.chat(input);
* mostrarMensaje("NEO-1", res);
* }
*
*
* 3) Llamada directa sin historial (para bΓΊsquedas puntuales):
*
* const neo = new NeoAPI("neo1-tu_api_key");
* const res = await neo.chatOnce("buscar juego Adopt Me");
* console.log(res);
*
*
* ─────────────────────────────────────────────
* ENDPOINTS DE ADMIN (para gestionar API keys)
* Se hacen desde tu backend, nunca desde el frontend.
* ─────────────────────────────────────────────
*
* Crear una nueva key:
* POST /api/keys
* Header: x-admin-secret: TU_ADMIN_SECRET
* Body: { "name": "mi-app-web" }
*
* Listar keys:
* GET /api/keys
* Header: x-admin-secret: TU_ADMIN_SECRET
*
* Revocar una key:
* DELETE /api/keys/mi-app-web
* Header: x-admin-secret: TU_ADMIN_SECRET
*/
export default NeoAPI;