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