@artmate/chat
Version:
**开箱即用的AI组件库(基于 Vue3 + ElementPlus)**
63 lines (61 loc) • 1.57 kB
JavaScript
const l = `
`, m = `
`, u = ":", c = (e) => (e ?? "").trim() !== "";
function h() {
let e = "";
return new TransformStream({
transform(r, o) {
e += r;
const t = e.split(l);
t.slice(0, -1).forEach((n) => {
c(n) && o.enqueue(n);
}), e = t[t.length - 1];
},
flush(r) {
c(e) && r.enqueue(e);
}
});
}
function p() {
return new TransformStream({
transform(e, r) {
const t = e.split(m).reduce((n, s) => {
const a = s.indexOf(u);
if (a === -1)
throw new Error(`键值分隔符 "${u}" 在sse行块中找不到!`);
const i = s.slice(0, a);
if (!c(i)) return n;
const f = s.slice(a + 1);
return { ...n, [i]: f };
}, {});
Object.keys(t).length !== 0 && r.enqueue(t);
}
});
}
function d(e) {
const { readableStream: r, transformStream: o } = e;
if (!(r instanceof ReadableStream))
throw new TypeError("options.readableStream 必须是 readableStream 的实例。");
const t = new TextDecoderStream(), n = o ? (
/**
* Uint8Array binary -> string -> Output
*/
r.pipeThrough(t).pipeThrough(o)
) : (
/**
* Uint8Array binary -> string -> SSE part string -> Default Output {@link SSEOutput}
*/
r.pipeThrough(t).pipeThrough(h()).pipeThrough(p())
);
return n[Symbol.asyncIterator] = async function* () {
const s = this.getReader();
for (; ; ) {
const { done: a, value: i } = await s.read();
if (a) break;
i && (yield i);
}
}, n;
}
export {
d as default
};