File size: 4,658 Bytes
bc2ac28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
 * 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;