@assistant-ui/react
Version:
Typescript/React library for AI Chat
66 lines (65 loc) • 1.99 kB
JavaScript
"use client";
// src/context/providers/TextContentPartProvider.tsx
import { useEffect, useState } from "react";
import { create } from "zustand";
import {
ContentPartContext
} from "../react/ContentPartContext.mjs";
import { writableStore } from "../ReadonlyStore.mjs";
import {
ContentPartRuntimeImpl
} from "../../api/ContentPartRuntime.mjs";
import { ensureBinding } from "../react/utils/ensureBinding.mjs";
import { jsx } from "react/jsx-runtime";
var COMPLETE_STATUS = {
type: "complete"
};
var RUNNING_STATUS = {
type: "running"
};
var 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.mjs.map