breadboard-ai / breadboard

A library for prototyping generative AI applications.
Apache License 2.0
96 stars 19 forks source link

Top level await declarations prevent module typed compilation #2269

Open j3ffgray opened 1 week ago

j3ffgray commented 1 week ago

Behavior

Deploying a project using a framework like Svelte requires a package.json set to "type: "module".

ESM modules don't support top level await functions, two of which are present in GeminiKit.

Steps to Reproduce the Problem

  1. Using sveltekit and svelte-adapter-appengine, deploy to App Engine.
  2. This causes the error noted below.
  3. Running locally works as intended, perhaps a vite configuration tweak could solve the issue

I haven't found a solution yet, and I'm not familiar enough with the structure of the kits yet, so I don't have a fix but wanted to note it, as it prevents deploying experiments with certain OS tools.

Specifications

Error response when running npm run build

> Using svelte-adapter-appengine
✘ [ERROR] Top-level await is currently not supported with the "cjs" output format

    node_modules/@google-labs/gemini-kit/dist/src/index.js:11:16:
      11 │ const adapter = await GraphToKitAdapter.create(kit, KIT_BASE_URL, []);
         ╵                 ~~~~~

✘ [ERROR] Top-level await is currently not supported with the "cjs" output format

    node_modules/@google-labs/gemini-kit/dist/src/boards/gemini-pro-vision.js:66:15:
      66 │ export default await board(() => {
         ╵                ~~~~~

error during build:
Error: Build failed with 2 errors:
node_modules/@google-labs/gemini-kit/dist/src/boards/gemini-pro-vision.js:66:15: ERROR: Top-level await is currently not supported with the "cjs" output format
node_modules/@google-labs/gemini-kit/dist/src/index.js:11:16: ERROR: Top-level await is currently not supported with the "cjs" output format
    at failureErrorWithLog (/Users/jeffgray/Dev/gdm-create-vaporware-emulator/node_modules/svelte-adapter-appengine/node_modules/esbuild/lib/main.js:1651:15)
    at /Users/jeffgray/Dev/gdm-create-vaporware-emulator/node_modules/svelte-adapter-appengine/node_modules/esbuild/lib/main.js:1059:25
    at /Users/jeffgray/Dev/gdm-create-vaporware-emulator/node_modules/svelte-adapter-appengine/node_modules/esbuild/lib/main.js:1004:52
    at buildResponseToResult (/Users/jeffgray/Dev/gdm-create-vaporware-emulator/node_modules/svelte-adapter-appengine/node_modules/esbuild/lib/main.js:1057:7)
    at /Users/jeffgray/Dev/gdm-create-vaporware-emulator/node_modules/svelte-adapter-appengine/node_modules/esbuild/lib/main.js:1086:16
    at responseCallbacks.<computed> (/Users/jeffgray/Dev/gdm-create-vaporware-emulator/node_modules/svelte-adapter-appengine/node_modules/esbuild/lib/main.js:704:9)
    at handleIncomingPacket (/Users/jeffgray/Dev/gdm-create-vaporware-emulator/node_modules/svelte-adapter-appengine/node_modules/esbuild/lib/main.js:764:9)
    at Socket.readFromStdout (/Users/jeffgray/Dev/gdm-create-vaporware-emulator/node_modules/svelte-adapter-appengine/node_modules/esbuild/lib/main.js:680:7)
    at Socket.emit (node:events:519:28)
    at addChunk (node:internal/streams/readable:559:12)
dglazkov commented 1 week ago

ESM most definitely support top-level awaits! This error is typically due to some misconfiguration. I've seen it happen before, and the fix is usually messing with esbuild/rollup config files.

dglazkov commented 1 week ago

The underlying issue is that the the bundler/compiler used by svelte-adapter-appengine is too pessimistic about the target on which this code is going to run, so it bails.

AppEngine supports node22 now, which most definitely supports top-level awaits.

For example, here's the fix for this problem for esbuild/vite:

https://github.com/breadboard-ai/breadboard/blob/8dbb1cfe75e4158a689abf2c75b91be26efb2b66/packages/board-server/vite.config.ts#L4-L5

j3ffgray commented 1 week ago

Hmmm, yeah, updating the vite.config.ts was the first thing I tried. I also tried manually setting the version of Node in the svelte config (for the app engine adapter), but I'm still receiving the build errors.

I'll keep poking around, thanks for the quick response!

dglazkov commented 1 week ago

One thing that might help -- I see that you're targeting CommonJS (cjs) -- why not use ESM?

j3ffgray commented 1 week ago

I'm not attempting to target cjs, and I'm no wizard at Vite or Rollup, mostly just using the App Engine adapter Halfdanj created and using its stock configuration.

Any tips would be appreciated, but in the meantime, I've swapped Sveltekit adapters in favor of sveltekit-static, which has its own issues, but allowed me to deploy something to App engine that can run for the time being.