@liveblocks/react-ui
Version:
A set of React pre-built components for the Liveblocks products. Liveblocks is the all-in-one toolkit to build collaborative products like Figma, Notion, and more.
77 lines (74 loc) • 2.4 kB
JavaScript
import { jsx } from 'react/jsx-runtime';
import { Slot } from '@radix-ui/react-slot';
import { forwardRef, useMemo } from 'react';
import { ErrorBoundary } from '../../utils/ErrorBoundary.js';
import { Markdown } from '../Markdown.js';
import { AiMessageToolInvocation } from './tool-invocation.js';
const AI_MESSAGE_CONTENT_NAME = "AiMessageContent";
const defaultMessageContentComponents = {
TextPart: ({ part }) => {
return /* @__PURE__ */ jsx(Markdown, {
content: part.text
});
},
ReasoningPart: ({ part }) => {
return /* @__PURE__ */ jsx(Markdown, {
content: part.text
});
},
ToolInvocationPart: ({ part, message }) => {
return /* @__PURE__ */ jsx(ErrorBoundary, {
fallback: null,
children: /* @__PURE__ */ jsx(AiMessageToolInvocation, {
part,
message
})
});
}
};
const AiMessageContent = forwardRef(
({ message, components, asChild, copilotId, ...props }, forwardedRef) => {
const Component = asChild ? Slot : "div";
const { TextPart, ReasoningPart, ToolInvocationPart } = useMemo(
() => ({ ...defaultMessageContentComponents, ...components }),
[components]
);
const content = message.content ?? message.contentSoFar;
const numParts = content.length;
const isGenerating = message.role === "assistant" && message.status === "generating";
return /* @__PURE__ */ jsx(Component, {
...props,
ref: forwardedRef,
children: content.map((part, index) => {
const isStreaming = isGenerating && index === numParts - 1;
const extra = { index, isStreaming };
switch (part.type) {
case "text":
return /* @__PURE__ */ jsx(TextPart, {
part,
...extra
}, index);
case "reasoning":
return /* @__PURE__ */ jsx(ReasoningPart, {
part,
...extra
}, index);
case "tool-invocation":
return /* @__PURE__ */ jsx(ToolInvocationPart, {
part,
...extra,
message,
copilotId
}, index);
default:
return null;
}
})
});
}
);
if (process.env.NODE_ENV !== "production") {
AiMessageContent.displayName = AI_MESSAGE_CONTENT_NAME;
}
export { AiMessageContent as Content };
//# sourceMappingURL=index.js.map