@schmitech/chatbot-api 2.1.5 → 2.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/api.mjs CHANGED
@@ -1,36 +1,49 @@
1
- var j = Object.defineProperty;
2
- var D = (l, r, e) => r in l ? j(l, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : l[r] = e;
3
- var P = (l, r, e) => D(l, typeof r != "symbol" ? r + "" : r, e);
4
- let I = null, $ = null;
1
+ var q = Object.defineProperty;
2
+ var D = (l, t, e) => t in l ? q(l, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : l[t] = e;
3
+ var g = (l, t, e) => D(l, typeof t != "symbol" ? t + "" : t, e);
4
+ let $ = null, F = null;
5
5
  typeof window > "u" && Promise.all([
6
6
  // @ts-expect-error - Dynamic import of Node.js built-in module (only available in Node.js runtime)
7
7
  import("http").catch(() => null),
8
8
  // @ts-expect-error - Dynamic import of Node.js built-in module (only available in Node.js runtime)
9
9
  import("https").catch(() => null)
10
- ]).then(([l, r]) => {
11
- var e, t;
12
- (e = l == null ? void 0 : l.default) != null && e.Agent ? I = new l.default.Agent({ keepAlive: !0 }) : l != null && l.Agent && (I = new l.Agent({ keepAlive: !0 })), (t = r == null ? void 0 : r.default) != null && t.Agent ? $ = new r.default.Agent({ keepAlive: !0 }) : r != null && r.Agent && ($ = new r.Agent({ keepAlive: !0 }));
10
+ ]).then(([l, t]) => {
11
+ var e, r;
12
+ (e = l == null ? void 0 : l.default) != null && e.Agent ? $ = new l.default.Agent({ keepAlive: !0 }) : l != null && l.Agent && ($ = new l.Agent({ keepAlive: !0 })), (r = t == null ? void 0 : t.default) != null && r.Agent ? F = new t.default.Agent({ keepAlive: !0 }) : t != null && t.Agent && (F = new t.Agent({ keepAlive: !0 }));
13
13
  }).catch((l) => {
14
14
  console.warn("Failed to initialize HTTP agents:", l.message);
15
15
  });
16
- class N {
16
+ class j {
17
17
  // Session ID can be mutable
18
- constructor(r) {
19
- P(this, "apiUrl");
20
- P(this, "apiKey");
21
- P(this, "sessionId");
22
- if (!r.apiUrl || typeof r.apiUrl != "string")
18
+ constructor(t) {
19
+ g(this, "apiUrl");
20
+ g(this, "apiKey");
21
+ g(this, "adapterName");
22
+ g(this, "middlewareBaseUrl");
23
+ g(this, "middlewareMode");
24
+ g(this, "sessionId");
25
+ if (!t.apiUrl || typeof t.apiUrl != "string")
23
26
  throw new Error("API URL must be a valid string");
24
- if (r.apiKey !== void 0 && r.apiKey !== null && typeof r.apiKey != "string")
27
+ if (t.apiKey !== void 0 && t.apiKey !== null && typeof t.apiKey != "string")
25
28
  throw new Error("API key must be a valid string or null");
26
- if (r.sessionId !== void 0 && r.sessionId !== null && typeof r.sessionId != "string")
29
+ if (t.sessionId !== void 0 && t.sessionId !== null && typeof t.sessionId != "string")
27
30
  throw new Error("Session ID must be a valid string or null");
28
- this.apiUrl = r.apiUrl, this.apiKey = r.apiKey ?? null, this.sessionId = r.sessionId ?? null;
31
+ if (t.adapterName !== void 0 && t.adapterName !== null && typeof t.adapterName != "string")
32
+ throw new Error("Adapter name must be a valid string or null");
33
+ if (t.middlewareBaseUrl !== void 0 && t.middlewareBaseUrl !== null && typeof t.middlewareBaseUrl != "string")
34
+ throw new Error("Middleware base URL must be a valid string or null");
35
+ this.apiUrl = t.apiUrl, this.apiKey = t.apiKey ?? null, this.sessionId = t.sessionId ?? null;
36
+ const e = typeof t.adapterName == "string" ? t.adapterName.trim() : "";
37
+ this.adapterName = e && e.length > 0 ? e : null;
38
+ const r = typeof t.middlewareBaseUrl == "string" ? t.middlewareBaseUrl.trim() : "";
39
+ this.middlewareBaseUrl = r.endsWith("/") ? r.slice(0, -1) : r;
40
+ const s = t.useMiddleware ?? (!this.apiKey && !!this.adapterName);
41
+ this.middlewareMode = !!(this.adapterName && s);
29
42
  }
30
- setSessionId(r) {
31
- if (r !== null && typeof r != "string")
43
+ setSessionId(t) {
44
+ if (t !== null && typeof t != "string")
32
45
  throw new Error("Session ID must be a valid string or null");
33
- this.sessionId = r;
46
+ this.sessionId = t;
34
47
  }
35
48
  getSessionId() {
36
49
  return this.sessionId;
@@ -42,7 +55,7 @@ class N {
42
55
  * @throws Error if API key is not provided, invalid, inactive, or validation fails
43
56
  */
44
57
  async validateApiKey() {
45
- var r;
58
+ var t;
46
59
  if (!this.apiKey)
47
60
  throw new Error("API key is required for validation");
48
61
  try {
@@ -86,19 +99,19 @@ class N {
86
99
  }
87
100
  throw new Error(n);
88
101
  }
89
- const t = await e.json();
90
- if (!t.exists) {
102
+ const r = await e.json();
103
+ if (!r.exists) {
91
104
  const s = "API key does not exist";
92
105
  throw new Error(s);
93
106
  }
94
- if (!t.active) {
107
+ if (!r.active) {
95
108
  const s = "API key is inactive";
96
109
  throw new Error(s);
97
110
  }
98
- return t;
111
+ return r;
99
112
  } catch (e) {
100
- let t;
101
- throw e instanceof Error && e.message ? e.message.includes("API key") || e.message.includes("Access denied") || e.message.includes("invalid") || e.message.includes("expired") || e.message.includes("inactive") || e.message.includes("not found") || e.message.includes("Could not connect") ? t = e.message : t = `API key validation failed: ${e.message}` : e.name === "TypeError" && ((r = e.message) != null && r.includes("Failed to fetch")) ? t = "Could not connect to the server. Please check if the server is running." : t = "API key validation failed. Please check your API key and try again.", console.warn(`[ApiClient] ${t}`), new Error(t);
113
+ let r;
114
+ throw e instanceof Error && e.message ? e.message.includes("API key") || e.message.includes("Access denied") || e.message.includes("invalid") || e.message.includes("expired") || e.message.includes("inactive") || e.message.includes("not found") || e.message.includes("Could not connect") ? r = e.message : r = `API key validation failed: ${e.message}` : e.name === "TypeError" && ((t = e.message) != null && t.includes("Failed to fetch")) ? r = "Could not connect to the server. Please check if the server is running." : r = "API key validation failed. Please check your API key and try again.", console.warn(`[ApiClient] ${r}`), new Error(r);
102
115
  }
103
116
  }
104
117
  /**
@@ -111,7 +124,7 @@ class N {
111
124
  * @throws Error if API key is not provided, invalid, disabled, or request fails
112
125
  */
113
126
  async getAdapterInfo() {
114
- var r;
127
+ var t;
115
128
  if (!this.apiKey)
116
129
  throw new Error("API key is required to get adapter information");
117
130
  try {
@@ -154,48 +167,94 @@ class N {
154
167
  }
155
168
  return await e.json();
156
169
  } catch (e) {
157
- let t;
158
- throw e instanceof Error && e.message ? e.message.includes("API key") || e.message.includes("Adapter") || e.message.includes("invalid") || e.message.includes("disabled") || e.message.includes("not found") || e.message.includes("Could not connect") ? t = e.message : t = `Failed to get adapter info: ${e.message}` : e.name === "TypeError" && ((r = e.message) != null && r.includes("Failed to fetch")) ? t = "Could not connect to the server. Please check if the server is running." : t = "Failed to get adapter information. Please try again.", console.warn(`[ApiClient] ${t}`), new Error(t);
170
+ let r;
171
+ throw e instanceof Error && e.message ? e.message.includes("API key") || e.message.includes("Adapter") || e.message.includes("invalid") || e.message.includes("disabled") || e.message.includes("not found") || e.message.includes("Could not connect") ? r = e.message : r = `Failed to get adapter info: ${e.message}` : e.name === "TypeError" && ((t = e.message) != null && t.includes("Failed to fetch")) ? r = "Could not connect to the server. Please check if the server is running." : r = "Failed to get adapter information. Please try again.", console.warn(`[ApiClient] ${r}`), new Error(r);
172
+ }
173
+ }
174
+ /**
175
+ * Get autocomplete suggestions based on query prefix.
176
+ *
177
+ * Returns query suggestions based on nl_examples from intent adapter templates.
178
+ * This is useful for helping users discover available queries.
179
+ *
180
+ * @param query - The query prefix (minimum 3 characters)
181
+ * @param limit - Maximum number of suggestions (default: 5)
182
+ * @returns Promise resolving to autocomplete suggestions
183
+ */
184
+ async getAutocompleteSuggestions(t, e = 5) {
185
+ if (t.length < 3)
186
+ return { suggestions: [], query: t };
187
+ const r = this.middlewareMode && !!this.adapterName;
188
+ if (!r && !this.apiKey)
189
+ return { suggestions: [], query: t };
190
+ try {
191
+ const s = new URLSearchParams();
192
+ s.set("q", t), s.set("limit", String(e));
193
+ let i, n;
194
+ if (r) {
195
+ if (!this.middlewareBaseUrl && typeof window > "u")
196
+ throw new Error("middlewareBaseUrl must be set when using middleware outside the browser environment.");
197
+ i = `${this.middlewareBaseUrl ? this.middlewareBaseUrl : ""}/api/v1/autocomplete?${s.toString()}`;
198
+ const f = {
199
+ "X-Adapter-Name": this.adapterName,
200
+ "X-Request-ID": Date.now().toString(36) + Math.random().toString(36).substring(2)
201
+ };
202
+ this.sessionId && (f["X-Session-ID"] = this.sessionId), n = {
203
+ method: "GET",
204
+ headers: f
205
+ };
206
+ } else {
207
+ const d = new URL(`${this.apiUrl}/v1/autocomplete`);
208
+ d.searchParams.set("q", t), d.searchParams.set("limit", String(e)), i = d.toString(), n = {
209
+ ...this.getFetchOptions({
210
+ method: "GET"
211
+ })
212
+ };
213
+ }
214
+ const o = await fetch(i, n);
215
+ return o.ok ? await o.json() : (console.warn("[ApiClient] Autocomplete request failed:", o.status), { suggestions: [], query: t });
216
+ } catch (s) {
217
+ return console.warn("[ApiClient] Autocomplete error:", s.message), { suggestions: [], query: t };
159
218
  }
160
219
  }
161
220
  // Helper to get fetch options with connection pooling if available
162
- getFetchOptions(r = {}) {
221
+ getFetchOptions(t = {}) {
163
222
  const e = {};
164
223
  if (typeof window > "u") {
165
- const i = this.apiUrl.startsWith("https:") ? $ : I;
224
+ const i = this.apiUrl.startsWith("https:") ? F : $;
166
225
  i && (e.agent = i);
167
226
  } else
168
227
  e.headers = { Connection: "keep-alive" };
169
- const t = {
228
+ const r = {
170
229
  "X-Request-ID": Date.now().toString(36) + Math.random().toString(36).substring(2)
171
230
  };
172
- if (e.headers && Object.assign(t, e.headers), r.headers) {
173
- const s = r.headers;
231
+ if (e.headers && Object.assign(r, e.headers), t.headers) {
232
+ const s = t.headers;
174
233
  for (const [i, n] of Object.entries(s))
175
- (i.toLowerCase() !== "x-api-key" || !this.apiKey) && (t[i] = n);
234
+ (i.toLowerCase() !== "x-api-key" || !this.apiKey) && (r[i] = n);
176
235
  }
177
- return this.apiKey && (t["X-API-Key"] = this.apiKey), this.sessionId && (t["X-Session-ID"] = this.sessionId), {
178
- ...r,
236
+ return this.apiKey && (r["X-API-Key"] = this.apiKey), this.sessionId && (r["X-Session-ID"] = this.sessionId), {
237
+ ...t,
179
238
  ...e,
180
- headers: t
239
+ headers: r
181
240
  };
182
241
  }
183
242
  // Create Chat request
184
- createChatRequest(r, e = !0, t, s, i, n, o, d, u, f, g) {
243
+ createChatRequest(t, e = !0, r, s, i, n, o, d, u, f, m) {
185
244
  const c = {
186
245
  messages: [
187
- { role: "user", content: r }
246
+ { role: "user", content: t }
188
247
  ],
189
248
  stream: e
190
249
  };
191
- return t && t.length > 0 && (c.file_ids = t), s && (c.thread_id = s), i && (c.audio_input = i), n && (c.audio_format = n), o && (c.language = o), d !== void 0 && (c.return_audio = d), u && (c.tts_voice = u), f && (c.source_language = f), g && (c.target_language = g), c;
250
+ return r && r.length > 0 && (c.file_ids = r), s && (c.thread_id = s), i && (c.audio_input = i), n && (c.audio_format = n), o && (c.language = o), d !== void 0 && (c.return_audio = d), u && (c.tts_voice = u), f && (c.source_language = f), m && (c.target_language = m), c;
192
251
  }
193
- async *streamChat(r, e = !0, t, s, i, n, o, d, u, f, g, c) {
194
- var C, x, b;
252
+ async *streamChat(t, e = !0, r, s, i, n, o, d, u, f, m, c) {
253
+ var C, b, x;
195
254
  try {
196
- const w = new AbortController(), S = setTimeout(() => w.abort(), 6e4);
255
+ const w = new AbortController(), K = setTimeout(() => w.abort(), 6e4);
197
256
  c && c.addEventListener("abort", () => w.abort());
198
- const m = await fetch(`${this.apiUrl}/v1/chat`, {
257
+ const v = await fetch(`${this.apiUrl}/v1/chat`, {
199
258
  ...this.getFetchOptions({
200
259
  method: "POST",
201
260
  headers: {
@@ -203,9 +262,9 @@ class N {
203
262
  Accept: e ? "text/event-stream" : "application/json"
204
263
  },
205
264
  body: JSON.stringify(this.createChatRequest(
206
- r,
207
- e,
208
265
  t,
266
+ e,
267
+ r,
209
268
  s,
210
269
  i,
211
270
  n,
@@ -213,42 +272,42 @@ class N {
213
272
  d,
214
273
  u,
215
274
  f,
216
- g
275
+ m
217
276
  ))
218
277
  }),
219
278
  signal: w.signal
220
279
  });
221
- if (clearTimeout(S), !m.ok) {
222
- const p = await m.text();
223
- throw new Error(`Network response was not ok: ${m.status} ${p}`);
280
+ if (clearTimeout(K), !v.ok) {
281
+ const y = await v.text();
282
+ throw new Error(`Network response was not ok: ${v.status} ${y}`);
224
283
  }
225
284
  if (!e) {
226
- const p = await m.json();
227
- p.response && (yield {
228
- text: p.response,
285
+ const y = await v.json();
286
+ y.response && (yield {
287
+ text: y.response,
229
288
  done: !0,
230
- audio: p.audio,
231
- audioFormat: p.audio_format
289
+ audio: y.audio,
290
+ audioFormat: y.audio_format
232
291
  });
233
292
  return;
234
293
  }
235
- const T = (C = m.body) == null ? void 0 : C.getReader();
236
- if (!T) throw new Error("No reader available");
237
- const _ = new TextDecoder();
238
- let y = "", k = !1;
294
+ const I = (C = v.body) == null ? void 0 : C.getReader();
295
+ if (!I) throw new Error("No reader available");
296
+ const S = new TextDecoder();
297
+ let p = "", A = !1;
239
298
  try {
240
299
  for (; ; ) {
241
- const { done: p, value: U } = await T.read();
242
- if (p)
300
+ const { done: y, value: N } = await I.read();
301
+ if (y)
243
302
  break;
244
- const q = _.decode(U, { stream: !0 });
245
- y += q;
246
- let A = 0, F;
247
- for (; (F = y.indexOf(`
248
- `, A)) !== -1; ) {
249
- const v = y.slice(A, F).trim();
250
- if (A = F + 1, v && v.startsWith("data: ")) {
251
- const h = v.slice(6).trim();
303
+ const _ = S.decode(N, { stream: !0 });
304
+ p += _;
305
+ let P = 0, T;
306
+ for (; (T = p.indexOf(`
307
+ `, P)) !== -1; ) {
308
+ const E = p.slice(P, T).trim();
309
+ if (P = T + 1, E && E.startsWith("data: ")) {
310
+ const h = E.slice(6).trim();
252
311
  if (!h || h === "[DONE]") {
253
312
  yield { text: "", done: !0 };
254
313
  return;
@@ -256,8 +315,8 @@ class N {
256
315
  try {
257
316
  const a = JSON.parse(h);
258
317
  if (a.error) {
259
- const K = `Server error: ${((x = a.error) == null ? void 0 : x.message) || a.error || "Unknown server error"}`;
260
- throw console.warn(`[ApiClient] ${K}`), new Error(K);
318
+ const O = `Server error: ${((b = a.error) == null ? void 0 : b.message) || a.error || "Unknown server error"}`;
319
+ throw console.warn(`[ApiClient] ${O}`), new Error(O);
261
320
  }
262
321
  if (a.request_id && !a.response && !a.done) {
263
322
  yield {
@@ -268,7 +327,7 @@ class N {
268
327
  continue;
269
328
  }
270
329
  if (a.done === !0) {
271
- k = !0, yield {
330
+ A = !0, yield {
272
331
  text: "",
273
332
  done: !0,
274
333
  audio: a.audio,
@@ -278,15 +337,15 @@ class N {
278
337
  };
279
338
  return;
280
339
  }
281
- const O = a.response || "";
340
+ const U = a.response || "";
282
341
  a.audio_chunk !== void 0 && (yield {
283
342
  text: "",
284
343
  done: !1,
285
344
  audio_chunk: a.audio_chunk,
286
345
  audioFormat: a.audioFormat || a.audio_format || "opus",
287
346
  chunk_index: a.chunk_index ?? 0
288
- }), (O || a.audio) && (k = !0, yield {
289
- text: O,
347
+ }), (U || a.audio) && (A = !0, yield {
348
+ text: U,
290
349
  done: a.done || !1,
291
350
  audio: a.audio,
292
351
  audioFormat: a.audio_format || a.audioFormat,
@@ -294,17 +353,17 @@ class N {
294
353
  // Include threading if present
295
354
  });
296
355
  } catch (a) {
297
- if ((b = a == null ? void 0 : a.message) != null && b.startsWith("Server error:"))
356
+ if ((x = a == null ? void 0 : a.message) != null && x.startsWith("Server error:"))
298
357
  throw a;
299
358
  console.warn("[ApiClient] Unable to parse server response. This may be a temporary issue."), console.warn("[ApiClient] Parse error details:", a == null ? void 0 : a.message), console.warn("[ApiClient] JSON text length:", h == null ? void 0 : h.length), console.warn("[ApiClient] JSON text preview (first 200 chars):", h == null ? void 0 : h.substring(0, 200)), console.warn("[ApiClient] JSON text preview (last 200 chars):", h == null ? void 0 : h.substring(h.length - 200));
300
359
  }
301
- } else v && (k = !0, yield { text: v, done: !1 });
360
+ } else E && (A = !0, yield { text: E, done: !1 });
302
361
  }
303
- y = y.slice(A), y.length > 1e6 && (console.warn("[ApiClient] Buffer too large, truncating..."), y = y.slice(-5e5));
362
+ p = p.slice(P), p.length > 1e6 && (console.warn("[ApiClient] Buffer too large, truncating..."), p = p.slice(-5e5));
304
363
  }
305
- k && (yield { text: "", done: !0 });
364
+ A && (yield { text: "", done: !0 });
306
365
  } finally {
307
- T.releaseLock();
366
+ I.releaseLock();
308
367
  }
309
368
  } catch (w) {
310
369
  throw w.name === "AbortError" ? c != null && c.aborted ? new Error("Stream cancelled by user") : new Error("Connection timed out. Please check if the server is running.") : w.name === "TypeError" && w.message.includes("Failed to fetch") ? new Error("Could not connect to the server. Please check if the server is running.") : w;
@@ -317,33 +376,33 @@ class N {
317
376
  * @param requestId - The request identifier (from first stream chunk)
318
377
  * @returns Promise resolving to true if stream was cancelled, false otherwise
319
378
  */
320
- async stopChat(r, e) {
379
+ async stopChat(t, e) {
321
380
  try {
322
- const t = await fetch(`${this.apiUrl}/v1/chat/stop`, {
381
+ const r = await fetch(`${this.apiUrl}/v1/chat/stop`, {
323
382
  ...this.getFetchOptions({
324
383
  method: "POST",
325
384
  headers: {
326
385
  "Content-Type": "application/json"
327
386
  },
328
387
  body: JSON.stringify({
329
- session_id: r,
388
+ session_id: t,
330
389
  request_id: e
331
390
  })
332
391
  })
333
392
  });
334
- return t.ok ? (await t.json()).status === "cancelled" : (console.warn("[ApiClient] Failed to stop stream:", t.status), !1);
335
- } catch (t) {
336
- return console.error("[ApiClient] Error stopping stream:", t), !1;
393
+ return r.ok ? (await r.json()).status === "cancelled" : (console.warn("[ApiClient] Failed to stop stream:", r.status), !1);
394
+ } catch (r) {
395
+ return console.error("[ApiClient] Error stopping stream:", r), !1;
337
396
  }
338
397
  }
339
- async getConversationHistory(r, e) {
340
- const t = r || this.sessionId;
341
- if (!t)
398
+ async getConversationHistory(t, e) {
399
+ const r = t || this.sessionId;
400
+ if (!r)
342
401
  throw new Error("No session ID provided and no current session available");
343
402
  const s = {};
344
403
  this.apiKey && (s["X-API-Key"] = this.apiKey);
345
404
  try {
346
- const i = new URL(`${this.apiUrl}/admin/chat-history/${t}`);
405
+ const i = new URL(`${this.apiUrl}/admin/chat-history/${r}`);
347
406
  typeof e == "number" && Number.isFinite(e) && e > 0 && i.searchParams.set("limit", String(Math.floor(e)));
348
407
  const n = await fetch(i.toString(), {
349
408
  ...this.getFetchOptions({
@@ -357,7 +416,7 @@ class N {
357
416
  }
358
417
  const o = await n.json(), d = Array.isArray(o == null ? void 0 : o.messages) ? o.messages : [], u = typeof (o == null ? void 0 : o.count) == "number" ? o.count : d.length;
359
418
  return {
360
- session_id: (o == null ? void 0 : o.session_id) || t,
419
+ session_id: (o == null ? void 0 : o.session_id) || r,
361
420
  messages: d,
362
421
  count: u
363
422
  };
@@ -365,13 +424,13 @@ class N {
365
424
  throw i.name === "TypeError" && i.message.includes("Failed to fetch") ? new Error("Could not connect to the server. Please check if the server is running.") : i;
366
425
  }
367
426
  }
368
- async clearConversationHistory(r) {
369
- const e = r || this.sessionId;
427
+ async clearConversationHistory(t) {
428
+ const e = t || this.sessionId;
370
429
  if (!e)
371
430
  throw new Error("No session ID provided and no current session available");
372
431
  if (!this.apiKey)
373
432
  throw new Error("API key is required for clearing conversation history");
374
- const t = {
433
+ const r = {
375
434
  "Content-Type": "application/json",
376
435
  "X-Session-ID": e,
377
436
  "X-API-Key": this.apiKey
@@ -380,7 +439,7 @@ class N {
380
439
  const s = await fetch(`${this.apiUrl}/admin/chat-history/${e}`, {
381
440
  ...this.getFetchOptions({
382
441
  method: "DELETE",
383
- headers: t
442
+ headers: r
384
443
  })
385
444
  });
386
445
  if (!s.ok) {
@@ -392,17 +451,17 @@ class N {
392
451
  throw s.name === "TypeError" && s.message.includes("Failed to fetch") ? new Error("Could not connect to the server. Please check if the server is running.") : s;
393
452
  }
394
453
  }
395
- async deleteConversationWithFiles(r, e) {
396
- const t = r || this.sessionId;
397
- if (!t)
454
+ async deleteConversationWithFiles(t, e) {
455
+ const r = t || this.sessionId;
456
+ if (!r)
398
457
  throw new Error("No session ID provided and no current session available");
399
458
  if (!this.apiKey)
400
459
  throw new Error("API key is required for deleting conversation");
401
460
  const s = {
402
461
  "Content-Type": "application/json",
403
- "X-Session-ID": t,
462
+ "X-Session-ID": r,
404
463
  "X-API-Key": this.apiKey
405
- }, i = e && e.length > 0 ? `?file_ids=${e.join(",")}` : "", n = `${this.apiUrl}/admin/conversations/${t}${i}`;
464
+ }, i = e && e.length > 0 ? `?file_ids=${e.join(",")}` : "", n = `${this.apiUrl}/admin/conversations/${r}${i}`;
406
465
  try {
407
466
  const o = await fetch(n, {
408
467
  ...this.getFetchOptions({
@@ -427,10 +486,10 @@ class N {
427
486
  * @returns Promise resolving to thread information
428
487
  * @throws Error if the operation fails
429
488
  */
430
- async createThread(r, e) {
489
+ async createThread(t, e) {
431
490
  if (!this.apiKey)
432
491
  throw new Error("API key is required for creating threads");
433
- const t = {
492
+ const r = {
434
493
  "Content-Type": "application/json",
435
494
  "X-API-Key": this.apiKey
436
495
  };
@@ -438,9 +497,9 @@ class N {
438
497
  const s = await fetch(`${this.apiUrl}/api/threads`, {
439
498
  ...this.getFetchOptions({
440
499
  method: "POST",
441
- headers: t,
500
+ headers: r,
442
501
  body: JSON.stringify({
443
- message_id: r,
502
+ message_id: t,
444
503
  session_id: e
445
504
  })
446
505
  })
@@ -461,26 +520,26 @@ class N {
461
520
  * @returns Promise resolving to thread information
462
521
  * @throws Error if the operation fails
463
522
  */
464
- async getThreadInfo(r) {
523
+ async getThreadInfo(t) {
465
524
  if (!this.apiKey)
466
525
  throw new Error("API key is required for getting thread info");
467
526
  const e = {
468
527
  "X-API-Key": this.apiKey
469
528
  };
470
529
  try {
471
- const t = await fetch(`${this.apiUrl}/api/threads/${r}`, {
530
+ const r = await fetch(`${this.apiUrl}/api/threads/${t}`, {
472
531
  ...this.getFetchOptions({
473
532
  method: "GET",
474
533
  headers: e
475
534
  })
476
535
  });
477
- if (!t.ok) {
478
- const i = await t.text();
479
- throw new Error(`Failed to get thread info: ${t.status} ${i}`);
536
+ if (!r.ok) {
537
+ const i = await r.text();
538
+ throw new Error(`Failed to get thread info: ${r.status} ${i}`);
480
539
  }
481
- return await t.json();
482
- } catch (t) {
483
- throw t.name === "TypeError" && t.message.includes("Failed to fetch") ? new Error("Could not connect to the server. Please check if the server is running.") : t;
540
+ return await r.json();
541
+ } catch (r) {
542
+ throw r.name === "TypeError" && r.message.includes("Failed to fetch") ? new Error("Could not connect to the server. Please check if the server is running.") : r;
484
543
  }
485
544
  }
486
545
  /**
@@ -490,26 +549,26 @@ class N {
490
549
  * @returns Promise resolving to deletion result
491
550
  * @throws Error if the operation fails
492
551
  */
493
- async deleteThread(r) {
552
+ async deleteThread(t) {
494
553
  if (!this.apiKey)
495
554
  throw new Error("API key is required for deleting threads");
496
555
  const e = {
497
556
  "X-API-Key": this.apiKey
498
557
  };
499
558
  try {
500
- const t = await fetch(`${this.apiUrl}/api/threads/${r}`, {
559
+ const r = await fetch(`${this.apiUrl}/api/threads/${t}`, {
501
560
  ...this.getFetchOptions({
502
561
  method: "DELETE",
503
562
  headers: e
504
563
  })
505
564
  });
506
- if (!t.ok) {
507
- const i = await t.text();
508
- throw new Error(`Failed to delete thread: ${t.status} ${i}`);
565
+ if (!r.ok) {
566
+ const i = await r.text();
567
+ throw new Error(`Failed to delete thread: ${r.status} ${i}`);
509
568
  }
510
- return await t.json();
511
- } catch (t) {
512
- throw t.name === "TypeError" && t.message.includes("Failed to fetch") ? new Error("Could not connect to the server. Please check if the server is running.") : t;
569
+ return await r.json();
570
+ } catch (r) {
571
+ throw r.name === "TypeError" && r.message.includes("Failed to fetch") ? new Error("Could not connect to the server. Please check if the server is running.") : r;
513
572
  }
514
573
  }
515
574
  /**
@@ -519,25 +578,25 @@ class N {
519
578
  * @returns Promise resolving to upload response with file_id
520
579
  * @throws Error if upload fails
521
580
  */
522
- async uploadFile(r) {
581
+ async uploadFile(t) {
523
582
  if (!this.apiKey)
524
583
  throw new Error("API key is required for file upload");
525
584
  const e = new FormData();
526
- e.append("file", r);
585
+ e.append("file", t);
527
586
  try {
528
- const t = await fetch(`${this.apiUrl}/api/files/upload`, {
587
+ const r = await fetch(`${this.apiUrl}/api/files/upload`, {
529
588
  ...this.getFetchOptions({
530
589
  method: "POST",
531
590
  body: e
532
591
  })
533
592
  });
534
- if (!t.ok) {
535
- const s = await t.text();
536
- throw new Error(`Failed to upload file: ${t.status} ${s}`);
593
+ if (!r.ok) {
594
+ const s = await r.text();
595
+ throw new Error(`Failed to upload file: ${r.status} ${s}`);
537
596
  }
538
- return await t.json();
539
- } catch (t) {
540
- throw t.name === "TypeError" && t.message.includes("Failed to fetch") ? new Error("Could not connect to the server. Please check if the server is running.") : t;
597
+ return await r.json();
598
+ } catch (r) {
599
+ throw r.name === "TypeError" && r.message.includes("Failed to fetch") ? new Error("Could not connect to the server. Please check if the server is running.") : r;
541
600
  }
542
601
  }
543
602
  /**
@@ -550,18 +609,18 @@ class N {
550
609
  if (!this.apiKey)
551
610
  throw new Error("API key is required for listing files");
552
611
  try {
553
- const r = await fetch(`${this.apiUrl}/api/files`, {
612
+ const t = await fetch(`${this.apiUrl}/api/files`, {
554
613
  ...this.getFetchOptions({
555
614
  method: "GET"
556
615
  })
557
616
  });
558
- if (!r.ok) {
559
- const e = await r.text();
560
- throw new Error(`Failed to list files: ${r.status} ${e}`);
617
+ if (!t.ok) {
618
+ const e = await t.text();
619
+ throw new Error(`Failed to list files: ${t.status} ${e}`);
561
620
  }
562
- return await r.json();
563
- } catch (r) {
564
- throw r.name === "TypeError" && r.message.includes("Failed to fetch") ? new Error("Could not connect to the server. Please check if the server is running.") : r;
621
+ return await t.json();
622
+ } catch (t) {
623
+ throw t.name === "TypeError" && t.message.includes("Failed to fetch") ? new Error("Could not connect to the server. Please check if the server is running.") : t;
565
624
  }
566
625
  }
567
626
  /**
@@ -571,18 +630,18 @@ class N {
571
630
  * @returns Promise resolving to file information
572
631
  * @throws Error if file not found or request fails
573
632
  */
574
- async getFileInfo(r) {
633
+ async getFileInfo(t) {
575
634
  if (!this.apiKey)
576
635
  throw new Error("API key is required for getting file info");
577
636
  try {
578
- const e = await fetch(`${this.apiUrl}/api/files/${r}`, {
637
+ const e = await fetch(`${this.apiUrl}/api/files/${t}`, {
579
638
  ...this.getFetchOptions({
580
639
  method: "GET"
581
640
  })
582
641
  });
583
642
  if (!e.ok) {
584
- const t = await e.text();
585
- throw new Error(`Failed to get file info: ${e.status} ${t}`);
643
+ const r = await e.text();
644
+ throw new Error(`Failed to get file info: ${e.status} ${r}`);
586
645
  }
587
646
  return await e.json();
588
647
  } catch (e) {
@@ -598,17 +657,17 @@ class N {
598
657
  * @returns Promise resolving to query results
599
658
  * @throws Error if query fails
600
659
  */
601
- async queryFile(r, e, t = 10) {
660
+ async queryFile(t, e, r = 10) {
602
661
  if (!this.apiKey)
603
662
  throw new Error("API key is required for querying files");
604
663
  try {
605
- const s = await fetch(`${this.apiUrl}/api/files/${r}/query`, {
664
+ const s = await fetch(`${this.apiUrl}/api/files/${t}/query`, {
606
665
  ...this.getFetchOptions({
607
666
  method: "POST",
608
667
  headers: {
609
668
  "Content-Type": "application/json"
610
669
  },
611
- body: JSON.stringify({ query: e, max_results: t })
670
+ body: JSON.stringify({ query: e, max_results: r })
612
671
  })
613
672
  });
614
673
  if (!s.ok) {
@@ -627,14 +686,14 @@ class N {
627
686
  * @returns Promise resolving to deletion result
628
687
  * @throws Error if deletion fails
629
688
  */
630
- async deleteFile(r) {
689
+ async deleteFile(t) {
631
690
  if (!this.apiKey)
632
691
  throw new Error("API key is required for deleting files");
633
- const e = `${this.apiUrl}/api/files/${r}`, t = this.getFetchOptions({
692
+ const e = `${this.apiUrl}/api/files/${t}`, r = this.getFetchOptions({
634
693
  method: "DELETE"
635
694
  });
636
695
  try {
637
- const s = await fetch(e, t);
696
+ const s = await fetch(e, r);
638
697
  if (!s.ok) {
639
698
  const n = await s.text();
640
699
  let o;
@@ -653,18 +712,18 @@ class N {
653
712
  }
654
713
  }
655
714
  }
656
- let E = null;
657
- const H = (l, r = null, e = null) => {
658
- E = new N({ apiUrl: l, apiKey: r, sessionId: e });
715
+ let k = null;
716
+ const R = (l, t = null, e = null) => {
717
+ k = new j({ apiUrl: l, apiKey: t, sessionId: e });
659
718
  };
660
- async function* X(l, r = !0, e, t, s, i, n, o, d, u, f, g) {
661
- if (!E)
719
+ async function* X(l, t = !0, e, r, s, i, n, o, d, u, f, m) {
720
+ if (!k)
662
721
  throw new Error("API not configured. Please call configureApi() with your server URL before using any API functions.");
663
- yield* E.streamChat(
722
+ yield* k.streamChat(
664
723
  l,
665
- r,
666
- e,
667
724
  t,
725
+ e,
726
+ r,
668
727
  s,
669
728
  i,
670
729
  n,
@@ -672,18 +731,18 @@ async function* X(l, r = !0, e, t, s, i, n, o, d, u, f, g) {
672
731
  d,
673
732
  u,
674
733
  f,
675
- g
734
+ m
676
735
  );
677
736
  }
678
- async function L(l, r) {
679
- if (!E)
737
+ async function B(l, t) {
738
+ if (!k)
680
739
  throw new Error("API not configured. Please call configureApi() with your server URL before using any API functions.");
681
- return E.stopChat(l, r);
740
+ return k.stopChat(l, t);
682
741
  }
683
742
  export {
684
- N as ApiClient,
685
- H as configureApi,
686
- L as stopChat,
743
+ j as ApiClient,
744
+ R as configureApi,
745
+ B as stopChat,
687
746
  X as streamChat
688
747
  };
689
748
  //# sourceMappingURL=api.mjs.map