UNPKG

jadq-x-vue

Version:

Craft AI-driven interfaces effortlessly

59 lines (58 loc) 1.53 kB
import p from "../x-request/x-request.mjs"; import { computed as f, toValue as q, ref as d } from "vue"; let a = 0; class v { constructor(i) { this.requestingMap = d({}), this.request = (n, s, g) => { const { request: u } = this.config, { onUpdate: c, onSuccess: l, onError: h, onStream: r } = s, e = a; a += 1, this.requestingMap.value[e] = !0, u == null || u( n, { onStream: (t) => { this.requestingMap.value[e] && (r == null || r(t)); }, // Status should be unique. // One get success or error should not get more message onUpdate: (t) => { this.requestingMap.value[e] && c(t); }, onSuccess: (t) => { this.requestingMap.value[e] && (l(t), this.finishRequest(e)); }, onError: (t) => { this.requestingMap.value[e] && (h(t), this.finishRequest(e)); } }, g ); }, this.config = i; } finishRequest(i) { delete this.requestingMap.value[i]; } isRequesting() { return Object.keys(this.requestingMap.value).length > 0; } } function R(o) { return [ f( () => { const { request: n, ...s } = q(o); return new v({ // request: n || p({ baseURL: s.baseURL, model: s.model, dangerouslyApiKey: s.dangerouslyApiKey }).value.create, ...s }); } ) ]; } export { v as XAgent, R as default };