@schmitech/chatbot-api 2.1.2 → 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/README.md +29 -15
- package/dist/api.cjs +2 -2
- package/dist/api.cjs.map +1 -1
- package/dist/api.d.ts +13 -0
- package/dist/api.mjs +211 -180
- package/dist/api.mjs.map +1 -1
- package/package.json +1 -1
package/dist/api.mjs
CHANGED
|
@@ -1,36 +1,36 @@
|
|
|
1
1
|
var q = Object.defineProperty;
|
|
2
|
-
var
|
|
3
|
-
var A = (l,
|
|
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
|
-
class
|
|
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 D {
|
|
|
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 D {
|
|
|
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 D {
|
|
|
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 D {
|
|
|
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,64 +201,64 @@ class D {
|
|
|
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
|
-
o,
|
|
210
209
|
n,
|
|
211
|
-
|
|
210
|
+
o,
|
|
211
|
+
c,
|
|
212
212
|
u,
|
|
213
|
-
|
|
214
|
-
|
|
213
|
+
f,
|
|
214
|
+
v
|
|
215
215
|
))
|
|
216
216
|
}),
|
|
217
|
-
signal:
|
|
217
|
+
signal: p.signal
|
|
218
218
|
});
|
|
219
|
-
if (clearTimeout(
|
|
220
|
-
const
|
|
221
|
-
throw new Error(`Network response was not ok: ${g.status} ${
|
|
219
|
+
if (clearTimeout(O), !g.ok) {
|
|
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
|
-
const
|
|
236
|
-
let
|
|
235
|
+
const S = new TextDecoder();
|
|
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 a = JSON.parse(
|
|
255
|
+
const a = JSON.parse(h);
|
|
256
256
|
if (a.error) {
|
|
257
|
-
const
|
|
258
|
-
throw console.warn(`[ApiClient] ${
|
|
257
|
+
const K = `Server error: ${((C = a.error) == null ? void 0 : C.message) || a.error || "Unknown server error"}`;
|
|
258
|
+
throw console.warn(`[ApiClient] ${K}`), new Error(K);
|
|
259
259
|
}
|
|
260
260
|
if (a.done === !0) {
|
|
261
|
-
|
|
261
|
+
k = !0, yield {
|
|
262
262
|
text: "",
|
|
263
263
|
done: !0,
|
|
264
264
|
audio: a.audio,
|
|
@@ -268,15 +268,15 @@ class D {
|
|
|
268
268
|
};
|
|
269
269
|
return;
|
|
270
270
|
}
|
|
271
|
-
const
|
|
271
|
+
const b = a.response || "";
|
|
272
272
|
a.audio_chunk !== void 0 && (yield {
|
|
273
273
|
text: "",
|
|
274
274
|
done: !1,
|
|
275
275
|
audio_chunk: a.audio_chunk,
|
|
276
276
|
audioFormat: a.audioFormat || a.audio_format || "opus",
|
|
277
277
|
chunk_index: a.chunk_index ?? 0
|
|
278
|
-
}), (
|
|
279
|
-
text:
|
|
278
|
+
}), (b || a.audio) && (k = !0, yield {
|
|
279
|
+
text: b,
|
|
280
280
|
done: a.done || !1,
|
|
281
281
|
audio: a.audio,
|
|
282
282
|
audioFormat: a.audio_format || a.audioFormat,
|
|
@@ -284,27 +284,58 @@ class D {
|
|
|
284
284
|
// Include threading if present
|
|
285
285
|
});
|
|
286
286
|
} catch (a) {
|
|
287
|
-
|
|
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));
|
|
288
290
|
}
|
|
289
|
-
} else m && (
|
|
291
|
+
} else m && (k = !0, yield { text: m, done: !1 });
|
|
290
292
|
}
|
|
291
|
-
|
|
293
|
+
w = w.slice(E), w.length > 1e6 && (console.warn("[ApiClient] Buffer too large, truncating..."), w = w.slice(-5e5));
|
|
292
294
|
}
|
|
293
|
-
|
|
295
|
+
k && (yield { text: "", done: !0 });
|
|
294
296
|
} finally {
|
|
295
297
|
P.releaseLock();
|
|
296
298
|
}
|
|
297
|
-
} catch (
|
|
298
|
-
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
|
+
}
|
|
302
|
+
}
|
|
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;
|
|
299
330
|
}
|
|
300
331
|
}
|
|
301
|
-
async clearConversationHistory(
|
|
302
|
-
const e =
|
|
332
|
+
async clearConversationHistory(s) {
|
|
333
|
+
const e = s || this.sessionId;
|
|
303
334
|
if (!e)
|
|
304
335
|
throw new Error("No session ID provided and no current session available");
|
|
305
336
|
if (!this.apiKey)
|
|
306
337
|
throw new Error("API key is required for clearing conversation history");
|
|
307
|
-
const
|
|
338
|
+
const r = {
|
|
308
339
|
"Content-Type": "application/json",
|
|
309
340
|
"X-Session-ID": e,
|
|
310
341
|
"X-API-Key": this.apiKey
|
|
@@ -313,43 +344,43 @@ class D {
|
|
|
313
344
|
const t = await fetch(`${this.apiUrl}/admin/chat-history/${e}`, {
|
|
314
345
|
...this.getFetchOptions({
|
|
315
346
|
method: "DELETE",
|
|
316
|
-
headers:
|
|
347
|
+
headers: r
|
|
317
348
|
})
|
|
318
349
|
});
|
|
319
350
|
if (!t.ok) {
|
|
320
|
-
const
|
|
321
|
-
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}`);
|
|
322
353
|
}
|
|
323
354
|
return await t.json();
|
|
324
355
|
} catch (t) {
|
|
325
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;
|
|
326
357
|
}
|
|
327
358
|
}
|
|
328
|
-
async deleteConversationWithFiles(
|
|
329
|
-
const
|
|
330
|
-
if (!
|
|
359
|
+
async deleteConversationWithFiles(s, e) {
|
|
360
|
+
const r = s || this.sessionId;
|
|
361
|
+
if (!r)
|
|
331
362
|
throw new Error("No session ID provided and no current session available");
|
|
332
363
|
if (!this.apiKey)
|
|
333
364
|
throw new Error("API key is required for deleting conversation");
|
|
334
365
|
const t = {
|
|
335
366
|
"Content-Type": "application/json",
|
|
336
|
-
"X-Session-ID":
|
|
367
|
+
"X-Session-ID": r,
|
|
337
368
|
"X-API-Key": this.apiKey
|
|
338
|
-
}, 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}`;
|
|
339
370
|
try {
|
|
340
|
-
const
|
|
371
|
+
const o = await fetch(n, {
|
|
341
372
|
...this.getFetchOptions({
|
|
342
373
|
method: "DELETE",
|
|
343
374
|
headers: t
|
|
344
375
|
})
|
|
345
376
|
});
|
|
346
|
-
if (!
|
|
347
|
-
const u = await
|
|
348
|
-
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}`);
|
|
349
380
|
}
|
|
350
|
-
return await
|
|
351
|
-
} catch (
|
|
352
|
-
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;
|
|
353
384
|
}
|
|
354
385
|
}
|
|
355
386
|
/**
|
|
@@ -360,10 +391,10 @@ class D {
|
|
|
360
391
|
* @returns Promise resolving to thread information
|
|
361
392
|
* @throws Error if the operation fails
|
|
362
393
|
*/
|
|
363
|
-
async createThread(
|
|
394
|
+
async createThread(s, e) {
|
|
364
395
|
if (!this.apiKey)
|
|
365
396
|
throw new Error("API key is required for creating threads");
|
|
366
|
-
const
|
|
397
|
+
const r = {
|
|
367
398
|
"Content-Type": "application/json",
|
|
368
399
|
"X-API-Key": this.apiKey
|
|
369
400
|
};
|
|
@@ -371,16 +402,16 @@ class D {
|
|
|
371
402
|
const t = await fetch(`${this.apiUrl}/api/threads`, {
|
|
372
403
|
...this.getFetchOptions({
|
|
373
404
|
method: "POST",
|
|
374
|
-
headers:
|
|
405
|
+
headers: r,
|
|
375
406
|
body: JSON.stringify({
|
|
376
|
-
message_id:
|
|
407
|
+
message_id: s,
|
|
377
408
|
session_id: e
|
|
378
409
|
})
|
|
379
410
|
})
|
|
380
411
|
});
|
|
381
412
|
if (!t.ok) {
|
|
382
|
-
const
|
|
383
|
-
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}`);
|
|
384
415
|
}
|
|
385
416
|
return await t.json();
|
|
386
417
|
} catch (t) {
|
|
@@ -394,26 +425,26 @@ class D {
|
|
|
394
425
|
* @returns Promise resolving to thread information
|
|
395
426
|
* @throws Error if the operation fails
|
|
396
427
|
*/
|
|
397
|
-
async getThreadInfo(
|
|
428
|
+
async getThreadInfo(s) {
|
|
398
429
|
if (!this.apiKey)
|
|
399
430
|
throw new Error("API key is required for getting thread info");
|
|
400
431
|
const e = {
|
|
401
432
|
"X-API-Key": this.apiKey
|
|
402
433
|
};
|
|
403
434
|
try {
|
|
404
|
-
const
|
|
435
|
+
const r = await fetch(`${this.apiUrl}/api/threads/${s}`, {
|
|
405
436
|
...this.getFetchOptions({
|
|
406
437
|
method: "GET",
|
|
407
438
|
headers: e
|
|
408
439
|
})
|
|
409
440
|
});
|
|
410
|
-
if (!
|
|
411
|
-
const i = await
|
|
412
|
-
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}`);
|
|
413
444
|
}
|
|
414
|
-
return await
|
|
415
|
-
} catch (
|
|
416
|
-
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;
|
|
417
448
|
}
|
|
418
449
|
}
|
|
419
450
|
/**
|
|
@@ -423,26 +454,26 @@ class D {
|
|
|
423
454
|
* @returns Promise resolving to deletion result
|
|
424
455
|
* @throws Error if the operation fails
|
|
425
456
|
*/
|
|
426
|
-
async deleteThread(
|
|
457
|
+
async deleteThread(s) {
|
|
427
458
|
if (!this.apiKey)
|
|
428
459
|
throw new Error("API key is required for deleting threads");
|
|
429
460
|
const e = {
|
|
430
461
|
"X-API-Key": this.apiKey
|
|
431
462
|
};
|
|
432
463
|
try {
|
|
433
|
-
const
|
|
464
|
+
const r = await fetch(`${this.apiUrl}/api/threads/${s}`, {
|
|
434
465
|
...this.getFetchOptions({
|
|
435
466
|
method: "DELETE",
|
|
436
467
|
headers: e
|
|
437
468
|
})
|
|
438
469
|
});
|
|
439
|
-
if (!
|
|
440
|
-
const i = await
|
|
441
|
-
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}`);
|
|
442
473
|
}
|
|
443
|
-
return await
|
|
444
|
-
} catch (
|
|
445
|
-
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;
|
|
446
477
|
}
|
|
447
478
|
}
|
|
448
479
|
/**
|
|
@@ -452,25 +483,25 @@ class D {
|
|
|
452
483
|
* @returns Promise resolving to upload response with file_id
|
|
453
484
|
* @throws Error if upload fails
|
|
454
485
|
*/
|
|
455
|
-
async uploadFile(
|
|
486
|
+
async uploadFile(s) {
|
|
456
487
|
if (!this.apiKey)
|
|
457
488
|
throw new Error("API key is required for file upload");
|
|
458
489
|
const e = new FormData();
|
|
459
|
-
e.append("file",
|
|
490
|
+
e.append("file", s);
|
|
460
491
|
try {
|
|
461
|
-
const
|
|
492
|
+
const r = await fetch(`${this.apiUrl}/api/files/upload`, {
|
|
462
493
|
...this.getFetchOptions({
|
|
463
494
|
method: "POST",
|
|
464
495
|
body: e
|
|
465
496
|
})
|
|
466
497
|
});
|
|
467
|
-
if (!
|
|
468
|
-
const t = await
|
|
469
|
-
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}`);
|
|
470
501
|
}
|
|
471
|
-
return await
|
|
472
|
-
} catch (
|
|
473
|
-
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;
|
|
474
505
|
}
|
|
475
506
|
}
|
|
476
507
|
/**
|
|
@@ -483,18 +514,18 @@ class D {
|
|
|
483
514
|
if (!this.apiKey)
|
|
484
515
|
throw new Error("API key is required for listing files");
|
|
485
516
|
try {
|
|
486
|
-
const
|
|
517
|
+
const s = await fetch(`${this.apiUrl}/api/files`, {
|
|
487
518
|
...this.getFetchOptions({
|
|
488
519
|
method: "GET"
|
|
489
520
|
})
|
|
490
521
|
});
|
|
491
|
-
if (!
|
|
492
|
-
const e = await
|
|
493
|
-
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}`);
|
|
494
525
|
}
|
|
495
|
-
return await
|
|
496
|
-
} catch (
|
|
497
|
-
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;
|
|
498
529
|
}
|
|
499
530
|
}
|
|
500
531
|
/**
|
|
@@ -504,18 +535,18 @@ class D {
|
|
|
504
535
|
* @returns Promise resolving to file information
|
|
505
536
|
* @throws Error if file not found or request fails
|
|
506
537
|
*/
|
|
507
|
-
async getFileInfo(
|
|
538
|
+
async getFileInfo(s) {
|
|
508
539
|
if (!this.apiKey)
|
|
509
540
|
throw new Error("API key is required for getting file info");
|
|
510
541
|
try {
|
|
511
|
-
const e = await fetch(`${this.apiUrl}/api/files/${
|
|
542
|
+
const e = await fetch(`${this.apiUrl}/api/files/${s}`, {
|
|
512
543
|
...this.getFetchOptions({
|
|
513
544
|
method: "GET"
|
|
514
545
|
})
|
|
515
546
|
});
|
|
516
547
|
if (!e.ok) {
|
|
517
|
-
const
|
|
518
|
-
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}`);
|
|
519
550
|
}
|
|
520
551
|
return await e.json();
|
|
521
552
|
} catch (e) {
|
|
@@ -531,17 +562,17 @@ class D {
|
|
|
531
562
|
* @returns Promise resolving to query results
|
|
532
563
|
* @throws Error if query fails
|
|
533
564
|
*/
|
|
534
|
-
async queryFile(
|
|
565
|
+
async queryFile(s, e, r = 10) {
|
|
535
566
|
if (!this.apiKey)
|
|
536
567
|
throw new Error("API key is required for querying files");
|
|
537
568
|
try {
|
|
538
|
-
const t = await fetch(`${this.apiUrl}/api/files/${
|
|
569
|
+
const t = await fetch(`${this.apiUrl}/api/files/${s}/query`, {
|
|
539
570
|
...this.getFetchOptions({
|
|
540
571
|
method: "POST",
|
|
541
572
|
headers: {
|
|
542
573
|
"Content-Type": "application/json"
|
|
543
574
|
},
|
|
544
|
-
body: JSON.stringify({ query: e, max_results:
|
|
575
|
+
body: JSON.stringify({ query: e, max_results: r })
|
|
545
576
|
})
|
|
546
577
|
});
|
|
547
578
|
if (!t.ok) {
|
|
@@ -560,24 +591,24 @@ class D {
|
|
|
560
591
|
* @returns Promise resolving to deletion result
|
|
561
592
|
* @throws Error if deletion fails
|
|
562
593
|
*/
|
|
563
|
-
async deleteFile(
|
|
594
|
+
async deleteFile(s) {
|
|
564
595
|
if (!this.apiKey)
|
|
565
596
|
throw new Error("API key is required for deleting files");
|
|
566
|
-
const e = `${this.apiUrl}/api/files/${
|
|
597
|
+
const e = `${this.apiUrl}/api/files/${s}`, r = this.getFetchOptions({
|
|
567
598
|
method: "DELETE"
|
|
568
599
|
});
|
|
569
600
|
try {
|
|
570
|
-
const t = await fetch(e,
|
|
601
|
+
const t = await fetch(e, r);
|
|
571
602
|
if (!t.ok) {
|
|
572
|
-
const
|
|
573
|
-
let
|
|
603
|
+
const n = await t.text();
|
|
604
|
+
let o;
|
|
574
605
|
try {
|
|
575
|
-
const
|
|
576
|
-
|
|
606
|
+
const c = JSON.parse(n);
|
|
607
|
+
o = c.detail || c.message || `Failed to delete file (HTTP ${t.status})`;
|
|
577
608
|
} catch {
|
|
578
|
-
|
|
609
|
+
o = `Failed to delete file (HTTP ${t.status})`;
|
|
579
610
|
}
|
|
580
|
-
throw console.warn(`[ApiClient] ${
|
|
611
|
+
throw console.warn(`[ApiClient] ${o}`), new Error(o);
|
|
581
612
|
}
|
|
582
613
|
return await t.json();
|
|
583
614
|
} catch (t) {
|
|
@@ -587,29 +618,29 @@ class D {
|
|
|
587
618
|
}
|
|
588
619
|
}
|
|
589
620
|
let $ = null;
|
|
590
|
-
const
|
|
591
|
-
$ = new
|
|
621
|
+
const M = (l, s = null, e = null) => {
|
|
622
|
+
$ = new j({ apiUrl: l, apiKey: s, sessionId: e });
|
|
592
623
|
};
|
|
593
|
-
async function*
|
|
624
|
+
async function* H(l, s = !0, e, r, t, i, n, o, c, u, f) {
|
|
594
625
|
if (!$)
|
|
595
626
|
throw new Error("API not configured. Please call configureApi() with your server URL before using any API functions.");
|
|
596
627
|
yield* $.streamChat(
|
|
597
628
|
l,
|
|
598
|
-
r,
|
|
599
|
-
e,
|
|
600
629
|
s,
|
|
630
|
+
e,
|
|
631
|
+
r,
|
|
601
632
|
t,
|
|
602
633
|
i,
|
|
603
|
-
o,
|
|
604
634
|
n,
|
|
605
|
-
|
|
635
|
+
o,
|
|
636
|
+
c,
|
|
606
637
|
u,
|
|
607
|
-
|
|
638
|
+
f
|
|
608
639
|
);
|
|
609
640
|
}
|
|
610
641
|
export {
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
642
|
+
j as ApiClient,
|
|
643
|
+
M as configureApi,
|
|
644
|
+
H as streamChat
|
|
614
645
|
};
|
|
615
646
|
//# sourceMappingURL=api.mjs.map
|