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.
This commit is contained in:
Armin Ronacher 2026-11-05 00:39:49 +01:00
parent 80cb316cd7
commit 5c6ce2b32f
2 changed files with 9 additions and 4 deletions

View file

@ -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<unknown> => {
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;

View file

@ -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<unknown> => {
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 = {