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