ant-design-x-vue
Version:
Craft AI-driven interfaces effortlessly
57 lines (56 loc) • 1.47 kB
JavaScript
import p from "../x-request/x-request.mjs";
import { computed as f, ref as q } from "vue";
let a = 0;
class d {
constructor(i) {
this.requestingMap = q({}), this.request = (s, o, g) => {
const { request: n } = this.config, { onUpdate: c, onSuccess: h, onError: l, onStream: u } = o, e = a;
a += 1, this.requestingMap.value[e] = !0, n == null || n(
s,
{
onStream: (t) => {
this.requestingMap.value[e] && (u == null || u(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] && (h(t), this.finishRequest(e));
},
onError: (t) => {
this.requestingMap.value[e] && (l(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 v(r) {
const { request: i, ...s } = r;
return [
f(
() => new d({
// @ts-expect-error
request: i || p({
baseURL: s.baseURL,
model: s.model,
dangerouslyApiKey: s.dangerouslyApiKey
}).create,
...s
})
)
];
}
export {
d as XAgent,
v as default
};