Skip to content

Commit 7098e33

Browse files
committed
fix(models): recover bundled provider aliases
1 parent 6d5061c commit 7098e33

2 files changed

Lines changed: 137 additions & 9 deletions

File tree

‎src/commands/models/list.configured.test.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import fs from "node:fs";
2+
import os from "node:os";
13
import path from "node:path";
24
import { afterEach, describe, expect, it, vi } from "vitest";
35

@@ -116,4 +118,63 @@ describe("resolveConfiguredEntries", () => {
116118
expect(entries[0]?.aliases).toEqual(["GLM"]);
117119
expect(entries[0]?.tags).toEqual(new Set(["default", "configured"]));
118120
});
121+
122+
it("recovers bundled source aliases when stale dist metadata omits them", () => {
123+
const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-model-alias-source-"));
124+
try {
125+
const distPluginRoot = path.join(root, "dist", "extensions", "zai");
126+
const sourcePluginRoot = path.join(root, "extensions", "zai");
127+
fs.mkdirSync(distPluginRoot, { recursive: true });
128+
fs.mkdirSync(sourcePluginRoot, { recursive: true });
129+
fs.writeFileSync(
130+
path.join(sourcePluginRoot, "openclaw.plugin.json"),
131+
JSON.stringify({
132+
id: "zai",
133+
configSchema: { type: "object" },
134+
providers: ["zai"],
135+
modelCatalog: {
136+
aliases: {
137+
"z.ai": { provider: "zai" },
138+
},
139+
},
140+
}),
141+
"utf8",
142+
);
143+
144+
const { entries } = resolveConfiguredEntries(
145+
{
146+
agents: {
147+
defaults: {
148+
model: { primary: "z.ai/glm-4.7" },
149+
},
150+
},
151+
models: { providers: {} },
152+
},
153+
{
154+
manifestRegistry: {
155+
diagnostics: [],
156+
plugins: [
157+
{
158+
id: "zai",
159+
origin: "bundled",
160+
rootDir: distPluginRoot,
161+
source: path.join(distPluginRoot, "index.js"),
162+
providers: ["zai"],
163+
channels: [],
164+
cliBackends: [],
165+
skills: [],
166+
hooks: [],
167+
modelCatalog: { providers: {}, discovery: { zai: "static" } },
168+
manifestPath: path.join(distPluginRoot, "openclaw.plugin.json"),
169+
},
170+
],
171+
},
172+
},
173+
);
174+
175+
expect(entries.map((entry) => entry.key)).toEqual(["zai/glm-4.7"]);
176+
} finally {
177+
fs.rmSync(root, { recursive: true, force: true });
178+
}
179+
});
119180
});

‎src/commands/models/provider-aliases.ts‎

Lines changed: 76 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,98 @@
1+
import fs from "node:fs";
2+
import path from "node:path";
13
import { normalizeProviderId } from "../../agents/model-selection.js";
24
import type { OpenClawConfig } from "../../config/types.openclaw.js";
35
import {
46
loadPluginManifestRegistry,
57
type PluginManifestRecord,
68
} from "../../plugins/manifest-registry.js";
9+
import { loadPluginManifest, type PluginManifestModelCatalog } from "../../plugins/manifest.js";
710
import type { PluginMetadataSnapshot } from "../../plugins/plugin-metadata-snapshot.js";
811

912
type ProviderAliasSource = {
1013
cfg: OpenClawConfig;
1114
metadataSnapshot?: Pick<PluginMetadataSnapshot, "manifestRegistry">;
1215
};
1316

17+
const sourcePeerModelCatalogCache = new Map<string, PluginManifestModelCatalog | null>();
18+
1419
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+
}
1888
}
1989

2090
function buildProviderAliasMap(params: ProviderAliasSource): ReadonlyMap<string, string> {
2191
const aliases = new Map<string, string>();
2292
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));
2996
}
3097
}
3198
return aliases;

0 commit comments

Comments
 (0)