fix: keep provider discovery on mockable lazy runtime paths

This commit is contained in:
Peter Steinberger
2026-03-28 10:38:45 +00:00
parent ff01d749fc
commit e34a770b8a
9 changed files with 103 additions and 21 deletions

View File

@@ -0,0 +1,39 @@
import { createRequire } from "node:module";
type ProviderRuntimeModule = Pick<
typeof import("../plugins/provider-runtime.js"),
"normalizeProviderModelIdWithPlugin"
>;
const require = createRequire(import.meta.url);
const PROVIDER_RUNTIME_CANDIDATES = [
"../plugins/provider-runtime.js",
"../plugins/provider-runtime.ts",
] as const;
let providerRuntimeModule: ProviderRuntimeModule | undefined;
function loadProviderRuntime(): ProviderRuntimeModule | null {
if (providerRuntimeModule) {
return providerRuntimeModule;
}
for (const candidate of PROVIDER_RUNTIME_CANDIDATES) {
try {
providerRuntimeModule = require(candidate) as ProviderRuntimeModule;
return providerRuntimeModule;
} catch {
// Try source/runtime candidates in order.
}
}
return null;
}
export function normalizeProviderModelIdWithRuntime(params: {
provider: string;
context: {
provider: string;
modelId: string;
};
}): string | undefined {
return loadProviderRuntime()?.normalizeProviderModelIdWithPlugin(params);
}