Files
openclaw/src/agents/provider-model-normalization.runtime.ts
Edward Abrams ef3e5f5e31 perf(plugins): thread prepared manifest plugins through runtime normalization (#85254)
Carry prepared manifest model-id normalization records through the runtime bridge so hot callers reuse existing metadata instead of consulting the snapshot fallback.

The final change preserves the existing no-prepared-record behavior, adds focused forwarding coverage, and removes the one-off proof script before landing.

Thanks @zeroaltitude.

Verification:
- 224 focused tests
- full CI run 27594070734
- real behavior proof run 27594081022
- final whole-branch autoreview clean

Co-authored-by: zeroaltitude <zeroaltitude@gmail.com>
2026-06-16 06:31:36 +02:00

55 lines
1.7 KiB
TypeScript

/**
* Runtime bridge for provider-owned model id normalization hooks. Source and
* built artifacts can resolve different extensions, so this module probes both
* once and caches the result.
*/
import { createRequire } from "node:module";
import type { PluginManifestRecord } from "../plugins/manifest-registry.js";
type ProviderRuntimeModule = Pick<
typeof import("../plugins/provider-runtime.js"),
"normalizeProviderModelIdWithPlugin"
>;
const require = createRequire(import.meta.url);
// Built code loads .js while source/test paths may still resolve .ts. Try both
// once, then cache the absence to avoid repeated require work on hot paths.
const PROVIDER_RUNTIME_CANDIDATES = [
"../plugins/provider-runtime.js",
"../plugins/provider-runtime.ts",
] as const;
let providerRuntimeModule: ProviderRuntimeModule | undefined;
let providerRuntimeLoadAttempted = false;
function loadProviderRuntime(): ProviderRuntimeModule | null {
if (providerRuntimeModule) {
return providerRuntimeModule;
}
if (providerRuntimeLoadAttempted) {
return null;
}
providerRuntimeLoadAttempted = true;
for (const candidate of PROVIDER_RUNTIME_CANDIDATES) {
try {
providerRuntimeModule = require(candidate) as ProviderRuntimeModule;
return providerRuntimeModule;
} catch {
// Try source/runtime candidates in order.
}
}
return null;
}
/** Normalizes provider model ids through plugin runtime hooks when available. */
export function normalizeProviderModelIdWithRuntime(params: {
provider: string;
plugins?: readonly Pick<PluginManifestRecord, "modelIdNormalization">[];
context: {
provider: string;
modelId: string;
};
}): string | undefined {
return loadProviderRuntime()?.normalizeProviderModelIdWithPlugin(params);
}