Skip to content

Commit 39892d4

Browse files
committed
fix(cli): resolve plugin-catalog metadata for configured models list rows
1 parent c44eb53 commit 39892d4

3 files changed

Lines changed: 166 additions & 32 deletions

File tree

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

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ const emptyPluginMetadataSnapshot = vi.hoisted(() => ({
99
plugins: [],
1010
}));
1111

12+
const mocks = vi.hoisted(() => ({
13+
loadPreparedModelCatalogSnapshot: vi.fn(),
14+
normalizeProviderResolvedModelWithPlugin: vi.fn(() => undefined),
15+
shouldSuppressBuiltInModelFromManifest: vi.fn(() => false),
16+
}));
17+
1218
vi.mock("../../agents/provider-model-normalization.runtime.js", () => ({
1319
normalizeProviderModelIdWithRuntime: vi.fn(() => {
1420
throw new Error("runtime model normalization should not load for models list entries");
@@ -19,7 +25,22 @@ vi.mock("../../plugins/current-plugin-metadata-snapshot.js", () => ({
1925
getCurrentPluginMetadataSnapshot: () => emptyPluginMetadataSnapshot,
2026
}));
2127

28+
vi.mock("../../agents/prepared-model-catalog.js", () => ({
29+
loadPreparedModelCatalogSnapshot: mocks.loadPreparedModelCatalogSnapshot,
30+
}));
31+
32+
vi.mock("../../agents/model-suppression.js", () => ({
33+
shouldSuppressBuiltInModel: vi.fn(() => false),
34+
shouldSuppressBuiltInModelFromManifest: mocks.shouldSuppressBuiltInModelFromManifest,
35+
}));
36+
37+
vi.mock("../../plugins/provider-runtime.js", () => ({
38+
normalizeProviderResolvedModelWithPlugin: mocks.normalizeProviderResolvedModelWithPlugin,
39+
}));
40+
2241
import { resolveConfiguredEntries } from "./list.configured.js";
42+
import { appendConfiguredModelRowSources } from "./list.row-sources.js";
43+
import type { ModelRow } from "./list.types.js";
2344

2445
afterEach(() => {
2546
vi.unstubAllEnvs();
@@ -179,3 +200,93 @@ describe("resolveConfiguredEntries", () => {
179200
}
180201
});
181202
});
203+
204+
describe("configured model list rows", () => {
205+
it("renders plugin-catalog metadata for a fallback ref instead of default placeholders", async () => {
206+
const catalogEntry = {
207+
id: "k3",
208+
name: "Kimi K3",
209+
provider: "kimi",
210+
api: "openai-completions" as const,
211+
baseUrl: "https://api.moonshot.ai/v1",
212+
contextWindow: 1_048_576,
213+
input: ["text", "image"] as const,
214+
};
215+
// Conflicting catalog metadata for an explicitly configured provider model:
216+
// the user's models.providers definition must win over the catalog row.
217+
const conflictingCatalogEntry = {
218+
id: "own-model",
219+
name: "Catalog Own Model",
220+
provider: "custom",
221+
contextWindow: 999_999,
222+
input: ["text", "image"] as const,
223+
};
224+
mocks.loadPreparedModelCatalogSnapshot.mockResolvedValue({
225+
entries: [catalogEntry, conflictingCatalogEntry],
226+
routeVariants: [catalogEntry, conflictingCatalogEntry],
227+
});
228+
const cfg = {
229+
agents: {
230+
defaults: {
231+
model: { primary: "kimi/kimi-for-coding", fallbacks: ["kimi/k3", "custom/own-model"] },
232+
},
233+
},
234+
models: {
235+
providers: {
236+
custom: {
237+
api: "openai-completions" as const,
238+
baseUrl: "https://custom.test/v1",
239+
models: [
240+
{
241+
id: "own-model",
242+
name: "Own Model",
243+
reasoning: false,
244+
input: ["text" as const],
245+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
246+
contextWindow: 32_000,
247+
maxTokens: 8_000,
248+
},
249+
],
250+
},
251+
},
252+
},
253+
};
254+
const { entries } = resolveConfiguredEntries(cfg);
255+
const rows: ModelRow[] = [];
256+
257+
await appendConfiguredModelRowSources({
258+
rows,
259+
entries,
260+
context: {
261+
cfg,
262+
agentDir: "/tmp/openclaw-agent",
263+
authIndex: { evaluateModelAuth: () => ({ availability: true, routeResolution: null }) },
264+
configuredByKey: new Map(entries.map((entry) => [entry.key, entry])),
265+
discoveredKeys: new Set<string>(),
266+
filter: {},
267+
skipRuntimeModelSuppression: true,
268+
},
269+
});
270+
271+
const fallbackRow = rows.find((row) => row.key === "kimi/k3");
272+
expect(fallbackRow).toMatchObject({
273+
name: "Kimi K3",
274+
input: "text+image",
275+
contextWindow: 1_048_576,
276+
});
277+
expect(fallbackRow?.tags).toContain("fallback#1");
278+
// Explicit models.providers metadata beats the conflicting catalog row.
279+
expect(rows.find((row) => row.key === "custom/own-model")).toMatchObject({
280+
name: "Own Model",
281+
input: "text",
282+
contextWindow: 32_000,
283+
});
284+
// Refs the catalog does not know still fall back to the default placeholder row.
285+
expect(rows.find((row) => row.key === "kimi/kimi-for-coding")).toMatchObject({
286+
input: "text",
287+
contextWindow: 200_000,
288+
});
289+
// The catalog is a single committed generation; configured and catalog rows share one load.
290+
expect(mocks.loadPreparedModelCatalogSnapshot).toHaveBeenCalledTimes(1);
291+
});
292+
});

‎src/commands/models/list.row-sources.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
appendConfiguredRows,
77
appendDiscoveredRows,
88
appendPreparedModelCatalogRows,
9+
loadListModelCatalogSnapshot,
910
type RowBuilderContext,
1011
} from "./list.rows.js";
1112
import type { ConfiguredEntry, ModelRow } from "./list.types.js";
@@ -69,7 +70,11 @@ export async function appendConfiguredModelRowSources(params: {
6970
modelRegistry?: ModelRegistry;
7071
context: RowBuilderContext;
7172
}): Promise<void> {
72-
await appendConfiguredRows(params);
73+
// Configured rows are emitted first for tag ordering, so they must read the
74+
// same committed generation the catalog rows below use; otherwise a ref that
75+
// only exists in a plugin catalog renders default placeholder metadata.
76+
const catalogSnapshot = await loadListModelCatalogSnapshot(params.context);
77+
await appendConfiguredRows({ ...params, catalogSnapshot });
7378
const seenKeys = new Set(params.rows.map((row) => row.key));
7479
await appendConfiguredProviderRows({
7580
rows: params.rows,
@@ -80,5 +85,6 @@ export async function appendConfiguredModelRowSources(params: {
8085
rows: params.rows,
8186
context: params.context,
8287
seenKeys,
88+
catalogSnapshot,
8389
});
8490
}

‎src/commands/models/list.rows.ts‎

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -424,13 +424,21 @@ function findConfiguredProviderModel(params: {
424424
});
425425
}
426426

427-
function toFallbackConfiguredListModel(entry: ConfiguredEntry, cfg: OpenClawConfig): ListRowModel {
427+
function toFallbackConfiguredListModel(
428+
entry: ConfiguredEntry,
429+
cfg: OpenClawConfig,
430+
catalogEntry?: ModelCatalogEntry,
431+
): ListRowModel {
432+
// Explicit models.providers definitions stay authoritative; the prepared
433+
// catalog fills plugin-owned refs so this view matches `--all`, and the
434+
// placeholder is a last resort for refs nothing knows.
428435
return (
429436
findConfiguredProviderModel({
430437
cfg,
431438
provider: entry.ref.provider,
432439
modelId: entry.ref.model,
433-
}) ?? {
440+
}) ??
441+
(catalogEntry ? toPreparedCatalogListModel(catalogEntry) : undefined) ?? {
434442
provider: entry.ref.provider,
435443
id: entry.ref.model,
436444
name: entry.ref.model,
@@ -440,6 +448,35 @@ function toFallbackConfiguredListModel(entry: ConfiguredEntry, cfg: OpenClawConf
440448
);
441449
}
442450

451+
/** Loads the committed catalog generation shared by every model-list row source. */
452+
export async function loadListModelCatalogSnapshot(
453+
context: RowBuilderContext,
454+
): Promise<ModelCatalogSnapshot> {
455+
const { loadPreparedModelCatalogSnapshot } = await loadPreparedModelCatalogModule();
456+
const workspaceDir = context.workspaceDir ?? context.metadataSnapshot?.workspaceDir;
457+
return loadPreparedModelCatalogSnapshot({
458+
config: context.cfg,
459+
...(context.agentId ? { agentId: context.agentId } : {}),
460+
agentDir: context.agentDir,
461+
...(workspaceDir ? { workspaceDir } : {}),
462+
readOnly: true,
463+
});
464+
}
465+
466+
/** Indexes a catalog generation by model key so configured refs can reuse its metadata. */
467+
function indexModelCatalogEntriesByKey(
468+
snapshot: ModelCatalogSnapshot,
469+
): ReadonlyMap<string, ModelCatalogEntry> {
470+
const byKey = new Map<string, ModelCatalogEntry>();
471+
for (const entry of [...snapshot.entries, ...(snapshot.staticEntries ?? [])]) {
472+
const key = modelKey(entry.provider, entry.id);
473+
if (!byKey.has(key)) {
474+
byKey.set(key, entry);
475+
}
476+
}
477+
return byKey;
478+
}
479+
443480
/** Appends rows discovered from the loaded model registry. */
444481
export async function appendDiscoveredRows(params: {
445482
rows: ModelRow[];
@@ -538,20 +575,10 @@ export async function appendAuthenticatedCatalogRows(params: {
538575
rows: ModelRow[];
539576
context: RowBuilderContext;
540577
seenKeys: Set<string>;
578+
catalogSnapshot?: ModelCatalogSnapshot;
541579
}): Promise<void> {
542-
const { loadPreparedModelCatalogSnapshot } = await loadPreparedModelCatalogModule();
543-
const { entries: catalog, routeVariants } = await loadPreparedModelCatalogSnapshot({
544-
config: params.context.cfg,
545-
...(params.context.agentId ? { agentId: params.context.agentId } : {}),
546-
agentDir: params.context.agentDir,
547-
...((params.context.workspaceDir ?? params.context.metadataSnapshot?.workspaceDir)
548-
? {
549-
workspaceDir:
550-
params.context.workspaceDir ?? params.context.metadataSnapshot?.workspaceDir,
551-
}
552-
: {}),
553-
readOnly: true,
554-
});
580+
const { entries: catalog, routeVariants } =
581+
params.catalogSnapshot ?? (await loadListModelCatalogSnapshot(params.context));
555582
const routeIndex = createModelCatalogLogicalRouteIndex(routeVariants);
556583
for (const entry of catalog) {
557584
const model = toPreparedCatalogListModel(entry);
@@ -587,21 +614,7 @@ export async function appendPreparedModelCatalogRows(params: {
587614
catalogSnapshot?: ModelCatalogSnapshot;
588615
}): Promise<void> {
589616
const catalogSnapshot =
590-
params.catalogSnapshot ??
591-
(await (
592-
await loadPreparedModelCatalogModule()
593-
).loadPreparedModelCatalogSnapshot({
594-
config: params.context.cfg,
595-
...(params.context.agentId ? { agentId: params.context.agentId } : {}),
596-
agentDir: params.context.agentDir,
597-
...((params.context.workspaceDir ?? params.context.metadataSnapshot?.workspaceDir)
598-
? {
599-
workspaceDir:
600-
params.context.workspaceDir ?? params.context.metadataSnapshot?.workspaceDir,
601-
}
602-
: {}),
603-
readOnly: true,
604-
}));
617+
params.catalogSnapshot ?? (await loadListModelCatalogSnapshot(params.context));
605618
const staticEntries = catalogSnapshot.staticEntries ?? [];
606619
const routeVariants = [...catalogSnapshot.routeVariants];
607620
const seenRouteVariants = new Set(
@@ -640,10 +653,14 @@ export async function appendConfiguredRows(params: {
640653
entries: ConfiguredEntry[];
641654
modelRegistry?: ModelRegistry;
642655
context: RowBuilderContext;
656+
catalogSnapshot?: ModelCatalogSnapshot;
643657
}): Promise<void> {
644658
const resolveModelWithRegistry = params.modelRegistry
645659
? (await loadModelResolverModule()).resolveModelWithRegistry
646660
: undefined;
661+
const catalogByKey = params.catalogSnapshot
662+
? indexModelCatalogEntriesByKey(params.catalogSnapshot)
663+
: undefined;
647664
for (const entry of params.entries) {
648665
if (!matchesProviderFilter(params.context, entry.ref.provider)) {
649666
continue;
@@ -656,7 +673,7 @@ export async function appendConfiguredRows(params: {
656673
modelRegistry: params.modelRegistry,
657674
cfg: params.context.cfg,
658675
})
659-
: toFallbackConfiguredListModel(entry, params.context.cfg);
676+
: toFallbackConfiguredListModel(entry, params.context.cfg, catalogByKey?.get(entry.key));
660677
const model = resolvedModel
661678
? normalizeListRowWithProviderPlugin({ model: resolvedModel, context: params.context })
662679
: resolvedModel;

0 commit comments

Comments
 (0)