Fix Vite SSR agentContext duplication#891
Conversation
Re-export agentContext from the main agents entry point and change @cloudflare/ai-chat to import it from 'agents' instead of the 'agents/internal_context' subpath. This ensures both packages share the same AsyncLocalStorage instance even when Vite SSR pre-bundles @cloudflare/ai-chat separately. Fixes #880
🦋 Changeset detectedLatest commit: 4e5224f The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
/bonk I want to make it so that this isn't a public api, so it should have a scary name "DO NOT USE THIS WILL BREAK" |
commit: |
Co-authored-by: threepointone <threepointone@users.noreply.github.com>
|
Everything looks correct. Here's a summary of the changes: Changes MadeThe Files changed:
Verification:
|
|
/bonk I don't want backward compatibility, this was never a public api, and I'm fine breaking anyone that used it. |
…h export Drop the deprecated agentContext re-export from internal_context.ts and remove the agents/internal_context subpath from package.json exports. This was never a public API.
|
Done. Here's a summary of the changes: Changes Made
Verification
|
* fix: prevent agentContext duplication by Vite SSR pre-bundling Re-export agentContext from the main agents entry point and change @cloudflare/ai-chat to import it from 'agents' instead of the 'agents/internal_context' subpath. This ensures both packages share the same AsyncLocalStorage instance even when Vite SSR pre-bundles @cloudflare/ai-chat separately. Fixes #880 * Rename agentContext export to scary name Co-authored-by: threepointone <threepointone@users.noreply.github.com> * Remove backward-compat agentContext alias and internal_context subpath export Drop the deprecated agentContext re-export from internal_context.ts and remove the agents/internal_context subpath from package.json exports. This was never a public API. --------- Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: threepointone <threepointone@users.noreply.github.com>
The fix is committed and ready. Here's a summary of what was done:
Summary
Problem: When
@cloudflare/ai-chatandagentsare used with Vite SSR, theagentContextAsyncLocalStoragegets duplicated into two separate instances. This happens because@cloudflare/ai-chatimportsagentContextvia the bare specifieragents/internal_context, while theAgentclass inagentsimports it via./internal_context. Vite SSR pre-bundling treats these as separate modules and inlinesagents/internal_contextinto its bundled copy of@cloudflare/ai-chat, creating a secondAsyncLocalStorage— sogetCurrentAgent().connectionreturnsundefined.Fix (2 lines changed):
packages/agents/src/index.ts:3— Addedexport { agentContext } from "./internal_context"to re-exportagentContextfrom the mainagentsentry pointpackages/ai-chat/src/index.ts:17— Changedimport { agentContext } from "agents/internal_context"to importagentContextfrom"agents"(alongsideAgent,Connection, etc.)This ensures that when Vite SSR pre-bundles
@cloudflare/ai-chat, it seesagentContextcoming from the sameagentsmodule as everything else — no separate subpath module for Vite to split and duplicate. The./internal_contextsubpath export is kept for backward compatibility but is no longer used cross-package.Verification: Build passes, all checks pass (sherif, exports, formatting, linting, typecheck across 35 projects), and all 458 Workers tests pass.
Closes #880
github run