





















@@ -63,7 +63,19 @@ type ModelsJsonCostCache = {
6363rawEntries: Map<string, ModelCostConfig> | null;
6464};
656566+type ProviderCostIndexCacheEntry = {
67+fingerprint: string;
68+normalizedEntries?: Map<string, ModelCostConfig>;
69+rawEntries?: Map<string, ModelCostConfig>;
70+};
71+72+const EMPTY_PROVIDER_COST_INDEX = new Map<string, ModelCostConfig>();
73+6674let modelsJsonCostCache: ModelsJsonCostCache | null = null;
75+let providerCostIndexByConfig = new WeakMap<
76+Record<string, ModelProviderConfig>,
77+ProviderCostIndexCacheEntry
78+>();
67796880export function formatTokenCount(value?: number): string {
6981if (value === undefined || !Number.isFinite(value)) {
@@ -108,6 +120,7 @@ function toResolvedModelKey(params: {
108120return null;
109121}
110122const normalized = normalizeModelRef(provider, model, {
123+allowManifestNormalization: params.allowPluginNormalization === false ? false : undefined,
111124allowPluginNormalization: params.allowPluginNormalization,
112125});
113126return modelKey(normalized.provider, normalized.model);
@@ -174,7 +187,7 @@ function normalizeTieredPricing(raw: RawPricingTier[] | undefined): PricingTier[
174187175188function buildProviderCostIndex(
176189providers: Record<string, ModelProviderConfig> | undefined,
177-options?: { allowPluginNormalization?: boolean },
190+options?: { allowManifestNormalization?: boolean; allowPluginNormalization?: boolean },
178191): Map<string, ModelCostConfig> {
179192const entries = new Map<string, ModelCostConfig>();
180193if (!providers) {
@@ -184,6 +197,9 @@ function buildProviderCostIndex(
184197const normalizedProvider = normalizeProviderId(providerKey);
185198for (const model of providerConfig?.models ?? []) {
186199const normalized = normalizeModelRef(normalizedProvider, model.id, {
200+allowManifestNormalization:
201+options?.allowManifestNormalization ??
202+(options?.allowPluginNormalization === false ? false : undefined),
187203allowPluginNormalization: options?.allowPluginNormalization,
188204});
189205const cost = { ...model.cost };
@@ -201,6 +217,41 @@ function buildProviderCostIndex(
201217return entries;
202218}
203219220+function getProviderCostIndex(
221+providers: Record<string, ModelProviderConfig> | undefined,
222+options?: { allowManifestNormalization?: boolean; allowPluginNormalization?: boolean },
223+): Map<string, ModelCostConfig> {
224+if (!providers) {
225+return EMPTY_PROVIDER_COST_INDEX;
226+}
227+const isRawLookup =
228+options?.allowPluginNormalization === false &&
229+(options.allowManifestNormalization === false ||
230+options.allowManifestNormalization === undefined);
231+const isDefaultNormalizedLookup =
232+options?.allowPluginNormalization !== false &&
233+options?.allowManifestNormalization === undefined;
234+if (!isRawLookup && !isDefaultNormalizedLookup) {
235+return buildProviderCostIndex(providers, options);
236+}
237+238+const fingerprint = stableCostFingerprintValue(providers);
239+let cache = providerCostIndexByConfig.get(providers);
240+if (!cache || cache.fingerprint !== fingerprint) {
241+cache = { fingerprint };
242+providerCostIndexByConfig.set(providers, cache);
243+}
244+if (isRawLookup) {
245+cache.rawEntries ??= buildProviderCostIndex(providers, {
246+allowManifestNormalization: false,
247+allowPluginNormalization: false,
248+});
249+return cache.rawEntries;
250+}
251+cache.normalizedEntries ??= buildProviderCostIndex(providers);
252+return cache.normalizedEntries;
253+}
254+204255function loadModelsJsonCostIndex(options?: {
205256allowPluginNormalization?: boolean;
206257}): Map<string, ModelCostConfig> {
@@ -226,13 +277,13 @@ function loadModelsJsonCostIndex(options?: {
226277}
227278228279if (useRawEntries) {
229-modelsJsonCostCache.rawEntries ??= buildProviderCostIndex(modelsJsonCostCache.providers, {
280+modelsJsonCostCache.rawEntries ??= getProviderCostIndex(modelsJsonCostCache.providers, {
230281allowPluginNormalization: false,
231282});
232283return modelsJsonCostCache.rawEntries;
233284}
234285235-modelsJsonCostCache.normalizedEntries ??= buildProviderCostIndex(modelsJsonCostCache.providers);
286+modelsJsonCostCache.normalizedEntries ??= getProviderCostIndex(modelsJsonCostCache.providers);
236287return modelsJsonCostCache.normalizedEntries;
237288} catch {
238289const empty = new Map<string, ModelCostConfig>();
@@ -257,7 +308,7 @@ function findConfiguredProviderCost(params: {
257308if (!key) {
258309return undefined;
259310}
260-return buildProviderCostIndex(params.config?.models?.providers, {
311+return getProviderCostIndex(params.config?.models?.providers, {
261312allowPluginNormalization: params.allowPluginNormalization,
262313}).get(key);
263314}
@@ -289,9 +340,9 @@ function serializeCostIndex(
289340export function resolveModelCostConfigFingerprint(config?: OpenClawConfig): string {
290341return stableCostFingerprintValue({
291342configuredRaw: serializeCostIndex(
292-buildProviderCostIndex(config?.models?.providers, { allowPluginNormalization: false }),
343+getProviderCostIndex(config?.models?.providers, { allowPluginNormalization: false }),
293344),
294-configuredNormalized: serializeCostIndex(buildProviderCostIndex(config?.models?.providers)),
345+configuredNormalized: serializeCostIndex(getProviderCostIndex(config?.models?.providers)),
295346modelsJsonRaw: serializeCostIndex(loadModelsJsonCostIndex({ allowPluginNormalization: false })),
296347modelsJsonNormalized: serializeCostIndex(loadModelsJsonCostIndex()),
297348gatewayPricing: getGatewayModelPricingCacheFingerprint(),
@@ -430,4 +481,5 @@ export function estimateUsageCost(params: {
430481431482export function resetUsageFormatCachesForTest(): void {
432483modelsJsonCostCache = null;
484+providerCostIndexByConfig = new WeakMap();
433485}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。