{"version":3,"file":"AIUIComponent.mjs","names":[],"sources":["../../../../../src/plugins/AIComponents/GenerativeUI/AIUIComponent.tsx"],"sourcesContent":["import React, { type ComponentType, useMemo } from 'react';\n\nimport { decodeAIUIArguments } from './AIUIArguments';\nimport type { AIUIComponentDefinition } from './AIUIComponentDefinition';\nimport { AIUIEntrance } from './AIUIEntrance';\nimport { AIUIErrorBoundary } from './AIUIErrorBoundary';\nimport type { AIUIHost } from './AIUIHost';\n\n/** Where an app's component is shown, and how it tells the agent what the person did. */\nexport type AIUIComponentContext = {\n  /**\n   * Tells the agent about one of the component's events, as the person's next message.\n   * `label` is what the person did, in their words, such as \"Tell me more about St. Naum\".\n   */\n  emit: (event: string, label: string, payload?: Record<string, unknown>) => void;\n  /** The conversation's host, for running the app's tools and opening links. */\n  host: AIUIHost;\n  /** Whether the agent is still writing the reply's UI, so the arguments may still change. */\n  isStreaming: boolean;\n  /** The reply the component is in. */\n  messageId: string;\n};\n\nexport type AIUIComponentProps<Arguments> = {\n  /** What the agent filled in: keys in camelCase, dates as `Date`s. */\n  args: Arguments;\n  context: AIUIComponentContext;\n};\n\n/**\n * One of the app's views that the agent can place in a reply's UI, in whichever format it\n * writes, such as a map or a card over the app's own data. The counterpart of `AIUIComponent`\n * in StreamChatAI.\n */\nexport type AIUIComponent = {\n  Component: ComponentType<AIUIComponentProps<never>>;\n  /** Its description, the same on every platform and for the agent. */\n  definition: AIUIComponentDefinition;\n};\n\n/**\n * One of the app's components, with what its view reads from the agent's arguments. The view\n * gets them only once they fit the definition's parameters, so it can rely on their types.\n */\nexport const defineAIUIComponent = <Arguments,>(\n  definition: AIUIComponentDefinition,\n  Component: ComponentType<AIUIComponentProps<Arguments>>,\n): AIUIComponent => ({\n  // The arguments are checked against `definition` before the view gets them.\n  Component: Component as unknown as ComponentType<AIUIComponentProps<never>>,\n  definition,\n});\n\n// What an app's component was given, to try it again once that changes or the reply is written.\nconst attemptOf = (args: Record<string, unknown>, isStreaming: boolean) => {\n  try {\n    return `${isStreaming}\\u0000${JSON.stringify(args)}`;\n  } catch {\n    return args;\n  }\n};\n\n/**\n * An app's component for the arguments the agent wrote, coming in as it shows, or nothing\n * while they don't fit its parameters, such as while they are still streaming in. Renderers\n * place the app's components with it. One that throws leaves only itself out of the reply's\n * UI, is tried again when its arguments change, and is reported once the reply is written.\n */\nexport const AIUIComponentView = ({\n  args,\n  component,\n  context,\n}: {\n  args: Record<string, unknown>;\n  component: AIUIComponent;\n  context: AIUIComponentContext;\n}) => {\n  const { host, isStreaming, messageId } = context;\n  // Worked out again only when the arguments change, for a renderer that keeps the same object\n  // while they don't, as the A2UI renderer does.\n  const decoded = useMemo(\n    () => decodeAIUIArguments(args, component.definition.parameters),\n    [args, component],\n  );\n  const resetKey = useMemo(\n    () => (decoded === undefined ? undefined : attemptOf(decoded, isStreaming)),\n    [decoded, isStreaming],\n  );\n  if (decoded === undefined) return null;\n  const { Component } = component;\n  return (\n    <AIUIErrorBoundary\n      onError={(error) => {\n        // While the reply streams, what the component was given may still be incomplete.\n        if (!isStreaming) host.reportRenderError(error, messageId);\n      }}\n      resetKey={resetKey}\n    >\n      <AIUIEntrance>\n        <Component args={decoded as never} context={context} />\n      </AIUIEntrance>\n    </AIUIErrorBoundary>\n  );\n};\n"],"mappings":";;;;;;;;;;AA4CA,IAAa,uBACX,YACA,eACmB;CAER;CACX;AACF;AAGA,IAAM,aAAa,MAA+B,gBAAyB;CACzE,IAAI;EACF,OAAO,GAAG,YAAY,QAAQ,KAAK,UAAU,IAAI;CACnD,QAAQ;EACN,OAAO;CACT;AACF;;;;;;;AAQA,IAAa,qBAAqB,EAChC,MACA,WACA,cAKI;CACJ,MAAM,EAAE,MAAM,aAAa,cAAc;CAGzC,MAAM,UAAU,cACR,oBAAoB,MAAM,UAAU,WAAW,UAAU,GAC/D,CAAC,MAAM,SAAS,CAClB;CACA,MAAM,WAAW,cACR,YAAY,KAAA,IAAY,KAAA,IAAY,UAAU,SAAS,WAAW,GACzE,CAAC,SAAS,WAAW,CACvB;CACA,IAAI,YAAY,KAAA,GAAW,OAAO;CAClC,MAAM,EAAE,cAAc;CACtB,OACE,oBAAC,mBAAD;EACE,UAAU,UAAU;GAElB,IAAI,CAAC,aAAa,KAAK,kBAAkB,OAAO,SAAS;EAC3D;EACU;YAEV,oBAAC,cAAD,EAAA,UACE,oBAAC,WAAD;GAAW,MAAM;GAA2B;EAAU,CAAA,EAC1C,CAAA;CACG,CAAA;AAEvB"}