From 5c6ce2b32f8b559c216fbf56de2b4376b965ea0e Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Thu, 5 Nov 2026 00:39:49 +0100 Subject: [PATCH] fix(ai): resolve variable lazy imports against the module URL Chord's worker base inlines pi-ai into a bundle in a temporary directory. esbuild keeps the variable-specifier imports of the OAuth flows and the bedrock API, so Node resolved them against the bundle and failed (radius OAuth: Cannot find module .../chord-worker-*/radius.ts). Resolving against import.meta.url, which Chord defines per source module, finds the real file. --- packages/ai/src/api/bedrock-converse-stream.lazy.ts | 6 ++++-- packages/ai/src/auth/oauth/load.ts | 7 +++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/ai/src/api/bedrock-converse-stream.lazy.ts b/packages/ai/src/api/bedrock-converse-stream.lazy.ts index f9b30b35f..b0530a1fb 100644 --- a/packages/ai/src/api/bedrock-converse-stream.lazy.ts +++ b/packages/ai/src/api/bedrock-converse-stream.lazy.ts @@ -5,11 +5,13 @@ import { lazyApi } from "./lazy.ts"; * Loads the bedrock implementation through a variable specifier so bundlers * (browser smoke, Bun compile) cannot follow the import into the Node-only * AWS SDK. The `.ts`/`.js` rewrite keeps the trick working from both source - * and built output. + * and built output. The specifier is resolved against this module's URL, not + * left relative: a bundler inlining this file (Chord's worker base) keeps the + * variable import, which would otherwise resolve against the bundle's location. */ const importNodeOnlyApi = (specifier: string): Promise => { const runtimeSpecifier = import.meta.url.endsWith(".js") ? specifier.replace(/\.ts$/, ".js") : specifier; - return import(runtimeSpecifier); + return import(new URL(runtimeSpecifier, import.meta.url).href); }; let bedrockModuleOverride: ProviderStreams | undefined; diff --git a/packages/ai/src/auth/oauth/load.ts b/packages/ai/src/auth/oauth/load.ts index 839fe3152..be63e8acf 100644 --- a/packages/ai/src/auth/oauth/load.ts +++ b/packages/ai/src/auth/oauth/load.ts @@ -4,11 +4,14 @@ import type { OAuthAuth } from "../types.ts"; * Loads an OAuth flow module through a variable specifier so bundlers cannot * follow the import into Node-only flow code (`node:http` callback servers, * `node:crypto` PKCE). The `.ts`/`.js` rewrite keeps the trick working from - * both source and built output. + * both source and built output. The specifier is resolved against this + * module's URL, not left relative: a bundler inlining this file (Chord's + * worker base) keeps the variable import, which would otherwise resolve + * against the bundle's location. */ const importOAuthModule = (specifier: string): Promise => { const runtimeSpecifier = import.meta.url.endsWith(".js") ? specifier.replace(/\.ts$/, ".js") : specifier; - return import(runtimeSpecifier); + return import(new URL(runtimeSpecifier, import.meta.url).href); }; type OAuthFlowLoaders = {