Skip to content

Hand-written Worker: the root entry (createAgent) does not bundle without the generated Worker's Node shim #289

Description

@LinuxDevil

Found while working on #189 (R2).

docs/deployment.md now shows a hand-written Cloudflare Worker that imports KVStore from @lousho/build-ai-agent/kv (Node-free; src/deploy/kv.test.ts guards it) and createAgent from the package root. The root entry is not Node-free. Bundling the built dist/index.mjs with esbuild (--platform=browser, external: ai, zod, @opentelemetry/api) fails with:

Could not resolve "async_hooks"
Could not resolve "crypto"
Could not resolve "fs/promises"
Could not resolve "http"
Could not resolve "net"
Could not resolve "stream"

The generated Worker (lousho build --target cloudflare-worker) works only because workerNodeShimPlugin() (src/deploy/bundle.ts) redirects those imports. A user's own Worker has no such shim. Nobody has tested whether Wrangler's nodejs_compat flag covers all of them.

Possible directions (needs a decision):

  • a Worker-safe entry point (for example @lousho/build-ai-agent/worker) that exports createAgent without the Node-only modules, or
  • documenting the nodejs_compat flag with a tested wrangler.toml, or
  • shipping the shim as a plugin users can add to their own build.

Until then, the deployment page's hand-written Worker example type-checks but has not been shown to bundle.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions