Repository navigation
feat: @lousho/build-ai-agent/worker entry for hand-written Workers (#289) - #393
Merged
Merged
Conversation
LinuxDevil
force-pushed
the
fix/lou-s4-wroot
branch
3 times, most recently
from
October 4, 2026 12:28
a6912b8 to
c30f6c5
Compare
) 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
force-pushed
the
fix/lou-s4-wroot
branch
from
October 4, 2026 13:03
c30f6c5 to
06291ad
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A hand-written Cloudflare Worker that imports
createAgentcannot 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 resolvingasync_hooks,crypto,fs/promises,http,net,streamand friends. Only the generated Worker ever bundled, becauselousho build --target=cloudflare-workerredirects those imports to shims at build time.Fix
New public subpath
@lousho/build-ai-agent/worker(src/deploy/worker.ts) exportingcreateAgentplus the Worker-safe surface: the wholeworkerSdksubset (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) andserveFetch.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/sdkspecifier 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.mcpServersis 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/apiand the@ai-sdk/*provider packages stay external (WORKER_ENTRY_EXTERNALS) so they resolve from the user's install and share theiraicopy. The publisheddist/deploy/worker.*(esm + cjs + d.ts) contains nonode:import and no unresolvable specifier.Regression test
src/deploy/worker.test.tsbundles the entry with the exact plugins and externals the real build uses, forplatform: 'browser', and fails if anode:import, anollama-ai-providerspecifier or a@modelcontextprotocol/sdkspecifier survives in graph or output; a second test asserts the export surface.Notes
scripts/pack-smoke.tsunpacked cap raised 16 -> 18 MiB: [R4] Slim the npm tarball: drop test files and source map weight, fix pack-smoke #191's slimming leaves room but this entry + Cloudflare Worker target: sub-agents, schedules, channels and memory from an agent directory #298's Worker channel helpers bring the tarball to 917 entries / ~16.4 MiB / ~4.3 MiB packed (caps: 1050 / 18 MiB / 4.5 MiB).docs/cloudflare-workers.mdgains a "Writing your own Worker" section; its snippets now import from/worker.llms.txt/llms-full.txtregenerated.95855325(Cloudflare Worker target: sub-agents, schedules, channels and memory from an agent directory #298):/workerre-exports the expandedworkerSdk(channels, memory, schedules) unchanged.Verification
npm run build— clean;dist/deploy/worker.{mjs,js}~617 KB each, onlyai/zod/@opentelemetry/api/@ai-sdk/*specifiers remain.src/deploysuite: 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./workerbundles forplatform: 'browser'with zero externals flags.Closes #289