@schmitech/chatbot-api 2.1.3 → 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 +32 -2
- package/dist/api.mjs +283 -211
- package/dist/api.mjs.map +1 -1
- package/package.json +1 -1
package/dist/api.mjs
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
var
|
|
2
|
-
var D = (l, r, e) => r in 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
10
|
]).then(([l, r]) => {
|
|
11
|
-
var e,
|
|
12
|
-
(e = l == null ? void 0 : l.default) != null && e.Agent ?
|
|
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
18
|
constructor(r) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
P(this, "apiUrl");
|
|
20
|
+
P(this, "apiKey");
|
|
21
|
+
P(this, "sessionId");
|
|
22
22
|
if (!r.apiUrl || typeof r.apiUrl != "string")
|
|
23
23
|
throw new Error("API URL must be a valid string");
|
|
24
24
|
if (r.apiKey !== void 0 && r.apiKey !== null && typeof r.apiKey != "string")
|
|
@@ -50,55 +50,55 @@ 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
|
-
let i,
|
|
63
|
+
let i, n;
|
|
64
64
|
try {
|
|
65
|
-
const
|
|
66
|
-
i =
|
|
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:
|
|
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 (!
|
|
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
|
/**
|
|
@@ -115,85 +115,87 @@ class j {
|
|
|
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
|
})
|
|
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
|
-
let i,
|
|
132
|
+
let i, n;
|
|
133
133
|
try {
|
|
134
|
-
const
|
|
135
|
-
i =
|
|
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:
|
|
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 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
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,
|
|
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 && (
|
|
177
|
+
return this.apiKey && (t["X-API-Key"] = this.apiKey), this.sessionId && (t["X-Session-ID"] = this.sessionId), {
|
|
178
178
|
...r,
|
|
179
179
|
...e,
|
|
180
|
-
headers:
|
|
180
|
+
headers: t
|
|
181
181
|
};
|
|
182
182
|
}
|
|
183
183
|
// Create Chat request
|
|
184
|
-
createChatRequest(r, e = !0,
|
|
184
|
+
createChatRequest(r, e = !0, t, s, i, n, o, d, u, f, g) {
|
|
185
185
|
const c = {
|
|
186
186
|
messages: [
|
|
187
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(r, e = !0,
|
|
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: {
|
|
@@ -203,101 +205,164 @@ class j {
|
|
|
203
205
|
body: JSON.stringify(this.createChatRequest(
|
|
204
206
|
r,
|
|
205
207
|
e,
|
|
206
|
-
s,
|
|
207
208
|
t,
|
|
209
|
+
s,
|
|
208
210
|
i,
|
|
211
|
+
n,
|
|
209
212
|
o,
|
|
210
|
-
|
|
211
|
-
h,
|
|
213
|
+
d,
|
|
212
214
|
u,
|
|
213
|
-
|
|
214
|
-
|
|
215
|
+
f,
|
|
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 q =
|
|
243
|
-
|
|
244
|
-
let
|
|
245
|
-
for (; (
|
|
246
|
-
`,
|
|
247
|
-
const
|
|
248
|
-
if (
|
|
249
|
-
const
|
|
250
|
-
if (!
|
|
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();
|
|
252
|
+
if (!h || h === "[DONE]") {
|
|
251
253
|
yield { text: "", done: !0 };
|
|
252
254
|
return;
|
|
253
255
|
}
|
|
254
256
|
try {
|
|
255
|
-
const
|
|
256
|
-
if (
|
|
257
|
-
const K = `Server error: ${((
|
|
257
|
+
const a = JSON.parse(h);
|
|
258
|
+
if (a.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
|
}
|
|
260
|
-
if (
|
|
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
|
+
}
|
|
270
|
+
if (a.done === !0) {
|
|
271
|
+
k = !0, yield {
|
|
262
272
|
text: "",
|
|
263
273
|
done: !0,
|
|
264
|
-
audio:
|
|
265
|
-
audioFormat:
|
|
266
|
-
threading:
|
|
274
|
+
audio: a.audio,
|
|
275
|
+
audioFormat: a.audio_format || a.audioFormat,
|
|
276
|
+
threading: a.threading
|
|
267
277
|
// Pass through threading metadata
|
|
268
278
|
};
|
|
269
279
|
return;
|
|
270
280
|
}
|
|
271
|
-
const
|
|
272
|
-
|
|
281
|
+
const O = a.response || "";
|
|
282
|
+
a.audio_chunk !== void 0 && (yield {
|
|
273
283
|
text: "",
|
|
274
284
|
done: !1,
|
|
275
|
-
audio_chunk:
|
|
276
|
-
audioFormat:
|
|
277
|
-
chunk_index:
|
|
278
|
-
}), (
|
|
279
|
-
text:
|
|
280
|
-
done:
|
|
281
|
-
audio:
|
|
282
|
-
audioFormat:
|
|
283
|
-
threading:
|
|
285
|
+
audio_chunk: a.audio_chunk,
|
|
286
|
+
audioFormat: a.audioFormat || a.audio_format || "opus",
|
|
287
|
+
chunk_index: a.chunk_index ?? 0
|
|
288
|
+
}), (O || a.audio) && (k = !0, yield {
|
|
289
|
+
text: O,
|
|
290
|
+
done: a.done || !1,
|
|
291
|
+
audio: a.audio,
|
|
292
|
+
audioFormat: a.audio_format || a.audioFormat,
|
|
293
|
+
threading: a.threading
|
|
284
294
|
// Include threading if present
|
|
285
295
|
});
|
|
286
|
-
} catch (
|
|
287
|
-
if ((
|
|
288
|
-
throw
|
|
289
|
-
console.warn("[ApiClient] Unable to parse server response. This may be a temporary issue."), console.warn("[ApiClient] Parse error details:",
|
|
296
|
+
} catch (a) {
|
|
297
|
+
if ((b = a == null ? void 0 : a.message) != null && b.startsWith("Server error:"))
|
|
298
|
+
throw a;
|
|
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();
|
|
308
|
+
}
|
|
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;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
async getConversationHistory(r, e) {
|
|
340
|
+
const t = r || this.sessionId;
|
|
341
|
+
if (!t)
|
|
342
|
+
throw new Error("No session ID provided and no current session available");
|
|
343
|
+
const s = {};
|
|
344
|
+
this.apiKey && (s["X-API-Key"] = this.apiKey);
|
|
345
|
+
try {
|
|
346
|
+
const i = new URL(`${this.apiUrl}/admin/chat-history/${t}`);
|
|
347
|
+
typeof e == "number" && Number.isFinite(e) && e > 0 && i.searchParams.set("limit", String(Math.floor(e)));
|
|
348
|
+
const n = await fetch(i.toString(), {
|
|
349
|
+
...this.getFetchOptions({
|
|
350
|
+
method: "GET",
|
|
351
|
+
headers: s
|
|
352
|
+
})
|
|
353
|
+
});
|
|
354
|
+
if (!n.ok) {
|
|
355
|
+
const f = await n.text();
|
|
356
|
+
throw new Error(`Failed to fetch conversation history: ${n.status} ${f}`);
|
|
298
357
|
}
|
|
299
|
-
|
|
300
|
-
|
|
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;
|
|
359
|
+
return {
|
|
360
|
+
session_id: (o == null ? void 0 : o.session_id) || t,
|
|
361
|
+
messages: d,
|
|
362
|
+
count: u
|
|
363
|
+
};
|
|
364
|
+
} catch (i) {
|
|
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;
|
|
301
366
|
}
|
|
302
367
|
}
|
|
303
368
|
async clearConversationHistory(r) {
|
|
@@ -306,52 +371,52 @@ class j {
|
|
|
306
371
|
throw new Error("No session ID provided and no current session available");
|
|
307
372
|
if (!this.apiKey)
|
|
308
373
|
throw new Error("API key is required for clearing conversation history");
|
|
309
|
-
const
|
|
374
|
+
const t = {
|
|
310
375
|
"Content-Type": "application/json",
|
|
311
376
|
"X-Session-ID": e,
|
|
312
377
|
"X-API-Key": this.apiKey
|
|
313
378
|
};
|
|
314
379
|
try {
|
|
315
|
-
const
|
|
380
|
+
const s = await fetch(`${this.apiUrl}/admin/chat-history/${e}`, {
|
|
316
381
|
...this.getFetchOptions({
|
|
317
382
|
method: "DELETE",
|
|
318
|
-
headers:
|
|
383
|
+
headers: t
|
|
319
384
|
})
|
|
320
385
|
});
|
|
321
|
-
if (!
|
|
322
|
-
const
|
|
323
|
-
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}`);
|
|
324
389
|
}
|
|
325
|
-
return await
|
|
326
|
-
} catch (
|
|
327
|
-
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;
|
|
328
393
|
}
|
|
329
394
|
}
|
|
330
395
|
async deleteConversationWithFiles(r, e) {
|
|
331
|
-
const
|
|
332
|
-
if (!
|
|
396
|
+
const t = r || this.sessionId;
|
|
397
|
+
if (!t)
|
|
333
398
|
throw new Error("No session ID provided and no current session available");
|
|
334
399
|
if (!this.apiKey)
|
|
335
400
|
throw new Error("API key is required for deleting conversation");
|
|
336
|
-
const
|
|
401
|
+
const s = {
|
|
337
402
|
"Content-Type": "application/json",
|
|
338
|
-
"X-Session-ID":
|
|
403
|
+
"X-Session-ID": t,
|
|
339
404
|
"X-API-Key": this.apiKey
|
|
340
|
-
}, i = e && e.length > 0 ? `?file_ids=${e.join(",")}` : "",
|
|
405
|
+
}, i = e && e.length > 0 ? `?file_ids=${e.join(",")}` : "", n = `${this.apiUrl}/admin/conversations/${t}${i}`;
|
|
341
406
|
try {
|
|
342
|
-
const
|
|
407
|
+
const o = await fetch(n, {
|
|
343
408
|
...this.getFetchOptions({
|
|
344
409
|
method: "DELETE",
|
|
345
|
-
headers:
|
|
410
|
+
headers: s
|
|
346
411
|
})
|
|
347
412
|
});
|
|
348
|
-
if (!
|
|
349
|
-
const u = await
|
|
350
|
-
throw new Error(`Failed to delete conversation: ${
|
|
413
|
+
if (!o.ok) {
|
|
414
|
+
const u = await o.text();
|
|
415
|
+
throw new Error(`Failed to delete conversation: ${o.status} ${u}`);
|
|
351
416
|
}
|
|
352
|
-
return await
|
|
353
|
-
} catch (
|
|
354
|
-
throw
|
|
417
|
+
return await o.json();
|
|
418
|
+
} catch (o) {
|
|
419
|
+
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
420
|
}
|
|
356
421
|
}
|
|
357
422
|
/**
|
|
@@ -365,28 +430,28 @@ class j {
|
|
|
365
430
|
async createThread(r, e) {
|
|
366
431
|
if (!this.apiKey)
|
|
367
432
|
throw new Error("API key is required for creating threads");
|
|
368
|
-
const
|
|
433
|
+
const t = {
|
|
369
434
|
"Content-Type": "application/json",
|
|
370
435
|
"X-API-Key": this.apiKey
|
|
371
436
|
};
|
|
372
437
|
try {
|
|
373
|
-
const
|
|
438
|
+
const s = await fetch(`${this.apiUrl}/api/threads`, {
|
|
374
439
|
...this.getFetchOptions({
|
|
375
440
|
method: "POST",
|
|
376
|
-
headers:
|
|
441
|
+
headers: t,
|
|
377
442
|
body: JSON.stringify({
|
|
378
443
|
message_id: r,
|
|
379
444
|
session_id: e
|
|
380
445
|
})
|
|
381
446
|
})
|
|
382
447
|
});
|
|
383
|
-
if (!
|
|
384
|
-
const
|
|
385
|
-
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}`);
|
|
386
451
|
}
|
|
387
|
-
return await
|
|
388
|
-
} catch (
|
|
389
|
-
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;
|
|
390
455
|
}
|
|
391
456
|
}
|
|
392
457
|
/**
|
|
@@ -403,19 +468,19 @@ class j {
|
|
|
403
468
|
"X-API-Key": this.apiKey
|
|
404
469
|
};
|
|
405
470
|
try {
|
|
406
|
-
const
|
|
471
|
+
const t = await fetch(`${this.apiUrl}/api/threads/${r}`, {
|
|
407
472
|
...this.getFetchOptions({
|
|
408
473
|
method: "GET",
|
|
409
474
|
headers: e
|
|
410
475
|
})
|
|
411
476
|
});
|
|
412
|
-
if (!
|
|
413
|
-
const i = await
|
|
414
|
-
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}`);
|
|
415
480
|
}
|
|
416
|
-
return await
|
|
417
|
-
} catch (
|
|
418
|
-
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;
|
|
419
484
|
}
|
|
420
485
|
}
|
|
421
486
|
/**
|
|
@@ -432,19 +497,19 @@ class j {
|
|
|
432
497
|
"X-API-Key": this.apiKey
|
|
433
498
|
};
|
|
434
499
|
try {
|
|
435
|
-
const
|
|
500
|
+
const t = await fetch(`${this.apiUrl}/api/threads/${r}`, {
|
|
436
501
|
...this.getFetchOptions({
|
|
437
502
|
method: "DELETE",
|
|
438
503
|
headers: e
|
|
439
504
|
})
|
|
440
505
|
});
|
|
441
|
-
if (!
|
|
442
|
-
const i = await
|
|
443
|
-
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}`);
|
|
444
509
|
}
|
|
445
|
-
return await
|
|
446
|
-
} catch (
|
|
447
|
-
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;
|
|
448
513
|
}
|
|
449
514
|
}
|
|
450
515
|
/**
|
|
@@ -460,19 +525,19 @@ class j {
|
|
|
460
525
|
const e = new FormData();
|
|
461
526
|
e.append("file", r);
|
|
462
527
|
try {
|
|
463
|
-
const
|
|
528
|
+
const t = await fetch(`${this.apiUrl}/api/files/upload`, {
|
|
464
529
|
...this.getFetchOptions({
|
|
465
530
|
method: "POST",
|
|
466
531
|
body: e
|
|
467
532
|
})
|
|
468
533
|
});
|
|
469
|
-
if (!
|
|
470
|
-
const
|
|
471
|
-
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}`);
|
|
472
537
|
}
|
|
473
|
-
return await
|
|
474
|
-
} catch (
|
|
475
|
-
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;
|
|
476
541
|
}
|
|
477
542
|
}
|
|
478
543
|
/**
|
|
@@ -516,8 +581,8 @@ class j {
|
|
|
516
581
|
})
|
|
517
582
|
});
|
|
518
583
|
if (!e.ok) {
|
|
519
|
-
const
|
|
520
|
-
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}`);
|
|
521
586
|
}
|
|
522
587
|
return await e.json();
|
|
523
588
|
} catch (e) {
|
|
@@ -533,26 +598,26 @@ class j {
|
|
|
533
598
|
* @returns Promise resolving to query results
|
|
534
599
|
* @throws Error if query fails
|
|
535
600
|
*/
|
|
536
|
-
async queryFile(r, e,
|
|
601
|
+
async queryFile(r, e, t = 10) {
|
|
537
602
|
if (!this.apiKey)
|
|
538
603
|
throw new Error("API key is required for querying files");
|
|
539
604
|
try {
|
|
540
|
-
const
|
|
605
|
+
const s = await fetch(`${this.apiUrl}/api/files/${r}/query`, {
|
|
541
606
|
...this.getFetchOptions({
|
|
542
607
|
method: "POST",
|
|
543
608
|
headers: {
|
|
544
609
|
"Content-Type": "application/json"
|
|
545
610
|
},
|
|
546
|
-
body: JSON.stringify({ query: e, max_results:
|
|
611
|
+
body: JSON.stringify({ query: e, max_results: t })
|
|
547
612
|
})
|
|
548
613
|
});
|
|
549
|
-
if (!
|
|
550
|
-
const i = await
|
|
551
|
-
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}`);
|
|
552
617
|
}
|
|
553
|
-
return await
|
|
554
|
-
} catch (
|
|
555
|
-
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;
|
|
556
621
|
}
|
|
557
622
|
}
|
|
558
623
|
/**
|
|
@@ -565,53 +630,60 @@ class j {
|
|
|
565
630
|
async deleteFile(r) {
|
|
566
631
|
if (!this.apiKey)
|
|
567
632
|
throw new Error("API key is required for deleting files");
|
|
568
|
-
const e = `${this.apiUrl}/api/files/${r}`,
|
|
633
|
+
const e = `${this.apiUrl}/api/files/${r}`, t = this.getFetchOptions({
|
|
569
634
|
method: "DELETE"
|
|
570
635
|
});
|
|
571
636
|
try {
|
|
572
|
-
const
|
|
573
|
-
if (!
|
|
574
|
-
const
|
|
575
|
-
let
|
|
637
|
+
const s = await fetch(e, t);
|
|
638
|
+
if (!s.ok) {
|
|
639
|
+
const n = await s.text();
|
|
640
|
+
let o;
|
|
576
641
|
try {
|
|
577
|
-
const
|
|
578
|
-
|
|
642
|
+
const d = JSON.parse(n);
|
|
643
|
+
o = d.detail || d.message || `Failed to delete file (HTTP ${s.status})`;
|
|
579
644
|
} catch {
|
|
580
|
-
|
|
645
|
+
o = `Failed to delete file (HTTP ${s.status})`;
|
|
581
646
|
}
|
|
582
|
-
throw console.warn(`[ApiClient] ${
|
|
647
|
+
throw console.warn(`[ApiClient] ${o}`), new Error(o);
|
|
583
648
|
}
|
|
584
|
-
return await
|
|
585
|
-
} catch (
|
|
649
|
+
return await s.json();
|
|
650
|
+
} catch (s) {
|
|
586
651
|
let i;
|
|
587
|
-
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);
|
|
588
653
|
}
|
|
589
654
|
}
|
|
590
655
|
}
|
|
591
|
-
let
|
|
592
|
-
const
|
|
593
|
-
|
|
656
|
+
let E = null;
|
|
657
|
+
const H = (l, r = null, e = null) => {
|
|
658
|
+
E = new N({ apiUrl: l, apiKey: r, sessionId: e });
|
|
594
659
|
};
|
|
595
|
-
async function*
|
|
596
|
-
if (
|
|
660
|
+
async function* X(l, r = !0, e, t, s, i, n, o, d, u, f, g) {
|
|
661
|
+
if (!E)
|
|
597
662
|
throw new Error("API not configured. Please call configureApi() with your server URL before using any API functions.");
|
|
598
|
-
yield*
|
|
663
|
+
yield* E.streamChat(
|
|
599
664
|
l,
|
|
600
665
|
r,
|
|
601
666
|
e,
|
|
602
|
-
s,
|
|
603
667
|
t,
|
|
668
|
+
s,
|
|
604
669
|
i,
|
|
670
|
+
n,
|
|
605
671
|
o,
|
|
606
|
-
|
|
607
|
-
h,
|
|
672
|
+
d,
|
|
608
673
|
u,
|
|
609
|
-
|
|
674
|
+
f,
|
|
675
|
+
g
|
|
610
676
|
);
|
|
611
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
|
+
}
|
|
612
683
|
export {
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
684
|
+
N as ApiClient,
|
|
685
|
+
H as configureApi,
|
|
686
|
+
L as stopChat,
|
|
687
|
+
X as streamChat
|
|
616
688
|
};
|
|
617
689
|
//# sourceMappingURL=api.mjs.map
|