@schmitech/chatbot-api 2.1.4 → 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,36 +1,36 @@
1
- var q = Object.defineProperty;
2
- var D = (l, s, e) => s in l ? q(l, s, { enumerable: !0, configurable: !0, writable: !0, value: e }) : l[s] = e;
3
- var A = (l, s, e) => D(l, typeof s != "symbol" ? s + "" : s, 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
- ]).then(([l, s]) => {
11
- var e, r;
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 })), (r = s == null ? void 0 : s.default) != null && r.Agent ? I = new s.default.Agent({ keepAlive: !0 }) : s != null && s.Agent && (I = new s.Agent({ keepAlive: !0 }));
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 }));
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
- constructor(s) {
19
- A(this, "apiUrl");
20
- A(this, "apiKey");
21
- A(this, "sessionId");
22
- if (!s.apiUrl || typeof s.apiUrl != "string")
18
+ constructor(r) {
19
+ P(this, "apiUrl");
20
+ P(this, "apiKey");
21
+ P(this, "sessionId");
22
+ if (!r.apiUrl || typeof r.apiUrl != "string")
23
23
  throw new Error("API URL must be a valid string");
24
- if (s.apiKey !== void 0 && s.apiKey !== null && typeof s.apiKey != "string")
24
+ if (r.apiKey !== void 0 && r.apiKey !== null && typeof r.apiKey != "string")
25
25
  throw new Error("API key must be a valid string or null");
26
- if (s.sessionId !== void 0 && s.sessionId !== null && typeof s.sessionId != "string")
26
+ if (r.sessionId !== void 0 && r.sessionId !== null && typeof r.sessionId != "string")
27
27
  throw new Error("Session ID must be a valid string or null");
28
- this.apiUrl = s.apiUrl, this.apiKey = s.apiKey ?? null, this.sessionId = s.sessionId ?? null;
28
+ this.apiUrl = r.apiUrl, this.apiKey = r.apiKey ?? null, this.sessionId = r.sessionId ?? null;
29
29
  }
30
- setSessionId(s) {
31
- if (s !== null && typeof s != "string")
30
+ setSessionId(r) {
31
+ if (r !== null && typeof r != "string")
32
32
  throw new Error("Session ID must be a valid string or null");
33
- this.sessionId = s;
33
+ this.sessionId = r;
34
34
  }
35
35
  getSessionId() {
36
36
  return this.sessionId;
@@ -42,7 +42,7 @@ class j {
42
42
  * @throws Error if API key is not provided, invalid, inactive, or validation fails
43
43
  */
44
44
  async validateApiKey() {
45
- var s;
45
+ var r;
46
46
  if (!this.apiKey)
47
47
  throw new Error("API key is required for validation");
48
48
  try {
@@ -50,22 +50,22 @@ 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
63
  let i, n;
64
64
  try {
65
- const o = JSON.parse(t);
66
- i = o.detail || o.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:
@@ -86,19 +86,19 @@ class j {
86
86
  }
87
87
  throw new Error(n);
88
88
  }
89
- const r = await e.json();
90
- if (!r.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 (!r.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 r;
98
+ return t;
99
99
  } catch (e) {
100
- let r;
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") ? r = e.message : r = `API key validation failed: ${e.message}` : e.name === "TypeError" && ((s = e.message) != null && s.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);
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
  /**
@@ -111,7 +111,7 @@ class j {
111
111
  * @throws Error if API key is not provided, invalid, disabled, or request fails
112
112
  */
113
113
  async getAdapterInfo() {
114
- var s;
114
+ var r;
115
115
  if (!this.apiKey)
116
116
  throw new Error("API key is required to get adapter information");
117
117
  try {
@@ -119,22 +119,22 @@ class j {
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
132
  let i, n;
133
133
  try {
134
- const o = JSON.parse(t);
135
- i = o.detail || o.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:
@@ -154,46 +154,48 @@ class j {
154
154
  }
155
155
  return await e.json();
156
156
  } catch (e) {
157
- let r;
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") ? r = e.message : r = `Failed to get adapter info: ${e.message}` : e.name === "TypeError" && ((s = e.message) != null && s.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);
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
- getFetchOptions(s = {}) {
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 r = {
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(r, e.headers), s.headers) {
173
- const t = s.headers;
174
- for (const [i, n] of Object.entries(t))
175
- (i.toLowerCase() !== "x-api-key" || !this.apiKey) && (r[i] = n);
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 && (r["X-API-Key"] = this.apiKey), this.sessionId && (r["X-Session-ID"] = this.sessionId), {
178
- ...s,
177
+ return this.apiKey && (t["X-API-Key"] = this.apiKey), this.sessionId && (t["X-Session-ID"] = this.sessionId), {
178
+ ...r,
179
179
  ...e,
180
- headers: r
180
+ headers: t
181
181
  };
182
182
  }
183
183
  // Create Chat request
184
- createChatRequest(s, e = !0, r, t, i, n, o, c, u, f, v) {
185
- const d = {
184
+ createChatRequest(r, e = !0, t, s, i, n, o, d, u, f, g) {
185
+ const c = {
186
186
  messages: [
187
- { role: "user", content: s }
187
+ { role: "user", content: r }
188
188
  ],
189
189
  stream: e
190
190
  };
191
- return r && r.length > 0 && (d.file_ids = r), t && (d.thread_id = t), i && (d.audio_input = i), n && (d.audio_format = n), o && (d.language = o), c !== void 0 && (d.return_audio = c), u && (d.tts_voice = u), f && (d.source_language = f), v && (d.target_language = v), d;
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(s, e = !0, r, t, i, n, o, c, u, f, v) {
194
- var d, 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 p = new AbortController(), O = setTimeout(() => p.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: {
@@ -201,52 +203,52 @@ class j {
201
203
  Accept: e ? "text/event-stream" : "application/json"
202
204
  },
203
205
  body: JSON.stringify(this.createChatRequest(
204
- s,
205
- e,
206
206
  r,
207
+ e,
207
208
  t,
209
+ s,
208
210
  i,
209
211
  n,
210
212
  o,
211
- c,
213
+ d,
212
214
  u,
213
215
  f,
214
- v
216
+ g
215
217
  ))
216
218
  }),
217
- signal: p.signal
219
+ signal: w.signal
218
220
  });
219
- if (clearTimeout(O), !g.ok) {
220
- const y = await g.text();
221
- throw new Error(`Network response was not ok: ${g.status} ${y}`);
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 y = await g.json();
225
- y.response && (yield {
226
- text: y.response,
226
+ const p = await m.json();
227
+ p.response && (yield {
228
+ text: p.response,
227
229
  done: !0,
228
- audio: y.audio,
229
- audioFormat: y.audio_format
230
+ audio: p.audio,
231
+ audioFormat: p.audio_format
230
232
  });
231
233
  return;
232
234
  }
233
- const P = (d = g.body) == null ? void 0 : d.getReader();
234
- if (!P) throw new Error("No reader available");
235
- const S = new TextDecoder();
236
- let w = "", k = !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: y, value: U } = await P.read();
240
- if (y)
241
+ const { done: p, value: U } = await T.read();
242
+ if (p)
241
243
  break;
242
- const _ = S.decode(U, { stream: !0 });
243
- w += _;
244
- let E = 0, T;
245
- for (; (T = w.indexOf(`
246
- `, E)) !== -1; ) {
247
- const m = w.slice(E, T).trim();
248
- if (E = T + 1, m && m.startsWith("data: ")) {
249
- const h = m.slice(6).trim();
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();
250
252
  if (!h || h === "[DONE]") {
251
253
  yield { text: "", done: !0 };
252
254
  return;
@@ -254,9 +256,17 @@ class j {
254
256
  try {
255
257
  const a = JSON.parse(h);
256
258
  if (a.error) {
257
- const K = `Server error: ${((C = a.error) == null ? void 0 : C.message) || a.error || "Unknown server 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
  }
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
+ }
260
270
  if (a.done === !0) {
261
271
  k = !0, yield {
262
272
  text: "",
@@ -268,15 +278,15 @@ class j {
268
278
  };
269
279
  return;
270
280
  }
271
- const b = a.response || "";
281
+ const O = a.response || "";
272
282
  a.audio_chunk !== void 0 && (yield {
273
283
  text: "",
274
284
  done: !1,
275
285
  audio_chunk: a.audio_chunk,
276
286
  audioFormat: a.audioFormat || a.audio_format || "opus",
277
287
  chunk_index: a.chunk_index ?? 0
278
- }), (b || a.audio) && (k = !0, yield {
279
- text: b,
288
+ }), (O || a.audio) && (k = !0, yield {
289
+ text: O,
280
290
  done: a.done || !1,
281
291
  audio: a.audio,
282
292
  audioFormat: a.audio_format || a.audioFormat,
@@ -284,94 +294,120 @@ class j {
284
294
  // Include threading if present
285
295
  });
286
296
  } catch (a) {
287
- if ((x = a == null ? void 0 : a.message) != null && x.startsWith("Server error:"))
297
+ if ((b = a == null ? void 0 : a.message) != null && b.startsWith("Server error:"))
288
298
  throw a;
289
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 && (k = !0, yield { text: m, done: !1 });
301
+ } else v && (k = !0, yield { text: v, done: !1 });
292
302
  }
293
- w = w.slice(E), w.length > 1e6 && (console.warn("[ApiClient] Buffer too large, truncating..."), w = w.slice(-5e5));
303
+ y = y.slice(A), y.length > 1e6 && (console.warn("[ApiClient] Buffer too large, truncating..."), y = y.slice(-5e5));
294
304
  }
295
305
  k && (yield { text: "", done: !0 });
296
306
  } finally {
297
- P.releaseLock();
307
+ T.releaseLock();
298
308
  }
299
- } catch (p) {
300
- throw p.name === "AbortError" ? new Error("Connection timed out. Please check if the server is running.") : p.name === "TypeError" && p.message.includes("Failed to fetch") ? new Error("Could not connect to the server. Please check if the server is running.") : p;
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;
301
337
  }
302
338
  }
303
- async getConversationHistory(s, e) {
304
- const r = s || this.sessionId;
305
- if (!r)
339
+ async getConversationHistory(r, e) {
340
+ const t = r || this.sessionId;
341
+ if (!t)
306
342
  throw new Error("No session ID provided and no current session available");
307
- const t = {};
308
- this.apiKey && (t["X-API-Key"] = this.apiKey);
343
+ const s = {};
344
+ this.apiKey && (s["X-API-Key"] = this.apiKey);
309
345
  try {
310
- const i = new URL(`${this.apiUrl}/admin/chat-history/${r}`);
346
+ const i = new URL(`${this.apiUrl}/admin/chat-history/${t}`);
311
347
  typeof e == "number" && Number.isFinite(e) && e > 0 && i.searchParams.set("limit", String(Math.floor(e)));
312
348
  const n = await fetch(i.toString(), {
313
349
  ...this.getFetchOptions({
314
350
  method: "GET",
315
- headers: t
351
+ headers: s
316
352
  })
317
353
  });
318
354
  if (!n.ok) {
319
355
  const f = await n.text();
320
356
  throw new Error(`Failed to fetch conversation history: ${n.status} ${f}`);
321
357
  }
322
- const o = await n.json(), c = Array.isArray(o == null ? void 0 : o.messages) ? o.messages : [], u = typeof (o == null ? void 0 : o.count) == "number" ? o.count : c.length;
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;
323
359
  return {
324
- session_id: (o == null ? void 0 : o.session_id) || r,
325
- messages: c,
360
+ session_id: (o == null ? void 0 : o.session_id) || t,
361
+ messages: d,
326
362
  count: u
327
363
  };
328
364
  } catch (i) {
329
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;
330
366
  }
331
367
  }
332
- async clearConversationHistory(s) {
333
- const e = s || this.sessionId;
368
+ async clearConversationHistory(r) {
369
+ const e = r || this.sessionId;
334
370
  if (!e)
335
371
  throw new Error("No session ID provided and no current session available");
336
372
  if (!this.apiKey)
337
373
  throw new Error("API key is required for clearing conversation history");
338
- const r = {
374
+ const t = {
339
375
  "Content-Type": "application/json",
340
376
  "X-Session-ID": e,
341
377
  "X-API-Key": this.apiKey
342
378
  };
343
379
  try {
344
- const t = await fetch(`${this.apiUrl}/admin/chat-history/${e}`, {
380
+ const s = await fetch(`${this.apiUrl}/admin/chat-history/${e}`, {
345
381
  ...this.getFetchOptions({
346
382
  method: "DELETE",
347
- headers: r
383
+ headers: t
348
384
  })
349
385
  });
350
- if (!t.ok) {
351
- const n = await t.text();
352
- throw new Error(`Failed to clear conversation history: ${t.status} ${n}`);
386
+ if (!s.ok) {
387
+ const n = await s.text();
388
+ throw new Error(`Failed to clear conversation history: ${s.status} ${n}`);
353
389
  }
354
- return await t.json();
355
- } catch (t) {
356
- 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;
357
393
  }
358
394
  }
359
- async deleteConversationWithFiles(s, e) {
360
- const r = s || this.sessionId;
361
- if (!r)
395
+ async deleteConversationWithFiles(r, e) {
396
+ const t = r || this.sessionId;
397
+ if (!t)
362
398
  throw new Error("No session ID provided and no current session available");
363
399
  if (!this.apiKey)
364
400
  throw new Error("API key is required for deleting conversation");
365
- const t = {
401
+ const s = {
366
402
  "Content-Type": "application/json",
367
- "X-Session-ID": r,
403
+ "X-Session-ID": t,
368
404
  "X-API-Key": this.apiKey
369
- }, i = e && e.length > 0 ? `?file_ids=${e.join(",")}` : "", n = `${this.apiUrl}/admin/conversations/${r}${i}`;
405
+ }, i = e && e.length > 0 ? `?file_ids=${e.join(",")}` : "", n = `${this.apiUrl}/admin/conversations/${t}${i}`;
370
406
  try {
371
407
  const o = await fetch(n, {
372
408
  ...this.getFetchOptions({
373
409
  method: "DELETE",
374
- headers: t
410
+ headers: s
375
411
  })
376
412
  });
377
413
  if (!o.ok) {
@@ -391,31 +427,31 @@ class j {
391
427
  * @returns Promise resolving to thread information
392
428
  * @throws Error if the operation fails
393
429
  */
394
- async createThread(s, e) {
430
+ async createThread(r, e) {
395
431
  if (!this.apiKey)
396
432
  throw new Error("API key is required for creating threads");
397
- const r = {
433
+ const t = {
398
434
  "Content-Type": "application/json",
399
435
  "X-API-Key": this.apiKey
400
436
  };
401
437
  try {
402
- const t = await fetch(`${this.apiUrl}/api/threads`, {
438
+ const s = await fetch(`${this.apiUrl}/api/threads`, {
403
439
  ...this.getFetchOptions({
404
440
  method: "POST",
405
- headers: r,
441
+ headers: t,
406
442
  body: JSON.stringify({
407
- message_id: s,
443
+ message_id: r,
408
444
  session_id: e
409
445
  })
410
446
  })
411
447
  });
412
- if (!t.ok) {
413
- const n = await t.text();
414
- throw new Error(`Failed to create thread: ${t.status} ${n}`);
448
+ if (!s.ok) {
449
+ const n = await s.text();
450
+ throw new Error(`Failed to create thread: ${s.status} ${n}`);
415
451
  }
416
- return await t.json();
417
- } catch (t) {
418
- 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;
419
455
  }
420
456
  }
421
457
  /**
@@ -425,26 +461,26 @@ class j {
425
461
  * @returns Promise resolving to thread information
426
462
  * @throws Error if the operation fails
427
463
  */
428
- async getThreadInfo(s) {
464
+ async getThreadInfo(r) {
429
465
  if (!this.apiKey)
430
466
  throw new Error("API key is required for getting thread info");
431
467
  const e = {
432
468
  "X-API-Key": this.apiKey
433
469
  };
434
470
  try {
435
- const r = await fetch(`${this.apiUrl}/api/threads/${s}`, {
471
+ const t = await fetch(`${this.apiUrl}/api/threads/${r}`, {
436
472
  ...this.getFetchOptions({
437
473
  method: "GET",
438
474
  headers: e
439
475
  })
440
476
  });
441
- if (!r.ok) {
442
- const i = await r.text();
443
- throw new Error(`Failed to get thread info: ${r.status} ${i}`);
477
+ if (!t.ok) {
478
+ const i = await t.text();
479
+ throw new Error(`Failed to get thread info: ${t.status} ${i}`);
444
480
  }
445
- return await r.json();
446
- } catch (r) {
447
- 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;
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;
448
484
  }
449
485
  }
450
486
  /**
@@ -454,26 +490,26 @@ class j {
454
490
  * @returns Promise resolving to deletion result
455
491
  * @throws Error if the operation fails
456
492
  */
457
- async deleteThread(s) {
493
+ async deleteThread(r) {
458
494
  if (!this.apiKey)
459
495
  throw new Error("API key is required for deleting threads");
460
496
  const e = {
461
497
  "X-API-Key": this.apiKey
462
498
  };
463
499
  try {
464
- const r = await fetch(`${this.apiUrl}/api/threads/${s}`, {
500
+ const t = await fetch(`${this.apiUrl}/api/threads/${r}`, {
465
501
  ...this.getFetchOptions({
466
502
  method: "DELETE",
467
503
  headers: e
468
504
  })
469
505
  });
470
- if (!r.ok) {
471
- const i = await r.text();
472
- throw new Error(`Failed to delete thread: ${r.status} ${i}`);
506
+ if (!t.ok) {
507
+ const i = await t.text();
508
+ throw new Error(`Failed to delete thread: ${t.status} ${i}`);
473
509
  }
474
- return await r.json();
475
- } catch (r) {
476
- 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;
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;
477
513
  }
478
514
  }
479
515
  /**
@@ -483,25 +519,25 @@ class j {
483
519
  * @returns Promise resolving to upload response with file_id
484
520
  * @throws Error if upload fails
485
521
  */
486
- async uploadFile(s) {
522
+ async uploadFile(r) {
487
523
  if (!this.apiKey)
488
524
  throw new Error("API key is required for file upload");
489
525
  const e = new FormData();
490
- e.append("file", s);
526
+ e.append("file", r);
491
527
  try {
492
- const r = await fetch(`${this.apiUrl}/api/files/upload`, {
528
+ const t = await fetch(`${this.apiUrl}/api/files/upload`, {
493
529
  ...this.getFetchOptions({
494
530
  method: "POST",
495
531
  body: e
496
532
  })
497
533
  });
498
- if (!r.ok) {
499
- const t = await r.text();
500
- throw new Error(`Failed to upload file: ${r.status} ${t}`);
534
+ if (!t.ok) {
535
+ const s = await t.text();
536
+ throw new Error(`Failed to upload file: ${t.status} ${s}`);
501
537
  }
502
- return await r.json();
503
- } catch (r) {
504
- 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;
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;
505
541
  }
506
542
  }
507
543
  /**
@@ -514,18 +550,18 @@ class j {
514
550
  if (!this.apiKey)
515
551
  throw new Error("API key is required for listing files");
516
552
  try {
517
- const s = await fetch(`${this.apiUrl}/api/files`, {
553
+ const r = await fetch(`${this.apiUrl}/api/files`, {
518
554
  ...this.getFetchOptions({
519
555
  method: "GET"
520
556
  })
521
557
  });
522
- if (!s.ok) {
523
- const e = await s.text();
524
- throw new Error(`Failed to list files: ${s.status} ${e}`);
558
+ if (!r.ok) {
559
+ const e = await r.text();
560
+ throw new Error(`Failed to list files: ${r.status} ${e}`);
525
561
  }
526
- return await s.json();
527
- } catch (s) {
528
- 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;
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;
529
565
  }
530
566
  }
531
567
  /**
@@ -535,18 +571,18 @@ class j {
535
571
  * @returns Promise resolving to file information
536
572
  * @throws Error if file not found or request fails
537
573
  */
538
- async getFileInfo(s) {
574
+ async getFileInfo(r) {
539
575
  if (!this.apiKey)
540
576
  throw new Error("API key is required for getting file info");
541
577
  try {
542
- const e = await fetch(`${this.apiUrl}/api/files/${s}`, {
578
+ const e = await fetch(`${this.apiUrl}/api/files/${r}`, {
543
579
  ...this.getFetchOptions({
544
580
  method: "GET"
545
581
  })
546
582
  });
547
583
  if (!e.ok) {
548
- const r = await e.text();
549
- throw new Error(`Failed to get file info: ${e.status} ${r}`);
584
+ const t = await e.text();
585
+ throw new Error(`Failed to get file info: ${e.status} ${t}`);
550
586
  }
551
587
  return await e.json();
552
588
  } catch (e) {
@@ -562,26 +598,26 @@ class j {
562
598
  * @returns Promise resolving to query results
563
599
  * @throws Error if query fails
564
600
  */
565
- async queryFile(s, e, r = 10) {
601
+ async queryFile(r, e, t = 10) {
566
602
  if (!this.apiKey)
567
603
  throw new Error("API key is required for querying files");
568
604
  try {
569
- const t = await fetch(`${this.apiUrl}/api/files/${s}/query`, {
605
+ const s = await fetch(`${this.apiUrl}/api/files/${r}/query`, {
570
606
  ...this.getFetchOptions({
571
607
  method: "POST",
572
608
  headers: {
573
609
  "Content-Type": "application/json"
574
610
  },
575
- body: JSON.stringify({ query: e, max_results: r })
611
+ body: JSON.stringify({ query: e, max_results: t })
576
612
  })
577
613
  });
578
- if (!t.ok) {
579
- const i = await t.text();
580
- 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}`);
581
617
  }
582
- return await t.json();
583
- } catch (t) {
584
- 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;
585
621
  }
586
622
  }
587
623
  /**
@@ -591,56 +627,63 @@ class j {
591
627
  * @returns Promise resolving to deletion result
592
628
  * @throws Error if deletion fails
593
629
  */
594
- async deleteFile(s) {
630
+ async deleteFile(r) {
595
631
  if (!this.apiKey)
596
632
  throw new Error("API key is required for deleting files");
597
- const e = `${this.apiUrl}/api/files/${s}`, r = this.getFetchOptions({
633
+ const e = `${this.apiUrl}/api/files/${r}`, t = this.getFetchOptions({
598
634
  method: "DELETE"
599
635
  });
600
636
  try {
601
- const t = await fetch(e, r);
602
- if (!t.ok) {
603
- const n = await t.text();
637
+ const s = await fetch(e, t);
638
+ if (!s.ok) {
639
+ const n = await s.text();
604
640
  let o;
605
641
  try {
606
- const c = JSON.parse(n);
607
- o = c.detail || c.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})`;
608
644
  } catch {
609
- o = `Failed to delete file (HTTP ${t.status})`;
645
+ o = `Failed to delete file (HTTP ${s.status})`;
610
646
  }
611
647
  throw console.warn(`[ApiClient] ${o}`), new Error(o);
612
648
  }
613
- return await t.json();
614
- } catch (t) {
649
+ return await s.json();
650
+ } catch (s) {
615
651
  let i;
616
- 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);
617
653
  }
618
654
  }
619
655
  }
620
- let $ = null;
621
- const M = (l, s = null, e = null) => {
622
- $ = new j({ apiUrl: l, apiKey: s, sessionId: e });
656
+ let E = null;
657
+ const H = (l, r = null, e = null) => {
658
+ E = new N({ apiUrl: l, apiKey: r, sessionId: e });
623
659
  };
624
- async function* H(l, s = !0, e, r, t, i, n, o, c, u, f) {
625
- if (!$)
660
+ async function* X(l, r = !0, e, t, s, i, n, o, d, u, f, g) {
661
+ if (!E)
626
662
  throw new Error("API not configured. Please call configureApi() with your server URL before using any API functions.");
627
- yield* $.streamChat(
663
+ yield* E.streamChat(
628
664
  l,
629
- s,
630
- e,
631
665
  r,
666
+ e,
632
667
  t,
668
+ s,
633
669
  i,
634
670
  n,
635
671
  o,
636
- c,
672
+ d,
637
673
  u,
638
- f
674
+ f,
675
+ g
639
676
  );
640
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
+ }
641
683
  export {
642
- j as ApiClient,
643
- M as configureApi,
644
- H as streamChat
684
+ N as ApiClient,
685
+ H as configureApi,
686
+ L as stopChat,
687
+ X as streamChat
645
688
  };
646
689
  //# sourceMappingURL=api.mjs.map