UNPKG

@assistant-ui/react

Version:

Typescript/React library for AI Chat

64 lines 1.92 kB
"use client"; import { jsx } from "react/jsx-runtime"; import { useEffect, useState } from "react"; import { create } from "zustand"; import { ContentPartContext } from "../react/ContentPartContext"; import { writableStore } from "../ReadonlyStore"; import { ContentPartRuntimeImpl } from "../../api/ContentPartRuntime"; import { ensureBinding } from "../react/utils/ensureBinding"; const COMPLETE_STATUS = { type: "complete" }; const RUNNING_STATUS = { type: "running" }; const TextContentPartProvider = ({ children, text, isRunning }) => { const [context] = useState(() => { const useContentPart = create(() => ({ status: isRunning ? RUNNING_STATUS : COMPLETE_STATUS, type: "text", text })); const contentPartRuntime = new ContentPartRuntimeImpl({ path: { ref: "text", threadSelector: { type: "main" }, messageSelector: { type: "messageId", messageId: "" }, contentPartSelector: { type: "index", index: 0 } }, getState: useContentPart.getState, subscribe: useContentPart.subscribe }); ensureBinding(contentPartRuntime); const useContentPartRuntime = create(() => contentPartRuntime); return { useContentPartRuntime, useContentPart }; }); useEffect(() => { const state = context.useContentPart.getState(); const textUpdated = state.text !== text; const targetStatus = isRunning ? RUNNING_STATUS : COMPLETE_STATUS; const statusUpdated = state.status !== targetStatus; if (!textUpdated && !statusUpdated) return; writableStore(context.useContentPart).setState( { type: "text", text, status: targetStatus }, true ); }, [context, isRunning, text]); return /* @__PURE__ */ jsx(ContentPartContext.Provider, { value: context, children }); }; export { TextContentPartProvider }; //# sourceMappingURL=TextContentPartProvider.js.map