diff --git a/src/commands/models/list.rows.projection.test.ts b/src/commands/models/list.rows.projection.test.ts new file mode 100644 index 00000000000..b44117bcb17 --- /dev/null +++ b/src/commands/models/list.rows.projection.test.ts @@ -0,0 +1,79 @@ +import { describe, expect, it, vi } from "vitest"; +import type { ModelProviderConfig } from "../../config/types.models.js"; +import type { Model } from "../../llm/types.js"; +import { appendDiscoveredRows, type RowBuilderContext } from "./list.rows.js"; +import type { ModelRow } from "./list.types.js"; + +describe("appendDiscoveredRows projection", () => { + it("does not repeat configured metadata lookup after filtering", async () => { + const providerCatalogScan = vi.fn(); + const providers = new Proxy>( + { + bench: { + api: "openai-completions", + baseUrl: "https://models.example.test/v1", + models: [ + { + id: "model-1", + name: "Configured Model", + reasoning: false, + input: ["text", "image"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 64_000, + maxTokens: 4096, + }, + ], + }, + }, + { + ownKeys(target) { + providerCatalogScan(); + return Reflect.ownKeys(target); + }, + }, + ); + const rows: ModelRow[] = []; + const models: Model[] = [ + { + id: "model-1", + name: "Catalog Model", + api: "openai-completions", + provider: "bench", + baseUrl: "https://models.example.test/v1", + input: ["text"], + reasoning: false, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 8192, + maxTokens: 4096, + }, + ]; + const context: RowBuilderContext = { + cfg: { models: { providers } }, + agentDir: "/tmp/openclaw-agent", + authIndex: { + evaluateModelAuth: () => ({ availability: true, routeResolution: null }), + }, + configuredByKey: new Map(), + discoveredKeys: new Set(["bench/model-1"]), + filter: {}, + skipRuntimeModelSuppression: true, + }; + + await appendDiscoveredRows({ + rows, + models, + context, + }); + + expect(providerCatalogScan.mock.calls.length).toBeLessThanOrEqual(1); + expect(rows).toEqual([ + expect.objectContaining({ + key: "bench/model-1", + name: "Configured Model", + input: "text+image", + contextWindow: 64_000, + available: true, + }), + ]); + }); +}); diff --git a/src/commands/models/list.rows.ts b/src/commands/models/list.rows.ts index 38fa3e9cd82..949f1a6388a 100644 --- a/src/commands/models/list.rows.ts +++ b/src/commands/models/list.rows.ts @@ -193,33 +193,22 @@ async function buildRow(params: { model: ListRowModel; key: string; context: RowBuilderContext; - routeIndex?: ModelCatalogLogicalRouteIndex; - authEvaluation?: ModelListAuthEvaluation; + authEvaluation: ModelListAuthEvaluation; allowAuthAvailabilityOverride?: boolean; configuredEntry?: ConfiguredEntry; }): Promise { const configured = params.configuredEntry ?? params.context.configuredByKey.get(params.key); - const authRef = toModelAuthRef(params.model, params.routeIndex); - const authEvaluation = - params.authEvaluation ?? - params.context.authIndex.evaluateModelAuth(params.model.provider, authRef); - const model = projectListRowModel({ - model: params.model, - evaluation: authEvaluation, - cfg: params.context.cfg, - ...(params.routeIndex ? { routeIndex: params.routeIndex } : {}), - }); return toModelRow({ - model, + model: params.model, key: params.key, tags: configured ? Array.from(configured.tags) : [], aliases: configured?.aliases ?? [], availableKeys: params.context.availableKeys, - authAvailability: authEvaluation.availability, + authAvailability: params.authEvaluation.availability, authAvailabilityAuthoritative: params.allowAuthAvailabilityOverride === true || normalizeProviderIdForAuth(params.model.provider) === "openai" || - authEvaluation.routeResolution !== null, + params.authEvaluation.routeResolution !== null, }); } @@ -288,10 +277,9 @@ async function appendVisibleRow(params: { } params.rows.push( await buildRow({ - model, + model: projectedModel, key: params.key, context: params.context, - ...(params.routeIndex ? { routeIndex: params.routeIndex } : {}), authEvaluation, allowAuthAvailabilityOverride: params.allowAuthAvailabilityOverride, ...(params.configuredEntry ? { configuredEntry: params.configuredEntry } : {}),