Skip to content

feat: @lousho/build-ai-agent/worker entry for hand-written Workers (#289) - #393

Merged
LinuxDevil merged 1 commit into
mainfrom
fix/lou-s4-wroot
Oct 4, 2026
Merged

LinuxDevil merged 1 commit into
mainfrom
fix/lou-s4-wroot

Conversation

@LinuxDevil

@LinuxDevil LinuxDevil commented Oct 4, 2026 •

Copy link
Copy Markdown
Owner

Problem

A hand-written Cloudflare Worker that imports createAgent cannot bundle the package root (#289): the root barrel also exports file stores, the agent-directory loader, MCP over stdio, sandboxes and other Node-only modules, so a browser-platform esbuild fails resolving async_hooks, crypto, fs/promises, http, net, stream and friends. Only the generated Worker ever bundled, because lousho build --target=cloudflare-worker redirects those imports to shims at build time.

Fix

New public subpath @lousho/build-ai-agent/worker (src/deploy/worker.ts) exporting createAgent plus the Worker-safe surface: the whole workerSdk subset (defineTool/defineSkill/defineChannel/defineMemory, the channel factories, approval policies, Worker providers, fromAiSdk, errors), the KV stores (KVStore, KVCheckpointStore, CHECKPOINT_KV_BINDING), memoryStore, the schedule helpers (defineSchedule, handleScheduled) and serveFetch.

tsup builds it in a dedicated browser-platform config through workerEntryPlugins() (src/deploy/bundle.ts) — the same shims the adapter applies, baked into the published files — plus two new ones:

  • src/deploy/shims/ollama.worker.ts: ollama-ai-provider(-v2) fails on first use (Ollama targets localhost anyway).
  • src/deploy/shims/mcp.worker.ts: every @modelcontextprotocol/sdk specifier fails on first connect. An external specifier would break a user's build when the optional peer is not installed; bundling it would add ~700 KB of MCP SDK + ajv to every Worker. mcpServers is therefore unsupported from this entry — lousho build (streamable-HTTP MCP on the generated Worker) or a Node target remains the way to run MCP.

ai, zod, @opentelemetry/api and the @ai-sdk/* provider packages stay external (WORKER_ENTRY_EXTERNALS) so they resolve from the user's install and share their ai copy. The published dist/deploy/worker.* (esm + cjs + d.ts) contains no node: import and no unresolvable specifier.

Regression test

src/deploy/worker.test.ts bundles the entry with the exact plugins and externals the real build uses, for platform: 'browser', and fails if a node: import, an ollama-ai-provider specifier or a @modelcontextprotocol/sdk specifier survives in graph or output; a second test asserts the export surface.

Notes

Verification

  • npm run build — clean; dist/deploy/worker.{mjs,js} ~617 KB each, only ai/zod/@opentelemetry/api/@ai-sdk/* specifiers remain.
  • src/deploy suite: 179 tests pass (incl. the new regression test); full suite 4230 tests pass.
  • npm run pack-smoke -- --skip-build — all checks pass; all 18 export subpaths load in esm and cjs.
  • npm run docs:verify-snippets (full run), npm run docs:llms:check, npm run typecheck — pass.
  • esbuild repro from the issue now succeeds: a consumer Worker importing /worker bundles for platform: 'browser' with zero externals flags.

Closes #289

@LinuxDevil
LinuxDevil force-pushed the fix/lou-s4-wroot branch 3 times, most recently from a6912b8 to c30f6c5 Compare October 4, 2026 12:28
)

A hand-written Cloudflare Worker cannot bundle the package root: its barrel
also exports file stores, the agent-directory loader, MCP over stdio,
sandboxes and other Node-only modules, so esbuild platform:browser fails on
node:* imports. Only the generated Worker ever bundled, because
'lousho build --target=cloudflare-worker' applies the Node shims itself.

The new /worker subpath exports createAgent plus the Worker-safe surface
(the workerSdk subset, the KV stores, memoryStore, the schedule helpers and
serveFetch). tsup builds it through the same shim plugins the adapter uses
(workerEntryPlugins in src/deploy/bundle.ts), plus two new ones:
ollama-ai-provider(-v2) and @modelcontextprotocol/sdk are redirected to
fail-on-use shims - external specifiers would fail a user's build when the
optional peers are not installed, and bundling MCP would add ~700 KB to
every Worker. mcpServers therefore fails on first connect; 'lousho build'
(generated Worker, streamable HTTP supported) or a Node target remains the
way to run MCP.

ai, zod, @opentelemetry/api and the @ai-sdk/* provider packages stay
external specifiers resolved from the user's install. Guarded by
src/deploy/worker.test.ts, which bundles the entry with the same plugins
and externals and fails on any node: or unresolvable specifier. The
pack-smoke unpacked cap grows to 18 MiB for the extra entry's files.

Closes #289
@LinuxDevil
LinuxDevil merged commit 3dab5ae into main Oct 4, 2026
5 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant