{"version":3,"file":"AIUIRuntimeLoader.mjs","names":[],"sources":["../../../../../src/plugins/AIComponents/GenerativeUI/AIUIRuntimeLoader.tsx"],"sourcesContent":["import React, { type ReactNode, useEffect, useState } from 'react';\n\nimport type { AIUIDocument } from './AIUIDocument';\nimport type { AIUIHost } from './AIUIHost';\nimport { AIThemeScope } from '../themeScope';\n\n/**\n * Loads the runtime of a renderer, its format's packages, once and in the browser only, when a\n * reply first shows the format: the packages may not run on a server, and are of no use to an\n * app whose agent never writes it. When the runtime can't load, such as when a deploy replaced\n * the file it is in, the replies already written show their text, the next reply the agent\n * writes in the format tries again, and the failure is reported once.\n */\nexport class AIUIRuntimeLoader<Runtime> {\n  /** The runtime, once loaded. */\n  runtime?: Runtime;\n\n  private failed = false;\n  private readonly host: AIUIHost;\n  private readonly importRuntime: () => Promise<Runtime>;\n  private loading?: Promise<Runtime>;\n  private readonly onFailure: (error: unknown) => void;\n\n  constructor(\n    host: AIUIHost,\n    importRuntime: () => Promise<Runtime>,\n    onFailure: (error: unknown) => void,\n  ) {\n    this.host = host;\n    this.importRuntime = importRuntime;\n    this.onFailure = onFailure;\n  }\n\n  /** Loads the runtime, once while it loads; again after it failed to. */\n  load() {\n    this.loading ??= this.importRuntime().then(\n      (runtime) => {\n        this.runtime = runtime;\n        // What a reply shows can depend on the runtime, such as whether it shows its text.\n        this.host.refresh();\n        return runtime;\n      },\n      (error: unknown) => {\n        this.loading = undefined;\n        if (!this.failed) {\n          this.failed = true;\n          this.onFailure(error);\n          this.host.refresh();\n        }\n        throw error;\n      },\n    );\n    return this.loading;\n  }\n\n  /**\n   * Whether a reply shows its text because the runtime couldn't load: one already written does,\n   * and one the agent is writing shows its placeholder while the runtime loads again.\n   */\n  showsText(document: AIUIDocument) {\n    return this.failed && !this.runtime && !document.isStreaming;\n  }\n}\n\nexport type AIUIRuntimeViewProps<Runtime> = {\n  loader: AIUIRuntimeLoader<Runtime>;\n  /** What shows while the runtime loads. */\n  placeholder: ReactNode;\n  /** The reply's UI, once the runtime has loaded. */\n  render: (runtime: Runtime) => ReactNode;\n};\n\n/** A reply's UI from a renderer's runtime, which it loads the first time one shows. */\nexport const AIUIRuntimeView = <Runtime,>({\n  loader,\n  placeholder,\n  render,\n}: AIUIRuntimeViewProps<Runtime>) => {\n  const [runtime, setRuntime] = useState(loader.runtime);\n  useEffect(() => {\n    if (runtime) return;\n    let live = true;\n    loader.load().then(\n      (loaded) => {\n        if (live) setRuntime(loaded);\n      },\n      // The loader has said why, once, and the host shows the reply's text.\n      () => undefined,\n    );\n    return () => {\n      live = false;\n    };\n  }, [loader, runtime]);\n  // What the renderer draws has no root element of the SDK's, so the scope is a layout-neutral\n  // wrapper, added only when nothing above provides one.\n  return <AIThemeScope>{runtime ? render(runtime) : placeholder}</AIThemeScope>;\n};\n"],"mappings":";;;;;;;;;;;AAaA,IAAa,oBAAb,MAAwC;;CAEtC;CAEA,SAAiB;CACjB;CACA;CACA;CACA;CAEA,YACE,MACA,eACA,WACA;EACA,KAAK,OAAO;EACZ,KAAK,gBAAgB;EACrB,KAAK,YAAY;CACnB;;CAGA,OAAO;EACL,KAAK,YAAY,KAAK,cAAc,CAAC,CAAC,MACnC,YAAY;GACX,KAAK,UAAU;GAEf,KAAK,KAAK,QAAQ;GAClB,OAAO;EACT,IACC,UAAmB;GAClB,KAAK,UAAU,KAAA;GACf,IAAI,CAAC,KAAK,QAAQ;IAChB,KAAK,SAAS;IACd,KAAK,UAAU,KAAK;IACpB,KAAK,KAAK,QAAQ;GACpB;GACA,MAAM;EACR,CACF;EACA,OAAO,KAAK;CACd;;;;;CAMA,UAAU,UAAwB;EAChC,OAAO,KAAK,UAAU,CAAC,KAAK,WAAW,CAAC,SAAS;CACnD;AACF;;AAWA,IAAa,mBAA6B,EACxC,QACA,aACA,aACmC;CACnC,MAAM,CAAC,SAAS,cAAc,SAAS,OAAO,OAAO;CACrD,gBAAgB;EACd,IAAI,SAAS;EACb,IAAI,OAAO;EACX,OAAO,KAAK,CAAC,CAAC,MACX,WAAW;GACV,IAAI,MAAM,WAAW,MAAM;EAC7B,SAEM,KAAA,CACR;EACA,aAAa;GACX,OAAO;EACT;CACF,GAAG,CAAC,QAAQ,OAAO,CAAC;CAGpB,OAAO,oBAAC,cAAD,EAAA,UAAe,UAAU,OAAO,OAAO,IAAI,YAA0B,CAAA;AAC9E"}