@schmitech/chatbot-api 2.1.3 → 2.1.4

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 _ = 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);
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
4
  let F = null, I = 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, 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 }));
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 }));
13
13
  }).catch((l) => {
14
14
  console.warn("Failed to initialize HTTP agents:", l.message);
15
15
  });
16
16
  class j {
17
17
  // Session ID can be mutable
18
- constructor(r) {
18
+ constructor(s) {
19
19
  A(this, "apiUrl");
20
20
  A(this, "apiKey");
21
21
  A(this, "sessionId");
22
- if (!r.apiUrl || typeof r.apiUrl != "string")
22
+ if (!s.apiUrl || typeof s.apiUrl != "string")
23
23
  throw new Error("API URL must be a valid string");
24
- if (r.apiKey !== void 0 && r.apiKey !== null && typeof r.apiKey != "string")
24
+ if (s.apiKey !== void 0 && s.apiKey !== null && typeof s.apiKey != "string")
25
25
  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")
26
+ if (s.sessionId !== void 0 && s.sessionId !== null && typeof s.sessionId != "string")
27
27
  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;
28
+ this.apiUrl = s.apiUrl, this.apiKey = s.apiKey ?? null, this.sessionId = s.sessionId ?? null;
29
29
  }
30
- setSessionId(r) {
31
- if (r !== null && typeof r != "string")
30
+ setSessionId(s) {
31
+ if (s !== null && typeof s != "string")
32
32
  throw new Error("Session ID must be a valid string or null");
33
- this.sessionId = r;
33
+ this.sessionId = s;
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 r;
45
+ var s;
46
46
  if (!this.apiKey)
47
47
  throw new Error("API key is required for validation");
48
48
  try {
@@ -60,45 +60,45 @@ class j {
60
60
  } catch {
61
61
  t = `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(t);
66
+ i = o.detail || o.message || t;
67
67
  } catch {
68
68
  i = t || `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) {
89
+ const r = await e.json();
90
+ if (!r.exists) {
91
91
  const t = "API key does not exist";
92
92
  throw new Error(t);
93
93
  }
94
- if (!s.active) {
94
+ if (!r.active) {
95
95
  const t = "API key is inactive";
96
96
  throw new Error(t);
97
97
  }
98
- return s;
98
+ return r;
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 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);
102
102
  }
103
103
  }
104
104
  /**
@@ -111,11 +111,11 @@ 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 r;
114
+ var s;
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
  })
@@ -129,71 +129,71 @@ class j {
129
129
  } catch {
130
130
  t = `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(t);
135
+ i = o.detail || o.message || t;
136
136
  } catch {
137
137
  i = t || `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 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);
159
159
  }
160
160
  }
161
161
  // Helper to get fetch options with connection pooling if available
162
- getFetchOptions(r = {}) {
162
+ getFetchOptions(s = {}) {
163
163
  const e = {};
164
164
  if (typeof window > "u") {
165
165
  const i = this.apiUrl.startsWith("https:") ? I : F;
166
166
  i && (e.agent = i);
167
167
  } else
168
168
  e.headers = { Connection: "keep-alive" };
169
- const s = {
169
+ const r = {
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(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);
176
176
  }
177
- return this.apiKey && (s["X-API-Key"] = this.apiKey), this.sessionId && (s["X-Session-ID"] = this.sessionId), {
178
- ...r,
177
+ return this.apiKey && (r["X-API-Key"] = this.apiKey), this.sessionId && (r["X-Session-ID"] = this.sessionId), {
178
+ ...s,
179
179
  ...e,
180
- headers: s
180
+ headers: r
181
181
  };
182
182
  }
183
183
  // Create Chat request
184
- createChatRequest(r, e = !0, s, t, i, o, a, h, u, p, k) {
185
- const c = {
184
+ createChatRequest(s, e = !0, r, t, i, n, o, c, u, f, v) {
185
+ const d = {
186
186
  messages: [
187
- { role: "user", content: r }
187
+ { role: "user", content: s }
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 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;
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(s, e = !0, r, t, i, n, o, c, u, f, v) {
194
+ var d, C, x;
195
195
  try {
196
- const y = new AbortController(), O = setTimeout(() => y.abort(), 6e4), g = await fetch(`${this.apiUrl}/v1/chat`, {
196
+ const p = new AbortController(), O = setTimeout(() => p.abort(), 6e4), g = await fetch(`${this.apiUrl}/v1/chat`, {
197
197
  ...this.getFetchOptions({
198
198
  method: "POST",
199
199
  headers: {
@@ -201,112 +201,141 @@ class j {
201
201
  Accept: e ? "text/event-stream" : "application/json"
202
202
  },
203
203
  body: JSON.stringify(this.createChatRequest(
204
- r,
205
- e,
206
204
  s,
205
+ e,
206
+ r,
207
207
  t,
208
208
  i,
209
+ n,
209
210
  o,
210
- a,
211
- h,
211
+ c,
212
212
  u,
213
- p,
214
- k
213
+ f,
214
+ v
215
215
  ))
216
216
  }),
217
- signal: y.signal
217
+ signal: p.signal
218
218
  });
219
219
  if (clearTimeout(O), !g.ok) {
220
- const w = await g.text();
221
- throw new Error(`Network response was not ok: ${g.status} ${w}`);
220
+ const y = await g.text();
221
+ throw new Error(`Network response was not ok: ${g.status} ${y}`);
222
222
  }
223
223
  if (!e) {
224
- const w = await g.json();
225
- w.response && (yield {
226
- text: w.response,
224
+ const y = await g.json();
225
+ y.response && (yield {
226
+ text: y.response,
227
227
  done: !0,
228
- audio: w.audio,
229
- audioFormat: w.audio_format
228
+ audio: y.audio,
229
+ audioFormat: y.audio_format
230
230
  });
231
231
  return;
232
232
  }
233
- const P = (c = g.body) == null ? void 0 : c.getReader();
233
+ const P = (d = g.body) == null ? void 0 : d.getReader();
234
234
  if (!P) throw new Error("No reader available");
235
235
  const S = new TextDecoder();
236
- let f = "", v = !1;
236
+ let w = "", k = !1;
237
237
  try {
238
238
  for (; ; ) {
239
- const { done: w, value: U } = await P.read();
240
- if (w)
239
+ const { done: y, value: U } = await P.read();
240
+ if (y)
241
241
  break;
242
- const q = S.decode(U, { stream: !0 });
243
- f += q;
242
+ const _ = S.decode(U, { stream: !0 });
243
+ w += _;
244
244
  let E = 0, T;
245
- for (; (T = f.indexOf(`
245
+ for (; (T = w.indexOf(`
246
246
  `, E)) !== -1; ) {
247
- const m = f.slice(E, T).trim();
247
+ const m = w.slice(E, T).trim();
248
248
  if (E = T + 1, m && m.startsWith("data: ")) {
249
- const d = m.slice(6).trim();
250
- if (!d || d === "[DONE]") {
249
+ const h = m.slice(6).trim();
250
+ if (!h || h === "[DONE]") {
251
251
  yield { text: "", done: !0 };
252
252
  return;
253
253
  }
254
254
  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"}`;
255
+ const a = JSON.parse(h);
256
+ if (a.error) {
257
+ const K = `Server error: ${((C = a.error) == null ? void 0 : C.message) || a.error || "Unknown server error"}`;
258
258
  throw console.warn(`[ApiClient] ${K}`), new Error(K);
259
259
  }
260
- if (n.done === !0) {
261
- v = !0, yield {
260
+ if (a.done === !0) {
261
+ k = !0, yield {
262
262
  text: "",
263
263
  done: !0,
264
- audio: n.audio,
265
- audioFormat: n.audio_format || n.audioFormat,
266
- threading: n.threading
264
+ audio: a.audio,
265
+ audioFormat: a.audio_format || a.audioFormat,
266
+ threading: a.threading
267
267
  // Pass through threading metadata
268
268
  };
269
269
  return;
270
270
  }
271
- const b = n.response || "";
272
- n.audio_chunk !== void 0 && (yield {
271
+ const b = a.response || "";
272
+ a.audio_chunk !== void 0 && (yield {
273
273
  text: "",
274
274
  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 {
275
+ audio_chunk: a.audio_chunk,
276
+ audioFormat: a.audioFormat || a.audio_format || "opus",
277
+ chunk_index: a.chunk_index ?? 0
278
+ }), (b || a.audio) && (k = !0, yield {
279
279
  text: b,
280
- done: n.done || !1,
281
- audio: n.audio,
282
- audioFormat: n.audio_format || n.audioFormat,
283
- threading: n.threading
280
+ done: a.done || !1,
281
+ audio: a.audio,
282
+ audioFormat: a.audio_format || a.audioFormat,
283
+ threading: a.threading
284
284
  // Include threading if present
285
285
  });
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));
286
+ } catch (a) {
287
+ if ((x = a == null ? void 0 : a.message) != null && x.startsWith("Server error:"))
288
+ throw a;
289
+ 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
290
  }
291
- } else m && (v = !0, yield { text: m, done: !1 });
291
+ } else m && (k = !0, yield { text: m, done: !1 });
292
292
  }
293
- f = f.slice(E), f.length > 1e6 && (console.warn("[ApiClient] Buffer too large, truncating..."), f = f.slice(-5e5));
293
+ w = w.slice(E), w.length > 1e6 && (console.warn("[ApiClient] Buffer too large, truncating..."), w = w.slice(-5e5));
294
294
  }
295
- v && (yield { text: "", done: !0 });
295
+ k && (yield { text: "", done: !0 });
296
296
  } finally {
297
297
  P.releaseLock();
298
298
  }
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;
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;
301
301
  }
302
302
  }
303
- async clearConversationHistory(r) {
304
- const e = r || this.sessionId;
303
+ async getConversationHistory(s, e) {
304
+ const r = s || this.sessionId;
305
+ if (!r)
306
+ throw new Error("No session ID provided and no current session available");
307
+ const t = {};
308
+ this.apiKey && (t["X-API-Key"] = this.apiKey);
309
+ try {
310
+ const i = new URL(`${this.apiUrl}/admin/chat-history/${r}`);
311
+ typeof e == "number" && Number.isFinite(e) && e > 0 && i.searchParams.set("limit", String(Math.floor(e)));
312
+ const n = await fetch(i.toString(), {
313
+ ...this.getFetchOptions({
314
+ method: "GET",
315
+ headers: t
316
+ })
317
+ });
318
+ if (!n.ok) {
319
+ const f = await n.text();
320
+ throw new Error(`Failed to fetch conversation history: ${n.status} ${f}`);
321
+ }
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;
323
+ return {
324
+ session_id: (o == null ? void 0 : o.session_id) || r,
325
+ messages: c,
326
+ count: u
327
+ };
328
+ } catch (i) {
329
+ 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
+ }
331
+ }
332
+ async clearConversationHistory(s) {
333
+ const e = s || this.sessionId;
305
334
  if (!e)
306
335
  throw new Error("No session ID provided and no current session available");
307
336
  if (!this.apiKey)
308
337
  throw new Error("API key is required for clearing conversation history");
309
- const s = {
338
+ const r = {
310
339
  "Content-Type": "application/json",
311
340
  "X-Session-ID": e,
312
341
  "X-API-Key": this.apiKey
@@ -315,43 +344,43 @@ class j {
315
344
  const t = await fetch(`${this.apiUrl}/admin/chat-history/${e}`, {
316
345
  ...this.getFetchOptions({
317
346
  method: "DELETE",
318
- headers: s
347
+ headers: r
319
348
  })
320
349
  });
321
350
  if (!t.ok) {
322
- const o = await t.text();
323
- throw new Error(`Failed to clear conversation history: ${t.status} ${o}`);
351
+ const n = await t.text();
352
+ throw new Error(`Failed to clear conversation history: ${t.status} ${n}`);
324
353
  }
325
354
  return await t.json();
326
355
  } catch (t) {
327
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;
328
357
  }
329
358
  }
330
- async deleteConversationWithFiles(r, e) {
331
- const s = r || this.sessionId;
332
- if (!s)
359
+ async deleteConversationWithFiles(s, e) {
360
+ const r = s || this.sessionId;
361
+ if (!r)
333
362
  throw new Error("No session ID provided and no current session available");
334
363
  if (!this.apiKey)
335
364
  throw new Error("API key is required for deleting conversation");
336
365
  const t = {
337
366
  "Content-Type": "application/json",
338
- "X-Session-ID": s,
367
+ "X-Session-ID": r,
339
368
  "X-API-Key": this.apiKey
340
- }, i = e && e.length > 0 ? `?file_ids=${e.join(",")}` : "", o = `${this.apiUrl}/admin/conversations/${s}${i}`;
369
+ }, i = e && e.length > 0 ? `?file_ids=${e.join(",")}` : "", n = `${this.apiUrl}/admin/conversations/${r}${i}`;
341
370
  try {
342
- const a = await fetch(o, {
371
+ const o = await fetch(n, {
343
372
  ...this.getFetchOptions({
344
373
  method: "DELETE",
345
374
  headers: t
346
375
  })
347
376
  });
348
- if (!a.ok) {
349
- const u = await a.text();
350
- throw new Error(`Failed to delete conversation: ${a.status} ${u}`);
377
+ if (!o.ok) {
378
+ const u = await o.text();
379
+ throw new Error(`Failed to delete conversation: ${o.status} ${u}`);
351
380
  }
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;
381
+ return await o.json();
382
+ } catch (o) {
383
+ 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
384
  }
356
385
  }
357
386
  /**
@@ -362,10 +391,10 @@ class j {
362
391
  * @returns Promise resolving to thread information
363
392
  * @throws Error if the operation fails
364
393
  */
365
- async createThread(r, e) {
394
+ async createThread(s, e) {
366
395
  if (!this.apiKey)
367
396
  throw new Error("API key is required for creating threads");
368
- const s = {
397
+ const r = {
369
398
  "Content-Type": "application/json",
370
399
  "X-API-Key": this.apiKey
371
400
  };
@@ -373,16 +402,16 @@ class j {
373
402
  const t = await fetch(`${this.apiUrl}/api/threads`, {
374
403
  ...this.getFetchOptions({
375
404
  method: "POST",
376
- headers: s,
405
+ headers: r,
377
406
  body: JSON.stringify({
378
- message_id: r,
407
+ message_id: s,
379
408
  session_id: e
380
409
  })
381
410
  })
382
411
  });
383
412
  if (!t.ok) {
384
- const o = await t.text();
385
- throw new Error(`Failed to create thread: ${t.status} ${o}`);
413
+ const n = await t.text();
414
+ throw new Error(`Failed to create thread: ${t.status} ${n}`);
386
415
  }
387
416
  return await t.json();
388
417
  } catch (t) {
@@ -396,26 +425,26 @@ class j {
396
425
  * @returns Promise resolving to thread information
397
426
  * @throws Error if the operation fails
398
427
  */
399
- async getThreadInfo(r) {
428
+ async getThreadInfo(s) {
400
429
  if (!this.apiKey)
401
430
  throw new Error("API key is required for getting thread info");
402
431
  const e = {
403
432
  "X-API-Key": this.apiKey
404
433
  };
405
434
  try {
406
- const s = await fetch(`${this.apiUrl}/api/threads/${r}`, {
435
+ const r = await fetch(`${this.apiUrl}/api/threads/${s}`, {
407
436
  ...this.getFetchOptions({
408
437
  method: "GET",
409
438
  headers: e
410
439
  })
411
440
  });
412
- if (!s.ok) {
413
- const i = await s.text();
414
- throw new Error(`Failed to get thread info: ${s.status} ${i}`);
441
+ if (!r.ok) {
442
+ const i = await r.text();
443
+ throw new Error(`Failed to get thread info: ${r.status} ${i}`);
415
444
  }
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;
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;
419
448
  }
420
449
  }
421
450
  /**
@@ -425,26 +454,26 @@ class j {
425
454
  * @returns Promise resolving to deletion result
426
455
  * @throws Error if the operation fails
427
456
  */
428
- async deleteThread(r) {
457
+ async deleteThread(s) {
429
458
  if (!this.apiKey)
430
459
  throw new Error("API key is required for deleting threads");
431
460
  const e = {
432
461
  "X-API-Key": this.apiKey
433
462
  };
434
463
  try {
435
- const s = await fetch(`${this.apiUrl}/api/threads/${r}`, {
464
+ const r = await fetch(`${this.apiUrl}/api/threads/${s}`, {
436
465
  ...this.getFetchOptions({
437
466
  method: "DELETE",
438
467
  headers: e
439
468
  })
440
469
  });
441
- if (!s.ok) {
442
- const i = await s.text();
443
- throw new Error(`Failed to delete thread: ${s.status} ${i}`);
470
+ if (!r.ok) {
471
+ const i = await r.text();
472
+ throw new Error(`Failed to delete thread: ${r.status} ${i}`);
444
473
  }
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;
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;
448
477
  }
449
478
  }
450
479
  /**
@@ -454,25 +483,25 @@ class j {
454
483
  * @returns Promise resolving to upload response with file_id
455
484
  * @throws Error if upload fails
456
485
  */
457
- async uploadFile(r) {
486
+ async uploadFile(s) {
458
487
  if (!this.apiKey)
459
488
  throw new Error("API key is required for file upload");
460
489
  const e = new FormData();
461
- e.append("file", r);
490
+ e.append("file", s);
462
491
  try {
463
- const s = await fetch(`${this.apiUrl}/api/files/upload`, {
492
+ const r = await fetch(`${this.apiUrl}/api/files/upload`, {
464
493
  ...this.getFetchOptions({
465
494
  method: "POST",
466
495
  body: e
467
496
  })
468
497
  });
469
- if (!s.ok) {
470
- const t = await s.text();
471
- throw new Error(`Failed to upload file: ${s.status} ${t}`);
498
+ if (!r.ok) {
499
+ const t = await r.text();
500
+ throw new Error(`Failed to upload file: ${r.status} ${t}`);
472
501
  }
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;
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;
476
505
  }
477
506
  }
478
507
  /**
@@ -485,18 +514,18 @@ class j {
485
514
  if (!this.apiKey)
486
515
  throw new Error("API key is required for listing files");
487
516
  try {
488
- const r = await fetch(`${this.apiUrl}/api/files`, {
517
+ const s = await fetch(`${this.apiUrl}/api/files`, {
489
518
  ...this.getFetchOptions({
490
519
  method: "GET"
491
520
  })
492
521
  });
493
- if (!r.ok) {
494
- const e = await r.text();
495
- throw new Error(`Failed to list files: ${r.status} ${e}`);
522
+ if (!s.ok) {
523
+ const e = await s.text();
524
+ throw new Error(`Failed to list files: ${s.status} ${e}`);
496
525
  }
497
- return await r.json();
498
- } catch (r) {
499
- 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;
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;
500
529
  }
501
530
  }
502
531
  /**
@@ -506,18 +535,18 @@ class j {
506
535
  * @returns Promise resolving to file information
507
536
  * @throws Error if file not found or request fails
508
537
  */
509
- async getFileInfo(r) {
538
+ async getFileInfo(s) {
510
539
  if (!this.apiKey)
511
540
  throw new Error("API key is required for getting file info");
512
541
  try {
513
- const e = await fetch(`${this.apiUrl}/api/files/${r}`, {
542
+ const e = await fetch(`${this.apiUrl}/api/files/${s}`, {
514
543
  ...this.getFetchOptions({
515
544
  method: "GET"
516
545
  })
517
546
  });
518
547
  if (!e.ok) {
519
- const s = await e.text();
520
- throw new Error(`Failed to get file info: ${e.status} ${s}`);
548
+ const r = await e.text();
549
+ throw new Error(`Failed to get file info: ${e.status} ${r}`);
521
550
  }
522
551
  return await e.json();
523
552
  } catch (e) {
@@ -533,17 +562,17 @@ class j {
533
562
  * @returns Promise resolving to query results
534
563
  * @throws Error if query fails
535
564
  */
536
- async queryFile(r, e, s = 10) {
565
+ async queryFile(s, e, r = 10) {
537
566
  if (!this.apiKey)
538
567
  throw new Error("API key is required for querying files");
539
568
  try {
540
- const t = await fetch(`${this.apiUrl}/api/files/${r}/query`, {
569
+ const t = await fetch(`${this.apiUrl}/api/files/${s}/query`, {
541
570
  ...this.getFetchOptions({
542
571
  method: "POST",
543
572
  headers: {
544
573
  "Content-Type": "application/json"
545
574
  },
546
- body: JSON.stringify({ query: e, max_results: s })
575
+ body: JSON.stringify({ query: e, max_results: r })
547
576
  })
548
577
  });
549
578
  if (!t.ok) {
@@ -562,24 +591,24 @@ class j {
562
591
  * @returns Promise resolving to deletion result
563
592
  * @throws Error if deletion fails
564
593
  */
565
- async deleteFile(r) {
594
+ async deleteFile(s) {
566
595
  if (!this.apiKey)
567
596
  throw new Error("API key is required for deleting files");
568
- const e = `${this.apiUrl}/api/files/${r}`, s = this.getFetchOptions({
597
+ const e = `${this.apiUrl}/api/files/${s}`, r = this.getFetchOptions({
569
598
  method: "DELETE"
570
599
  });
571
600
  try {
572
- const t = await fetch(e, s);
601
+ const t = await fetch(e, r);
573
602
  if (!t.ok) {
574
- const o = await t.text();
575
- let a;
603
+ const n = await t.text();
604
+ let o;
576
605
  try {
577
- const h = JSON.parse(o);
578
- a = h.detail || h.message || `Failed to delete file (HTTP ${t.status})`;
606
+ const c = JSON.parse(n);
607
+ o = c.detail || c.message || `Failed to delete file (HTTP ${t.status})`;
579
608
  } catch {
580
- a = `Failed to delete file (HTTP ${t.status})`;
609
+ o = `Failed to delete file (HTTP ${t.status})`;
581
610
  }
582
- throw console.warn(`[ApiClient] ${a}`), new Error(a);
611
+ throw console.warn(`[ApiClient] ${o}`), new Error(o);
583
612
  }
584
613
  return await t.json();
585
614
  } catch (t) {
@@ -589,24 +618,24 @@ class j {
589
618
  }
590
619
  }
591
620
  let $ = null;
592
- const M = (l, r = null, e = null) => {
593
- $ = new j({ apiUrl: l, apiKey: r, sessionId: e });
621
+ const M = (l, s = null, e = null) => {
622
+ $ = new j({ apiUrl: l, apiKey: s, sessionId: e });
594
623
  };
595
- async function* H(l, r = !0, e, s, t, i, o, a, h, u, p) {
624
+ async function* H(l, s = !0, e, r, t, i, n, o, c, u, f) {
596
625
  if (!$)
597
626
  throw new Error("API not configured. Please call configureApi() with your server URL before using any API functions.");
598
627
  yield* $.streamChat(
599
628
  l,
600
- r,
601
- e,
602
629
  s,
630
+ e,
631
+ r,
603
632
  t,
604
633
  i,
634
+ n,
605
635
  o,
606
- a,
607
- h,
636
+ c,
608
637
  u,
609
- p
638
+ f
610
639
  );
611
640
  }
612
641
  export {