UNPKG

ant-design-x-vue

Version:

Craft AI-driven interfaces effortlessly

84 lines (83 loc) 2.79 kB
import m from "../x-stream/x-stream.mjs"; import w from "./x-fetch.mjs"; import { computed as R, toValue as U } from "vue"; class s { constructor(r) { this.create = async (h, e, n) => { var i, u; const t = new AbortController(), o = { method: "POST", body: JSON.stringify({ model: this.model, ...h }), headers: this.defaultHeaders, signal: t.signal }; (i = e == null ? void 0 : e.onStream) == null || i.call(e, t); try { const d = await w(this.baseURL, { fetch: this.customOptions.fetch, ...o }); if (n) { await this.customResponseHandler(d, e, n); return; } const p = d.headers.get("content-type") || ""; switch (p.split(";")[0].trim()) { /** SSE */ case "text/event-stream": await this.sseResponseHandler(d, e); break; /** JSON */ 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 (u = e == null ? void 0 : e.onError) == null || u.call(e, p), p; } }, this.customResponseHandler = async (h, e, n) => { var o, i; const t = []; for await (const u of m({ readableStream: h.body, transformStream: n })) t.push(u), (o = e == null ? void 0 : e.onUpdate) == null || o.call(e, u); (i = e == null ? void 0 : e.onSuccess) == null || i.call(e, t); }, this.sseResponseHandler = async (h, e) => { var t, o; const n = []; for await (const i of m({ readableStream: h.body })) n.push(i), (t = e == null ? void 0 : e.onUpdate) == null || t.call(e, i); (o = e == null ? void 0 : e.onSuccess) == null || o.call(e, n); }, this.jsonResponseHandler = async (h, e) => { var t, o; const n = await h.json(); (t = e == null ? void 0 : e.onUpdate) == null || t.call(e, n), (o = e == null ? void 0 : e.onSuccess) == null || o.call(e, [n]); }; const { baseURL: g, model: H, dangerouslyApiKey: S, ...f } = r; this.baseURL = r.baseURL, this.model = r.model, this.defaultHeaders = { "Content-Type": "application/json", ...r.dangerouslyApiKey && { Authorization: r.dangerouslyApiKey } }, this.customOptions = f; } static init(r) { if (!r.baseURL || typeof r.baseURL != "string") throw new Error("The baseURL is not valid!"); return new s(r); } } const E = (y) => R(() => s.init(U(y))); export { E as default };