UNPKG

@artmate/chat

Version:

借鉴字节开源react库AntX,通过vue实现的版本

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