UNPKG

@artmate/chat

Version:

**开箱即用的AI组件库(基于 Vue3 + ElementPlus)**

81 lines (80 loc) 2.67 kB
import m from "../artStream/index.js"; import w from "./artFetch.js"; class u { constructor(r) { this.create = async (s, e, t) => { var i, h; const n = new AbortController(), o = { method: "POST", body: JSON.stringify({ model: this.model, ...s }), headers: this.defaultHeaders, signal: n.signal }; (i = e == null ? void 0 : e.onStream) == null || i.call(e, n); try { const d = await w(this.baseURL, { fetch: this.customOptions.fetch, ...o }); if (t) { await this.customResponseHandler(d, e, t); return; } const p = d.headers.get("content-type") || ""; switch (p.split(";")[0].trim()) { case "text/event-stream": await this.sseResponseHandler(d, e); break; case "application/json": await this.jsonResponseHandler(d, e); break; default: throw new Error(`The response content-type: ${p} is not support!`); } } catch (d) { const p = d instanceof Error ? d : new Error("Unknown error!"); throw (h = e == null ? void 0 : e.onError) == null || h.call(e, p), p; } }, this.customResponseHandler = async (s, e, t) => { var o, i; const n = []; for await (const h of m({ readableStream: s.body, transformStream: t })) n.push(h), (o = e == null ? void 0 : e.onUpdate) == null || o.call(e, h); (i = e == null ? void 0 : e.onSuccess) == null || i.call(e, n); }, this.sseResponseHandler = async (s, e) => { var o, i; const t = [], n = m({ readableStream: s.body }); for await (const h of n) t.push(h), (o = e == null ? void 0 : e.onUpdate) == null || o.call(e, h); (i = e == null ? void 0 : e.onSuccess) == null || i.call(e, t); }, this.jsonResponseHandler = async (s, e) => { var n, o; const t = await s.json(); (n = e == null ? void 0 : e.onUpdate) == null || n.call(e, t), (o = e == null ? void 0 : e.onSuccess) == null || o.call(e, [t]); }; const { baseURL: f, model: R, dangerouslyApiKey: U, ...y } = r; this.baseURL = r.baseURL, this.model = r.model, this.defaultHeaders = { "Content-Type": "application/json", ...r.dangerouslyApiKey && { Authorization: r.dangerouslyApiKey } }, this.customOptions = y; } static init(r) { if (!r.baseURL || typeof r.baseURL != "string") throw new Error("The baseURL is not valid!"); return new u(r); } } const S = u.init; export { S as default };