UNPKG

@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.

1 lines 6.49 kB
{"version":3,"file":"index.cjs","sources":["../../../src/primitives/AiMessage/index.tsx"],"sourcesContent":["import type { AiAssistantMessage } from \"@liveblocks/core\";\nimport { Slot } from \"@radix-ui/react-slot\";\nimport { forwardRef, useMemo } from \"react\";\n\nimport { ErrorBoundary } from \"../../utils/ErrorBoundary\";\nimport { Markdown } from \"../Markdown\";\nimport { AiMessageToolInvocation } from \"./tool-invocation\";\nimport type {\n AiMessageContentComponents,\n AiMessageContentProps,\n} from \"./types\";\n\nconst AI_MESSAGE_CONTENT_NAME = \"AiMessageContent\";\n\nconst defaultMessageContentComponents: AiMessageContentComponents = {\n TextPart: ({ part }) => {\n return <Markdown content={part.text} />;\n },\n ReasoningPart: ({ part }) => {\n return <Markdown content={part.text} />;\n },\n RetrievalPart: () => null,\n ToolInvocationPart: ({ part, message }) => {\n return (\n <ErrorBoundary fallback={null}>\n <AiMessageToolInvocation part={part} message={message} />\n </ErrorBoundary>\n );\n },\n SourcesPart: () => null,\n};\n\n/**\n * --------------------------------------------------------------------------\n * @private The API for this component is not yet stable.\n * --------------------------------------------------------------------------\n *\n * Primitive to help display an user or assistant message’s content, which is\n * an array of parts.\n *\n * @example\n * <AiMessage.Content message={message} components={{ TextPart }} />\n */\nconst AiMessageContent = forwardRef<HTMLDivElement, AiMessageContentProps>(\n ({ message, components, asChild, ...props }, forwardedRef) => {\n const Component = asChild ? Slot : \"div\";\n const {\n ReasoningPart,\n RetrievalPart,\n TextPart,\n ToolInvocationPart,\n SourcesPart,\n } = useMemo(\n () => ({ ...defaultMessageContentComponents, ...components }),\n [components]\n );\n\n const content = message.content ?? message.contentSoFar;\n const numParts = content.length;\n const isGenerating =\n message.role === \"assistant\" && message.status === \"generating\";\n\n return (\n <Component {...props} ref={forwardedRef}>\n {content.map((part, index) => {\n // A part is considered to be still \"streaming in\" if it's the last\n // part in the content array, and the message is in \"generating\"\n // state.\n const isStreaming = isGenerating && index === numParts - 1;\n const extra = { index, isStreaming };\n switch (part.type) {\n case \"text\":\n return <TextPart key={index} part={part} {...extra} />;\n\n case \"reasoning\":\n return <ReasoningPart key={index} part={part} {...extra} />;\n\n case \"retrieval\":\n return <RetrievalPart key={index} part={part} {...extra} />;\n\n case \"tool-invocation\":\n return (\n <ToolInvocationPart\n key={index}\n part={part}\n {...extra}\n // For tool invocations, we know for sure it's an AiAssistantMessage\n message={message as AiAssistantMessage}\n />\n );\n\n case \"sources\":\n // Sources are only shown after the message is either completed or failed.\n if (\n message.role === \"assistant\" &&\n message.status !== \"completed\" &&\n message.status !== \"failed\"\n ) {\n return null;\n }\n\n return <SourcesPart key={index} part={part} />;\n\n default:\n return null;\n }\n })}\n </Component>\n );\n }\n);\n\nif (process.env.NODE_ENV !== \"production\") {\n AiMessageContent.displayName = AI_MESSAGE_CONTENT_NAME;\n}\n\n// NOTE: Every export from this file will be available publicly as AiMessage.*\nexport { AiMessageContent as Content };\n"],"names":["jsx","Markdown","ErrorBoundary","AiMessageToolInvocation","forwardRef","Slot","useMemo"],"mappings":";;;;;;;;;AAYA,MAAM,uBAA0B,GAAA,kBAAA,CAAA;AAEhC,MAAM,+BAA8D,GAAA;AAAA,EAClE,QAAU,EAAA,CAAC,EAAE,IAAA,EAAW,KAAA;AACtB,IAAA,uBAAQA,cAAA,CAAAC,iBAAA,EAAA,EAAS,OAAS,EAAA,IAAA,CAAK,IAAM,EAAA,CAAA,CAAA;AAAA,GACvC;AAAA,EACA,aAAe,EAAA,CAAC,EAAE,IAAA,EAAW,KAAA;AAC3B,IAAA,uBAAQD,cAAA,CAAAC,iBAAA,EAAA,EAAS,OAAS,EAAA,IAAA,CAAK,IAAM,EAAA,CAAA,CAAA;AAAA,GACvC;AAAA,EACA,eAAe,MAAM,IAAA;AAAA,EACrB,kBAAoB,EAAA,CAAC,EAAE,IAAA,EAAM,SAAc,KAAA;AACzC,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