@schmitech/chatbot-api 1.0.1 → 2.1.0

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 F = (o, t, e) => t in o ? $(o, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[t] = e;
3
- var y = (o, t, e) => F(o, typeof t != "symbol" ? t + "" : t, e);
4
- let v = null, k = null;
1
+ var q = Object.defineProperty;
2
+ var _ = (l, r, e) => r in l ? q(l, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : l[r] = e;
3
+ var A = (l, r, e) => _(l, typeof r != "symbol" ? r + "" : r, e);
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(([o, t]) => {
10
+ ]).then(([l, r]) => {
11
11
  var e, s;
12
- (e = o == null ? void 0 : o.default) != null && e.Agent ? v = new o.default.Agent({ keepAlive: !0 }) : o != null && o.Agent && (v = new o.Agent({ keepAlive: !0 })), (s = t == null ? void 0 : t.default) != null && s.Agent ? k = new t.default.Agent({ keepAlive: !0 }) : t != null && t.Agent && (k = new t.Agent({ keepAlive: !0 }));
13
- }).catch((o) => {
14
- console.warn("Failed to initialize HTTP agents:", o.message);
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 }));
13
+ }).catch((l) => {
14
+ console.warn("Failed to initialize HTTP agents:", l.message);
15
15
  });
16
- class x {
16
+ class D {
17
17
  // Session ID can be mutable
18
- constructor(t) {
19
- y(this, "apiUrl");
20
- y(this, "apiKey");
21
- y(this, "sessionId");
22
- if (!t.apiUrl || typeof t.apiUrl != "string")
18
+ constructor(r) {
19
+ A(this, "apiUrl");
20
+ A(this, "apiKey");
21
+ A(this, "sessionId");
22
+ if (!r.apiUrl || typeof r.apiUrl != "string")
23
23
  throw new Error("API URL must be a valid string");
24
- if (t.apiKey !== void 0 && t.apiKey !== null && typeof t.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 (t.sessionId !== void 0 && t.sessionId !== null && typeof t.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 = t.apiUrl, this.apiKey = t.apiKey ?? null, this.sessionId = t.sessionId ?? null;
28
+ this.apiUrl = r.apiUrl, this.apiKey = r.apiKey ?? null, this.sessionId = r.sessionId ?? null;
29
29
  }
30
- setSessionId(t) {
31
- if (t !== null && typeof t != "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 = t;
33
+ this.sessionId = r;
34
34
  }
35
35
  getSessionId() {
36
36
  return this.sessionId;
@@ -42,7 +42,7 @@ class x {
42
42
  * @throws Error if API key is not provided, invalid, inactive, or validation fails
43
43
  */
44
44
  async validateApiKey() {
45
- var t;
45
+ var r;
46
46
  if (!this.apiKey)
47
47
  throw new Error("API key is required for validation");
48
48
  try {
@@ -50,55 +50,55 @@ class x {
50
50
  ...this.getFetchOptions({
51
51
  method: "GET"
52
52
  })
53
- }).catch((r) => {
54
- 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;
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;
55
55
  });
56
56
  if (!e.ok) {
57
- let r = "";
57
+ let t = "";
58
58
  try {
59
- r = await e.text();
59
+ t = await e.text();
60
60
  } catch {
61
- r = `HTTP ${e.status}`;
61
+ t = `HTTP ${e.status}`;
62
62
  }
63
- let i, n;
63
+ let i, o;
64
64
  try {
65
- const a = JSON.parse(r);
66
- i = a.detail || a.message || r;
65
+ const n = JSON.parse(t);
66
+ i = n.detail || n.message || t;
67
67
  } catch {
68
- i = r || `HTTP ${e.status}`;
68
+ i = t || `HTTP ${e.status}`;
69
69
  }
70
70
  switch (e.status) {
71
71
  case 401:
72
- n = "API key is invalid or expired";
72
+ o = "API key is invalid or expired";
73
73
  break;
74
74
  case 403:
75
- n = "Access denied: API key does not have required permissions";
75
+ o = "Access denied: API key does not have required permissions";
76
76
  break;
77
77
  case 404:
78
- n = "API key not found";
78
+ o = "API key not found";
79
79
  break;
80
80
  case 503:
81
- n = "API key management is not available in inference-only mode";
81
+ o = "API key management is not available in inference-only mode";
82
82
  break;
83
83
  default:
84
- n = `Failed to validate API key: ${i}`;
84
+ o = `Failed to validate API key: ${i}`;
85
85
  break;
86
86
  }
87
- throw new Error(n);
87
+ throw new Error(o);
88
88
  }
89
89
  const s = await e.json();
90
90
  if (!s.exists) {
91
- const r = "API key does not exist";
92
- throw new Error(r);
91
+ const t = "API key does not exist";
92
+ throw new Error(t);
93
93
  }
94
94
  if (!s.active) {
95
- const r = "API key is inactive";
96
- throw new Error(r);
95
+ const t = "API key is inactive";
96
+ throw new Error(t);
97
97
  }
98
98
  return s;
99
99
  } catch (e) {
100
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" && ((t = e.message) != null && t.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);
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);
102
102
  }
103
103
  }
104
104
  /**
@@ -111,7 +111,7 @@ class x {
111
111
  * @throws Error if API key is not provided, invalid, disabled, or request fails
112
112
  */
113
113
  async getAdapterInfo() {
114
- var t;
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,151 +119,187 @@ class x {
119
119
  ...this.getFetchOptions({
120
120
  method: "GET"
121
121
  })
122
- }).catch((r) => {
123
- 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;
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;
124
124
  });
125
125
  if (!e.ok) {
126
- let r = "";
126
+ let t = "";
127
127
  try {
128
- r = await e.text();
128
+ t = await e.text();
129
129
  } catch {
130
- r = `HTTP ${e.status}`;
130
+ t = `HTTP ${e.status}`;
131
131
  }
132
- let i, n;
132
+ let i, o;
133
133
  try {
134
- const a = JSON.parse(r);
135
- i = a.detail || a.message || r;
134
+ const n = JSON.parse(t);
135
+ i = n.detail || n.message || t;
136
136
  } catch {
137
- i = r || `HTTP ${e.status}`;
137
+ i = t || `HTTP ${e.status}`;
138
138
  }
139
139
  switch (e.status) {
140
140
  case 401:
141
- n = "API key is invalid, disabled, or has no associated adapter";
141
+ o = "API key is invalid, disabled, or has no associated adapter";
142
142
  break;
143
143
  case 404:
144
- n = "Adapter configuration not found";
144
+ o = "Adapter configuration not found";
145
145
  break;
146
146
  case 503:
147
- n = "Service is not available";
147
+ o = "Service is not available";
148
148
  break;
149
149
  default:
150
- n = `Failed to get adapter info: ${i}`;
150
+ o = `Failed to get adapter info: ${i}`;
151
151
  break;
152
152
  }
153
- throw new Error(n);
153
+ throw new Error(o);
154
154
  }
155
155
  return await e.json();
156
156
  } catch (e) {
157
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" && ((t = e.message) != null && t.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);
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);
159
159
  }
160
160
  }
161
161
  // Helper to get fetch options with connection pooling if available
162
- getFetchOptions(t = {}) {
162
+ getFetchOptions(r = {}) {
163
163
  const e = {};
164
164
  if (typeof window > "u") {
165
- const i = this.apiUrl.startsWith("https:") ? k : v;
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
169
  const s = {
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), t.headers) {
173
- const r = t.headers;
174
- for (const [i, n] of Object.entries(r))
175
- (i.toLowerCase() !== "x-api-key" || !this.apiKey) && (s[i] = n);
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);
176
176
  }
177
177
  return this.apiKey && (s["X-API-Key"] = this.apiKey), this.sessionId && (s["X-Session-ID"] = this.sessionId), {
178
- ...t,
178
+ ...r,
179
179
  ...e,
180
180
  headers: s
181
181
  };
182
182
  }
183
183
  // Create Chat request
184
- createChatRequest(t, e = !0, s) {
185
- const r = {
184
+ createChatRequest(r, e = !0, s, t, i, o, n, h, u, p, k) {
185
+ const c = {
186
186
  messages: [
187
- { role: "user", content: t }
187
+ { role: "user", content: r }
188
188
  ],
189
189
  stream: e
190
190
  };
191
- return s && s.length > 0 && (r.file_ids = s), r;
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), n && (c.language = n), h !== void 0 && (c.return_audio = h), u && (c.tts_voice = u), p && (c.source_language = p), k && (c.target_language = k), c;
192
192
  }
193
- async *streamChat(t, e = !0, s) {
194
- var r, i;
193
+ async *streamChat(r, e = !0, s, t, i, o, n, h, u, p, k) {
194
+ var c, C;
195
195
  try {
196
- const n = new AbortController(), a = setTimeout(() => n.abort(), 6e4), l = await fetch(`${this.apiUrl}/v1/chat`, {
196
+ const y = new AbortController(), K = setTimeout(() => y.abort(), 6e4), g = await fetch(`${this.apiUrl}/v1/chat`, {
197
197
  ...this.getFetchOptions({
198
198
  method: "POST",
199
199
  headers: {
200
200
  "Content-Type": "application/json",
201
201
  Accept: e ? "text/event-stream" : "application/json"
202
202
  },
203
- body: JSON.stringify(this.createChatRequest(t, e, s))
203
+ body: JSON.stringify(this.createChatRequest(
204
+ r,
205
+ e,
206
+ s,
207
+ t,
208
+ i,
209
+ o,
210
+ n,
211
+ h,
212
+ u,
213
+ p,
214
+ k
215
+ ))
204
216
  }),
205
- signal: n.signal
217
+ signal: y.signal
206
218
  });
207
- if (clearTimeout(a), !l.ok) {
208
- const h = await l.text();
209
- throw new Error(`Network response was not ok: ${l.status} ${h}`);
219
+ if (clearTimeout(K), !g.ok) {
220
+ const w = await g.text();
221
+ throw new Error(`Network response was not ok: ${g.status} ${w}`);
210
222
  }
211
223
  if (!e) {
212
- const h = await l.json();
213
- h.response && (yield {
214
- text: h.response,
215
- done: !0
224
+ const w = await g.json();
225
+ w.response && (yield {
226
+ text: w.response,
227
+ done: !0,
228
+ audio: w.audio,
229
+ audioFormat: w.audio_format
216
230
  });
217
231
  return;
218
232
  }
219
- const u = (r = l.body) == null ? void 0 : r.getReader();
220
- if (!u) throw new Error("No reader available");
221
- const P = new TextDecoder();
222
- let c = "", p = !1;
233
+ const P = (c = g.body) == null ? void 0 : c.getReader();
234
+ if (!P) throw new Error("No reader available");
235
+ const O = new TextDecoder();
236
+ let f = "", v = !1;
223
237
  try {
224
238
  for (; ; ) {
225
- const { done: h, value: I } = await u.read();
226
- if (h)
239
+ const { done: w, value: S } = await P.read();
240
+ if (w)
227
241
  break;
228
- const T = P.decode(I, { stream: !0 });
229
- c += T;
230
- let w = 0, g;
231
- for (; (g = c.indexOf(`
232
- `, w)) !== -1; ) {
233
- const f = c.slice(w, g).trim();
234
- if (w = g + 1, f && f.startsWith("data: ")) {
235
- const m = f.slice(6).trim();
236
- if (!m || m === "[DONE]") {
242
+ const U = O.decode(S, { stream: !0 });
243
+ f += U;
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]") {
237
251
  yield { text: "", done: !0 };
238
252
  return;
239
253
  }
240
254
  try {
241
- const d = JSON.parse(m);
242
- if (d.error) {
243
- const A = `Server error: ${((i = d.error) == null ? void 0 : i.message) || d.error || "Unknown server error"}`;
244
- throw console.warn(`[ApiClient] ${A}`), new Error(A);
255
+ const a = JSON.parse(d);
256
+ if (a.error) {
257
+ const b = `Server error: ${((C = a.error) == null ? void 0 : C.message) || a.error || "Unknown server error"}`;
258
+ throw console.warn(`[ApiClient] ${b}`), new Error(b);
245
259
  }
246
- if (d.response && (p = !0, yield { text: d.response, done: d.done || !1 }), d.done) {
247
- yield { text: "", done: !0 };
260
+ if (a.done === !0) {
261
+ v = !0, yield {
262
+ text: "",
263
+ done: !0,
264
+ audio: a.audio,
265
+ audioFormat: a.audio_format || a.audioFormat,
266
+ threading: a.threading
267
+ // Pass through threading metadata
268
+ };
248
269
  return;
249
270
  }
250
- } catch {
251
- console.warn("[ApiClient] Unable to parse server response. This may be a temporary issue.");
271
+ const x = a.response || "";
272
+ a.audio_chunk !== void 0 && (yield {
273
+ text: "",
274
+ done: !1,
275
+ audio_chunk: a.audio_chunk,
276
+ audioFormat: a.audioFormat || a.audio_format || "opus",
277
+ chunk_index: a.chunk_index ?? 0
278
+ }), (x || a.audio) && (v = !0, yield {
279
+ text: x,
280
+ done: a.done || !1,
281
+ audio: a.audio,
282
+ audioFormat: a.audio_format || a.audioFormat,
283
+ threading: a.threading
284
+ // Include threading if present
285
+ });
286
+ } catch (a) {
287
+ 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:", 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));
252
288
  }
253
- } else f && (p = !0, yield { text: f, done: !1 });
289
+ } else m && (v = !0, yield { text: m, done: !1 });
254
290
  }
255
- c = c.slice(w), c.length > 1e6 && (console.warn("Buffer too large, truncating..."), c = c.slice(-5e5));
291
+ f = f.slice(E), f.length > 1e6 && (console.warn("[ApiClient] Buffer too large, truncating..."), f = f.slice(-5e5));
256
292
  }
257
- p && (yield { text: "", done: !0 });
293
+ v && (yield { text: "", done: !0 });
258
294
  } finally {
259
- u.releaseLock();
295
+ P.releaseLock();
260
296
  }
261
- } catch (n) {
262
- throw n.name === "AbortError" ? new Error("Connection timed out. Please check if the server is running.") : n.name === "TypeError" && n.message.includes("Failed to fetch") ? new Error("Could not connect to the server. Please check if the server is running.") : n;
297
+ } catch (y) {
298
+ 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;
263
299
  }
264
300
  }
265
- async clearConversationHistory(t) {
266
- const e = t || this.sessionId;
301
+ async clearConversationHistory(r) {
302
+ const e = r || this.sessionId;
267
303
  if (!e)
268
304
  throw new Error("No session ID provided and no current session available");
269
305
  if (!this.apiKey)
@@ -274,46 +310,139 @@ class x {
274
310
  "X-API-Key": this.apiKey
275
311
  };
276
312
  try {
277
- const r = await fetch(`${this.apiUrl}/admin/chat-history/${e}`, {
313
+ const t = await fetch(`${this.apiUrl}/admin/chat-history/${e}`, {
278
314
  ...this.getFetchOptions({
279
315
  method: "DELETE",
280
316
  headers: s
281
317
  })
282
318
  });
283
- if (!r.ok) {
284
- const n = await r.text();
285
- throw new Error(`Failed to clear conversation history: ${r.status} ${n}`);
319
+ if (!t.ok) {
320
+ const o = await t.text();
321
+ throw new Error(`Failed to clear conversation history: ${t.status} ${o}`);
286
322
  }
287
- return await r.json();
288
- } catch (r) {
289
- 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;
323
+ return await t.json();
324
+ } catch (t) {
325
+ 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;
290
326
  }
291
327
  }
292
- async deleteConversationWithFiles(t, e) {
293
- const s = t || this.sessionId;
328
+ async deleteConversationWithFiles(r, e) {
329
+ const s = r || this.sessionId;
294
330
  if (!s)
295
331
  throw new Error("No session ID provided and no current session available");
296
332
  if (!this.apiKey)
297
333
  throw new Error("API key is required for deleting conversation");
298
- const r = {
334
+ const t = {
299
335
  "Content-Type": "application/json",
300
336
  "X-Session-ID": s,
301
337
  "X-API-Key": this.apiKey
302
- }, i = e && e.length > 0 ? `?file_ids=${e.join(",")}` : "", n = `${this.apiUrl}/admin/conversations/${s}${i}`;
338
+ }, i = e && e.length > 0 ? `?file_ids=${e.join(",")}` : "", o = `${this.apiUrl}/admin/conversations/${s}${i}`;
339
+ try {
340
+ const n = await fetch(o, {
341
+ ...this.getFetchOptions({
342
+ method: "DELETE",
343
+ headers: t
344
+ })
345
+ });
346
+ if (!n.ok) {
347
+ const u = await n.text();
348
+ throw new Error(`Failed to delete conversation: ${n.status} ${u}`);
349
+ }
350
+ return await n.json();
351
+ } catch (n) {
352
+ throw n.name === "TypeError" && n.message.includes("Failed to fetch") ? new Error("Could not connect to the server. Please check if the server is running.") : n;
353
+ }
354
+ }
355
+ /**
356
+ * Create a conversation thread from a parent message.
357
+ *
358
+ * @param messageId - ID of the parent message
359
+ * @param sessionId - Session ID of the parent conversation
360
+ * @returns Promise resolving to thread information
361
+ * @throws Error if the operation fails
362
+ */
363
+ async createThread(r, e) {
364
+ if (!this.apiKey)
365
+ throw new Error("API key is required for creating threads");
366
+ const s = {
367
+ "Content-Type": "application/json",
368
+ "X-API-Key": this.apiKey
369
+ };
370
+ try {
371
+ const t = await fetch(`${this.apiUrl}/api/threads`, {
372
+ ...this.getFetchOptions({
373
+ method: "POST",
374
+ headers: s,
375
+ body: JSON.stringify({
376
+ message_id: r,
377
+ session_id: e
378
+ })
379
+ })
380
+ });
381
+ if (!t.ok) {
382
+ const o = await t.text();
383
+ throw new Error(`Failed to create thread: ${t.status} ${o}`);
384
+ }
385
+ return await t.json();
386
+ } catch (t) {
387
+ 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;
388
+ }
389
+ }
390
+ /**
391
+ * Get thread information by thread ID.
392
+ *
393
+ * @param threadId - Thread identifier
394
+ * @returns Promise resolving to thread information
395
+ * @throws Error if the operation fails
396
+ */
397
+ async getThreadInfo(r) {
398
+ if (!this.apiKey)
399
+ throw new Error("API key is required for getting thread info");
400
+ const e = {
401
+ "X-API-Key": this.apiKey
402
+ };
403
+ try {
404
+ const s = await fetch(`${this.apiUrl}/api/threads/${r}`, {
405
+ ...this.getFetchOptions({
406
+ method: "GET",
407
+ headers: e
408
+ })
409
+ });
410
+ if (!s.ok) {
411
+ const i = await s.text();
412
+ throw new Error(`Failed to get thread info: ${s.status} ${i}`);
413
+ }
414
+ return await s.json();
415
+ } catch (s) {
416
+ 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;
417
+ }
418
+ }
419
+ /**
420
+ * Delete a thread and its associated dataset.
421
+ *
422
+ * @param threadId - Thread identifier
423
+ * @returns Promise resolving to deletion result
424
+ * @throws Error if the operation fails
425
+ */
426
+ async deleteThread(r) {
427
+ if (!this.apiKey)
428
+ throw new Error("API key is required for deleting threads");
429
+ const e = {
430
+ "X-API-Key": this.apiKey
431
+ };
303
432
  try {
304
- const a = await fetch(n, {
433
+ const s = await fetch(`${this.apiUrl}/api/threads/${r}`, {
305
434
  ...this.getFetchOptions({
306
435
  method: "DELETE",
307
- headers: r
436
+ headers: e
308
437
  })
309
438
  });
310
- if (!a.ok) {
311
- const u = await a.text();
312
- throw new Error(`Failed to delete conversation: ${a.status} ${u}`);
439
+ if (!s.ok) {
440
+ const i = await s.text();
441
+ throw new Error(`Failed to delete thread: ${s.status} ${i}`);
313
442
  }
314
- return await a.json();
315
- } catch (a) {
316
- 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;
443
+ return await s.json();
444
+ } catch (s) {
445
+ 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;
317
446
  }
318
447
  }
319
448
  /**
@@ -323,11 +452,11 @@ class x {
323
452
  * @returns Promise resolving to upload response with file_id
324
453
  * @throws Error if upload fails
325
454
  */
326
- async uploadFile(t) {
455
+ async uploadFile(r) {
327
456
  if (!this.apiKey)
328
457
  throw new Error("API key is required for file upload");
329
458
  const e = new FormData();
330
- e.append("file", t);
459
+ e.append("file", r);
331
460
  try {
332
461
  const s = await fetch(`${this.apiUrl}/api/files/upload`, {
333
462
  ...this.getFetchOptions({
@@ -336,8 +465,8 @@ class x {
336
465
  })
337
466
  });
338
467
  if (!s.ok) {
339
- const r = await s.text();
340
- throw new Error(`Failed to upload file: ${s.status} ${r}`);
468
+ const t = await s.text();
469
+ throw new Error(`Failed to upload file: ${s.status} ${t}`);
341
470
  }
342
471
  return await s.json();
343
472
  } catch (s) {
@@ -354,18 +483,18 @@ class x {
354
483
  if (!this.apiKey)
355
484
  throw new Error("API key is required for listing files");
356
485
  try {
357
- const t = await fetch(`${this.apiUrl}/api/files`, {
486
+ const r = await fetch(`${this.apiUrl}/api/files`, {
358
487
  ...this.getFetchOptions({
359
488
  method: "GET"
360
489
  })
361
490
  });
362
- if (!t.ok) {
363
- const e = await t.text();
364
- throw new Error(`Failed to list files: ${t.status} ${e}`);
491
+ if (!r.ok) {
492
+ const e = await r.text();
493
+ throw new Error(`Failed to list files: ${r.status} ${e}`);
365
494
  }
366
- return await t.json();
367
- } catch (t) {
368
- 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;
495
+ return await r.json();
496
+ } catch (r) {
497
+ 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;
369
498
  }
370
499
  }
371
500
  /**
@@ -375,11 +504,11 @@ class x {
375
504
  * @returns Promise resolving to file information
376
505
  * @throws Error if file not found or request fails
377
506
  */
378
- async getFileInfo(t) {
507
+ async getFileInfo(r) {
379
508
  if (!this.apiKey)
380
509
  throw new Error("API key is required for getting file info");
381
510
  try {
382
- const e = await fetch(`${this.apiUrl}/api/files/${t}`, {
511
+ const e = await fetch(`${this.apiUrl}/api/files/${r}`, {
383
512
  ...this.getFetchOptions({
384
513
  method: "GET"
385
514
  })
@@ -402,11 +531,11 @@ class x {
402
531
  * @returns Promise resolving to query results
403
532
  * @throws Error if query fails
404
533
  */
405
- async queryFile(t, e, s = 10) {
534
+ async queryFile(r, e, s = 10) {
406
535
  if (!this.apiKey)
407
536
  throw new Error("API key is required for querying files");
408
537
  try {
409
- const r = await fetch(`${this.apiUrl}/api/files/${t}/query`, {
538
+ const t = await fetch(`${this.apiUrl}/api/files/${r}/query`, {
410
539
  ...this.getFetchOptions({
411
540
  method: "POST",
412
541
  headers: {
@@ -415,13 +544,13 @@ class x {
415
544
  body: JSON.stringify({ query: e, max_results: s })
416
545
  })
417
546
  });
418
- if (!r.ok) {
419
- const i = await r.text();
420
- throw new Error(`Failed to query file: ${r.status} ${i}`);
547
+ if (!t.ok) {
548
+ const i = await t.text();
549
+ throw new Error(`Failed to query file: ${t.status} ${i}`);
421
550
  }
422
- return await r.json();
423
- } catch (r) {
424
- 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;
551
+ return await t.json();
552
+ } catch (t) {
553
+ 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;
425
554
  }
426
555
  }
427
556
  /**
@@ -431,44 +560,56 @@ class x {
431
560
  * @returns Promise resolving to deletion result
432
561
  * @throws Error if deletion fails
433
562
  */
434
- async deleteFile(t) {
563
+ async deleteFile(r) {
435
564
  if (!this.apiKey)
436
565
  throw new Error("API key is required for deleting files");
437
- const e = `${this.apiUrl}/api/files/${t}`, s = this.getFetchOptions({
566
+ const e = `${this.apiUrl}/api/files/${r}`, s = this.getFetchOptions({
438
567
  method: "DELETE"
439
568
  });
440
569
  try {
441
- const r = await fetch(e, s);
442
- if (!r.ok) {
443
- const n = await r.text();
444
- let a;
570
+ const t = await fetch(e, s);
571
+ if (!t.ok) {
572
+ const o = await t.text();
573
+ let n;
445
574
  try {
446
- const l = JSON.parse(n);
447
- a = l.detail || l.message || `Failed to delete file (HTTP ${r.status})`;
575
+ const h = JSON.parse(o);
576
+ n = h.detail || h.message || `Failed to delete file (HTTP ${t.status})`;
448
577
  } catch {
449
- a = `Failed to delete file (HTTP ${r.status})`;
578
+ n = `Failed to delete file (HTTP ${t.status})`;
450
579
  }
451
- throw console.warn(`[ApiClient] ${a}`), new Error(a);
580
+ throw console.warn(`[ApiClient] ${n}`), new Error(n);
452
581
  }
453
- return await r.json();
454
- } catch (r) {
582
+ return await t.json();
583
+ } catch (t) {
455
584
  let i;
456
- throw r.name === "TypeError" && r.message.includes("Failed to fetch") ? i = "Could not connect to the server. Please check if the server is running." : r.message && !r.message.includes("Failed to delete file") ? i = r.message : i = "Failed to delete file. Please try again.", console.warn(`[ApiClient] ${i}`), new Error(i);
585
+ 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);
457
586
  }
458
587
  }
459
588
  }
460
- let E = null;
461
- const O = (o, t = null, e = null) => {
462
- E = new x({ apiUrl: o, apiKey: t, sessionId: e });
589
+ let $ = null;
590
+ const J = (l, r = null, e = null) => {
591
+ $ = new D({ apiUrl: l, apiKey: r, sessionId: e });
463
592
  };
464
- async function* K(o, t = !0, e) {
465
- if (!E)
593
+ async function* M(l, r = !0, e, s, t, i, o, n, h, u, p) {
594
+ if (!$)
466
595
  throw new Error("API not configured. Please call configureApi() with your server URL before using any API functions.");
467
- yield* E.streamChat(o, t, e);
596
+ yield* $.streamChat(
597
+ l,
598
+ r,
599
+ e,
600
+ s,
601
+ t,
602
+ i,
603
+ o,
604
+ n,
605
+ h,
606
+ u,
607
+ p
608
+ );
468
609
  }
469
610
  export {
470
- x as ApiClient,
471
- O as configureApi,
472
- K as streamChat
611
+ D as ApiClient,
612
+ J as configureApi,
613
+ M as streamChat
473
614
  };
474
615
  //# sourceMappingURL=api.mjs.map