UNPKG

@kitn.ai/ui

Version:

Framework-agnostic, Shadow-DOM web components for building AI chat interfaces — works in React, Vue, Angular, Svelte, or plain HTML. Authored in SolidJS.

118 lines (117 loc) 3.14 kB
function c(t, u) { return [...t, u]; } function f(t, u) { const o = t.findIndex((d) => d.id === u.id); if (o === -1) return [...t, u]; const e = t.slice(); return e[o] = u, e; } function T(t, u, o) { return t.map( (e) => e.id === u ? typeof o == "function" ? o(e) : { ...e, ...o } : e ); } function g(t, u) { return t.filter((o) => o.id !== u); } function I(t, u, o) { return t.map((e) => e.id === u ? { ...e, content: e.content + o } : e); } function x(t, u) { return t.includes(u) ? t.slice() : [...t, u]; } function y(t, u) { return t.filter((o) => o !== u); } function l() { return typeof crypto < "u" && typeof crypto.randomUUID == "function" ? crypto.randomUUID() : "kai-" + Math.random().toString(36).slice(2); } function b(t, u = {}) { const o = u.id ?? l(), e = { id: o, role: "assistant", content: "", ...u }; t((n) => [...n, e]); const d = (n) => t((r) => r.map((a) => a.id === o ? n(a) : a)), i = { id: o, appendText(n) { return d((r) => ({ ...r, content: r.content + n })), i; }, setText(n) { return d((r) => ({ ...r, content: n })), i; }, appendReasoning(n, r) { return d((a) => { var s, p; return { ...a, reasoning: { text: (((s = a.reasoning) == null ? void 0 : s.text) ?? "") + n, label: r ?? ((p = a.reasoning) == null ? void 0 : p.label) } }; }), i; }, setReasoning(n, r) { return d((a) => { var s; return { ...a, reasoning: { text: n, label: r ?? ((s = a.reasoning) == null ? void 0 : s.label) } }; }), i; }, upsertTool(n) { return d((r) => { const a = r.tools ? r.tools.slice() : [], s = n.toolCallId != null ? a.findIndex((p) => p.toolCallId === n.toolCallId) : a.findIndex((p) => p.type === n.type); return s === -1 ? a.push(n) : a[s] = n, { ...r, tools: a }; }), i; }, updateTool(n, r) { return d((a) => ({ ...a, tools: (a.tools ?? []).map((s) => s.toolCallId === n ? { ...s, ...r } : s) })), i; }, patch(n) { return d((r) => ({ ...r, ...n })), i; }, done(n) { n && d((r) => ({ ...r, ...n })); }, abort(n) { n ? d((r) => ({ ...r, tools: (r.tools ?? []).map((a) => ({ ...a, state: "output-error", errorText: n })) })) : t((r) => r.filter((a) => a.id !== o)); } }; return i; } function R(t, u) { const o = { id: t.id, appendText(e) { return t.appendText(e), o; }, setText(e) { return t.setText(e), o; }, appendReasoning(e, d) { return t.appendReasoning(e, d), o; }, setReasoning(e, d) { return t.setReasoning(e, d), o; }, upsertTool(e) { return t.upsertTool(e), o; }, updateTool(e, d) { return t.updateTool(e, d), o; }, patch(e) { return t.patch(e), o; }, done(e) { t.done(e), u(); }, abort(e) { t.abort(e), u(); } }; return o; } export { x as addSuggestion, I as appendContent, c as appendMessage, b as createAssistantStream, R as onStreamSettled, g as removeMessage, y as removeSuggestion, T as updateMessage, f as upsertMessage };