@schmitech/chatbot-api 2.1.3 → 2.1.5

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,24 +1,24 @@
1
- var _ = Object.defineProperty;
2
- var D = (l, r, e) => r in l ? _(l, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : l[r] = e;
3
- var A = (l, r, e) => D(l, typeof r != "symbol" ? r + "" : r, e);
4
- let F = null, I = null;
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;
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
10
  ]).then(([l, r]) => {
11
- var e, s;
12
- (e = l == null ? void 0 : l.default) != null && e.Agent ? F = new l.default.Agent({ keepAlive: !0 }) : l != null && l.Agent && (F = new l.Agent({ keepAlive: !0 })), (s = r == null ? void 0 : r.default) != null && s.Agent ? I = new r.default.Agent({ keepAlive: !0 }) : r != null && r.Agent && (I = new r.Agent({ keepAlive: !0 }));
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 }));
13
13
  }).catch((l) => {
14
14
  console.warn("Failed to initialize HTTP agents:", l.message);
15
15
  });
16
- class j {
16
+ class N {
17
17
  // Session ID can be mutable
18
18
  constructor(r) {
19
- A(this, "apiUrl");
20
- A(this, "apiKey");
21
- A(this, "sessionId");
19
+ P(this, "apiUrl");
20
+ P(this, "apiKey");
21
+ P(this, "sessionId");
22
22
  if (!r.apiUrl || typeof r.apiUrl != "string")
23
23
  throw new Error("API URL must be a valid string");
24
24
  if (r.apiKey !== void 0 && r.apiKey !== null && typeof r.apiKey != "string")
@@ -50,55 +50,55 @@ class j {
50
50
  ...this.getFetchOptions({
51
51
  method: "GET"
52
52
  })
53
- }).catch((t) => {
54
- 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;
53
+ }).catch((s) => {
54
+ 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;
55
55
  });
56
56
  if (!e.ok) {
57
- let t = "";
57
+ let s = "";
58
58
  try {
59
- t = await e.text();
59
+ s = await e.text();
60
60
  } catch {
61
- t = `HTTP ${e.status}`;
61
+ s = `HTTP ${e.status}`;
62
62
  }
63
- let i, o;
63
+ let i, n;
64
64
  try {
65
- const a = JSON.parse(t);
66
- i = a.detail || a.message || t;
65
+ const o = JSON.parse(s);
66
+ i = o.detail || o.message || s;
67
67
  } catch {
68
- i = t || `HTTP ${e.status}`;
68
+ i = s || `HTTP ${e.status}`;
69
69
  }
70
70
  switch (e.status) {
71
71
  case 401:
72
- o = "API key is invalid or expired";
72
+ n = "API key is invalid or expired";
73
73
  break;
74
74
  case 403:
75
- o = "Access denied: API key does not have required permissions";
75
+ n = "Access denied: API key does not have required permissions";
76
76
  break;
77
77
  case 404:
78
- o = "API key not found";
78
+ n = "API key not found";
79
79
  break;
80
80
  case 503:
81
- o = "API key management is not available in inference-only mode";
81
+ n = "API key management is not available in inference-only mode";
82
82
  break;
83
83
  default:
84
- o = `Failed to validate API key: ${i}`;
84
+ n = `Failed to validate API key: ${i}`;
85
85
  break;
86
86
  }
87
- throw new Error(o);
87
+ throw new Error(n);
88
88
  }
89
- const s = await e.json();
90
- if (!s.exists) {
91
- const t = "API key does not exist";
92
- throw new Error(t);
89
+ const t = await e.json();
90
+ if (!t.exists) {
91
+ const s = "API key does not exist";
92
+ throw new Error(s);
93
93
  }
94
- if (!s.active) {
95
- const t = "API key is inactive";
96
- throw new Error(t);
94
+ if (!t.active) {
95
+ const s = "API key is inactive";
96
+ throw new Error(s);
97
97
  }
98
- return s;
98
+ return t;
99
99
  } catch (e) {
100
- let s;
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") ? s = e.message : s = `API key validation failed: ${e.message}` : e.name === "TypeError" && ((r = e.message) != null && r.includes("Failed to fetch")) ? s = "Could not connect to the server. Please check if the server is running." : s = "API key validation failed. Please check your API key and try again.", console.warn(`[ApiClient] ${s}`), new Error(s);
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);
102
102
  }
103
103
  }
104
104
  /**
@@ -115,85 +115,87 @@ class j {
115
115
  if (!this.apiKey)
116
116
  throw new Error("API key is required to get adapter information");
117
117
  try {
118
- const e = await fetch(`${this.apiUrl}/admin/api-keys/info`, {
118
+ const e = await fetch(`${this.apiUrl}/admin/adapters/info`, {
119
119
  ...this.getFetchOptions({
120
120
  method: "GET"
121
121
  })
122
- }).catch((t) => {
123
- 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;
122
+ }).catch((s) => {
123
+ 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;
124
124
  });
125
125
  if (!e.ok) {
126
- let t = "";
126
+ let s = "";
127
127
  try {
128
- t = await e.text();
128
+ s = await e.text();
129
129
  } catch {
130
- t = `HTTP ${e.status}`;
130
+ s = `HTTP ${e.status}`;
131
131
  }
132
- let i, o;
132
+ let i, n;
133
133
  try {
134
- const a = JSON.parse(t);
135
- i = a.detail || a.message || t;
134
+ const o = JSON.parse(s);
135
+ i = o.detail || o.message || s;
136
136
  } catch {
137
- i = t || `HTTP ${e.status}`;
137
+ i = s || `HTTP ${e.status}`;
138
138
  }
139
139
  switch (e.status) {
140
140
  case 401:
141
- o = "API key is invalid, disabled, or has no associated adapter";
141
+ n = "API key is invalid, disabled, or has no associated adapter";
142
142
  break;
143
143
  case 404:
144
- o = "Adapter configuration not found";
144
+ n = "Adapter configuration not found";
145
145
  break;
146
146
  case 503:
147
- o = "Service is not available";
147
+ n = "Service is not available";
148
148
  break;
149
149
  default:
150
- o = `Failed to get adapter info: ${i}`;
150
+ n = `Failed to get adapter info: ${i}`;
151
151
  break;
152
152
  }
153
- throw new Error(o);
153
+ throw new Error(n);
154
154
  }
155
155
  return await e.json();
156
156
  } catch (e) {
157
- let s;
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") ? s = e.message : s = `Failed to get adapter info: ${e.message}` : e.name === "TypeError" && ((r = e.message) != null && r.includes("Failed to fetch")) ? s = "Could not connect to the server. Please check if the server is running." : s = "Failed to get adapter information. Please try again.", console.warn(`[ApiClient] ${s}`), new Error(s);
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);
159
159
  }
160
160
  }
161
161
  // Helper to get fetch options with connection pooling if available
162
162
  getFetchOptions(r = {}) {
163
163
  const e = {};
164
164
  if (typeof window > "u") {
165
- const i = this.apiUrl.startsWith("https:") ? I : F;
165
+ const i = this.apiUrl.startsWith("https:") ? $ : I;
166
166
  i && (e.agent = i);
167
167
  } else
168
168
  e.headers = { Connection: "keep-alive" };
169
- const s = {
169
+ const t = {
170
170
  "X-Request-ID": Date.now().toString(36) + Math.random().toString(36).substring(2)
171
171
  };
172
- if (e.headers && Object.assign(s, e.headers), r.headers) {
173
- const t = r.headers;
174
- for (const [i, o] of Object.entries(t))
175
- (i.toLowerCase() !== "x-api-key" || !this.apiKey) && (s[i] = o);
172
+ if (e.headers && Object.assign(t, e.headers), r.headers) {
173
+ const s = r.headers;
174
+ for (const [i, n] of Object.entries(s))
175
+ (i.toLowerCase() !== "x-api-key" || !this.apiKey) && (t[i] = n);
176
176
  }
177
- return this.apiKey && (s["X-API-Key"] = this.apiKey), this.sessionId && (s["X-Session-ID"] = this.sessionId), {
177
+ return this.apiKey && (t["X-API-Key"] = this.apiKey), this.sessionId && (t["X-Session-ID"] = this.sessionId), {
178
178
  ...r,
179
179
  ...e,
180
- headers: s
180
+ headers: t
181
181
  };
182
182
  }
183
183
  // Create Chat request
184
- createChatRequest(r, e = !0, s, t, i, o, a, h, u, p, k) {
184
+ createChatRequest(r, e = !0, t, s, i, n, o, d, u, f, g) {
185
185
  const c = {
186
186
  messages: [
187
187
  { role: "user", content: r }
188
188
  ],
189
189
  stream: e
190
190
  };
191
- return s && s.length > 0 && (c.file_ids = s), t && (c.thread_id = t), i && (c.audio_input = i), o && (c.audio_format = o), a && (c.language = a), h !== void 0 && (c.return_audio = h), u && (c.tts_voice = u), p && (c.source_language = p), k && (c.target_language = k), c;
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;
192
192
  }
193
- async *streamChat(r, e = !0, s, t, i, o, a, h, u, p, k) {
194
- var c, C, x;
193
+ async *streamChat(r, e = !0, t, s, i, n, o, d, u, f, g, c) {
194
+ var C, x, b;
195
195
  try {
196
- const y = new AbortController(), O = setTimeout(() => y.abort(), 6e4), g = await fetch(`${this.apiUrl}/v1/chat`, {
196
+ const w = new AbortController(), S = setTimeout(() => w.abort(), 6e4);
197
+ c && c.addEventListener("abort", () => w.abort());
198
+ const m = await fetch(`${this.apiUrl}/v1/chat`, {
197
199
  ...this.getFetchOptions({
198
200
  method: "POST",
199
201
  headers: {
@@ -203,101 +205,164 @@ class j {
203
205
  body: JSON.stringify(this.createChatRequest(
204
206
  r,
205
207
  e,
206
- s,
207
208
  t,
209
+ s,
208
210
  i,
211
+ n,
209
212
  o,
210
- a,
211
- h,
213
+ d,
212
214
  u,
213
- p,
214
- k
215
+ f,
216
+ g
215
217
  ))
216
218
  }),
217
- signal: y.signal
219
+ signal: w.signal
218
220
  });
219
- if (clearTimeout(O), !g.ok) {
220
- const w = await g.text();
221
- throw new Error(`Network response was not ok: ${g.status} ${w}`);
221
+ if (clearTimeout(S), !m.ok) {
222
+ const p = await m.text();
223
+ throw new Error(`Network response was not ok: ${m.status} ${p}`);
222
224
  }
223
225
  if (!e) {
224
- const w = await g.json();
225
- w.response && (yield {
226
- text: w.response,
226
+ const p = await m.json();
227
+ p.response && (yield {
228
+ text: p.response,
227
229
  done: !0,
228
- audio: w.audio,
229
- audioFormat: w.audio_format
230
+ audio: p.audio,
231
+ audioFormat: p.audio_format
230
232
  });
231
233
  return;
232
234
  }
233
- const P = (c = g.body) == null ? void 0 : c.getReader();
234
- if (!P) throw new Error("No reader available");
235
- const S = new TextDecoder();
236
- let f = "", v = !1;
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;
237
239
  try {
238
240
  for (; ; ) {
239
- const { done: w, value: U } = await P.read();
240
- if (w)
241
+ const { done: p, value: U } = await T.read();
242
+ if (p)
241
243
  break;
242
- const q = S.decode(U, { stream: !0 });
243
- f += q;
244
- let E = 0, T;
245
- for (; (T = f.indexOf(`
246
- `, E)) !== -1; ) {
247
- const m = f.slice(E, T).trim();
248
- if (E = T + 1, m && m.startsWith("data: ")) {
249
- const d = m.slice(6).trim();
250
- if (!d || d === "[DONE]") {
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();
252
+ if (!h || h === "[DONE]") {
251
253
  yield { text: "", done: !0 };
252
254
  return;
253
255
  }
254
256
  try {
255
- const n = JSON.parse(d);
256
- if (n.error) {
257
- const K = `Server error: ${((C = n.error) == null ? void 0 : C.message) || n.error || "Unknown server error"}`;
257
+ const a = JSON.parse(h);
258
+ if (a.error) {
259
+ const K = `Server error: ${((x = a.error) == null ? void 0 : x.message) || a.error || "Unknown server error"}`;
258
260
  throw console.warn(`[ApiClient] ${K}`), new Error(K);
259
261
  }
260
- if (n.done === !0) {
261
- v = !0, yield {
262
+ if (a.request_id && !a.response && !a.done) {
263
+ yield {
264
+ text: "",
265
+ done: !1,
266
+ request_id: a.request_id
267
+ };
268
+ continue;
269
+ }
270
+ if (a.done === !0) {
271
+ k = !0, yield {
262
272
  text: "",
263
273
  done: !0,
264
- audio: n.audio,
265
- audioFormat: n.audio_format || n.audioFormat,
266
- threading: n.threading
274
+ audio: a.audio,
275
+ audioFormat: a.audio_format || a.audioFormat,
276
+ threading: a.threading
267
277
  // Pass through threading metadata
268
278
  };
269
279
  return;
270
280
  }
271
- const b = n.response || "";
272
- n.audio_chunk !== void 0 && (yield {
281
+ const O = a.response || "";
282
+ a.audio_chunk !== void 0 && (yield {
273
283
  text: "",
274
284
  done: !1,
275
- audio_chunk: n.audio_chunk,
276
- audioFormat: n.audioFormat || n.audio_format || "opus",
277
- chunk_index: n.chunk_index ?? 0
278
- }), (b || n.audio) && (v = !0, yield {
279
- text: b,
280
- done: n.done || !1,
281
- audio: n.audio,
282
- audioFormat: n.audio_format || n.audioFormat,
283
- threading: n.threading
285
+ audio_chunk: a.audio_chunk,
286
+ audioFormat: a.audioFormat || a.audio_format || "opus",
287
+ chunk_index: a.chunk_index ?? 0
288
+ }), (O || a.audio) && (k = !0, yield {
289
+ text: O,
290
+ done: a.done || !1,
291
+ audio: a.audio,
292
+ audioFormat: a.audio_format || a.audioFormat,
293
+ threading: a.threading
284
294
  // Include threading if present
285
295
  });
286
- } catch (n) {
287
- if ((x = n == null ? void 0 : n.message) != null && x.startsWith("Server error:"))
288
- throw n;
289
- console.warn("[ApiClient] Unable to parse server response. This may be a temporary issue."), console.warn("[ApiClient] Parse error details:", n == null ? void 0 : n.message), console.warn("[ApiClient] JSON text length:", d == null ? void 0 : d.length), console.warn("[ApiClient] JSON text preview (first 200 chars):", d == null ? void 0 : d.substring(0, 200)), console.warn("[ApiClient] JSON text preview (last 200 chars):", d == null ? void 0 : d.substring(d.length - 200));
296
+ } catch (a) {
297
+ if ((b = a == null ? void 0 : a.message) != null && b.startsWith("Server error:"))
298
+ throw a;
299
+ 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));
290
300
  }
291
- } else m && (v = !0, yield { text: m, done: !1 });
301
+ } else v && (k = !0, yield { text: v, done: !1 });
292
302
  }
293
- f = f.slice(E), f.length > 1e6 && (console.warn("[ApiClient] Buffer too large, truncating..."), f = f.slice(-5e5));
303
+ y = y.slice(A), y.length > 1e6 && (console.warn("[ApiClient] Buffer too large, truncating..."), y = y.slice(-5e5));
294
304
  }
295
- v && (yield { text: "", done: !0 });
305
+ k && (yield { text: "", done: !0 });
296
306
  } finally {
297
- P.releaseLock();
307
+ T.releaseLock();
308
+ }
309
+ } catch (w) {
310
+ 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;
311
+ }
312
+ }
313
+ /**
314
+ * Stop an active streaming request.
315
+ *
316
+ * @param sessionId - The session identifier
317
+ * @param requestId - The request identifier (from first stream chunk)
318
+ * @returns Promise resolving to true if stream was cancelled, false otherwise
319
+ */
320
+ async stopChat(r, e) {
321
+ try {
322
+ const t = await fetch(`${this.apiUrl}/v1/chat/stop`, {
323
+ ...this.getFetchOptions({
324
+ method: "POST",
325
+ headers: {
326
+ "Content-Type": "application/json"
327
+ },
328
+ body: JSON.stringify({
329
+ session_id: r,
330
+ request_id: e
331
+ })
332
+ })
333
+ });
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;
337
+ }
338
+ }
339
+ async getConversationHistory(r, e) {
340
+ const t = r || this.sessionId;
341
+ if (!t)
342
+ throw new Error("No session ID provided and no current session available");
343
+ const s = {};
344
+ this.apiKey && (s["X-API-Key"] = this.apiKey);
345
+ try {
346
+ const i = new URL(`${this.apiUrl}/admin/chat-history/${t}`);
347
+ typeof e == "number" && Number.isFinite(e) && e > 0 && i.searchParams.set("limit", String(Math.floor(e)));
348
+ const n = await fetch(i.toString(), {
349
+ ...this.getFetchOptions({
350
+ method: "GET",
351
+ headers: s
352
+ })
353
+ });
354
+ if (!n.ok) {
355
+ const f = await n.text();
356
+ throw new Error(`Failed to fetch conversation history: ${n.status} ${f}`);
298
357
  }
299
- } catch (y) {
300
- throw y.name === "AbortError" ? new Error("Connection timed out. Please check if the server is running.") : y.name === "TypeError" && y.message.includes("Failed to fetch") ? new Error("Could not connect to the server. Please check if the server is running.") : y;
358
+ 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
+ return {
360
+ session_id: (o == null ? void 0 : o.session_id) || t,
361
+ messages: d,
362
+ count: u
363
+ };
364
+ } catch (i) {
365
+ 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;
301
366
  }
302
367
  }
303
368
  async clearConversationHistory(r) {
@@ -306,52 +371,52 @@ class j {
306
371
  throw new Error("No session ID provided and no current session available");
307
372
  if (!this.apiKey)
308
373
  throw new Error("API key is required for clearing conversation history");
309
- const s = {
374
+ const t = {
310
375
  "Content-Type": "application/json",
311
376
  "X-Session-ID": e,
312
377
  "X-API-Key": this.apiKey
313
378
  };
314
379
  try {
315
- const t = await fetch(`${this.apiUrl}/admin/chat-history/${e}`, {
380
+ const s = await fetch(`${this.apiUrl}/admin/chat-history/${e}`, {
316
381
  ...this.getFetchOptions({
317
382
  method: "DELETE",
318
- headers: s
383
+ headers: t
319
384
  })
320
385
  });
321
- if (!t.ok) {
322
- const o = await t.text();
323
- throw new Error(`Failed to clear conversation history: ${t.status} ${o}`);
386
+ if (!s.ok) {
387
+ const n = await s.text();
388
+ throw new Error(`Failed to clear conversation history: ${s.status} ${n}`);
324
389
  }
325
- return await t.json();
326
- } catch (t) {
327
- 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;
390
+ return await s.json();
391
+ } catch (s) {
392
+ 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;
328
393
  }
329
394
  }
330
395
  async deleteConversationWithFiles(r, e) {
331
- const s = r || this.sessionId;
332
- if (!s)
396
+ const t = r || this.sessionId;
397
+ if (!t)
333
398
  throw new Error("No session ID provided and no current session available");
334
399
  if (!this.apiKey)
335
400
  throw new Error("API key is required for deleting conversation");
336
- const t = {
401
+ const s = {
337
402
  "Content-Type": "application/json",
338
- "X-Session-ID": s,
403
+ "X-Session-ID": t,
339
404
  "X-API-Key": this.apiKey
340
- }, i = e && e.length > 0 ? `?file_ids=${e.join(",")}` : "", o = `${this.apiUrl}/admin/conversations/${s}${i}`;
405
+ }, i = e && e.length > 0 ? `?file_ids=${e.join(",")}` : "", n = `${this.apiUrl}/admin/conversations/${t}${i}`;
341
406
  try {
342
- const a = await fetch(o, {
407
+ const o = await fetch(n, {
343
408
  ...this.getFetchOptions({
344
409
  method: "DELETE",
345
- headers: t
410
+ headers: s
346
411
  })
347
412
  });
348
- if (!a.ok) {
349
- const u = await a.text();
350
- throw new Error(`Failed to delete conversation: ${a.status} ${u}`);
413
+ if (!o.ok) {
414
+ const u = await o.text();
415
+ throw new Error(`Failed to delete conversation: ${o.status} ${u}`);
351
416
  }
352
- return await a.json();
353
- } catch (a) {
354
- throw a.name === "TypeError" && a.message.includes("Failed to fetch") ? new Error("Could not connect to the server. Please check if the server is running.") : a;
417
+ return await o.json();
418
+ } catch (o) {
419
+ throw o.name === "TypeError" && o.message.includes("Failed to fetch") ? new Error("Could not connect to the server. Please check if the server is running.") : o;
355
420
  }
356
421
  }
357
422
  /**
@@ -365,28 +430,28 @@ class j {
365
430
  async createThread(r, e) {
366
431
  if (!this.apiKey)
367
432
  throw new Error("API key is required for creating threads");
368
- const s = {
433
+ const t = {
369
434
  "Content-Type": "application/json",
370
435
  "X-API-Key": this.apiKey
371
436
  };
372
437
  try {
373
- const t = await fetch(`${this.apiUrl}/api/threads`, {
438
+ const s = await fetch(`${this.apiUrl}/api/threads`, {
374
439
  ...this.getFetchOptions({
375
440
  method: "POST",
376
- headers: s,
441
+ headers: t,
377
442
  body: JSON.stringify({
378
443
  message_id: r,
379
444
  session_id: e
380
445
  })
381
446
  })
382
447
  });
383
- if (!t.ok) {
384
- const o = await t.text();
385
- throw new Error(`Failed to create thread: ${t.status} ${o}`);
448
+ if (!s.ok) {
449
+ const n = await s.text();
450
+ throw new Error(`Failed to create thread: ${s.status} ${n}`);
386
451
  }
387
- return await t.json();
388
- } catch (t) {
389
- 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;
452
+ return await s.json();
453
+ } catch (s) {
454
+ 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;
390
455
  }
391
456
  }
392
457
  /**
@@ -403,19 +468,19 @@ class j {
403
468
  "X-API-Key": this.apiKey
404
469
  };
405
470
  try {
406
- const s = await fetch(`${this.apiUrl}/api/threads/${r}`, {
471
+ const t = await fetch(`${this.apiUrl}/api/threads/${r}`, {
407
472
  ...this.getFetchOptions({
408
473
  method: "GET",
409
474
  headers: e
410
475
  })
411
476
  });
412
- if (!s.ok) {
413
- const i = await s.text();
414
- throw new Error(`Failed to get thread info: ${s.status} ${i}`);
477
+ if (!t.ok) {
478
+ const i = await t.text();
479
+ throw new Error(`Failed to get thread info: ${t.status} ${i}`);
415
480
  }
416
- return await s.json();
417
- } catch (s) {
418
- 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;
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;
419
484
  }
420
485
  }
421
486
  /**
@@ -432,19 +497,19 @@ class j {
432
497
  "X-API-Key": this.apiKey
433
498
  };
434
499
  try {
435
- const s = await fetch(`${this.apiUrl}/api/threads/${r}`, {
500
+ const t = await fetch(`${this.apiUrl}/api/threads/${r}`, {
436
501
  ...this.getFetchOptions({
437
502
  method: "DELETE",
438
503
  headers: e
439
504
  })
440
505
  });
441
- if (!s.ok) {
442
- const i = await s.text();
443
- throw new Error(`Failed to delete thread: ${s.status} ${i}`);
506
+ if (!t.ok) {
507
+ const i = await t.text();
508
+ throw new Error(`Failed to delete thread: ${t.status} ${i}`);
444
509
  }
445
- return await s.json();
446
- } catch (s) {
447
- 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;
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;
448
513
  }
449
514
  }
450
515
  /**
@@ -460,19 +525,19 @@ class j {
460
525
  const e = new FormData();
461
526
  e.append("file", r);
462
527
  try {
463
- const s = await fetch(`${this.apiUrl}/api/files/upload`, {
528
+ const t = await fetch(`${this.apiUrl}/api/files/upload`, {
464
529
  ...this.getFetchOptions({
465
530
  method: "POST",
466
531
  body: e
467
532
  })
468
533
  });
469
- if (!s.ok) {
470
- const t = await s.text();
471
- throw new Error(`Failed to upload file: ${s.status} ${t}`);
534
+ if (!t.ok) {
535
+ const s = await t.text();
536
+ throw new Error(`Failed to upload file: ${t.status} ${s}`);
472
537
  }
473
- return await s.json();
474
- } catch (s) {
475
- 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;
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;
476
541
  }
477
542
  }
478
543
  /**
@@ -516,8 +581,8 @@ class j {
516
581
  })
517
582
  });
518
583
  if (!e.ok) {
519
- const s = await e.text();
520
- throw new Error(`Failed to get file info: ${e.status} ${s}`);
584
+ const t = await e.text();
585
+ throw new Error(`Failed to get file info: ${e.status} ${t}`);
521
586
  }
522
587
  return await e.json();
523
588
  } catch (e) {
@@ -533,26 +598,26 @@ class j {
533
598
  * @returns Promise resolving to query results
534
599
  * @throws Error if query fails
535
600
  */
536
- async queryFile(r, e, s = 10) {
601
+ async queryFile(r, e, t = 10) {
537
602
  if (!this.apiKey)
538
603
  throw new Error("API key is required for querying files");
539
604
  try {
540
- const t = await fetch(`${this.apiUrl}/api/files/${r}/query`, {
605
+ const s = await fetch(`${this.apiUrl}/api/files/${r}/query`, {
541
606
  ...this.getFetchOptions({
542
607
  method: "POST",
543
608
  headers: {
544
609
  "Content-Type": "application/json"
545
610
  },
546
- body: JSON.stringify({ query: e, max_results: s })
611
+ body: JSON.stringify({ query: e, max_results: t })
547
612
  })
548
613
  });
549
- if (!t.ok) {
550
- const i = await t.text();
551
- throw new Error(`Failed to query file: ${t.status} ${i}`);
614
+ if (!s.ok) {
615
+ const i = await s.text();
616
+ throw new Error(`Failed to query file: ${s.status} ${i}`);
552
617
  }
553
- return await t.json();
554
- } catch (t) {
555
- 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;
618
+ return await s.json();
619
+ } catch (s) {
620
+ 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;
556
621
  }
557
622
  }
558
623
  /**
@@ -565,53 +630,60 @@ class j {
565
630
  async deleteFile(r) {
566
631
  if (!this.apiKey)
567
632
  throw new Error("API key is required for deleting files");
568
- const e = `${this.apiUrl}/api/files/${r}`, s = this.getFetchOptions({
633
+ const e = `${this.apiUrl}/api/files/${r}`, t = this.getFetchOptions({
569
634
  method: "DELETE"
570
635
  });
571
636
  try {
572
- const t = await fetch(e, s);
573
- if (!t.ok) {
574
- const o = await t.text();
575
- let a;
637
+ const s = await fetch(e, t);
638
+ if (!s.ok) {
639
+ const n = await s.text();
640
+ let o;
576
641
  try {
577
- const h = JSON.parse(o);
578
- a = h.detail || h.message || `Failed to delete file (HTTP ${t.status})`;
642
+ const d = JSON.parse(n);
643
+ o = d.detail || d.message || `Failed to delete file (HTTP ${s.status})`;
579
644
  } catch {
580
- a = `Failed to delete file (HTTP ${t.status})`;
645
+ o = `Failed to delete file (HTTP ${s.status})`;
581
646
  }
582
- throw console.warn(`[ApiClient] ${a}`), new Error(a);
647
+ throw console.warn(`[ApiClient] ${o}`), new Error(o);
583
648
  }
584
- return await t.json();
585
- } catch (t) {
649
+ return await s.json();
650
+ } catch (s) {
586
651
  let i;
587
- throw t.name === "TypeError" && t.message.includes("Failed to fetch") ? i = "Could not connect to the server. Please check if the server is running." : t.message && !t.message.includes("Failed to delete file") ? i = t.message : i = "Failed to delete file. Please try again.", console.warn(`[ApiClient] ${i}`), new Error(i);
652
+ throw s.name === "TypeError" && s.message.includes("Failed to fetch") ? i = "Could not connect to the server. Please check if the server is running." : s.message && !s.message.includes("Failed to delete file") ? i = s.message : i = "Failed to delete file. Please try again.", console.warn(`[ApiClient] ${i}`), new Error(i);
588
653
  }
589
654
  }
590
655
  }
591
- let $ = null;
592
- const M = (l, r = null, e = null) => {
593
- $ = new j({ apiUrl: l, apiKey: r, sessionId: e });
656
+ let E = null;
657
+ const H = (l, r = null, e = null) => {
658
+ E = new N({ apiUrl: l, apiKey: r, sessionId: e });
594
659
  };
595
- async function* H(l, r = !0, e, s, t, i, o, a, h, u, p) {
596
- if (!$)
660
+ async function* X(l, r = !0, e, t, s, i, n, o, d, u, f, g) {
661
+ if (!E)
597
662
  throw new Error("API not configured. Please call configureApi() with your server URL before using any API functions.");
598
- yield* $.streamChat(
663
+ yield* E.streamChat(
599
664
  l,
600
665
  r,
601
666
  e,
602
- s,
603
667
  t,
668
+ s,
604
669
  i,
670
+ n,
605
671
  o,
606
- a,
607
- h,
672
+ d,
608
673
  u,
609
- p
674
+ f,
675
+ g
610
676
  );
611
677
  }
678
+ async function L(l, r) {
679
+ if (!E)
680
+ throw new Error("API not configured. Please call configureApi() with your server URL before using any API functions.");
681
+ return E.stopChat(l, r);
682
+ }
612
683
  export {
613
- j as ApiClient,
614
- M as configureApi,
615
- H as streamChat
684
+ N as ApiClient,
685
+ H as configureApi,
686
+ L as stopChat,
687
+ X as streamChat
616
688
  };
617
689
  //# sourceMappingURL=api.mjs.map