@assistant-ui/react
Version:
Typescript/React library for AI Chat
1 lines • 10.3 kB
Source Map (JSON)
{"version":3,"sources":["../../../src/primitives/message/MessageContent.tsx"],"sourcesContent":["\"use client\";\n\nimport { type ComponentType, type FC, memo, useMemo } from \"react\";\nimport {\n TextContentPartProvider,\n useContentPart,\n useContentPartRuntime,\n useToolUIs,\n} from \"../../context\";\nimport {\n useMessage,\n useMessageRuntime,\n} from \"../../context/react/MessageContext\";\nimport { ContentPartRuntimeProvider } from \"../../context/providers/ContentPartRuntimeProvider\";\nimport { ContentPartPrimitiveText } from \"../contentPart/ContentPartText\";\nimport { ContentPartPrimitiveImage } from \"../contentPart/ContentPartImage\";\nimport type {\n Unstable_AudioContentPartComponent,\n EmptyContentPartComponent,\n TextContentPartComponent,\n ImageContentPartComponent,\n SourceContentPartComponent,\n ToolCallContentPartComponent,\n ToolCallContentPartProps,\n FileContentPartComponent,\n ReasoningContentPartComponent,\n} from \"../../types/ContentPartComponentTypes\";\nimport { ContentPartPrimitiveInProgress } from \"../contentPart/ContentPartInProgress\";\nimport { ContentPartStatus } from \"../../types/AssistantTypes\";\n\nexport namespace MessagePrimitiveContent {\n export type Props = {\n components?:\n | {\n Empty?: EmptyContentPartComponent | undefined;\n Text?: TextContentPartComponent | undefined;\n Reasoning?: ReasoningContentPartComponent | undefined;\n Source?: SourceContentPartComponent | undefined;\n Image?: ImageContentPartComponent | undefined;\n File?: FileContentPartComponent | undefined;\n Unstable_Audio?: Unstable_AudioContentPartComponent | undefined;\n tools?:\n | {\n by_name?:\n | Record<string, ToolCallContentPartComponent | undefined>\n | undefined;\n Fallback?: ComponentType<ToolCallContentPartProps> | undefined;\n }\n | {\n Override: ComponentType<ToolCallContentPartProps>;\n }\n | undefined;\n }\n | undefined;\n };\n}\n\nconst ToolUIDisplay = ({\n Fallback,\n ...props\n}: {\n Fallback: ToolCallContentPartComponent | undefined;\n} & ToolCallContentPartProps) => {\n const Render = useToolUIs((s) => s.getToolUI(props.toolName)) ?? Fallback;\n if (!Render) return null;\n return <Render {...props} />;\n};\n\nconst defaultComponents = {\n Text: () => (\n <p style={{ whiteSpace: \"pre-line\" }}>\n <ContentPartPrimitiveText />\n <ContentPartPrimitiveInProgress>\n <span style={{ fontFamily: \"revert\" }}>{\" \\u25CF\"}</span>\n </ContentPartPrimitiveInProgress>\n </p>\n ),\n Reasoning: () => null,\n Source: () => null,\n Image: () => <ContentPartPrimitiveImage />,\n File: () => null,\n Unstable_Audio: () => null,\n} satisfies MessagePrimitiveContent.Props[\"components\"];\n\ntype MessageContentPartComponentProps = {\n components: MessagePrimitiveContent.Props[\"components\"];\n};\n\nconst MessageContentPartComponent: FC<MessageContentPartComponentProps> = ({\n components: {\n Text = defaultComponents.Text,\n Reasoning = defaultComponents.Reasoning,\n Image = defaultComponents.Image,\n Source = defaultComponents.Source,\n File = defaultComponents.File,\n Unstable_Audio: Audio = defaultComponents.Unstable_Audio,\n tools = {},\n } = {},\n}) => {\n const contentPartRuntime = useContentPartRuntime();\n\n const part = useContentPart();\n\n const type = part.type;\n if (type === \"tool-call\") {\n const addResult = (result: any) => contentPartRuntime.addToolResult(result);\n if (\"Override\" in tools)\n return <tools.Override {...part} addResult={addResult} />;\n const Tool = tools.by_name?.[part.toolName] ?? tools.Fallback;\n return <ToolUIDisplay {...part} Fallback={Tool} addResult={addResult} />;\n }\n\n if (part.status.type === \"requires-action\")\n throw new Error(\"Encountered unexpected requires-action status\");\n\n switch (type) {\n case \"text\":\n return <Text {...part} />;\n\n case \"reasoning\":\n return <Reasoning {...part} />;\n\n case \"source\":\n return <Source {...part} />;\n\n case \"image\":\n // eslint-disable-next-line jsx-a11y/alt-text\n return <Image {...part} />;\n\n case \"file\":\n return <File {...part} />;\n\n case \"audio\":\n return <Audio {...part} />;\n\n default:\n const unhandledType: never = type;\n throw new Error(`Unknown content part type: ${unhandledType}`);\n }\n};\n\ntype MessageContentPartProps = {\n partIndex: number;\n components: MessagePrimitiveContent.Props[\"components\"];\n};\n\nconst MessageContentPartImpl: FC<MessageContentPartProps> = ({\n partIndex,\n components,\n}) => {\n const messageRuntime = useMessageRuntime();\n const runtime = useMemo(\n () => messageRuntime.getContentPartByIndex(partIndex),\n [messageRuntime, partIndex],\n );\n\n return (\n <ContentPartRuntimeProvider runtime={runtime}>\n <MessageContentPartComponent components={components} />\n </ContentPartRuntimeProvider>\n );\n};\n\nconst MessageContentPart = memo(\n MessageContentPartImpl,\n (prev, next) =>\n prev.partIndex === next.partIndex &&\n prev.components?.Text === next.components?.Text &&\n prev.components?.Reasoning === next.components?.Reasoning &&\n prev.components?.Source === next.components?.Source &&\n prev.components?.Image === next.components?.Image &&\n prev.components?.File === next.components?.File &&\n prev.components?.Unstable_Audio === next.components?.Unstable_Audio &&\n prev.components?.tools === next.components?.tools,\n);\n\nconst COMPLETE_STATUS: ContentPartStatus = Object.freeze({\n type: \"complete\",\n});\n\nconst EmptyContentFallback: FC<{\n status: ContentPartStatus;\n component: TextContentPartComponent;\n}> = ({ status, component: Component }) => {\n return (\n <TextContentPartProvider text=\"\" isRunning={status.type === \"running\"}>\n <Component type=\"text\" text=\"\" status={status} />\n </TextContentPartProvider>\n );\n};\n\nconst EmptyContentImpl: FC<MessageContentPartComponentProps> = ({\n components,\n}) => {\n const status =\n useMessage((s) => s.status as ContentPartStatus) ?? COMPLETE_STATUS;\n\n if (components?.Empty) return <components.Empty status={status} />;\n\n return (\n <EmptyContentFallback\n status={status}\n component={components?.Text ?? defaultComponents.Text}\n />\n );\n};\n\nconst EmptyContent = memo(\n EmptyContentImpl,\n (prev, next) =>\n prev.components?.Empty === next.components?.Empty &&\n prev.components?.Text === next.components?.Text,\n);\n\nexport const MessagePrimitiveContent: FC<MessagePrimitiveContent.Props> = ({\n components,\n}) => {\n const contentLength = useMessage((s) => s.content.length);\n\n if (contentLength === 0) {\n return <EmptyContent components={components} />;\n }\n\n return Array.from({ length: contentLength }, (_, index) => (\n <MessageContentPart key={index} partIndex={index} components={components} />\n ));\n};\n\nMessagePrimitiveContent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