perf(models): reuse projected list rows (#128248)

Amp-Thread-ID: https://ampcode.com/threads/T-01a021f5-984a-7628-a30c-491c166ff247

Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Peter SteinbergerandAmp authored and GitHub committed 2026-08-23 08:20:59 -07:00
1 parent 49bdbe6005
commit cd86b59a35
2 files changed
+84 -17

No files matched your search

@@ -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<Record<string, ModelProviderConfig>>(
{
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,
}),
]);
});
});
+5 -17
View File
@@ -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<ModelRow> {
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 } : {}),