UNPKG

ant-design-x-vue-next

Version:

Ant Design X for Vue — community continuation aligned with @ant-design/x

347 lines (346 loc) 9.86 kB
import { ref as L, watch as me, toValue as k, computed as ee } from "vue"; import pe from "./useSyncState.mjs"; import { useEventCallback as te } from "../_util/hooks/use-event-callback.mjs"; import { getConversationMessages as _, setConversationMessages as K } from "./conversationStore.mjs"; function se(h) { return Array.isArray(h) ? h : [h]; } function he(h) { return h.name === "AbortError" || h.name === "TimeoutError" || h.name === "StreamTimeoutError"; } const I = /* @__PURE__ */ new Map(); function xe(h) { const { defaultMessages: y, agent: O, requestFallback: D, requestPlaceholder: R, parser: V, transformMessage: z, provider: a, transformStream: ne, resolveAbortController: Q, conversationKey: $ } = h, B = L(0), q = L(null), j = L(!1), T = /* @__PURE__ */ new Map(), re = Symbol("ConversationKey"), i = L($ === void 0 ? re : k($)), S = (e) => (e || []).map((t, s) => ({ id: `default_${s}`, status: "local", ...t })), ae = (e) => { if (typeof y == "function") { const t = y({ conversationKey: e }); return t && typeof t.then == "function" ? [] : S(t); } return S(y); }, oe = (e) => { const t = _(e); return t.length > 0 ? t : ae(e); }, [M, b] = pe( oe(i.value), () => { } ), d = (e) => { b((t) => { const s = typeof e == "function" ? e(t) : e; return K(i.value, s), s; }); }; let J = () => { }; const w = (e) => { const t = T.get(e); t != null && t.length && (T.delete(e), t.forEach(({ requestParams: s, opts: n }) => { J(s, n); })); }, N = async (e) => { const t = _(e); if (t.length > 0) { b(t), w(e); return; } if (typeof y != "function") { const n = S(y); b(n), K(e, n), w(e); return; } const s = y({ conversationKey: e }); if (!s || typeof s.then != "function") { const n = S(s); b(n), K(e, n), w(e); return; } j.value = !0; try { const n = await s; if (i.value !== e) return; const c = S(n); b(c), K(e, c); } catch (n) { console.error("[useXChat] defaultMessages load failed", n), i.value === e && b([]); } finally { i.value === e && (j.value = !1, w(e)); } }; if (typeof y == "function" && _(i.value).length === 0) { const e = y({ conversationKey: i.value }); e && typeof e.then == "function" && N(i.value); } me( () => $ === void 0 ? void 0 : k($), (e) => { if (e === void 0 || e === i.value) return; K(i.value, M.value), i.value = e; const t = _(e); if (t.length > 0) { b(t), w(e); return; } N(e); } ); const X = (e, t, s) => { const n = { id: `msg_${B.value}`, message: e, status: t }; return s && (n.extraInfo = s), B.value += 1, n; }, ue = ee(() => { const e = []; return M.value.forEach((t) => { const s = V ? V(t.message) : t.message, n = se(s); n.forEach((c, g) => { let m = t.id; n.length > 1 && (m = `${m}_${g}`), e.push({ id: m, message: c, status: t.status, extraInfo: t.extraInfo }); }); }), e; }), U = (e) => e.filter((t) => t.status !== "loading" && t.status !== "error").map((t) => t.message), A = () => U(M.value); a && a.injectGetMessages(() => A()); const Y = (e) => { const { chunk: t, chunks: s, originMessage: n } = e; if (typeof z == "function") return z(e); if (a) return a.transformMessage(e); if (t) return t; if (Array.isArray(s)) { const c = (s == null ? void 0 : s.length) > 0 ? s == null ? void 0 : s[(s == null ? void 0 : s.length) - 1] : void 0; return n || c; } return s; }, le = ee( () => I.get(i.value) === !0 ), ie = te(() => { var e; if (a != null && a.request) { a.request.abort(), I.set(i.value, !1); return; } (e = q.value) == null || e.abort(); }), ce = (e, t) => { const s = M.value.find((c) => c.id === e); if (!s) return !1; const n = typeof t == "function" ? t(s) : t; return d( (c) => c.map((g) => g.id === e ? { ...g, ...n, id: g.id } : g) ), !0; }, fe = (e) => M.value.some((s) => s.id === e) ? (d((s) => s.filter((n) => n.id !== e)), !0) : !1, W = (e, t) => { const s = !!(a != null && a.hasRequest && a.request); if (!s && !O) throw new Error( "The agent parameter is required when using the onRequest method in an agent generated by useXAgent. Or configure provider with a manual createManualXRequest." ); const { updatingId: n, reload: c, extraInfo: g } = t || {}; let m = null, p, E = [], G = {}; if (e && typeof e == "object" && "message" in e) { const { message: o, ...r } = e; p = o, E = [p], G = r; } else if (a && e && typeof e == "object" && !Array.isArray(e)) { const o = a.transformLocalMessage(e); E = se(o), p = E[E.length - 1] ?? e, G = { ...e }; } else p = e, E = [p]; let C = null; c ? (m = n ?? null, C = n ?? null, d( (o) => o.map((r) => { if (r.id !== n) return r; if (R) { const u = typeof R == "function" ? R(p, { messages: U(o) }) : R; return { ...r, status: "loading", message: u, ...g ? { extraInfo: g } : {} }; } return { ...r, status: "loading", ...g ? { extraInfo: g } : {} }; }) )) : d((o) => { let r = [ ...o, ...E.map((u) => X(u, "local", g)) ]; if (R) { let u; typeof R == "function" ? u = R(p, { messages: U(r) }) : u = R; const f = X(u, "loading"); m = f.id, r = [...r, f]; } return r; }), I.set(i.value, !0); const F = (o, r, u, f) => { let l = M.value.find((v) => v.id === C); if (l) d((v) => v.map((x) => { if (x.id === C) { const P = Y({ originMessage: x.message, chunk: r, chunks: u, status: o, ...f ? { responseHeaders: f } : {} }); return { ...x, message: P, status: o }; } return x; })); else { const v = Y({ chunk: r, status: o, chunks: u, ...f ? { responseHeaders: f } : {} }); l = X(v, o), d((x) => [...x.filter((ve) => ve.id !== m), l]), C = l.id; } return l; }, Z = async (o) => { const r = m, u = C; if (I.set(i.value, !1), he(o)) { d( (f) => f.filter((l) => !(r != null && l.id === r && r !== u)).map( (l) => u != null && l.id === u ? { ...l, status: "abort" } : l ) ), q.value = null; return; } if (D) { let f; typeof D == "function" ? f = await D(p, { error: o, messages: A() }) : f = D, c && u != null ? d( (l) => l.map( (v) => v.id === u ? { ...v, message: f, status: "error" } : v ) ) : d((l) => [ ...l.filter((v) => v.id !== r && v.id !== u), X(f, "error") ]); } else c && u != null ? d( (f) => f.map((l) => l.id === u ? { ...l, status: "error" } : l) ) : d((f) => f.filter((l) => l.id !== r && l.id !== u)); q.value = null; }; if (s && (a != null && a.request)) { a.injectRequest({ onUpdate: (r, u) => F("updating", r, [], u), onSuccess: (r, u) => { F("success", void 0, r, u), I.set(i.value, !1), q.value = null; }, onError: (r) => { Z(r instanceof Error ? r : new Error(String(r))); } }); const o = a.transformParams( e && typeof e == "object" ? e : { message: p }, A(), a.request.options ); a.request.run(o); return; } const H = { message: p, messages: A(), ...G }, de = a ? a.transformParams(H, A()) : H; O.request( de, { onUpdate: (o) => { F("updating", o, []); }, onSuccess: (o) => { F("success", void 0, o), I.set(i.value, !1), q.value = null; }, onError: async (o) => { await Z(o); }, onStream: (o) => { q.value = o, Q == null || Q(o); } }, ne ); }, ge = te( (e, t) => { W(e, t); } ); return J = (e, t) => { W(e, t); }, { onRequest: ge, onReload: (e, t, s) => { if (!O && !(a != null && a.hasRequest && a.request)) throw new Error( "The agent parameter is required when using the onReload method, or configure provider with createManualXRequest." ); const n = M.value.findIndex((g) => g.id === e); if (n < 0) throw new Error(`message [${e}] is not found`); let c = t; if (c === void 0) { const g = [...M.value.slice(0, n)].reverse().find((m) => m.status === "local"); if (!g) throw new Error(`message [${e}] has no previous user message to reload`); c = g.message; } W(c, { updatingId: e, reload: !0, extraInfo: s == null ? void 0 : s.extraInfo }); }, abort: ie, messages: M, parsedMessages: ue, setMessages: d, setMessage: ce, removeMessage: fe, conversationKey: i, isRequesting: le, isDefaultMessagesRequesting: j, queueRequest: (e, t, s) => { const n = T.get(e) || []; n.push({ requestParams: t, opts: s }), T.set(e, n), e === i.value && !j.value && w(e); } }; } export { xe as default };