@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.
75 lines (72 loc) • 2.69 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 });
},
RetrievalPart: () => null,
ToolInvocationPart: ({ part, message }) => {
return /* @__PURE__ */ jsx(ErrorBoundary, { fallback: null, children: /* @__PURE__ */ jsx(AiMessageToolInvocation, { part, message }) });
},
SourcesPart: () => null
};
const AiMessageContent = forwardRef(
({ message, components, asChild, ...props }, forwardedRef) => {
const Component = asChild ? Slot : "div";
const {
ReasoningPart,
RetrievalPart,
TextPart,
ToolInvocationPart,
SourcesPart
} = 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 "retrieval":
return /* @__PURE__ */ jsx(RetrievalPart, { part, ...extra }, index);
case "tool-invocation":
return /* @__PURE__ */ jsx(
ToolInvocationPart,
{
part,
...extra,
message
},
index
);
case "sources":
if (message.role === "assistant" && message.status !== "completed" && message.status !== "failed") {
return null;
}
return /* @__PURE__ */ jsx(SourcesPart, { part }, index);
default:
return null;
}
}) });
}
);
if (process.env.NODE_ENV !== "production") {
AiMessageContent.displayName = AI_MESSAGE_CONTENT_NAME;
}
export { AiMessageContent as Content };
//# sourceMappingURL=index.js.map