/** * 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} - 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} */ 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;