UNPKG

mongodb-chatbot-ui

Version:

UI React components for the MongoDB Assistant

276 lines (275 loc) 6.45 kB
import { jsx as b } from "react/jsx-runtime"; import { createContext as S, useState as A } from "react"; import { r as E } from "./utils.js"; function R() { const t = Date.now(), n = Math.floor(Math.random() * 1e5); return String(t + n); } function M(t) { return { id: t.id ?? R(), createdAt: (/* @__PURE__ */ new Date()).toISOString(), ...t }; } const w = (t) => { let n; const r = /* @__PURE__ */ new Set(), e = (c, f) => { const m = typeof c == "function" ? c(n) : c; if (!Object.is(m, n)) { const p = n; n = f ?? (typeof m != "object" || m === null) ? m : Object.assign({}, n, m), r.forEach((C) => C(n, p)); } }, s = () => n, l = { setState: e, getState: s, getInitialState: () => I, subscribe: (c) => (r.add(c), () => r.delete(c)) }, I = n = t(e, s, l); return l; }, j = (t) => t ? w(t) : w; function y(t, n) { return (...r) => Object.assign({}, t, n(...r)); } const h = { conversationId: void 0, error: void 0, messages: [], streamingMessageId: void 0 }; function i(t, n, r) { return t.map( (e) => e.id === n ? r(e) : e ); } const g = "streaming-response"; function d(t) { const n = t.findIndex( (e) => e.id === g ), r = n === -1 ? null : t[n]; return { index: n, data: r }; } const O = (t = "default") => j( y(h, (n, r) => ({ name: t, api: { initialize: (e) => { n(e); }, setConversationId: (e) => { e !== "" && (r().conversationId || n((s) => ({ ...s, conversationId: e }))); }, setConversationName: (e) => { n((s) => ({ ...s, conversationName: e })); }, setConversationError: (e) => { n((s) => ({ ...s, error: e })); }, addMessage: (e) => { n((s) => ({ ...s, messages: [...s.messages, M(e)] })); }, setMessageContent: (e, s) => { n((o) => ({ ...o, messages: i( o.messages, e, (a) => ({ ...a, content: s }) ) })); }, updateMessageMetadata: (e, s) => { n((o) => ({ ...o, messages: i( o.messages, e, (a) => ({ ...a, metadata: s(a.metadata ?? {}) }) ) })); }, deleteMessage: (e) => { n((s) => { const o = s.messages.filter( (a) => a.id !== e ); return o.length === s.messages.length && console.warn(`Message with id ${e} was not found`), { ...s, messages: o }; }); }, rateMessage: (e, s) => { n((o) => ({ ...o, messages: i( o.messages, e, (a) => ({ ...a, rating: s }) ) })); }, createStreamingResponse: () => { n((e) => { const { index: s } = d( e.messages ); return s !== -1 ? (console.warn( "Cannot create a new streaming response while one is already active" ), e) : { ...e, messages: [ ...e.messages, M({ id: g, role: "assistant", content: "" }) ], streamingMessageId: g }; }); }, appendStreamingResponse: (e) => { n((s) => { const { index: o } = d( s.messages ); return o === -1 ? (console.warn( "Attempted to append to a streaming response that does not exist" ), s) : { ...s, messages: i( s.messages, g, (a) => ({ ...a, content: a.content + e }) ) }; }); }, appendStreamingReferences: (e) => { n((s) => { const { index: o } = d( s.messages ); return o === -1 ? (console.warn( "Attempted to append references to a streaming response that does not exist" ), s) : { ...s, messages: i( s.messages, g, (a) => ({ ...a, references: e }) ) }; }); }, appendStreamingPromotion: (e) => { n((s) => { const { index: o } = d( s.messages ); return o === -1 ? (console.warn( "Attempted to append promotion to a streaming response that does not exist" ), s) : { ...s, messages: i( s.messages, g, (a) => ({ ...a, promotion: e }) ) }; }); }, finishStreamingResponse: (e) => { n((s) => d(s.messages).index === -1 ? (console.warn( "Cannot finish a streaming response that does not exist" ), s) : { ...s, messages: i( s.messages, g, (a) => ({ ...a, id: e }) ), streamingMessageId: void 0 }); }, cancelStreamingResponse: () => { n((e) => { const s = d(e.messages); return s.index === -1 ? (console.warn( "Cannot cancel a streaming response that does not exist" ), e) : { ...e, messages: E( e.messages, s.index ), streamingMessageId: void 0 }; }); } } })) ), u = "A conversation store was used before it was initialized.", x = { ...h, api: new Proxy( {}, { get: () => () => { throw new Error(u); } } ) }, P = { getInitialState: () => x, getState: () => x, setState: () => { throw new Error(u); }, subscribe: () => () => { throw new Error(u); } }, z = S(P); function _(t) { const [n] = A(() => O("state")); return /* @__PURE__ */ b(z.Provider, { value: n, children: t.children }); } export { _ as C, g as S, R as a, z as b, M as c };