perf: narrow default models list registry loading

This commit is contained in:
Shakker
2026-04-24 02:59:49 +01:00
committed by Shakker
parent f9b33b7d96
commit a6a2516cd8
3 changed files with 71 additions and 4 deletions

View File

@@ -4,7 +4,7 @@ import type { RuntimeEnv } from "../../runtime.js";
import { normalizeLowercaseStringOrEmpty } from "../../shared/string-coerce.js";
import { resolveConfiguredEntries } from "./list.configured.js";
import { formatErrorWithStack } from "./list.errors.js";
import { loadListModelRegistry } from "./list.registry-load.js";
import { loadConfiguredListModelRegistry, loadListModelRegistry } from "./list.registry-load.js";
import {
appendAllModelRowSources,
appendConfiguredModelRowSources,
@@ -59,6 +59,8 @@ export async function modelsListCommand(
let availableKeys: Set<string> | undefined;
let availabilityErrorMessage: string | undefined;
const useProviderCatalogFastPath = Boolean(opts.all && providerFilter === "codex");
const { entries } = resolveConfiguredEntries(cfg);
const configuredByKey = new Map(entries.map((entry) => [entry.key, entry]));
const shouldLoadRegistry = modelRowSourcesRequireRegistry({
all: opts.all,
useProviderCatalogFastPath,
@@ -70,6 +72,11 @@ export async function modelsListCommand(
discoveredKeys = loaded.discoveredKeys;
availableKeys = loaded.availableKeys;
availabilityErrorMessage = loaded.availabilityErrorMessage;
} else if (!opts.all) {
const loaded = loadConfiguredListModelRegistry(cfg, entries, { providerFilter });
modelRegistry = loaded.registry;
discoveredKeys = loaded.discoveredKeys;
availableKeys = loaded.availableKeys;
}
} catch (err) {
runtime.error(`Model registry unavailable:\n${formatErrorWithStack(err)}`);
@@ -81,8 +88,6 @@ export async function modelsListCommand(
`Model availability lookup failed; falling back to auth heuristics for discovered models: ${availabilityErrorMessage}`,
);
}
const { entries } = resolveConfiguredEntries(cfg);
const configuredByKey = new Map(entries.map((entry) => [entry.key, entry]));
const rows: ModelRow[] = [];
const rowContext = {

View File

@@ -1,5 +1,10 @@
import type { Api, Model } from "@mariozechner/pi-ai";
import type { ModelRegistry } from "@mariozechner/pi-coding-agent";
import { shouldSuppressBuiltInModel } from "../../agents/model-suppression.js";
import type { OpenClawConfig } from "../../config/types.openclaw.js";
import { loadModelRegistry } from "./list.registry.js";
import { discoverAuthStorage, discoverModels, resolveOpenClawAgentDir } from "./list.runtime.js";
import type { ConfiguredEntry } from "./list.types.js";
import { modelKey } from "./shared.js";
export async function loadListModelRegistry(
@@ -12,3 +17,57 @@ export async function loadListModelRegistry(
discoveredKeys: new Set(loaded.models.map((model) => modelKey(model.provider, model.id))),
};
}
function findConfiguredRegistryModel(params: {
registry: ModelRegistry;
entry: ConfiguredEntry;
cfg: OpenClawConfig;
}): Model<Api> | undefined {
const model = params.registry.find(params.entry.ref.provider, params.entry.ref.model);
if (!model) {
return undefined;
}
if (
shouldSuppressBuiltInModel({
provider: model.provider,
id: model.id,
baseUrl: model.baseUrl,
config: params.cfg,
})
) {
return undefined;
}
return model;
}
export function loadConfiguredListModelRegistry(
cfg: OpenClawConfig,
entries: ConfiguredEntry[],
opts?: { providerFilter?: string },
) {
const agentDir = resolveOpenClawAgentDir();
const authStorage = discoverAuthStorage(agentDir, { readOnly: true });
const registry = discoverModels(authStorage, agentDir, {
providerFilter: opts?.providerFilter,
});
const discoveredKeys = new Set<string>();
const availableKeys = new Set<string>();
for (const entry of entries) {
const model = findConfiguredRegistryModel({ registry, entry, cfg });
if (!model) {
continue;
}
const key = modelKey(model.provider, model.id);
discoveredKeys.add(key);
if (registry.hasConfiguredAuth(model)) {
availableKeys.add(key);
}
}
return {
registry,
discoveredKeys,
availableKeys,
};
}

View File

@@ -20,7 +20,10 @@ export function modelRowSourcesRequireRegistry(params: {
all?: boolean;
useProviderCatalogFastPath: boolean;
}): boolean {
return !(params.all && params.useProviderCatalogFastPath);
if (!params.all) {
return false;
}
return !params.useProviderCatalogFastPath;
}
export async function appendAllModelRowSources(params: AllModelRowSources): Promise<void> {