@artmate/chat
Version:
**开箱即用的AI组件库(基于 Vue3 + ElementPlus)**
118 lines (117 loc) • 4.11 kB
JavaScript
var T = (h) => {
throw TypeError(h);
};
var W = (h, t, s) => t.has(h) || T("Cannot " + s);
var e = (h, t, s) => (W(h, t, "read from private field"), s ? s.call(h) : t.get(h)), c = (h, t, s) => t.has(h) ? T("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(h) : t.set(h, s), o = (h, t, s, i) => (W(h, t, "write to private field"), i ? i.call(h, s) : t.set(h, s), s), j = (h, t, s) => (W(h, t, "access private method"), s);
import { ref as A } from "vue";
var S, m, p, R, v, w, b, k, D, l, g, d, E, X, z;
class I {
constructor({
baseURL: t,
onAbort: s,
onMessage: i,
onError: n,
baseOptions: r,
transformer: a,
type: u,
onFinish: f,
...O
} = {}) {
c(this, E);
c(this, S, null);
c(this, m);
c(this, p);
c(this, R);
c(this, v);
c(this, w);
c(this, b);
c(this, k);
c(this, D, "sse");
c(this, l, null);
c(this, g);
c(this, d, []);
o(this, p, t ?? ""), o(this, R, r ?? {}), s && o(this, v, s), i && o(this, w, i), n && o(this, b, n), f && o(this, g, f), O.onOpen && o(this, k, O.onOpen), a && o(this, m, a), u && o(this, D, u), this.abort = this.abort.bind(this), this.send = this.send.bind(this);
}
send(t, s = {}) {
switch (e(this, D)) {
case "fetch":
j(this, E, X).call(this, t, s);
break;
default:
j(this, E, z).call(this, t, s);
}
return this;
}
abort() {
var t, s, i, n;
(s = (t = e(this, S)) == null ? void 0 : t.close) == null || s.call(t), o(this, S, null), (i = e(this, l)) == null || i.abort(), o(this, l, null), (n = e(this, v)) == null || n.call(this, e(this, d)), o(this, d, []);
}
}
S = new WeakMap(), m = new WeakMap(), p = new WeakMap(), R = new WeakMap(), v = new WeakMap(), w = new WeakMap(), b = new WeakMap(), k = new WeakMap(), D = new WeakMap(), l = new WeakMap(), g = new WeakMap(), d = new WeakMap(), E = new WeakSet(), X = async function(t, s = {}) {
o(this, l, new AbortController());
const i = e(this, l).signal, n = {
...s,
signal: i
};
return fetch(e(this, p) + t, n).then((r) => r.body).then(async (r) => {
var O, x, C, L;
if (!r) return Promise.reject(new Error("Response body is null in stream mode"));
const a = r.getReader(), u = new TextDecoder("utf-8");
let f = !1;
for (; !f; ) {
const { value: P, done: U } = await a.read();
if (f = U, U) {
(O = e(this, g)) == null || O.call(this, e(this, d));
return;
}
if (P) {
const F = u.decode(P, { stream: !0 }), q = F.startsWith("data: ") ? F.slice(6) : F;
try {
const y = e(this, m) ? e(this, m).call(this, q) : q;
e(this, d).push(y), (x = e(this, w)) == null || x.call(this, y);
} catch (y) {
return (C = e(this, b)) == null || C.call(this, y), (L = e(this, l)) == null || L.abort(), Promise.reject(y);
}
}
}
}).catch((r) => {
var a, u, f;
if (r.name === "AbortError") {
(a = e(this, v)) == null || a.call(this, e(this, d));
return;
}
(u = e(this, b)) == null || u.call(this, r), (f = e(this, l)) == null || f.abort();
});
}, z = function(t, s = {}) {
const i = new EventSource(e(this, p) + t, { ...e(this, R), ...s });
return i.onmessage = (n) => {
var a;
const r = e(this, m) ? e(this, m).call(this, n.data) : n;
(a = e(this, w)) == null || a.call(this, r);
}, i.onopen = () => {
var n;
(n = e(this, k)) == null || n.call(this);
}, i.onerror = (n) => {
var r, a;
i.readyState === EventSource.CLOSED ? (r = e(this, g)) == null || r.call(this, e(this, d)) : (a = e(this, b)) == null || a.call(this, i, n), this.abort();
}, o(this, S, i), i;
};
function J({ onAbort: h, sendHandler: t, abortHandler: s } = {}) {
const i = A(!1);
return {
loading: i,
abort: () => {
i.value = !1, s == null || s(), h == null || h();
},
send: (...u) => {
i.value || (t == null || t(...u), i.value = !0);
},
finish: () => {
i.value = !1;
}
};
}
export {
I as XRequest,
J as useSend
};