@schmitech/chatbot-api 1.0.1 → 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 +51 -2
- package/dist/api.mjs +314 -173
- 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(([
|
|
10
|
+
]).then(([l, r]) => {
|
|
11
11
|
var e, s;
|
|
12
|
-
(e =
|
|
13
|
-
}).catch((
|
|
14
|
-
console.warn("Failed to initialize HTTP agents:",
|
|
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
|
+
}).catch((l) => {
|
|
14
|
+
console.warn("Failed to initialize HTTP agents:", l.message);
|
|
15
15
|
});
|
|
16
|
-
class
|
|
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 x {
|
|
|
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,55 +50,55 @@ class x {
|
|
|
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
|
-
let i,
|
|
63
|
+
let i, o;
|
|
64
64
|
try {
|
|
65
|
-
const
|
|
66
|
-
i =
|
|
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:
|
|
72
|
-
|
|
72
|
+
o = "API key is invalid or expired";
|
|
73
73
|
break;
|
|
74
74
|
case 403:
|
|
75
|
-
|
|
75
|
+
o = "Access denied: API key does not have required permissions";
|
|
76
76
|
break;
|
|
77
77
|
case 404:
|
|
78
|
-
|
|
78
|
+
o = "API key not found";
|
|
79
79
|
break;
|
|
80
80
|
case 503:
|
|
81
|
-
|
|
81
|
+
o = "API key management is not available in inference-only mode";
|
|
82
82
|
break;
|
|
83
83
|
default:
|
|
84
|
-
|
|
84
|
+
o = `Failed to validate API key: ${i}`;
|
|
85
85
|
break;
|
|
86
86
|
}
|
|
87
|
-
throw new Error(
|
|
87
|
+
throw new Error(o);
|
|
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 x {
|
|
|
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,151 +119,187 @@ class x {
|
|
|
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
|
-
let i,
|
|
132
|
+
let i, o;
|
|
133
133
|
try {
|
|
134
|
-
const
|
|
135
|
-
i =
|
|
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:
|
|
141
|
-
|
|
141
|
+
o = "API key is invalid, disabled, or has no associated adapter";
|
|
142
142
|
break;
|
|
143
143
|
case 404:
|
|
144
|
-
|
|
144
|
+
o = "Adapter configuration not found";
|
|
145
145
|
break;
|
|
146
146
|
case 503:
|
|
147
|
-
|
|
147
|
+
o = "Service is not available";
|
|
148
148
|
break;
|
|
149
149
|
default:
|
|
150
|
-
|
|
150
|
+
o = `Failed to get adapter info: ${i}`;
|
|
151
151
|
break;
|
|
152
152
|
}
|
|
153
|
-
throw new Error(
|
|
153
|
+
throw new Error(o);
|
|
154
154
|
}
|
|
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,
|
|
175
|
-
(i.toLowerCase() !== "x-api-key" || !this.apiKey) && (s[i] =
|
|
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
|
+
(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
|
|
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: {
|
|
200
200
|
"Content-Type": "application/json",
|
|
201
201
|
Accept: e ? "text/event-stream" : "application/json"
|
|
202
202
|
},
|
|
203
|
-
body: JSON.stringify(this.createChatRequest(
|
|
203
|
+
body: JSON.stringify(this.createChatRequest(
|
|
204
|
+
r,
|
|
205
|
+
e,
|
|
206
|
+
s,
|
|
207
|
+
t,
|
|
208
|
+
i,
|
|
209
|
+
o,
|
|
210
|
+
n,
|
|
211
|
+
h,
|
|
212
|
+
u,
|
|
213
|
+
p,
|
|
214
|
+
k
|
|
215
|
+
))
|
|
204
216
|
}),
|
|
205
|
-
signal:
|
|
217
|
+
signal: y.signal
|
|
206
218
|
});
|
|
207
|
-
if (clearTimeout(
|
|
208
|
-
const
|
|
209
|
-
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}`);
|
|
210
222
|
}
|
|
211
223
|
if (!e) {
|
|
212
|
-
const
|
|
213
|
-
|
|
214
|
-
text:
|
|
215
|
-
done: !0
|
|
224
|
+
const w = await g.json();
|
|
225
|
+
w.response && (yield {
|
|
226
|
+
text: w.response,
|
|
227
|
+
done: !0,
|
|
228
|
+
audio: w.audio,
|
|
229
|
+
audioFormat: w.audio_format
|
|
216
230
|
});
|
|
217
231
|
return;
|
|
218
232
|
}
|
|
219
|
-
const
|
|
220
|
-
if (!
|
|
221
|
-
const
|
|
222
|
-
let
|
|
233
|
+
const P = (c = g.body) == null ? void 0 : c.getReader();
|
|
234
|
+
if (!P) throw new Error("No reader available");
|
|
235
|
+
const O = new TextDecoder();
|
|
236
|
+
let f = "", v = !1;
|
|
223
237
|
try {
|
|
224
238
|
for (; ; ) {
|
|
225
|
-
const { done:
|
|
226
|
-
if (
|
|
239
|
+
const { done: w, value: S } = await P.read();
|
|
240
|
+
if (w)
|
|
227
241
|
break;
|
|
228
|
-
const
|
|
229
|
-
|
|
230
|
-
let
|
|
231
|
-
for (; (
|
|
232
|
-
`,
|
|
233
|
-
const
|
|
234
|
-
if (
|
|
235
|
-
const
|
|
236
|
-
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]") {
|
|
237
251
|
yield { text: "", done: !0 };
|
|
238
252
|
return;
|
|
239
253
|
}
|
|
240
254
|
try {
|
|
241
|
-
const
|
|
242
|
-
if (
|
|
243
|
-
const
|
|
244
|
-
throw console.warn(`[ApiClient] ${
|
|
255
|
+
const a = JSON.parse(d);
|
|
256
|
+
if (a.error) {
|
|
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);
|
|
245
259
|
}
|
|
246
|
-
if (
|
|
247
|
-
|
|
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
|
+
};
|
|
248
269
|
return;
|
|
249
270
|
}
|
|
250
|
-
|
|
251
|
-
|
|
271
|
+
const x = a.response || "";
|
|
272
|
+
a.audio_chunk !== void 0 && (yield {
|
|
273
|
+
text: "",
|
|
274
|
+
done: !1,
|
|
275
|
+
audio_chunk: a.audio_chunk,
|
|
276
|
+
audioFormat: a.audioFormat || a.audio_format || "opus",
|
|
277
|
+
chunk_index: a.chunk_index ?? 0
|
|
278
|
+
}), (x || a.audio) && (v = !0, yield {
|
|
279
|
+
text: x,
|
|
280
|
+
done: a.done || !1,
|
|
281
|
+
audio: a.audio,
|
|
282
|
+
audioFormat: a.audio_format || a.audioFormat,
|
|
283
|
+
threading: a.threading
|
|
284
|
+
// Include threading if present
|
|
285
|
+
});
|
|
286
|
+
} catch (a) {
|
|
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));
|
|
252
288
|
}
|
|
253
|
-
} else
|
|
289
|
+
} else m && (v = !0, yield { text: m, done: !1 });
|
|
254
290
|
}
|
|
255
|
-
|
|
291
|
+
f = f.slice(E), f.length > 1e6 && (console.warn("[ApiClient] Buffer too large, truncating..."), f = f.slice(-5e5));
|
|
256
292
|
}
|
|
257
|
-
|
|
293
|
+
v && (yield { text: "", done: !0 });
|
|
258
294
|
} finally {
|
|
259
|
-
|
|
295
|
+
P.releaseLock();
|
|
260
296
|
}
|
|
261
|
-
} catch (
|
|
262
|
-
throw
|
|
297
|
+
} catch (y) {
|
|
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;
|
|
263
299
|
}
|
|
264
300
|
}
|
|
265
|
-
async clearConversationHistory(
|
|
266
|
-
const e =
|
|
301
|
+
async clearConversationHistory(r) {
|
|
302
|
+
const e = r || this.sessionId;
|
|
267
303
|
if (!e)
|
|
268
304
|
throw new Error("No session ID provided and no current session available");
|
|
269
305
|
if (!this.apiKey)
|
|
@@ -274,46 +310,139 @@ class x {
|
|
|
274
310
|
"X-API-Key": this.apiKey
|
|
275
311
|
};
|
|
276
312
|
try {
|
|
277
|
-
const
|
|
313
|
+
const t = await fetch(`${this.apiUrl}/admin/chat-history/${e}`, {
|
|
278
314
|
...this.getFetchOptions({
|
|
279
315
|
method: "DELETE",
|
|
280
316
|
headers: s
|
|
281
317
|
})
|
|
282
318
|
});
|
|
283
|
-
if (!
|
|
284
|
-
const
|
|
285
|
-
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}`);
|
|
286
322
|
}
|
|
287
|
-
return await
|
|
288
|
-
} catch (
|
|
289
|
-
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;
|
|
290
326
|
}
|
|
291
327
|
}
|
|
292
|
-
async deleteConversationWithFiles(
|
|
293
|
-
const s =
|
|
328
|
+
async deleteConversationWithFiles(r, e) {
|
|
329
|
+
const s = r || this.sessionId;
|
|
294
330
|
if (!s)
|
|
295
331
|
throw new Error("No session ID provided and no current session available");
|
|
296
332
|
if (!this.apiKey)
|
|
297
333
|
throw new Error("API key is required for deleting conversation");
|
|
298
|
-
const
|
|
334
|
+
const t = {
|
|
299
335
|
"Content-Type": "application/json",
|
|
300
336
|
"X-Session-ID": s,
|
|
301
337
|
"X-API-Key": this.apiKey
|
|
302
|
-
}, i = e && e.length > 0 ? `?file_ids=${e.join(",")}` : "",
|
|
338
|
+
}, i = e && e.length > 0 ? `?file_ids=${e.join(",")}` : "", o = `${this.apiUrl}/admin/conversations/${s}${i}`;
|
|
339
|
+
try {
|
|
340
|
+
const n = await fetch(o, {
|
|
341
|
+
...this.getFetchOptions({
|
|
342
|
+
method: "DELETE",
|
|
343
|
+
headers: t
|
|
344
|
+
})
|
|
345
|
+
});
|
|
346
|
+
if (!n.ok) {
|
|
347
|
+
const u = await n.text();
|
|
348
|
+
throw new Error(`Failed to delete conversation: ${n.status} ${u}`);
|
|
349
|
+
}
|
|
350
|
+
return await n.json();
|
|
351
|
+
} catch (n) {
|
|
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;
|
|
353
|
+
}
|
|
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
|
+
};
|
|
303
432
|
try {
|
|
304
|
-
const
|
|
433
|
+
const s = await fetch(`${this.apiUrl}/api/threads/${r}`, {
|
|
305
434
|
...this.getFetchOptions({
|
|
306
435
|
method: "DELETE",
|
|
307
|
-
headers:
|
|
436
|
+
headers: e
|
|
308
437
|
})
|
|
309
438
|
});
|
|
310
|
-
if (!
|
|
311
|
-
const
|
|
312
|
-
throw new Error(`Failed to delete
|
|
439
|
+
if (!s.ok) {
|
|
440
|
+
const i = await s.text();
|
|
441
|
+
throw new Error(`Failed to delete thread: ${s.status} ${i}`);
|
|
313
442
|
}
|
|
314
|
-
return await
|
|
315
|
-
} catch (
|
|
316
|
-
throw
|
|
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;
|
|
317
446
|
}
|
|
318
447
|
}
|
|
319
448
|
/**
|
|
@@ -323,11 +452,11 @@ class x {
|
|
|
323
452
|
* @returns Promise resolving to upload response with file_id
|
|
324
453
|
* @throws Error if upload fails
|
|
325
454
|
*/
|
|
326
|
-
async uploadFile(
|
|
455
|
+
async uploadFile(r) {
|
|
327
456
|
if (!this.apiKey)
|
|
328
457
|
throw new Error("API key is required for file upload");
|
|
329
458
|
const e = new FormData();
|
|
330
|
-
e.append("file",
|
|
459
|
+
e.append("file", r);
|
|
331
460
|
try {
|
|
332
461
|
const s = await fetch(`${this.apiUrl}/api/files/upload`, {
|
|
333
462
|
...this.getFetchOptions({
|
|
@@ -336,8 +465,8 @@ class x {
|
|
|
336
465
|
})
|
|
337
466
|
});
|
|
338
467
|
if (!s.ok) {
|
|
339
|
-
const
|
|
340
|
-
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}`);
|
|
341
470
|
}
|
|
342
471
|
return await s.json();
|
|
343
472
|
} catch (s) {
|
|
@@ -354,18 +483,18 @@ class x {
|
|
|
354
483
|
if (!this.apiKey)
|
|
355
484
|
throw new Error("API key is required for listing files");
|
|
356
485
|
try {
|
|
357
|
-
const
|
|
486
|
+
const r = await fetch(`${this.apiUrl}/api/files`, {
|
|
358
487
|
...this.getFetchOptions({
|
|
359
488
|
method: "GET"
|
|
360
489
|
})
|
|
361
490
|
});
|
|
362
|
-
if (!
|
|
363
|
-
const e = await
|
|
364
|
-
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}`);
|
|
365
494
|
}
|
|
366
|
-
return await
|
|
367
|
-
} catch (
|
|
368
|
-
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;
|
|
369
498
|
}
|
|
370
499
|
}
|
|
371
500
|
/**
|
|
@@ -375,11 +504,11 @@ class x {
|
|
|
375
504
|
* @returns Promise resolving to file information
|
|
376
505
|
* @throws Error if file not found or request fails
|
|
377
506
|
*/
|
|
378
|
-
async getFileInfo(
|
|
507
|
+
async getFileInfo(r) {
|
|
379
508
|
if (!this.apiKey)
|
|
380
509
|
throw new Error("API key is required for getting file info");
|
|
381
510
|
try {
|
|
382
|
-
const e = await fetch(`${this.apiUrl}/api/files/${
|
|
511
|
+
const e = await fetch(`${this.apiUrl}/api/files/${r}`, {
|
|
383
512
|
...this.getFetchOptions({
|
|
384
513
|
method: "GET"
|
|
385
514
|
})
|
|
@@ -402,11 +531,11 @@ class x {
|
|
|
402
531
|
* @returns Promise resolving to query results
|
|
403
532
|
* @throws Error if query fails
|
|
404
533
|
*/
|
|
405
|
-
async queryFile(
|
|
534
|
+
async queryFile(r, e, s = 10) {
|
|
406
535
|
if (!this.apiKey)
|
|
407
536
|
throw new Error("API key is required for querying files");
|
|
408
537
|
try {
|
|
409
|
-
const
|
|
538
|
+
const t = await fetch(`${this.apiUrl}/api/files/${r}/query`, {
|
|
410
539
|
...this.getFetchOptions({
|
|
411
540
|
method: "POST",
|
|
412
541
|
headers: {
|
|
@@ -415,13 +544,13 @@ class x {
|
|
|
415
544
|
body: JSON.stringify({ query: e, max_results: s })
|
|
416
545
|
})
|
|
417
546
|
});
|
|
418
|
-
if (!
|
|
419
|
-
const i = await
|
|
420
|
-
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}`);
|
|
421
550
|
}
|
|
422
|
-
return await
|
|
423
|
-
} catch (
|
|
424
|
-
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;
|
|
425
554
|
}
|
|
426
555
|
}
|
|
427
556
|
/**
|
|
@@ -431,44 +560,56 @@ class x {
|
|
|
431
560
|
* @returns Promise resolving to deletion result
|
|
432
561
|
* @throws Error if deletion fails
|
|
433
562
|
*/
|
|
434
|
-
async deleteFile(
|
|
563
|
+
async deleteFile(r) {
|
|
435
564
|
if (!this.apiKey)
|
|
436
565
|
throw new Error("API key is required for deleting files");
|
|
437
|
-
const e = `${this.apiUrl}/api/files/${
|
|
566
|
+
const e = `${this.apiUrl}/api/files/${r}`, s = this.getFetchOptions({
|
|
438
567
|
method: "DELETE"
|
|
439
568
|
});
|
|
440
569
|
try {
|
|
441
|
-
const
|
|
442
|
-
if (!
|
|
443
|
-
const
|
|
444
|
-
let
|
|
570
|
+
const t = await fetch(e, s);
|
|
571
|
+
if (!t.ok) {
|
|
572
|
+
const o = await t.text();
|
|
573
|
+
let n;
|
|
445
574
|
try {
|
|
446
|
-
const
|
|
447
|
-
|
|
575
|
+
const h = JSON.parse(o);
|
|
576
|
+
n = h.detail || h.message || `Failed to delete file (HTTP ${t.status})`;
|
|
448
577
|
} catch {
|
|
449
|
-
|
|
578
|
+
n = `Failed to delete file (HTTP ${t.status})`;
|
|
450
579
|
}
|
|
451
|
-
throw console.warn(`[ApiClient] ${
|
|
580
|
+
throw console.warn(`[ApiClient] ${n}`), new Error(n);
|
|
452
581
|
}
|
|
453
|
-
return await
|
|
454
|
-
} catch (
|
|
582
|
+
return await t.json();
|
|
583
|
+
} catch (t) {
|
|
455
584
|
let i;
|
|
456
|
-
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);
|
|
457
586
|
}
|
|
458
587
|
}
|
|
459
588
|
}
|
|
460
|
-
let
|
|
461
|
-
const
|
|
462
|
-
|
|
589
|
+
let $ = null;
|
|
590
|
+
const J = (l, r = null, e = null) => {
|
|
591
|
+
$ = new D({ apiUrl: l, apiKey: r, sessionId: e });
|
|
463
592
|
};
|
|
464
|
-
async function*
|
|
465
|
-
if (
|
|
593
|
+
async function* M(l, r = !0, e, s, t, i, o, n, h, u, p) {
|
|
594
|
+
if (!$)
|
|
466
595
|
throw new Error("API not configured. Please call configureApi() with your server URL before using any API functions.");
|
|
467
|
-
yield*
|
|
596
|
+
yield* $.streamChat(
|
|
597
|
+
l,
|
|
598
|
+
r,
|
|
599
|
+
e,
|
|
600
|
+
s,
|
|
601
|
+
t,
|
|
602
|
+
i,
|
|
603
|
+
o,
|
|
604
|
+
n,
|
|
605
|
+
h,
|
|
606
|
+
u,
|
|
607
|
+
p
|
|
608
|
+
);
|
|
468
609
|
}
|
|
469
610
|
export {
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
611
|
+
D as ApiClient,
|
|
612
|
+
J as configureApi,
|
|
613
|
+
M as streamChat
|
|
473
614
|
};
|
|
474
615
|
//# sourceMappingURL=api.mjs.map
|