|
| 1 | +import fs from "node:fs"; |
| 2 | +import path from "node:path"; |
1 | 3 | import { normalizeProviderId } from "../../agents/model-selection.js"; |
2 | 4 | import type { OpenClawConfig } from "../../config/types.openclaw.js"; |
3 | 5 | import { |
4 | 6 | loadPluginManifestRegistry, |
5 | 7 | type PluginManifestRecord, |
6 | 8 | } from "../../plugins/manifest-registry.js"; |
| 9 | +import { loadPluginManifest, type PluginManifestModelCatalog } from "../../plugins/manifest.js"; |
7 | 10 | import type { PluginMetadataSnapshot } from "../../plugins/plugin-metadata-snapshot.js"; |
8 | 11 |
|
9 | 12 | type ProviderAliasSource = { |
10 | 13 | cfg: OpenClawConfig; |
11 | 14 | metadataSnapshot?: Pick<PluginMetadataSnapshot, "manifestRegistry">; |
12 | 15 | }; |
13 | 16 |
|
| 17 | +const sourcePeerModelCatalogCache = new Map<string, PluginManifestModelCatalog | null>(); |
| 18 | + |
14 | 19 | function listManifestPlugins(params: ProviderAliasSource): readonly PluginManifestRecord[] { |
15 | | - return params.metadataSnapshot?.manifestRegistry.plugins ?? loadPluginManifestRegistry({ |
16 | | - config: params.cfg, |
17 | | - }).plugins; |
| 20 | + return ( |
| 21 | + params.metadataSnapshot?.manifestRegistry.plugins ?? |
| 22 | + loadPluginManifestRegistry({ |
| 23 | + config: params.cfg, |
| 24 | + }).plugins |
| 25 | + ); |
| 26 | +} |
| 27 | + |
| 28 | +function resolveSourcePeerPluginRoot( |
| 29 | + plugin: Pick<PluginManifestRecord, "id" | "origin" | "rootDir">, |
| 30 | +): string | undefined { |
| 31 | + if (plugin.origin !== "bundled") { |
| 32 | + return undefined; |
| 33 | + } |
| 34 | + const parts = path.resolve(plugin.rootDir).split(path.sep); |
| 35 | + const pluginDirName = parts.at(-1); |
| 36 | + const extensionsDirName = parts.at(-2); |
| 37 | + const buildDirName = parts.at(-3); |
| 38 | + if ( |
| 39 | + pluginDirName !== plugin.id || |
| 40 | + extensionsDirName !== "extensions" || |
| 41 | + (buildDirName !== "dist" && buildDirName !== "dist-runtime") |
| 42 | + ) { |
| 43 | + return undefined; |
| 44 | + } |
| 45 | + const packageRoot = parts.slice(0, -3).join(path.sep) || path.sep; |
| 46 | + const sourceRoot = path.join(packageRoot, "extensions", plugin.id); |
| 47 | + return fs.existsSync(path.join(sourceRoot, "openclaw.plugin.json")) ? sourceRoot : undefined; |
| 48 | +} |
| 49 | + |
| 50 | +function loadSourcePeerModelCatalog( |
| 51 | + plugin: Pick<PluginManifestRecord, "id" | "origin" | "rootDir">, |
| 52 | +): PluginManifestModelCatalog | undefined { |
| 53 | + const cacheKey = path.resolve(plugin.rootDir); |
| 54 | + const cached = sourcePeerModelCatalogCache.get(cacheKey); |
| 55 | + if (cached !== undefined) { |
| 56 | + return cached ?? undefined; |
| 57 | + } |
| 58 | + const sourceRoot = resolveSourcePeerPluginRoot(plugin); |
| 59 | + if (!sourceRoot) { |
| 60 | + sourcePeerModelCatalogCache.set(cacheKey, null); |
| 61 | + return undefined; |
| 62 | + } |
| 63 | + const loaded = loadPluginManifest(sourceRoot, false); |
| 64 | + if (!loaded.ok || loaded.manifest.id !== plugin.id) { |
| 65 | + sourcePeerModelCatalogCache.set(cacheKey, null); |
| 66 | + return undefined; |
| 67 | + } |
| 68 | + const modelCatalog = loaded.manifest.modelCatalog ?? null; |
| 69 | + sourcePeerModelCatalogCache.set(cacheKey, modelCatalog); |
| 70 | + return modelCatalog ?? undefined; |
| 71 | +} |
| 72 | + |
| 73 | +function hasModelCatalogAliases(modelCatalog: PluginManifestModelCatalog | undefined): boolean { |
| 74 | + return Object.keys(modelCatalog?.aliases ?? {}).length > 0; |
| 75 | +} |
| 76 | + |
| 77 | +function collectModelCatalogAliases( |
| 78 | + aliases: Map<string, string>, |
| 79 | + modelCatalog: PluginManifestModelCatalog | undefined, |
| 80 | +): void { |
| 81 | + for (const [aliasProvider, target] of Object.entries(modelCatalog?.aliases ?? {})) { |
| 82 | + const alias = normalizeProviderId(aliasProvider); |
| 83 | + const provider = normalizeProviderId(target.provider); |
| 84 | + if (alias && provider) { |
| 85 | + aliases.set(alias, provider); |
| 86 | + } |
| 87 | + } |
18 | 88 | } |
19 | 89 |
|
20 | 90 | function buildProviderAliasMap(params: ProviderAliasSource): ReadonlyMap<string, string> { |
21 | 91 | const aliases = new Map<string, string>(); |
22 | 92 | for (const plugin of listManifestPlugins(params)) { |
23 | | - for (const [aliasProvider, target] of Object.entries(plugin.modelCatalog?.aliases ?? {})) { |
24 | | - const alias = normalizeProviderId(aliasProvider); |
25 | | - const provider = normalizeProviderId(target.provider); |
26 | | - if (alias && provider) { |
27 | | - aliases.set(alias, provider); |
28 | | - } |
| 93 | + collectModelCatalogAliases(aliases, plugin.modelCatalog); |
| 94 | + if (!hasModelCatalogAliases(plugin.modelCatalog) && plugin.origin === "bundled") { |
| 95 | + collectModelCatalogAliases(aliases, loadSourcePeerModelCatalog(plugin)); |
29 | 96 | } |
30 | 97 | } |
31 | 98 | return aliases; |
|
0 commit comments