{"version":3,"file":"AIUIView.mjs","names":[],"sources":["../../../../../src/plugins/AIComponents/GenerativeUI/AIUIView.tsx"],"sourcesContent":["import clsx from 'clsx';\nimport React, { type ReactNode } from 'react';\nimport { useSyncExternalStore } from 'use-sync-external-store/shim';\n\nimport type { AIUIDocument } from './AIUIDocument';\nimport { AIUIEntranceStage } from './AIUIEntrance';\nimport { AIUIErrorBoundary } from './AIUIErrorBoundary';\nimport type { AIUIHost } from './AIUIHost';\nimport { ThemeScopeProvider, useAIThemeScopeClassName } from '../themeScope';\n\n/**\n * A reply's generated UI as it shows in place of the reply's text: as wide as the reply, coming\n * in as it first shows while the agent writes it. UI that throws while it renders shows nothing,\n * and `onError` is told, such as to have the host show the reply's text. An app's component\n * that throws only leaves itself out.\n */\nexport const AIUIGenerated = ({\n  children,\n  isStreaming,\n  onError,\n}: {\n  children: ReactNode;\n  isStreaming: boolean;\n  onError: (error: unknown) => void;\n}) => {\n  const themeScopeClassName = useAIThemeScopeClassName();\n  return (\n    <ThemeScopeProvider>\n      <AIUIEntranceStage\n        className={clsx(themeScopeClassName, 'str-chat__ai-ui')}\n        isStreaming={isStreaming}\n      >\n        <AIUIErrorBoundary onError={onError}>{children}</AIUIErrorBoundary>\n      </AIUIEntranceStage>\n    </ThemeScopeProvider>\n  );\n};\n\nconst subscribeToNothing = () => () => undefined;\nconst noVersion = () => 0;\n\n/**\n * Shows the conversation's replies again whenever the host says what one of them shows changed,\n * such as once a renderer's runtime loads.\n */\nexport const useAIUIHostRefresh = (host: AIUIHost | undefined) =>\n  useSyncExternalStore(\n    host ? host.subscribe : subscribeToNothing,\n    host ? host.getVersion : noVersion,\n    host ? host.getVersion : noVersion,\n  );\n\nexport type AIUIViewProps = {\n  document: AIUIDocument;\n  host: AIUIHost;\n  /** The reply the document belongs to. */\n  messageId: string;\n};\n\n/**\n * One reply's generated UI, for showing replies outside `AIConversation`. It shows nothing when\n * none of the host's renderers shows the document, and the reply's text should show instead:\n * `host.content(document, messageId) !== null` tells which, and `host.subscribe` when that may\n * have changed. Give the host the conversation's messages with `host.receive(messages)` as they\n * change. The counterpart of `AIUIView` in StreamChatAI.\n */\nexport const AIUIView = ({ document, host, messageId }: AIUIViewProps) => {\n  useAIUIHostRefresh(host);\n  const content = host.content(document, messageId);\n  return content === null ? null : (\n    <AIUIGenerated\n      isStreaming={document.isStreaming}\n      onError={(error) => host.fail(messageId, error)}\n    >\n      {content}\n    </AIUIGenerated>\n  );\n};\n"],"mappings":";;;;;;;;;;;;;;;AAgBA,IAAa,iBAAiB,EAC5B,UACA,aACA,cAKI;CAEJ,OACE,oBAAC,oBAAD,EAAA,UACE,oBAAC,mBAAD;EACE,WAAW,KAJW,yBAIN,GAAqB,iBAAiB;EACzC;YAEb,oBAAC,mBAAD;GAA4B;GAAU;EAA4B,CAAA;CACjD,CAAA,EACD,CAAA;AAExB;AAEA,IAAM,iCAAiC,KAAA;AACvC,IAAM,kBAAkB;;;;;AAMxB,IAAa,sBAAsB,SACjC,qBACE,OAAO,KAAK,YAAY,oBACxB,OAAO,KAAK,aAAa,WACzB,OAAO,KAAK,aAAa,SAC3B;;;;;;;;AAgBF,IAAa,YAAY,EAAE,UAAU,MAAM,gBAA+B;CACxE,mBAAmB,IAAI;CACvB,MAAM,UAAU,KAAK,QAAQ,UAAU,SAAS;CAChD,OAAO,YAAY,OAAO,OACxB,oBAAC,eAAD;EACE,aAAa,SAAS;EACtB,UAAU,UAAU,KAAK,KAAK,WAAW,KAAK;YAE7C;CACY,CAAA;AAEnB"}