@ant-design-x-vue/x-sdk 0.0.0-stage → 2.0.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/README.md +37 -2
- package/dist/index.d.ts +518 -0
- package/dist/index.js +4 -0
- package/dist/index.mjs +775 -0
- package/package.json +63 -4
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,775 @@
|
|
|
1
|
+
var ee = Object.defineProperty;
|
|
2
|
+
var te = (r, e, t) => e in r ? ee(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t;
|
|
3
|
+
var a = (r, e, t) => te(r, typeof e != "symbol" ? e + "" : e, t);
|
|
4
|
+
import { shallowRef as I, watch as se, ref as N, computed as B, onBeforeUnmount as re } from "vue";
|
|
5
|
+
const b = {
|
|
6
|
+
_chatMessagesStores: /* @__PURE__ */ new Map(),
|
|
7
|
+
get: (r) => b._chatMessagesStores.get(r),
|
|
8
|
+
set: (r, e) => {
|
|
9
|
+
b._chatMessagesStores.set(r, e);
|
|
10
|
+
},
|
|
11
|
+
delete: (r) => {
|
|
12
|
+
b._chatMessagesStores.delete(r);
|
|
13
|
+
},
|
|
14
|
+
getMessages: (r) => {
|
|
15
|
+
const e = b._chatMessagesStores.get(r);
|
|
16
|
+
return e == null ? void 0 : e.getMessages();
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
class ne {
|
|
20
|
+
constructor(e, t) {
|
|
21
|
+
a(this, "messages", []);
|
|
22
|
+
a(this, "listeners", []);
|
|
23
|
+
a(this, "conversationKey");
|
|
24
|
+
a(this, "setMessages", (e) => {
|
|
25
|
+
let t;
|
|
26
|
+
return typeof e == "function" ? t = e(this.messages) : t = e, this.messages = [...t], this.emitListeners(), !0;
|
|
27
|
+
});
|
|
28
|
+
a(this, "getMessages", () => this.messages);
|
|
29
|
+
a(this, "getMessage", (e) => this.messages.find((t) => t.id === e));
|
|
30
|
+
a(this, "addMessage", (e) => this.getMessage(e.id) ? !1 : (this.setMessages([...this.messages, e]), !0));
|
|
31
|
+
a(this, "setMessage", (e, t) => {
|
|
32
|
+
const s = this.getMessage(e);
|
|
33
|
+
if (s) {
|
|
34
|
+
const n = typeof t == "function" ? t(s) : t;
|
|
35
|
+
return Object.assign(s, n), this.setMessages([...this.messages]), !0;
|
|
36
|
+
}
|
|
37
|
+
return !1;
|
|
38
|
+
});
|
|
39
|
+
a(this, "removeMessage", (e) => {
|
|
40
|
+
const t = this.messages.findIndex((s) => s.id === e);
|
|
41
|
+
return t !== -1 ? (this.messages.splice(t, 1), this.setMessages([...this.messages]), !0) : !1;
|
|
42
|
+
});
|
|
43
|
+
a(this, "getSnapshot", () => this.messages);
|
|
44
|
+
a(this, "subscribe", (e) => (this.listeners.push(e), () => {
|
|
45
|
+
this.listeners = this.listeners.filter((t) => t !== e);
|
|
46
|
+
}));
|
|
47
|
+
this.setMessages(e), t != null && (this.conversationKey = t, b.set(this.conversationKey, this));
|
|
48
|
+
}
|
|
49
|
+
emitListeners() {
|
|
50
|
+
this.listeners.forEach((e) => {
|
|
51
|
+
e();
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function oe(r, e) {
|
|
56
|
+
const t = e && typeof e == "object" && "value" in e ? e : I(e), s = (u) => {
|
|
57
|
+
if (u !== void 0 && b.get(u))
|
|
58
|
+
return b.get(u);
|
|
59
|
+
const d = typeof r == "function" ? r() : r;
|
|
60
|
+
return new ne(d || [], u);
|
|
61
|
+
}, n = I(s(t.value)), o = I(n.value.getMessages());
|
|
62
|
+
let i = null;
|
|
63
|
+
const l = () => {
|
|
64
|
+
i && i(), o.value = n.value.getMessages(), i = n.value.subscribe(() => {
|
|
65
|
+
o.value = [...n.value.getMessages()];
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
return l(), se(
|
|
69
|
+
() => t.value,
|
|
70
|
+
() => {
|
|
71
|
+
n.value = s(t.value), l();
|
|
72
|
+
}
|
|
73
|
+
), {
|
|
74
|
+
messages: o,
|
|
75
|
+
addMessage: (u) => n.value.addMessage(u),
|
|
76
|
+
removeMessage: (u) => n.value.removeMessage(u),
|
|
77
|
+
setMessage: (u, d) => n.value.setMessage(u, d),
|
|
78
|
+
getMessage: (u) => n.value.getMessage(u),
|
|
79
|
+
setMessages: (u) => n.value.setMessages(u),
|
|
80
|
+
getMessages: () => n.value.getMessages()
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
function ie(r) {
|
|
84
|
+
return Array.isArray(r) ? r : [r];
|
|
85
|
+
}
|
|
86
|
+
const U = /* @__PURE__ */ new Map();
|
|
87
|
+
function Ce(r) {
|
|
88
|
+
const {
|
|
89
|
+
defaultMessages: e,
|
|
90
|
+
requestFallback: t,
|
|
91
|
+
requestPlaceholder: s,
|
|
92
|
+
parser: n,
|
|
93
|
+
provider: o,
|
|
94
|
+
conversationKey: i
|
|
95
|
+
} = r, l = N(0), u = I(void 0), d = N(!1), f = i && typeof i == "object" && "value" in i ? i : I(i), {
|
|
96
|
+
messages: m,
|
|
97
|
+
setMessages: v,
|
|
98
|
+
getMessages: g,
|
|
99
|
+
setMessage: q
|
|
100
|
+
} = oe(
|
|
101
|
+
() => (e || []).map((h, c) => ({
|
|
102
|
+
id: `default_${c}`,
|
|
103
|
+
status: "local",
|
|
104
|
+
...h
|
|
105
|
+
})),
|
|
106
|
+
f
|
|
107
|
+
), T = (h, c, R) => {
|
|
108
|
+
const _ = {
|
|
109
|
+
id: `msg_${l.value}`,
|
|
110
|
+
message: h,
|
|
111
|
+
status: c
|
|
112
|
+
};
|
|
113
|
+
return R && (_.extraInfo = R), l.value += 1, _;
|
|
114
|
+
}, C = B(() => {
|
|
115
|
+
const h = [];
|
|
116
|
+
return m.value.forEach((c) => {
|
|
117
|
+
const R = n ? n(c.message) : c.message, _ = ie(R);
|
|
118
|
+
_.forEach((x, j) => {
|
|
119
|
+
let K = c.id;
|
|
120
|
+
_.length > 1 && (K = `${K}_${j}`), h.push({
|
|
121
|
+
id: K,
|
|
122
|
+
message: x,
|
|
123
|
+
status: c.status
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
}), h;
|
|
127
|
+
}), w = (h) => h.filter((c) => c.status !== "loading").map((c) => c.message);
|
|
128
|
+
o && (o.injectGetMessages(() => w(g())), u.value = o.request);
|
|
129
|
+
const Q = () => w(g()), X = (h, c) => {
|
|
130
|
+
if (!o) return;
|
|
131
|
+
const { updatingId: R, reload: _ } = c || {};
|
|
132
|
+
let x = null;
|
|
133
|
+
const j = o.transformLocalMessage(h), K = (Array.isArray(j) ? j : [j]).map(
|
|
134
|
+
(M) => T(M, "local", c == null ? void 0 : c.extraInfo)
|
|
135
|
+
);
|
|
136
|
+
_ ? (x = R, v((M) => {
|
|
137
|
+
const p = [...M];
|
|
138
|
+
if (s) {
|
|
139
|
+
let y;
|
|
140
|
+
typeof s == "function" ? y = s(
|
|
141
|
+
h,
|
|
142
|
+
{
|
|
143
|
+
messages: w(p)
|
|
144
|
+
}
|
|
145
|
+
) : y = s, p.forEach((S) => {
|
|
146
|
+
S.id === R && (S.status = "loading", S.message = y, c != null && c.extraInfo && (S.extraInfo = c.extraInfo));
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
return p;
|
|
150
|
+
})) : v((M) => {
|
|
151
|
+
let p = [...M, ...K];
|
|
152
|
+
if (s) {
|
|
153
|
+
let y;
|
|
154
|
+
typeof s == "function" ? y = s(
|
|
155
|
+
h,
|
|
156
|
+
{
|
|
157
|
+
messages: w(p)
|
|
158
|
+
}
|
|
159
|
+
) : y = s;
|
|
160
|
+
const S = T(y, "loading");
|
|
161
|
+
x = S.id, p = [...p, S];
|
|
162
|
+
}
|
|
163
|
+
return p;
|
|
164
|
+
});
|
|
165
|
+
let H = null;
|
|
166
|
+
const P = (M, p, y, S) => {
|
|
167
|
+
let E = g().find((A) => A.id === H);
|
|
168
|
+
if (E)
|
|
169
|
+
v((A) => A.map((O) => {
|
|
170
|
+
if (O.id === H) {
|
|
171
|
+
const $ = o.transformMessage({
|
|
172
|
+
originMessage: O.message,
|
|
173
|
+
chunk: p,
|
|
174
|
+
chunks: y,
|
|
175
|
+
status: M,
|
|
176
|
+
responseHeaders: S
|
|
177
|
+
});
|
|
178
|
+
return {
|
|
179
|
+
...O,
|
|
180
|
+
message: $,
|
|
181
|
+
status: M
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
return O;
|
|
185
|
+
}));
|
|
186
|
+
else if (_ && R)
|
|
187
|
+
E = g().find((A) => A.id === R), E && (E.status = M, E.message = o.transformMessage({ chunk: p, status: M, chunks: y, responseHeaders: S }), v((A) => [...A]), H = E.id);
|
|
188
|
+
else {
|
|
189
|
+
const A = o.transformMessage({
|
|
190
|
+
chunk: p,
|
|
191
|
+
status: M,
|
|
192
|
+
chunks: y,
|
|
193
|
+
responseHeaders: S
|
|
194
|
+
}), O = T(A, M);
|
|
195
|
+
v(($) => [...$.filter((k) => k.id !== x), O]), H = O.id, E = O;
|
|
196
|
+
}
|
|
197
|
+
return E;
|
|
198
|
+
};
|
|
199
|
+
o.injectRequest({
|
|
200
|
+
onUpdate: (M, p) => {
|
|
201
|
+
P("updating", M, [], p);
|
|
202
|
+
},
|
|
203
|
+
onSuccess: (M, p) => {
|
|
204
|
+
d.value = !1, f.value !== void 0 && f.value !== null && U.delete(f.value), P("success", void 0, M, p);
|
|
205
|
+
},
|
|
206
|
+
onError: async (M) => {
|
|
207
|
+
if (d.value = !1, f.value !== void 0 && f.value !== null && U.delete(f.value), t) {
|
|
208
|
+
let p;
|
|
209
|
+
if (typeof t == "function") {
|
|
210
|
+
const y = Q(), S = g().find(
|
|
211
|
+
(E) => E.id === x || E.id === H
|
|
212
|
+
);
|
|
213
|
+
p = await t(h, {
|
|
214
|
+
error: M,
|
|
215
|
+
messageInfo: S,
|
|
216
|
+
messages: y
|
|
217
|
+
});
|
|
218
|
+
} else
|
|
219
|
+
p = t;
|
|
220
|
+
v((y) => [
|
|
221
|
+
...y.filter(
|
|
222
|
+
(S) => S.id !== x && S.id !== H
|
|
223
|
+
),
|
|
224
|
+
T(p, M.name === "AbortError" ? "abort" : "error")
|
|
225
|
+
]);
|
|
226
|
+
} else
|
|
227
|
+
v((p) => p.map((y) => y.id === x || y.id === H ? {
|
|
228
|
+
...y,
|
|
229
|
+
status: M.name === "AbortError" ? "abort" : "error"
|
|
230
|
+
} : y));
|
|
231
|
+
}
|
|
232
|
+
}), d.value = !0, f.value !== void 0 && f.value !== null && U.set(f.value, !0), o.request.run(o.transformParams(h, o.request.options));
|
|
233
|
+
}, V = (h, c) => {
|
|
234
|
+
if (!o)
|
|
235
|
+
throw new Error("provider is required");
|
|
236
|
+
X(h, c);
|
|
237
|
+
}, Y = (h, c, R) => {
|
|
238
|
+
if (!o)
|
|
239
|
+
throw new Error("provider is required");
|
|
240
|
+
if (!h || !g().find((_) => _.id === h))
|
|
241
|
+
throw new Error(`message [${h}] is not found`);
|
|
242
|
+
X(c, {
|
|
243
|
+
updatingId: h,
|
|
244
|
+
reload: !0,
|
|
245
|
+
extraInfo: R == null ? void 0 : R.extraInfo
|
|
246
|
+
});
|
|
247
|
+
}, Z = B(() => {
|
|
248
|
+
const h = f.value;
|
|
249
|
+
return h != null ? U.get(h) || !1 : d.value;
|
|
250
|
+
});
|
|
251
|
+
return {
|
|
252
|
+
onRequest: V,
|
|
253
|
+
messages: m,
|
|
254
|
+
parsedMessages: C,
|
|
255
|
+
setMessages: v,
|
|
256
|
+
setMessage: q,
|
|
257
|
+
abort: () => {
|
|
258
|
+
var h;
|
|
259
|
+
if (!o)
|
|
260
|
+
throw new Error("provider is required");
|
|
261
|
+
(h = u.value) == null || h.abort();
|
|
262
|
+
},
|
|
263
|
+
isRequesting: Z,
|
|
264
|
+
onReload: Y
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
class D {
|
|
268
|
+
constructor(e) {
|
|
269
|
+
a(this, "_request");
|
|
270
|
+
a(this, "_getMessagesFn");
|
|
271
|
+
a(this, "_originalCallbacks");
|
|
272
|
+
var s;
|
|
273
|
+
const t = typeof e.request == "function" ? e.request() : e.request;
|
|
274
|
+
if (!t.manual)
|
|
275
|
+
throw new Error("request must be manual");
|
|
276
|
+
this._request = t, this._originalCallbacks = (s = this._request.options) == null ? void 0 : s.callbacks;
|
|
277
|
+
}
|
|
278
|
+
get request() {
|
|
279
|
+
return this._request;
|
|
280
|
+
}
|
|
281
|
+
getMessages() {
|
|
282
|
+
return this == null ? void 0 : this._getMessagesFn();
|
|
283
|
+
}
|
|
284
|
+
injectGetMessages(e) {
|
|
285
|
+
this._getMessagesFn = e;
|
|
286
|
+
}
|
|
287
|
+
injectRequest({
|
|
288
|
+
onUpdate: e,
|
|
289
|
+
onSuccess: t,
|
|
290
|
+
onError: s
|
|
291
|
+
}) {
|
|
292
|
+
var l, u, d;
|
|
293
|
+
const n = (l = this._originalCallbacks) == null ? void 0 : l.onUpdate, o = (u = this._originalCallbacks) == null ? void 0 : u.onSuccess, i = (d = this._originalCallbacks) == null ? void 0 : d.onError;
|
|
294
|
+
this._request.options.callbacks = {
|
|
295
|
+
onUpdate: (f, m) => {
|
|
296
|
+
e(f, m), n && n(f, m);
|
|
297
|
+
},
|
|
298
|
+
onSuccess: (f, m) => {
|
|
299
|
+
t(f, m), o && o(f, m);
|
|
300
|
+
},
|
|
301
|
+
onError: (f) => {
|
|
302
|
+
s(f), i && i(f);
|
|
303
|
+
}
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
class Re extends D {
|
|
308
|
+
transformParams(e, t) {
|
|
309
|
+
return {
|
|
310
|
+
...(t == null ? void 0 : t.params) || {},
|
|
311
|
+
...e,
|
|
312
|
+
messages: this.getMessages()
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
transformLocalMessage(e) {
|
|
316
|
+
return (e == null ? void 0 : e.messages) || [];
|
|
317
|
+
}
|
|
318
|
+
transformMessage(e) {
|
|
319
|
+
var v, g, q, T;
|
|
320
|
+
const { originMessage: t, chunk: s, chunks: n, responseHeaders: o } = e;
|
|
321
|
+
let i = "", l = "", u = "assistant";
|
|
322
|
+
try {
|
|
323
|
+
let C;
|
|
324
|
+
(v = o.get("content-type")) != null && v.includes("text/event-stream") ? s && ((g = s.data) == null ? void 0 : g.trim()) !== "[DONE]" && (C = JSON.parse(s.data)) : C = s || n[0], C && ((q = C == null ? void 0 : C.choices) == null || q.forEach((w) => {
|
|
325
|
+
w != null && w.delta ? (l = w.delta.reasoning_content || "", i += w.delta.content || "", u = w.delta.role) : w != null && w.message && (l = w.message.reasoning_content || "", i += w.message.content || "", u = w.message.role);
|
|
326
|
+
}));
|
|
327
|
+
} catch (C) {
|
|
328
|
+
console.error("transformMessage error", C);
|
|
329
|
+
}
|
|
330
|
+
let d = "";
|
|
331
|
+
const f = t;
|
|
332
|
+
let m = typeof (f == null ? void 0 : f.content) == "string" ? f == null ? void 0 : f.content : ((T = f == null ? void 0 : f.content) == null ? void 0 : T.text) || "";
|
|
333
|
+
return !m && l ? d = `<think>${l}` : m.includes("<think>") && !m.includes("</think>") && i ? (m = m.replace("<think>", '<think status="done">'), d = `${m}</think>${i}`) : d = `${m || ""}${l}${i}`, {
|
|
334
|
+
content: d,
|
|
335
|
+
role: u || "assistant"
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
class Te extends D {
|
|
340
|
+
transformParams(e, t) {
|
|
341
|
+
if (typeof e != "object")
|
|
342
|
+
throw new Error("requestParams must be an object");
|
|
343
|
+
return {
|
|
344
|
+
...(t == null ? void 0 : t.params) || {},
|
|
345
|
+
...e || {}
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
transformLocalMessage(e) {
|
|
349
|
+
return e;
|
|
350
|
+
}
|
|
351
|
+
transformMessage(e) {
|
|
352
|
+
const { chunk: t, chunks: s, originMessage: n } = e;
|
|
353
|
+
if (t)
|
|
354
|
+
return t;
|
|
355
|
+
if (Array.isArray(s)) {
|
|
356
|
+
const o = (s == null ? void 0 : s.length) > 0 ? s == null ? void 0 : s[(s == null ? void 0 : s.length) - 1] : void 0;
|
|
357
|
+
return n || o;
|
|
358
|
+
}
|
|
359
|
+
return s;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
class ae extends D {
|
|
363
|
+
transformParams(e, t) {
|
|
364
|
+
return {
|
|
365
|
+
...(t == null ? void 0 : t.params) || {},
|
|
366
|
+
...e,
|
|
367
|
+
messages: this.getMessages()
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
transformLocalMessage(e) {
|
|
371
|
+
return (e == null ? void 0 : e.messages) || [];
|
|
372
|
+
}
|
|
373
|
+
transformMessage(e) {
|
|
374
|
+
var d, f, m;
|
|
375
|
+
const { originMessage: t, chunk: s, chunks: n, responseHeaders: o } = e;
|
|
376
|
+
let i = "", l = "assistant";
|
|
377
|
+
try {
|
|
378
|
+
let v;
|
|
379
|
+
(d = o.get("content-type")) != null && d.includes("text/event-stream") ? s && ((f = s.data) == null ? void 0 : f.trim()) !== "[DONE]" && (v = JSON.parse(s.data)) : v = s || n[0], v && ((m = v == null ? void 0 : v.choices) == null || m.forEach((g) => {
|
|
380
|
+
g != null && g.delta ? (i += g.delta.content || "", l = g.delta.role || "assistant") : g != null && g.message && (i += g.message.content || "", l = g.message.role || "assistant");
|
|
381
|
+
}));
|
|
382
|
+
} catch (v) {
|
|
383
|
+
console.error("transformMessage error", v);
|
|
384
|
+
}
|
|
385
|
+
return {
|
|
386
|
+
content: `${(t == null ? void 0 : t.content) || ""}${i || ""}`,
|
|
387
|
+
role: l
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
const ue = `
|
|
392
|
+
|
|
393
|
+
`, fe = `
|
|
394
|
+
`, G = ":", F = (r) => (r ?? "").trim() !== "";
|
|
395
|
+
function le() {
|
|
396
|
+
let r = "";
|
|
397
|
+
return new TransformStream({
|
|
398
|
+
transform(e, t) {
|
|
399
|
+
r += e;
|
|
400
|
+
const s = r.split(ue);
|
|
401
|
+
s.slice(0, -1).forEach((n) => {
|
|
402
|
+
F(n) && t.enqueue(n);
|
|
403
|
+
}), r = s[s.length - 1];
|
|
404
|
+
},
|
|
405
|
+
flush(e) {
|
|
406
|
+
F(r) && e.enqueue(r);
|
|
407
|
+
}
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
function ge() {
|
|
411
|
+
return new TransformStream({
|
|
412
|
+
transform(r, e) {
|
|
413
|
+
const s = r.split(fe).reduce((n, o) => {
|
|
414
|
+
const i = o.indexOf(G);
|
|
415
|
+
if (i === -1)
|
|
416
|
+
throw new Error(
|
|
417
|
+
`The key-value separator "${G}" is not found in the sse line chunk!`
|
|
418
|
+
);
|
|
419
|
+
const l = o.slice(0, i);
|
|
420
|
+
if (!F(l)) return n;
|
|
421
|
+
const u = o.slice(i + 1);
|
|
422
|
+
return { ...n, [l]: u };
|
|
423
|
+
}, {});
|
|
424
|
+
Object.keys(s).length !== 0 && e.enqueue(s);
|
|
425
|
+
}
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
function J(r) {
|
|
429
|
+
const { readableStream: e, transformStream: t } = r;
|
|
430
|
+
if (!(e instanceof ReadableStream))
|
|
431
|
+
throw new Error("The options.readableStream must be an instance of ReadableStream.");
|
|
432
|
+
const s = new TextDecoderStream(), n = t ? e.pipeThrough(s).pipeThrough(t) : e.pipeThrough(s).pipeThrough(le()).pipeThrough(ge());
|
|
433
|
+
return n[Symbol.asyncIterator] = async function* () {
|
|
434
|
+
const o = this.getReader();
|
|
435
|
+
for (; ; ) {
|
|
436
|
+
const { done: i, value: l } = await o.read();
|
|
437
|
+
if (i) break;
|
|
438
|
+
l && (yield l);
|
|
439
|
+
}
|
|
440
|
+
}, n;
|
|
441
|
+
}
|
|
442
|
+
const de = async (r, e) => {
|
|
443
|
+
const { fetch: t = globalThis.fetch, middlewares: s = {}, ...n } = e;
|
|
444
|
+
if (typeof t != "function")
|
|
445
|
+
throw new Error("The options.fetch must be a typeof fetch function!");
|
|
446
|
+
let o = [
|
|
447
|
+
r,
|
|
448
|
+
n
|
|
449
|
+
];
|
|
450
|
+
typeof s.onRequest == "function" && (o = await s.onRequest(...o));
|
|
451
|
+
let i = await t(...o);
|
|
452
|
+
if (typeof s.onResponse == "function") {
|
|
453
|
+
const l = await s.onResponse(i);
|
|
454
|
+
if (!(l instanceof Response))
|
|
455
|
+
throw new Error("The options.onResponse must return a Response instance!");
|
|
456
|
+
i = l;
|
|
457
|
+
}
|
|
458
|
+
if (!i.ok)
|
|
459
|
+
throw new Error(`Fetch failed with status ${i.status}`);
|
|
460
|
+
if (!i.body)
|
|
461
|
+
throw new Error("The response body is empty.");
|
|
462
|
+
return i;
|
|
463
|
+
}, z = {
|
|
464
|
+
manual: !1,
|
|
465
|
+
headers: {
|
|
466
|
+
"Content-Type": "application/json"
|
|
467
|
+
}
|
|
468
|
+
};
|
|
469
|
+
function Ee(r) {
|
|
470
|
+
Object.assign(z, r);
|
|
471
|
+
}
|
|
472
|
+
class he {
|
|
473
|
+
constructor(e, t) {
|
|
474
|
+
a(this, "baseURL");
|
|
475
|
+
a(this, "options");
|
|
476
|
+
if (!e || typeof e != "string") throw new Error("The baseURL is not valid!");
|
|
477
|
+
this.baseURL = e, this.options = t || {};
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
class me extends he {
|
|
481
|
+
constructor(t, s) {
|
|
482
|
+
super(t, s);
|
|
483
|
+
a(this, "_asyncHandler");
|
|
484
|
+
a(this, "timeoutHandler", 0);
|
|
485
|
+
a(this, "_isTimeout", !1);
|
|
486
|
+
a(this, "streamTimeoutHandler", 0);
|
|
487
|
+
a(this, "_isStreamTimeout", !1);
|
|
488
|
+
a(this, "abortController");
|
|
489
|
+
a(this, "_isRequesting", !1);
|
|
490
|
+
a(this, "_manual", !1);
|
|
491
|
+
a(this, "customResponseHandler", async (t, s, n, o) => {
|
|
492
|
+
const i = J({
|
|
493
|
+
readableStream: t.body,
|
|
494
|
+
transformStream: n
|
|
495
|
+
});
|
|
496
|
+
await this.processStream(i, t, s, o);
|
|
497
|
+
});
|
|
498
|
+
a(this, "sseResponseHandler", async (t, s, n) => {
|
|
499
|
+
const o = J({
|
|
500
|
+
readableStream: t.body
|
|
501
|
+
});
|
|
502
|
+
await this.processStream(o, t, s, n);
|
|
503
|
+
});
|
|
504
|
+
a(this, "jsonResponseHandler", async (t, s) => {
|
|
505
|
+
var o, i, l;
|
|
506
|
+
const n = await t.json();
|
|
507
|
+
if ((n == null ? void 0 : n.success) === !1) {
|
|
508
|
+
const u = n, d = new Error(u.message || "System error");
|
|
509
|
+
d.name = u.name || "SystemError", (o = s == null ? void 0 : s.onError) == null || o.call(s, d);
|
|
510
|
+
} else
|
|
511
|
+
(i = s == null ? void 0 : s.onUpdate) == null || i.call(s, n, t.headers), this.finishRequest(), (l = s == null ? void 0 : s.onSuccess) == null || l.call(s, [n], t.headers);
|
|
512
|
+
});
|
|
513
|
+
this._manual = (s == null ? void 0 : s.manual) || !1, this.manual || this.init();
|
|
514
|
+
}
|
|
515
|
+
get asyncHandler() {
|
|
516
|
+
return this._asyncHandler;
|
|
517
|
+
}
|
|
518
|
+
get isTimeout() {
|
|
519
|
+
return this._isTimeout;
|
|
520
|
+
}
|
|
521
|
+
set isTimeout(t) {
|
|
522
|
+
this._isTimeout = t;
|
|
523
|
+
}
|
|
524
|
+
get isStreamTimeout() {
|
|
525
|
+
return this._isStreamTimeout;
|
|
526
|
+
}
|
|
527
|
+
set isStreamTimeout(t) {
|
|
528
|
+
this._isStreamTimeout = t;
|
|
529
|
+
}
|
|
530
|
+
get isRequesting() {
|
|
531
|
+
return this._isRequesting;
|
|
532
|
+
}
|
|
533
|
+
get manual() {
|
|
534
|
+
return this._manual;
|
|
535
|
+
}
|
|
536
|
+
run(t) {
|
|
537
|
+
this.manual ? this.init(t) : console.warn("The request is not manual, so it cannot be run!");
|
|
538
|
+
}
|
|
539
|
+
abort() {
|
|
540
|
+
var t;
|
|
541
|
+
clearTimeout(this.timeoutHandler), clearTimeout(this.streamTimeoutHandler), (t = this.abortController) == null || t.abort();
|
|
542
|
+
}
|
|
543
|
+
init(t) {
|
|
544
|
+
this.abortController = new AbortController();
|
|
545
|
+
const {
|
|
546
|
+
callbacks: s,
|
|
547
|
+
params: n,
|
|
548
|
+
headers: o = {},
|
|
549
|
+
transformStream: i,
|
|
550
|
+
fetch: l,
|
|
551
|
+
timeout: u,
|
|
552
|
+
streamTimeout: d,
|
|
553
|
+
middlewares: f,
|
|
554
|
+
...m
|
|
555
|
+
} = this.options, v = {
|
|
556
|
+
...m,
|
|
557
|
+
method: m.method || "POST",
|
|
558
|
+
body: m.body ?? JSON.stringify({
|
|
559
|
+
...n,
|
|
560
|
+
...t || {}
|
|
561
|
+
}),
|
|
562
|
+
params: {
|
|
563
|
+
...n,
|
|
564
|
+
...t
|
|
565
|
+
},
|
|
566
|
+
headers: Object.assign({}, z.headers || {}, o),
|
|
567
|
+
signal: this.abortController.signal,
|
|
568
|
+
middlewares: f
|
|
569
|
+
};
|
|
570
|
+
u && u > 0 && (this.timeoutHandler = setTimeout(() => {
|
|
571
|
+
var g;
|
|
572
|
+
this.isTimeout = !0, this.finishRequest(), (g = s == null ? void 0 : s.onError) == null || g.call(s, new Error("TimeoutError"));
|
|
573
|
+
}, u)), this.startRequest(), this._asyncHandler = de(this.baseURL, {
|
|
574
|
+
fetch: l,
|
|
575
|
+
...v
|
|
576
|
+
}).then(async (g) => {
|
|
577
|
+
if (clearTimeout(this.timeoutHandler), this.isTimeout) return;
|
|
578
|
+
if (i) {
|
|
579
|
+
let C = i;
|
|
580
|
+
typeof i == "function" && (C = i(this.baseURL, g.headers)), await this.customResponseHandler(g, s, C, d);
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
const q = g.headers.get("content-type") || "";
|
|
584
|
+
switch (q.split(";")[0].trim()) {
|
|
585
|
+
case "text/event-stream":
|
|
586
|
+
await this.sseResponseHandler(g, s, d);
|
|
587
|
+
break;
|
|
588
|
+
case "application/json":
|
|
589
|
+
await this.jsonResponseHandler(g, s);
|
|
590
|
+
break;
|
|
591
|
+
default:
|
|
592
|
+
throw new Error(`The response content-type: ${q} is not support!`);
|
|
593
|
+
}
|
|
594
|
+
}).catch((g) => {
|
|
595
|
+
var T;
|
|
596
|
+
clearTimeout(this.timeoutHandler), this.finishRequest();
|
|
597
|
+
const q = g instanceof Error || typeof DOMException < "u" && g instanceof DOMException ? g : new Error("Unknown error!");
|
|
598
|
+
(T = s == null ? void 0 : s.onError) == null || T.call(s, q);
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
startRequest() {
|
|
602
|
+
this._isRequesting = !0;
|
|
603
|
+
}
|
|
604
|
+
finishRequest() {
|
|
605
|
+
this._isRequesting = !1;
|
|
606
|
+
}
|
|
607
|
+
async processStream(t, s, n, o) {
|
|
608
|
+
var d, f;
|
|
609
|
+
const i = [], l = t[Symbol.asyncIterator]();
|
|
610
|
+
let u;
|
|
611
|
+
do
|
|
612
|
+
if (o && (this.streamTimeoutHandler = setTimeout(() => {
|
|
613
|
+
var m;
|
|
614
|
+
this.isStreamTimeout = !0, this.finishRequest(), (m = n == null ? void 0 : n.onError) == null || m.call(n, new Error("StreamTimeoutError"));
|
|
615
|
+
}, o)), u = await l.next(), u.value !== void 0 && (i.push(u.value), (d = n == null ? void 0 : n.onUpdate) == null || d.call(n, u.value, s.headers)), clearTimeout(this.streamTimeoutHandler), this.isStreamTimeout)
|
|
616
|
+
break;
|
|
617
|
+
while (!u.done);
|
|
618
|
+
if (o && (clearTimeout(this.streamTimeoutHandler), this.isStreamTimeout)) {
|
|
619
|
+
this.finishRequest();
|
|
620
|
+
return;
|
|
621
|
+
}
|
|
622
|
+
this.finishRequest(), (f = n == null ? void 0 : n.onSuccess) == null || f.call(n, i, s.headers);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
function W(r, e) {
|
|
626
|
+
return new me(r, e);
|
|
627
|
+
}
|
|
628
|
+
function ce(r) {
|
|
629
|
+
const e = r.dangerouslyApiKey || r.key;
|
|
630
|
+
if (e)
|
|
631
|
+
return /^(Bearer |Basic |Tbox-)/.test(e) ? e : `Bearer ${e}`;
|
|
632
|
+
}
|
|
633
|
+
function _e(r) {
|
|
634
|
+
const e = ce(r);
|
|
635
|
+
return new ae({
|
|
636
|
+
request: W(r.baseURL, {
|
|
637
|
+
manual: !0,
|
|
638
|
+
params: {
|
|
639
|
+
...r.model ? { model: r.model } : {},
|
|
640
|
+
stream: !0
|
|
641
|
+
},
|
|
642
|
+
headers: e ? { Authorization: e } : {},
|
|
643
|
+
// `fetch` is invariant in XRequest's generic options; the preset only needs the body.
|
|
644
|
+
...r.fetch ? { fetch: r.fetch } : {}
|
|
645
|
+
})
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
const L = {
|
|
649
|
+
_allConversationStores: /* @__PURE__ */ new Map(),
|
|
650
|
+
set(r, e) {
|
|
651
|
+
L._allConversationStores.set(r, e);
|
|
652
|
+
},
|
|
653
|
+
delete(r) {
|
|
654
|
+
L._allConversationStores.delete(r);
|
|
655
|
+
},
|
|
656
|
+
getConversation(r) {
|
|
657
|
+
for (const e of Array.from(
|
|
658
|
+
L._allConversationStores.values()
|
|
659
|
+
)) {
|
|
660
|
+
const t = e.getConversation(r);
|
|
661
|
+
if (t) return t;
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
};
|
|
665
|
+
class ve {
|
|
666
|
+
constructor(e, t) {
|
|
667
|
+
a(this, "conversations", []);
|
|
668
|
+
a(this, "listeners", []);
|
|
669
|
+
a(this, "storeKey");
|
|
670
|
+
a(this, "activeConversationKey");
|
|
671
|
+
a(this, "setActiveConversationKey", (e) => (this.activeConversationKey = e, this.emitListeners(), !0));
|
|
672
|
+
a(this, "setConversations", (e) => (this.conversations = [...e], this.emitListeners(), !0));
|
|
673
|
+
a(this, "getConversation", (e) => this.conversations.find((t) => t.key === e));
|
|
674
|
+
a(this, "addConversation", (e, t) => this.getConversation(e.key) ? !1 : (this.setConversations(
|
|
675
|
+
t === "prepend" ? [e, ...this.conversations] : [...this.conversations, e]
|
|
676
|
+
), !0));
|
|
677
|
+
a(this, "setConversation", (e, t) => {
|
|
678
|
+
const s = this.getConversation(e);
|
|
679
|
+
return s ? (Object.assign(s, t), this.setConversations([...this.conversations]), !0) : !1;
|
|
680
|
+
});
|
|
681
|
+
a(this, "removeConversation", (e) => {
|
|
682
|
+
const t = this.conversations.findIndex((s) => s.key === e);
|
|
683
|
+
return t !== -1 ? (this.conversations.splice(t, 1), this.setConversations([...this.conversations]), !0) : !1;
|
|
684
|
+
});
|
|
685
|
+
a(this, "getMessages", (e) => b.getMessages(e));
|
|
686
|
+
a(this, "getSnapshot", () => this.conversations);
|
|
687
|
+
a(this, "getActiveConversationKey", () => this.activeConversationKey);
|
|
688
|
+
a(this, "subscribe", (e) => (this.listeners.push(e), () => {
|
|
689
|
+
this.listeners = this.listeners.filter((t) => t !== e);
|
|
690
|
+
}));
|
|
691
|
+
a(this, "destroy", () => {
|
|
692
|
+
L.delete(this.storeKey);
|
|
693
|
+
});
|
|
694
|
+
this.conversations = [...e], this.storeKey = Math.random().toString(), L.set(this.storeKey, this), this.activeConversationKey = t;
|
|
695
|
+
}
|
|
696
|
+
emitListeners() {
|
|
697
|
+
for (const e of this.listeners) e();
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
function qe(r = {}) {
|
|
701
|
+
const e = new ve(
|
|
702
|
+
r.defaultConversations || [],
|
|
703
|
+
r.defaultActiveConversationKey || ""
|
|
704
|
+
), t = I(e.getSnapshot()), s = I(e.getActiveConversationKey()), n = e.subscribe(() => {
|
|
705
|
+
t.value = [...e.getSnapshot()], s.value = e.getActiveConversationKey();
|
|
706
|
+
});
|
|
707
|
+
return re(() => {
|
|
708
|
+
n(), e.destroy();
|
|
709
|
+
}), {
|
|
710
|
+
conversations: t,
|
|
711
|
+
activeConversationKey: s,
|
|
712
|
+
setActiveConversationKey: e.setActiveConversationKey,
|
|
713
|
+
addConversation: e.addConversation,
|
|
714
|
+
removeConversation: e.removeConversation,
|
|
715
|
+
setConversation: e.setConversation,
|
|
716
|
+
getConversation: e.getConversation,
|
|
717
|
+
setConversations: e.setConversations,
|
|
718
|
+
getMessages: e.getMessages
|
|
719
|
+
};
|
|
720
|
+
}
|
|
721
|
+
class pe {
|
|
722
|
+
constructor(e, t) {
|
|
723
|
+
a(this, "baseURL");
|
|
724
|
+
a(this, "options");
|
|
725
|
+
if (!e || typeof e != "string")
|
|
726
|
+
throw new Error("The baseURL is not valid!");
|
|
727
|
+
this.baseURL = e, this.options = t;
|
|
728
|
+
}
|
|
729
|
+
/**
|
|
730
|
+
* Fetch the list of tools published by the MCP server.
|
|
731
|
+
*/
|
|
732
|
+
async tools() {
|
|
733
|
+
return new Promise((e, t) => {
|
|
734
|
+
W(this.baseURL, {
|
|
735
|
+
...this.options || {},
|
|
736
|
+
callbacks: {
|
|
737
|
+
onSuccess(s) {
|
|
738
|
+
e((s == null ? void 0 : s[0]) || []);
|
|
739
|
+
},
|
|
740
|
+
onError(s) {
|
|
741
|
+
t(s);
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
});
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
function Me(r, e) {
|
|
749
|
+
return new pe(r, e);
|
|
750
|
+
}
|
|
751
|
+
const Ae = Me, xe = "2.0.0";
|
|
752
|
+
export {
|
|
753
|
+
D as AbstractChatProvider,
|
|
754
|
+
he as AbstractXRequestClass,
|
|
755
|
+
ne as ChatMessagesStore,
|
|
756
|
+
ve as ConversationStore,
|
|
757
|
+
Re as DeepSeekChatProvider,
|
|
758
|
+
Te as DefaultChatProvider,
|
|
759
|
+
ae as OpenAIChatProvider,
|
|
760
|
+
Me as XMCPClient,
|
|
761
|
+
pe as XMCPClientClass,
|
|
762
|
+
Ae as XMcpClient,
|
|
763
|
+
W as XRequest,
|
|
764
|
+
me as XRequestClass,
|
|
765
|
+
J as XStream,
|
|
766
|
+
b as chatMessagesStoreHelper,
|
|
767
|
+
L as conversationStoreHelper,
|
|
768
|
+
_e as createOpenAIChatProvider,
|
|
769
|
+
ce as resolvePresetApiKey,
|
|
770
|
+
Ee as setXRequestGlobalOptions,
|
|
771
|
+
oe as useChatStore,
|
|
772
|
+
Ce as useXChat,
|
|
773
|
+
qe as useXConversations,
|
|
774
|
+
xe as version
|
|
775
|
+
};
|