From 0af56c8ba66f47c487727139cfa47153d4d34031 Mon Sep 17 00:00:00 2001 From: Shakker Date: Fri, 24 Apr 2026 02:13:10 +0100 Subject: [PATCH] refactor: split models list row sources --- src/commands/models/list.list-command.ts | 38 +++------------ src/commands/models/list.registry-load.ts | 14 ++++++ src/commands/models/list.row-sources.ts | 58 +++++++++++++++++++++++ src/commands/models/list.rows.ts | 15 +----- 4 files changed, 80 insertions(+), 45 deletions(-) create mode 100644 src/commands/models/list.registry-load.ts create mode 100644 src/commands/models/list.row-sources.ts diff --git a/src/commands/models/list.list-command.ts b/src/commands/models/list.list-command.ts index 3345fc51836..cd34d74e6fe 100644 --- a/src/commands/models/list.list-command.ts +++ b/src/commands/models/list.list-command.ts @@ -4,14 +4,8 @@ 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 { - appendCatalogSupplementRows, - appendConfiguredProviderRows, - appendConfiguredRows, - appendDiscoveredRows, - appendProviderCatalogRows, - loadListModelRegistry, -} from "./list.rows.js"; +import { loadListModelRegistry } from "./list.registry-load.js"; +import { appendAllModelRowSources, appendConfiguredModelRowSources } from "./list.row-sources.js"; import { printModelTable } from "./list.table.js"; import type { ModelRow } from "./list.types.js"; import { loadModelsConfigWithSource } from "./load-config.js"; @@ -98,32 +92,12 @@ export async function modelsListCommand( }; if (opts.all) { - const seenKeys = appendDiscoveredRows({ - rows, - models: modelRegistry?.getAll() ?? [], - context: rowContext, - }); - - appendConfiguredProviderRows({ + await appendAllModelRowSources({ rows, context: rowContext, - seenKeys, + modelRegistry, + useProviderCatalogFastPath, }); - - if (modelRegistry) { - await appendCatalogSupplementRows({ - rows, - modelRegistry, - context: rowContext, - seenKeys, - }); - } else if (useProviderCatalogFastPath) { - await appendProviderCatalogRows({ - rows, - context: rowContext, - seenKeys, - }); - } } else { const registry = modelRegistry; if (!registry) { @@ -131,7 +105,7 @@ export async function modelsListCommand( process.exitCode = 1; return; } - appendConfiguredRows({ + appendConfiguredModelRowSources({ rows, entries, modelRegistry: registry, diff --git a/src/commands/models/list.registry-load.ts b/src/commands/models/list.registry-load.ts new file mode 100644 index 00000000000..f85def817b7 --- /dev/null +++ b/src/commands/models/list.registry-load.ts @@ -0,0 +1,14 @@ +import type { OpenClawConfig } from "../../config/types.openclaw.js"; +import { loadModelRegistry } from "./list.registry.js"; +import { modelKey } from "./shared.js"; + +export async function loadListModelRegistry( + cfg: OpenClawConfig, + opts?: { providerFilter?: string }, +) { + const loaded = await loadModelRegistry(cfg, opts); + return { + ...loaded, + discoveredKeys: new Set(loaded.models.map((model) => modelKey(model.provider, model.id))), + }; +} diff --git a/src/commands/models/list.row-sources.ts b/src/commands/models/list.row-sources.ts new file mode 100644 index 00000000000..9faceac4462 --- /dev/null +++ b/src/commands/models/list.row-sources.ts @@ -0,0 +1,58 @@ +import type { ModelRegistry } from "@mariozechner/pi-coding-agent"; +import { + appendCatalogSupplementRows, + appendConfiguredProviderRows, + appendConfiguredRows, + appendDiscoveredRows, + appendProviderCatalogRows, + type RowBuilderContext, +} from "./list.rows.js"; +import type { ConfiguredEntry, ModelRow } from "./list.types.js"; + +type AllModelRowSources = { + rows: ModelRow[]; + context: RowBuilderContext; + modelRegistry?: ModelRegistry; + useProviderCatalogFastPath: boolean; +}; + +export async function appendAllModelRowSources(params: AllModelRowSources): Promise { + const seenKeys = appendDiscoveredRows({ + rows: params.rows, + models: params.modelRegistry?.getAll() ?? [], + context: params.context, + }); + + appendConfiguredProviderRows({ + rows: params.rows, + context: params.context, + seenKeys, + }); + + if (params.modelRegistry) { + await appendCatalogSupplementRows({ + rows: params.rows, + modelRegistry: params.modelRegistry, + context: params.context, + seenKeys, + }); + return; + } + + if (params.useProviderCatalogFastPath) { + await appendProviderCatalogRows({ + rows: params.rows, + context: params.context, + seenKeys, + }); + } +} + +export function appendConfiguredModelRowSources(params: { + rows: ModelRow[]; + entries: ConfiguredEntry[]; + modelRegistry: ModelRegistry; + context: RowBuilderContext; +}): void { + appendConfiguredRows(params); +} diff --git a/src/commands/models/list.rows.ts b/src/commands/models/list.rows.ts index 1cd8d64b125..2c7e69ea448 100644 --- a/src/commands/models/list.rows.ts +++ b/src/commands/models/list.rows.ts @@ -7,7 +7,7 @@ import { normalizeProviderId } from "../../agents/provider-id.js"; import type { ModelDefinitionConfig, ModelProviderConfig } from "../../config/types.models.js"; import type { OpenClawConfig } from "../../config/types.openclaw.js"; import type { ListRowModel } from "./list.model-row.js"; -import { loadModelRegistry, toModelRow } from "./list.registry.js"; +import { toModelRow } from "./list.registry.js"; import { loadModelCatalog, loadProviderCatalogModelsForList, @@ -23,7 +23,7 @@ type RowFilter = { local?: boolean; }; -type RowBuilderContext = { +export type RowBuilderContext = { cfg: OpenClawConfig; agentDir: string; authStore: AuthProfileStore; @@ -114,17 +114,6 @@ function shouldListConfiguredProviderModel(params: { ); } -export async function loadListModelRegistry( - cfg: OpenClawConfig, - opts?: { providerFilter?: string }, -) { - const loaded = await loadModelRegistry(cfg, opts); - return { - ...loaded, - discoveredKeys: new Set(loaded.models.map((model) => modelKey(model.provider, model.id))), - }; -} - export function appendDiscoveredRows(params: { rows: ModelRow[]; models: Model[];