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