@schmitech/chatbot-api 2.0.0 → 2.1.0
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 +45 -2
- package/dist/api.mjs +251 -151
- 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
|
|
3
|
-
var
|
|
4
|
-
let
|
|
1
|
+
var q = Object.defineProperty;
|
|
2
|
+
var _ = (l, r, e) => r in l ? q(l, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : l[r] = e;
|
|
3
|
+
var A = (l, r, e) => _(l, typeof r != "symbol" ? r + "" : r, e);
|
|
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,
|
|
10
|
+
]).then(([l, r]) => {
|
|
11
11
|
var e, s;
|
|
12
|
-
(e = l == null ? void 0 : l.default) != null && e.Agent ?
|
|
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 })), (s = r == null ? void 0 : r.default) != null && s.Agent ? I = new r.default.Agent({ keepAlive: !0 }) : r != null && r.Agent && (I = new r.Agent({ keepAlive: !0 }));
|
|
13
13
|
}).catch((l) => {
|
|
14
14
|
console.warn("Failed to initialize HTTP agents:", l.message);
|
|
15
15
|
});
|
|
16
16
|
class D {
|
|
17
17
|
// Session ID can be mutable
|
|
18
|
-
constructor(
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
if (!
|
|
18
|
+
constructor(r) {
|
|
19
|
+
A(this, "apiUrl");
|
|
20
|
+
A(this, "apiKey");
|
|
21
|
+
A(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 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 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 D {
|
|
|
50
50
|
...this.getFetchOptions({
|
|
51
51
|
method: "GET"
|
|
52
52
|
})
|
|
53
|
-
}).catch((
|
|
54
|
-
throw
|
|
53
|
+
}).catch((t) => {
|
|
54
|
+
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;
|
|
55
55
|
});
|
|
56
56
|
if (!e.ok) {
|
|
57
|
-
let
|
|
57
|
+
let t = "";
|
|
58
58
|
try {
|
|
59
|
-
|
|
59
|
+
t = await e.text();
|
|
60
60
|
} catch {
|
|
61
|
-
|
|
61
|
+
t = `HTTP ${e.status}`;
|
|
62
62
|
}
|
|
63
63
|
let i, o;
|
|
64
64
|
try {
|
|
65
|
-
const n = JSON.parse(
|
|
66
|
-
i = n.detail || n.message ||
|
|
65
|
+
const n = JSON.parse(t);
|
|
66
|
+
i = n.detail || n.message || t;
|
|
67
67
|
} catch {
|
|
68
|
-
i =
|
|
68
|
+
i = t || `HTTP ${e.status}`;
|
|
69
69
|
}
|
|
70
70
|
switch (e.status) {
|
|
71
71
|
case 401:
|
|
@@ -88,17 +88,17 @@ class D {
|
|
|
88
88
|
}
|
|
89
89
|
const s = await e.json();
|
|
90
90
|
if (!s.exists) {
|
|
91
|
-
const
|
|
92
|
-
throw new Error(
|
|
91
|
+
const t = "API key does not exist";
|
|
92
|
+
throw new Error(t);
|
|
93
93
|
}
|
|
94
94
|
if (!s.active) {
|
|
95
|
-
const
|
|
96
|
-
throw new Error(
|
|
95
|
+
const t = "API key is inactive";
|
|
96
|
+
throw new Error(t);
|
|
97
97
|
}
|
|
98
98
|
return s;
|
|
99
99
|
} catch (e) {
|
|
100
100
|
let s;
|
|
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") ? s = e.message : s = `API key validation failed: ${e.message}` : e.name === "TypeError" && ((
|
|
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") ? s = e.message : s = `API key validation failed: ${e.message}` : e.name === "TypeError" && ((r = e.message) != null && r.includes("Failed to fetch")) ? s = "Could not connect to the server. Please check if the server is running." : s = "API key validation failed. Please check your API key and try again.", console.warn(`[ApiClient] ${s}`), new Error(s);
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
104
|
/**
|
|
@@ -111,7 +111,7 @@ 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 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 D {
|
|
|
119
119
|
...this.getFetchOptions({
|
|
120
120
|
method: "GET"
|
|
121
121
|
})
|
|
122
|
-
}).catch((
|
|
123
|
-
throw
|
|
122
|
+
}).catch((t) => {
|
|
123
|
+
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;
|
|
124
124
|
});
|
|
125
125
|
if (!e.ok) {
|
|
126
|
-
let
|
|
126
|
+
let t = "";
|
|
127
127
|
try {
|
|
128
|
-
|
|
128
|
+
t = await e.text();
|
|
129
129
|
} catch {
|
|
130
|
-
|
|
130
|
+
t = `HTTP ${e.status}`;
|
|
131
131
|
}
|
|
132
132
|
let i, o;
|
|
133
133
|
try {
|
|
134
|
-
const n = JSON.parse(
|
|
135
|
-
i = n.detail || n.message ||
|
|
134
|
+
const n = JSON.parse(t);
|
|
135
|
+
i = n.detail || n.message || t;
|
|
136
136
|
} catch {
|
|
137
|
-
i =
|
|
137
|
+
i = t || `HTTP ${e.status}`;
|
|
138
138
|
}
|
|
139
139
|
switch (e.status) {
|
|
140
140
|
case 401:
|
|
@@ -155,45 +155,45 @@ class D {
|
|
|
155
155
|
return await e.json();
|
|
156
156
|
} catch (e) {
|
|
157
157
|
let s;
|
|
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") ? s = e.message : s = `Failed to get adapter info: ${e.message}` : e.name === "TypeError" && ((
|
|
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") ? s = e.message : s = `Failed to get adapter info: ${e.message}` : e.name === "TypeError" && ((r = e.message) != null && r.includes("Failed to fetch")) ? s = "Could not connect to the server. Please check if the server is running." : s = "Failed to get adapter information. Please try again.", console.warn(`[ApiClient] ${s}`), new Error(s);
|
|
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 : F;
|
|
166
166
|
i && (e.agent = i);
|
|
167
167
|
} else
|
|
168
168
|
e.headers = { Connection: "keep-alive" };
|
|
169
169
|
const s = {
|
|
170
170
|
"X-Request-ID": Date.now().toString(36) + Math.random().toString(36).substring(2)
|
|
171
171
|
};
|
|
172
|
-
if (e.headers && Object.assign(s, e.headers),
|
|
173
|
-
const
|
|
174
|
-
for (const [i, o] of Object.entries(
|
|
172
|
+
if (e.headers && Object.assign(s, e.headers), r.headers) {
|
|
173
|
+
const t = r.headers;
|
|
174
|
+
for (const [i, o] of Object.entries(t))
|
|
175
175
|
(i.toLowerCase() !== "x-api-key" || !this.apiKey) && (s[i] = o);
|
|
176
176
|
}
|
|
177
177
|
return this.apiKey && (s["X-API-Key"] = this.apiKey), this.sessionId && (s["X-Session-ID"] = this.sessionId), {
|
|
178
|
-
...
|
|
178
|
+
...r,
|
|
179
179
|
...e,
|
|
180
180
|
headers: s
|
|
181
181
|
};
|
|
182
182
|
}
|
|
183
183
|
// Create Chat request
|
|
184
|
-
createChatRequest(
|
|
185
|
-
const
|
|
184
|
+
createChatRequest(r, e = !0, s, t, i, o, n, h, u, p, k) {
|
|
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 s && s.length > 0 && (
|
|
191
|
+
return s && s.length > 0 && (c.file_ids = s), t && (c.thread_id = t), i && (c.audio_input = i), o && (c.audio_format = o), n && (c.language = n), h !== void 0 && (c.return_audio = h), u && (c.tts_voice = u), p && (c.source_language = p), k && (c.target_language = k), c;
|
|
192
192
|
}
|
|
193
|
-
async *streamChat(
|
|
194
|
-
var
|
|
193
|
+
async *streamChat(r, e = !0, s, t, i, o, n, h, u, p, k) {
|
|
194
|
+
var c, C;
|
|
195
195
|
try {
|
|
196
|
-
const y = new AbortController(),
|
|
196
|
+
const y = new AbortController(), K = setTimeout(() => y.abort(), 6e4), g = await fetch(`${this.apiUrl}/v1/chat`, {
|
|
197
197
|
...this.getFetchOptions({
|
|
198
198
|
method: "POST",
|
|
199
199
|
headers: {
|
|
@@ -201,26 +201,27 @@ class D {
|
|
|
201
201
|
Accept: e ? "text/event-stream" : "application/json"
|
|
202
202
|
},
|
|
203
203
|
body: JSON.stringify(this.createChatRequest(
|
|
204
|
-
|
|
204
|
+
r,
|
|
205
205
|
e,
|
|
206
206
|
s,
|
|
207
|
-
|
|
207
|
+
t,
|
|
208
208
|
i,
|
|
209
209
|
o,
|
|
210
210
|
n,
|
|
211
211
|
h,
|
|
212
212
|
u,
|
|
213
|
-
|
|
213
|
+
p,
|
|
214
|
+
k
|
|
214
215
|
))
|
|
215
216
|
}),
|
|
216
217
|
signal: y.signal
|
|
217
218
|
});
|
|
218
|
-
if (clearTimeout(
|
|
219
|
-
const w = await
|
|
220
|
-
throw new Error(`Network response was not ok: ${
|
|
219
|
+
if (clearTimeout(K), !g.ok) {
|
|
220
|
+
const w = await g.text();
|
|
221
|
+
throw new Error(`Network response was not ok: ${g.status} ${w}`);
|
|
221
222
|
}
|
|
222
223
|
if (!e) {
|
|
223
|
-
const w = await
|
|
224
|
+
const w = await g.json();
|
|
224
225
|
w.response && (yield {
|
|
225
226
|
text: w.response,
|
|
226
227
|
done: !0,
|
|
@@ -229,71 +230,76 @@ class D {
|
|
|
229
230
|
});
|
|
230
231
|
return;
|
|
231
232
|
}
|
|
232
|
-
const
|
|
233
|
-
if (!
|
|
233
|
+
const P = (c = g.body) == null ? void 0 : c.getReader();
|
|
234
|
+
if (!P) throw new Error("No reader available");
|
|
234
235
|
const O = new TextDecoder();
|
|
235
|
-
let f = "",
|
|
236
|
+
let f = "", v = !1;
|
|
236
237
|
try {
|
|
237
238
|
for (; ; ) {
|
|
238
|
-
const { done: w, value: S } = await
|
|
239
|
+
const { done: w, value: S } = await P.read();
|
|
239
240
|
if (w)
|
|
240
241
|
break;
|
|
241
|
-
const
|
|
242
|
-
f +=
|
|
243
|
-
let
|
|
244
|
-
for (; (
|
|
245
|
-
`,
|
|
246
|
-
const
|
|
247
|
-
if (
|
|
248
|
-
const
|
|
249
|
-
if (!
|
|
242
|
+
const U = O.decode(S, { stream: !0 });
|
|
243
|
+
f += U;
|
|
244
|
+
let E = 0, T;
|
|
245
|
+
for (; (T = f.indexOf(`
|
|
246
|
+
`, E)) !== -1; ) {
|
|
247
|
+
const m = f.slice(E, T).trim();
|
|
248
|
+
if (E = T + 1, m && m.startsWith("data: ")) {
|
|
249
|
+
const d = m.slice(6).trim();
|
|
250
|
+
if (!d || d === "[DONE]") {
|
|
250
251
|
yield { text: "", done: !0 };
|
|
251
252
|
return;
|
|
252
253
|
}
|
|
253
254
|
try {
|
|
254
|
-
const a = JSON.parse(
|
|
255
|
+
const a = JSON.parse(d);
|
|
255
256
|
if (a.error) {
|
|
256
|
-
const
|
|
257
|
-
throw console.warn(`[ApiClient] ${
|
|
257
|
+
const b = `Server error: ${((C = a.error) == null ? void 0 : C.message) || a.error || "Unknown server error"}`;
|
|
258
|
+
throw console.warn(`[ApiClient] ${b}`), new Error(b);
|
|
259
|
+
}
|
|
260
|
+
if (a.done === !0) {
|
|
261
|
+
v = !0, yield {
|
|
262
|
+
text: "",
|
|
263
|
+
done: !0,
|
|
264
|
+
audio: a.audio,
|
|
265
|
+
audioFormat: a.audio_format || a.audioFormat,
|
|
266
|
+
threading: a.threading
|
|
267
|
+
// Pass through threading metadata
|
|
268
|
+
};
|
|
269
|
+
return;
|
|
258
270
|
}
|
|
259
|
-
const
|
|
260
|
-
|
|
271
|
+
const x = a.response || "";
|
|
272
|
+
a.audio_chunk !== void 0 && (yield {
|
|
261
273
|
text: "",
|
|
262
274
|
done: !1,
|
|
263
275
|
audio_chunk: a.audio_chunk,
|
|
264
276
|
audioFormat: a.audioFormat || a.audio_format || "opus",
|
|
265
277
|
chunk_index: a.chunk_index ?? 0
|
|
266
|
-
}), (
|
|
267
|
-
text:
|
|
278
|
+
}), (x || a.audio) && (v = !0, yield {
|
|
279
|
+
text: x,
|
|
268
280
|
done: a.done || !1,
|
|
269
281
|
audio: a.audio,
|
|
270
|
-
audioFormat: a.audio_format || a.audioFormat
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
done: !0,
|
|
275
|
-
audio: a.audio,
|
|
276
|
-
audioFormat: a.audio_format || a.audioFormat
|
|
277
|
-
};
|
|
278
|
-
return;
|
|
279
|
-
}
|
|
282
|
+
audioFormat: a.audio_format || a.audioFormat,
|
|
283
|
+
threading: a.threading
|
|
284
|
+
// Include threading if present
|
|
285
|
+
});
|
|
280
286
|
} catch (a) {
|
|
281
|
-
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:",
|
|
287
|
+
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:", d == null ? void 0 : d.length), console.warn("[ApiClient] JSON text preview (first 200 chars):", d == null ? void 0 : d.substring(0, 200)), console.warn("[ApiClient] JSON text preview (last 200 chars):", d == null ? void 0 : d.substring(d.length - 200));
|
|
282
288
|
}
|
|
283
|
-
} else
|
|
289
|
+
} else m && (v = !0, yield { text: m, done: !1 });
|
|
284
290
|
}
|
|
285
|
-
f = f.slice(
|
|
291
|
+
f = f.slice(E), f.length > 1e6 && (console.warn("[ApiClient] Buffer too large, truncating..."), f = f.slice(-5e5));
|
|
286
292
|
}
|
|
287
|
-
|
|
293
|
+
v && (yield { text: "", done: !0 });
|
|
288
294
|
} finally {
|
|
289
|
-
|
|
295
|
+
P.releaseLock();
|
|
290
296
|
}
|
|
291
297
|
} catch (y) {
|
|
292
298
|
throw y.name === "AbortError" ? new Error("Connection timed out. Please check if the server is running.") : y.name === "TypeError" && y.message.includes("Failed to fetch") ? new Error("Could not connect to the server. Please check if the server is running.") : y;
|
|
293
299
|
}
|
|
294
300
|
}
|
|
295
|
-
async clearConversationHistory(
|
|
296
|
-
const e =
|
|
301
|
+
async clearConversationHistory(r) {
|
|
302
|
+
const e = r || this.sessionId;
|
|
297
303
|
if (!e)
|
|
298
304
|
throw new Error("No session ID provided and no current session available");
|
|
299
305
|
if (!this.apiKey)
|
|
@@ -304,28 +310,28 @@ class D {
|
|
|
304
310
|
"X-API-Key": this.apiKey
|
|
305
311
|
};
|
|
306
312
|
try {
|
|
307
|
-
const
|
|
313
|
+
const t = await fetch(`${this.apiUrl}/admin/chat-history/${e}`, {
|
|
308
314
|
...this.getFetchOptions({
|
|
309
315
|
method: "DELETE",
|
|
310
316
|
headers: s
|
|
311
317
|
})
|
|
312
318
|
});
|
|
313
|
-
if (!
|
|
314
|
-
const o = await
|
|
315
|
-
throw new Error(`Failed to clear conversation history: ${
|
|
319
|
+
if (!t.ok) {
|
|
320
|
+
const o = await t.text();
|
|
321
|
+
throw new Error(`Failed to clear conversation history: ${t.status} ${o}`);
|
|
316
322
|
}
|
|
317
|
-
return await
|
|
318
|
-
} catch (
|
|
319
|
-
throw
|
|
323
|
+
return await t.json();
|
|
324
|
+
} catch (t) {
|
|
325
|
+
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;
|
|
320
326
|
}
|
|
321
327
|
}
|
|
322
|
-
async deleteConversationWithFiles(
|
|
323
|
-
const s =
|
|
328
|
+
async deleteConversationWithFiles(r, e) {
|
|
329
|
+
const s = r || this.sessionId;
|
|
324
330
|
if (!s)
|
|
325
331
|
throw new Error("No session ID provided and no current session available");
|
|
326
332
|
if (!this.apiKey)
|
|
327
333
|
throw new Error("API key is required for deleting conversation");
|
|
328
|
-
const
|
|
334
|
+
const t = {
|
|
329
335
|
"Content-Type": "application/json",
|
|
330
336
|
"X-Session-ID": s,
|
|
331
337
|
"X-API-Key": this.apiKey
|
|
@@ -334,7 +340,7 @@ class D {
|
|
|
334
340
|
const n = await fetch(o, {
|
|
335
341
|
...this.getFetchOptions({
|
|
336
342
|
method: "DELETE",
|
|
337
|
-
headers:
|
|
343
|
+
headers: t
|
|
338
344
|
})
|
|
339
345
|
});
|
|
340
346
|
if (!n.ok) {
|
|
@@ -346,6 +352,99 @@ class D {
|
|
|
346
352
|
throw n.name === "TypeError" && n.message.includes("Failed to fetch") ? new Error("Could not connect to the server. Please check if the server is running.") : n;
|
|
347
353
|
}
|
|
348
354
|
}
|
|
355
|
+
/**
|
|
356
|
+
* Create a conversation thread from a parent message.
|
|
357
|
+
*
|
|
358
|
+
* @param messageId - ID of the parent message
|
|
359
|
+
* @param sessionId - Session ID of the parent conversation
|
|
360
|
+
* @returns Promise resolving to thread information
|
|
361
|
+
* @throws Error if the operation fails
|
|
362
|
+
*/
|
|
363
|
+
async createThread(r, e) {
|
|
364
|
+
if (!this.apiKey)
|
|
365
|
+
throw new Error("API key is required for creating threads");
|
|
366
|
+
const s = {
|
|
367
|
+
"Content-Type": "application/json",
|
|
368
|
+
"X-API-Key": this.apiKey
|
|
369
|
+
};
|
|
370
|
+
try {
|
|
371
|
+
const t = await fetch(`${this.apiUrl}/api/threads`, {
|
|
372
|
+
...this.getFetchOptions({
|
|
373
|
+
method: "POST",
|
|
374
|
+
headers: s,
|
|
375
|
+
body: JSON.stringify({
|
|
376
|
+
message_id: r,
|
|
377
|
+
session_id: e
|
|
378
|
+
})
|
|
379
|
+
})
|
|
380
|
+
});
|
|
381
|
+
if (!t.ok) {
|
|
382
|
+
const o = await t.text();
|
|
383
|
+
throw new Error(`Failed to create thread: ${t.status} ${o}`);
|
|
384
|
+
}
|
|
385
|
+
return await t.json();
|
|
386
|
+
} catch (t) {
|
|
387
|
+
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;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Get thread information by thread ID.
|
|
392
|
+
*
|
|
393
|
+
* @param threadId - Thread identifier
|
|
394
|
+
* @returns Promise resolving to thread information
|
|
395
|
+
* @throws Error if the operation fails
|
|
396
|
+
*/
|
|
397
|
+
async getThreadInfo(r) {
|
|
398
|
+
if (!this.apiKey)
|
|
399
|
+
throw new Error("API key is required for getting thread info");
|
|
400
|
+
const e = {
|
|
401
|
+
"X-API-Key": this.apiKey
|
|
402
|
+
};
|
|
403
|
+
try {
|
|
404
|
+
const s = await fetch(`${this.apiUrl}/api/threads/${r}`, {
|
|
405
|
+
...this.getFetchOptions({
|
|
406
|
+
method: "GET",
|
|
407
|
+
headers: e
|
|
408
|
+
})
|
|
409
|
+
});
|
|
410
|
+
if (!s.ok) {
|
|
411
|
+
const i = await s.text();
|
|
412
|
+
throw new Error(`Failed to get thread info: ${s.status} ${i}`);
|
|
413
|
+
}
|
|
414
|
+
return await s.json();
|
|
415
|
+
} catch (s) {
|
|
416
|
+
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;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* Delete a thread and its associated dataset.
|
|
421
|
+
*
|
|
422
|
+
* @param threadId - Thread identifier
|
|
423
|
+
* @returns Promise resolving to deletion result
|
|
424
|
+
* @throws Error if the operation fails
|
|
425
|
+
*/
|
|
426
|
+
async deleteThread(r) {
|
|
427
|
+
if (!this.apiKey)
|
|
428
|
+
throw new Error("API key is required for deleting threads");
|
|
429
|
+
const e = {
|
|
430
|
+
"X-API-Key": this.apiKey
|
|
431
|
+
};
|
|
432
|
+
try {
|
|
433
|
+
const s = await fetch(`${this.apiUrl}/api/threads/${r}`, {
|
|
434
|
+
...this.getFetchOptions({
|
|
435
|
+
method: "DELETE",
|
|
436
|
+
headers: e
|
|
437
|
+
})
|
|
438
|
+
});
|
|
439
|
+
if (!s.ok) {
|
|
440
|
+
const i = await s.text();
|
|
441
|
+
throw new Error(`Failed to delete thread: ${s.status} ${i}`);
|
|
442
|
+
}
|
|
443
|
+
return await s.json();
|
|
444
|
+
} catch (s) {
|
|
445
|
+
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;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
349
448
|
/**
|
|
350
449
|
* Upload a file for processing and indexing.
|
|
351
450
|
*
|
|
@@ -353,11 +452,11 @@ class D {
|
|
|
353
452
|
* @returns Promise resolving to upload response with file_id
|
|
354
453
|
* @throws Error if upload fails
|
|
355
454
|
*/
|
|
356
|
-
async uploadFile(
|
|
455
|
+
async uploadFile(r) {
|
|
357
456
|
if (!this.apiKey)
|
|
358
457
|
throw new Error("API key is required for file upload");
|
|
359
458
|
const e = new FormData();
|
|
360
|
-
e.append("file",
|
|
459
|
+
e.append("file", r);
|
|
361
460
|
try {
|
|
362
461
|
const s = await fetch(`${this.apiUrl}/api/files/upload`, {
|
|
363
462
|
...this.getFetchOptions({
|
|
@@ -366,8 +465,8 @@ class D {
|
|
|
366
465
|
})
|
|
367
466
|
});
|
|
368
467
|
if (!s.ok) {
|
|
369
|
-
const
|
|
370
|
-
throw new Error(`Failed to upload file: ${s.status} ${
|
|
468
|
+
const t = await s.text();
|
|
469
|
+
throw new Error(`Failed to upload file: ${s.status} ${t}`);
|
|
371
470
|
}
|
|
372
471
|
return await s.json();
|
|
373
472
|
} catch (s) {
|
|
@@ -384,18 +483,18 @@ class D {
|
|
|
384
483
|
if (!this.apiKey)
|
|
385
484
|
throw new Error("API key is required for listing files");
|
|
386
485
|
try {
|
|
387
|
-
const
|
|
486
|
+
const r = await fetch(`${this.apiUrl}/api/files`, {
|
|
388
487
|
...this.getFetchOptions({
|
|
389
488
|
method: "GET"
|
|
390
489
|
})
|
|
391
490
|
});
|
|
392
|
-
if (!
|
|
393
|
-
const e = await
|
|
394
|
-
throw new Error(`Failed to list files: ${
|
|
491
|
+
if (!r.ok) {
|
|
492
|
+
const e = await r.text();
|
|
493
|
+
throw new Error(`Failed to list files: ${r.status} ${e}`);
|
|
395
494
|
}
|
|
396
|
-
return await
|
|
397
|
-
} catch (
|
|
398
|
-
throw
|
|
495
|
+
return await r.json();
|
|
496
|
+
} catch (r) {
|
|
497
|
+
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;
|
|
399
498
|
}
|
|
400
499
|
}
|
|
401
500
|
/**
|
|
@@ -405,11 +504,11 @@ class D {
|
|
|
405
504
|
* @returns Promise resolving to file information
|
|
406
505
|
* @throws Error if file not found or request fails
|
|
407
506
|
*/
|
|
408
|
-
async getFileInfo(
|
|
507
|
+
async getFileInfo(r) {
|
|
409
508
|
if (!this.apiKey)
|
|
410
509
|
throw new Error("API key is required for getting file info");
|
|
411
510
|
try {
|
|
412
|
-
const e = await fetch(`${this.apiUrl}/api/files/${
|
|
511
|
+
const e = await fetch(`${this.apiUrl}/api/files/${r}`, {
|
|
413
512
|
...this.getFetchOptions({
|
|
414
513
|
method: "GET"
|
|
415
514
|
})
|
|
@@ -432,11 +531,11 @@ class D {
|
|
|
432
531
|
* @returns Promise resolving to query results
|
|
433
532
|
* @throws Error if query fails
|
|
434
533
|
*/
|
|
435
|
-
async queryFile(
|
|
534
|
+
async queryFile(r, e, s = 10) {
|
|
436
535
|
if (!this.apiKey)
|
|
437
536
|
throw new Error("API key is required for querying files");
|
|
438
537
|
try {
|
|
439
|
-
const
|
|
538
|
+
const t = await fetch(`${this.apiUrl}/api/files/${r}/query`, {
|
|
440
539
|
...this.getFetchOptions({
|
|
441
540
|
method: "POST",
|
|
442
541
|
headers: {
|
|
@@ -445,13 +544,13 @@ class D {
|
|
|
445
544
|
body: JSON.stringify({ query: e, max_results: s })
|
|
446
545
|
})
|
|
447
546
|
});
|
|
448
|
-
if (!
|
|
449
|
-
const i = await
|
|
450
|
-
throw new Error(`Failed to query file: ${
|
|
547
|
+
if (!t.ok) {
|
|
548
|
+
const i = await t.text();
|
|
549
|
+
throw new Error(`Failed to query file: ${t.status} ${i}`);
|
|
451
550
|
}
|
|
452
|
-
return await
|
|
453
|
-
} catch (
|
|
454
|
-
throw
|
|
551
|
+
return await t.json();
|
|
552
|
+
} catch (t) {
|
|
553
|
+
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;
|
|
455
554
|
}
|
|
456
555
|
}
|
|
457
556
|
/**
|
|
@@ -461,55 +560,56 @@ class D {
|
|
|
461
560
|
* @returns Promise resolving to deletion result
|
|
462
561
|
* @throws Error if deletion fails
|
|
463
562
|
*/
|
|
464
|
-
async deleteFile(
|
|
563
|
+
async deleteFile(r) {
|
|
465
564
|
if (!this.apiKey)
|
|
466
565
|
throw new Error("API key is required for deleting files");
|
|
467
|
-
const e = `${this.apiUrl}/api/files/${
|
|
566
|
+
const e = `${this.apiUrl}/api/files/${r}`, s = this.getFetchOptions({
|
|
468
567
|
method: "DELETE"
|
|
469
568
|
});
|
|
470
569
|
try {
|
|
471
|
-
const
|
|
472
|
-
if (!
|
|
473
|
-
const o = await
|
|
570
|
+
const t = await fetch(e, s);
|
|
571
|
+
if (!t.ok) {
|
|
572
|
+
const o = await t.text();
|
|
474
573
|
let n;
|
|
475
574
|
try {
|
|
476
575
|
const h = JSON.parse(o);
|
|
477
|
-
n = h.detail || h.message || `Failed to delete file (HTTP ${
|
|
576
|
+
n = h.detail || h.message || `Failed to delete file (HTTP ${t.status})`;
|
|
478
577
|
} catch {
|
|
479
|
-
n = `Failed to delete file (HTTP ${
|
|
578
|
+
n = `Failed to delete file (HTTP ${t.status})`;
|
|
480
579
|
}
|
|
481
580
|
throw console.warn(`[ApiClient] ${n}`), new Error(n);
|
|
482
581
|
}
|
|
483
|
-
return await
|
|
484
|
-
} catch (
|
|
582
|
+
return await t.json();
|
|
583
|
+
} catch (t) {
|
|
485
584
|
let i;
|
|
486
|
-
throw
|
|
585
|
+
throw t.name === "TypeError" && t.message.includes("Failed to fetch") ? i = "Could not connect to the server. Please check if the server is running." : t.message && !t.message.includes("Failed to delete file") ? i = t.message : i = "Failed to delete file. Please try again.", console.warn(`[ApiClient] ${i}`), new Error(i);
|
|
487
586
|
}
|
|
488
587
|
}
|
|
489
588
|
}
|
|
490
|
-
let
|
|
491
|
-
const
|
|
492
|
-
|
|
589
|
+
let $ = null;
|
|
590
|
+
const J = (l, r = null, e = null) => {
|
|
591
|
+
$ = new D({ apiUrl: l, apiKey: r, sessionId: e });
|
|
493
592
|
};
|
|
494
|
-
async function*
|
|
495
|
-
if (
|
|
593
|
+
async function* M(l, r = !0, e, s, t, i, o, n, h, u, p) {
|
|
594
|
+
if (!$)
|
|
496
595
|
throw new Error("API not configured. Please call configureApi() with your server URL before using any API functions.");
|
|
497
|
-
yield*
|
|
596
|
+
yield* $.streamChat(
|
|
498
597
|
l,
|
|
499
|
-
|
|
598
|
+
r,
|
|
500
599
|
e,
|
|
501
600
|
s,
|
|
502
|
-
|
|
601
|
+
t,
|
|
503
602
|
i,
|
|
504
603
|
o,
|
|
505
604
|
n,
|
|
506
605
|
h,
|
|
507
|
-
u
|
|
606
|
+
u,
|
|
607
|
+
p
|
|
508
608
|
);
|
|
509
609
|
}
|
|
510
610
|
export {
|
|
511
611
|
D as ApiClient,
|
|
512
|
-
|
|
513
|
-
|
|
612
|
+
J as configureApi,
|
|
613
|
+
M as streamChat
|
|
514
614
|
};
|
|
515
615
|
//# sourceMappingURL=api.mjs.map
|