UNPKG

@artmate/chat

Version:

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

2 lines (1 loc) 1.98 kB
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const y=require("../artStream/index.js"),f=require("./artFetch.js");class p{constructor(r){this.create=async(d,e,t)=>{var i,u;const n=new AbortController,o={method:"POST",body:JSON.stringify({model:this.model,...d}),headers:this.defaultHeaders,signal:n.signal};(i=e==null?void 0:e.onStream)==null||i.call(e,n);try{const s=await f.default(this.baseURL,{fetch:this.customOptions.fetch,...o});if(t){await this.customResponseHandler(s,e,t);return}const h=s.headers.get("content-type")||"";switch(h.split(";")[0].trim()){case"text/event-stream":await this.sseResponseHandler(s,e);break;case"application/json":await this.jsonResponseHandler(s,e);break;default:throw new Error(`The response content-type: ${h} is not support!`)}}catch(s){const h=s instanceof Error?s:new Error("Unknown error!");throw(u=e==null?void 0:e.onError)==null||u.call(e,h),h}},this.customResponseHandler=async(d,e,t)=>{var o,i;const n=[];for await(const u of y.default({readableStream:d.body,transformStream:t}))n.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,n)},this.sseResponseHandler=async(d,e)=>{var o,i;const t=[],n=y.default({readableStream:d.body});for await(const u of 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.jsonResponseHandler=async(d,e)=>{var n,o;const t=await d.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:R,model:U,dangerouslyApiKey:g,...m}=r;this.baseURL=r.baseURL,this.model=r.model,this.defaultHeaders={"Content-Type":"application/json",...r.dangerouslyApiKey&&{Authorization:r.dangerouslyApiKey}},this.customOptions=m}static init(r){if(!r.baseURL||typeof r.baseURL!="string")throw new Error("The baseURL is not valid!");return new p(r)}}const w=p.init;exports.default=w;