|
| 1 | +import type { OpenClawConfig } from "../config/types.openclaw.js"; |
| 2 | +import { |
| 3 | + buildModelCatalogMergeKey, |
| 4 | + planManifestModelCatalogSuppressions, |
| 5 | + type ManifestModelCatalogSuppressionEntry, |
| 6 | +} from "../model-catalog/index.js"; |
| 7 | +import { normalizeLowercaseStringOrEmpty } from "../shared/string-coerce.js"; |
| 8 | +import { loadPluginManifestRegistryForPluginRegistry } from "./plugin-registry.js"; |
| 9 | + |
| 10 | +type ManifestSuppressionCache = Map<string, readonly ManifestModelCatalogSuppressionEntry[]>; |
| 11 | + |
| 12 | +let cacheWithoutConfig = new WeakMap<NodeJS.ProcessEnv, ManifestSuppressionCache>(); |
| 13 | +let cacheByConfig = new WeakMap< |
| 14 | + OpenClawConfig, |
| 15 | + WeakMap<NodeJS.ProcessEnv, ManifestSuppressionCache> |
| 16 | +>(); |
| 17 | + |
| 18 | +function resolveSuppressionCache(params: { |
| 19 | + config?: OpenClawConfig; |
| 20 | + env: NodeJS.ProcessEnv; |
| 21 | +}): ManifestSuppressionCache { |
| 22 | + if (!params.config) { |
| 23 | + let cache = cacheWithoutConfig.get(params.env); |
| 24 | + if (!cache) { |
| 25 | + cache = new Map(); |
| 26 | + cacheWithoutConfig.set(params.env, cache); |
| 27 | + } |
| 28 | + return cache; |
| 29 | + } |
| 30 | + let envCaches = cacheByConfig.get(params.config); |
| 31 | + if (!envCaches) { |
| 32 | + envCaches = new WeakMap(); |
| 33 | + cacheByConfig.set(params.config, envCaches); |
| 34 | + } |
| 35 | + let cache = envCaches.get(params.env); |
| 36 | + if (!cache) { |
| 37 | + cache = new Map(); |
| 38 | + envCaches.set(params.env, cache); |
| 39 | + } |
| 40 | + return cache; |
| 41 | +} |
| 42 | + |
| 43 | +function cacheKey(params: { workspaceDir?: string }): string { |
| 44 | + return params.workspaceDir ?? ""; |
| 45 | +} |
| 46 | + |
| 47 | +function listManifestModelCatalogSuppressions(params: { |
| 48 | + config?: OpenClawConfig; |
| 49 | + workspaceDir?: string; |
| 50 | + env: NodeJS.ProcessEnv; |
| 51 | +}): readonly ManifestModelCatalogSuppressionEntry[] { |
| 52 | + const cache = resolveSuppressionCache({ |
| 53 | + config: params.config, |
| 54 | + env: params.env, |
| 55 | + }); |
| 56 | + const key = cacheKey(params); |
| 57 | + const cached = cache.get(key); |
| 58 | + if (cached) { |
| 59 | + return cached; |
| 60 | + } |
| 61 | + const registry = loadPluginManifestRegistryForPluginRegistry({ |
| 62 | + config: params.config, |
| 63 | + workspaceDir: params.workspaceDir, |
| 64 | + env: params.env, |
| 65 | + }); |
| 66 | + const planned = planManifestModelCatalogSuppressions({ registry }); |
| 67 | + cache.set(key, planned.suppressions); |
| 68 | + return planned.suppressions; |
| 69 | +} |
| 70 | + |
| 71 | +function buildManifestSuppressionError(params: { |
| 72 | + provider: string; |
| 73 | + modelId: string; |
| 74 | + reason?: string; |
| 75 | +}): string { |
| 76 | + const ref = `${params.provider}/${params.modelId}`; |
| 77 | + return params.reason ? `Unknown model: ${ref}. ${params.reason}` : `Unknown model: ${ref}.`; |
| 78 | +} |
| 79 | + |
| 80 | +export function clearManifestModelSuppressionCacheForTest(): void { |
| 81 | + cacheWithoutConfig = new WeakMap<NodeJS.ProcessEnv, ManifestSuppressionCache>(); |
| 82 | + cacheByConfig = new WeakMap< |
| 83 | + OpenClawConfig, |
| 84 | + WeakMap<NodeJS.ProcessEnv, ManifestSuppressionCache> |
| 85 | + >(); |
| 86 | +} |
| 87 | + |
| 88 | +export function resolveManifestBuiltInModelSuppression(params: { |
| 89 | + provider?: string | null; |
| 90 | + id?: string | null; |
| 91 | + config?: OpenClawConfig; |
| 92 | + workspaceDir?: string; |
| 93 | + env?: NodeJS.ProcessEnv; |
| 94 | +}) { |
| 95 | + const provider = normalizeLowercaseStringOrEmpty(params.provider); |
| 96 | + const modelId = normalizeLowercaseStringOrEmpty(params.id); |
| 97 | + if (!provider || !modelId) { |
| 98 | + return undefined; |
| 99 | + } |
| 100 | + const mergeKey = buildModelCatalogMergeKey(provider, modelId); |
| 101 | + const suppression = listManifestModelCatalogSuppressions({ |
| 102 | + config: params.config, |
| 103 | + workspaceDir: params.workspaceDir, |
| 104 | + env: params.env ?? process.env, |
| 105 | + }).find((entry) => entry.mergeKey === mergeKey); |
| 106 | + if (!suppression) { |
| 107 | + return undefined; |
| 108 | + } |
| 109 | + return { |
| 110 | + suppress: true, |
| 111 | + errorMessage: buildManifestSuppressionError({ |
| 112 | + provider, |
| 113 | + modelId, |
| 114 | + reason: suppression.reason, |
| 115 | + }), |
| 116 | + }; |
| 117 | +} |
0 commit comments