From e66aa357f85b46b5d370efe181cd005a75fbab22 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Mon, 22 Jun 2026 22:48:47 +0800 Subject: [PATCH] refactor(models): share auth command agent resolution --- src/commands/models/auth-list.test.ts | 9 ++++++--- src/commands/models/auth-list.ts | 17 ++--------------- src/commands/models/auth-order.ts | 17 ++--------------- src/commands/models/shared.ts | 15 ++++++++++++++- 4 files changed, 24 insertions(+), 34 deletions(-) diff --git a/src/commands/models/auth-list.test.ts b/src/commands/models/auth-list.test.ts index eab54f7ddf07..ef2892899398 100644 --- a/src/commands/models/auth-list.test.ts +++ b/src/commands/models/auth-list.test.ts @@ -10,7 +10,10 @@ const mocks = vi.hoisted(() => ({ externalCliDiscoveryForProviderAuth: vi.fn(() => ({ kind: "none" })), loadModelsConfig: vi.fn(), resolveAuthProfileDisplayLabel: vi.fn(({ profileId }: { profileId: string }) => profileId), - resolveKnownAgentId: vi.fn(({ rawAgentId }: { rawAgentId?: string }) => rawAgentId ?? undefined), + resolveModelsTargetAgent: vi.fn((_cfg: OpenClawConfig, rawAgentId?: string) => { + const agentId = rawAgentId ?? "main"; + return { agentDir: `/tmp/openclaw/agents/${agentId}`, agentId }; + }), })); vi.mock("../../agents/agent-scope.js", () => ({ @@ -30,7 +33,7 @@ vi.mock("./load-config.js", () => ({ })); vi.mock("./shared.js", () => ({ - resolveKnownAgentId: mocks.resolveKnownAgentId, + resolveModelsTargetAgent: mocks.resolveModelsTargetAgent, })); function createRuntime(): OutputRuntimeEnv & { logs: string[]; jsonPayloads: unknown[] } { @@ -59,7 +62,7 @@ describe("modelsAuthListCommand", () => { mocks.ensureAuthProfileStore.mockReset(); mocks.externalCliDiscoveryForProviderAuth.mockClear(); mocks.resolveAuthProfileDisplayLabel.mockClear(); - mocks.resolveKnownAgentId.mockClear(); + mocks.resolveModelsTargetAgent.mockClear(); }); it("filters profiles by provider and redacts credential material in JSON output", async () => { diff --git a/src/commands/models/auth-list.ts b/src/commands/models/auth-list.ts index f0204d50d9d6..05c4200c8912 100644 --- a/src/commands/models/auth-list.ts +++ b/src/commands/models/auth-list.ts @@ -1,6 +1,5 @@ /** Command helpers for listing saved model auth profiles. */ import { timestampMsToIsoString } from "@openclaw/normalization-core/number-coercion"; -import { resolveAgentDir, resolveDefaultAgentId } from "../../agents/agent-scope.js"; import { ensureAuthProfileStore, externalCliDiscoveryForProviderAuth, @@ -14,7 +13,7 @@ import { resolveProviderIdForAuth } from "../../agents/provider-auth-aliases.js" import { type RuntimeEnv, writeRuntimeJson } from "../../runtime.js"; import { shortenHomePath } from "../../utils.js"; import { loadModelsConfig } from "./load-config.js"; -import { resolveKnownAgentId } from "./shared.js"; +import { resolveModelsTargetAgent } from "./shared.js"; type AuthProfileSummary = { id: string; @@ -48,18 +47,6 @@ function resolveProviderFilter(rawProvider: string | undefined): { }; } -function resolveTargetAgent( - cfg: Awaited>, - raw?: string, -): { - agentId: string; - agentDir: string; -} { - const agentId = resolveKnownAgentId({ cfg, rawAgentId: raw }) ?? resolveDefaultAgentId(cfg); - const agentDir = resolveAgentDir(cfg, agentId); - return { agentId, agentDir }; -} - function formatTimestamp(value: number | undefined): string | undefined { return timestampMsToIsoString(value); } @@ -115,7 +102,7 @@ export async function modelsAuthListCommand( runtime: RuntimeEnv, ) { const cfg = await loadModelsConfig({ commandName: "models auth list", runtime }); - const { agentId, agentDir } = resolveTargetAgent(cfg, opts.agent); + const { agentId, agentDir } = resolveModelsTargetAgent(cfg, opts.agent); const providerFilter = resolveProviderFilter(opts.provider); const store = ensureAuthProfileStore( agentDir, diff --git a/src/commands/models/auth-order.ts b/src/commands/models/auth-order.ts index d461dc8cb234..2cffb60a4cfc 100644 --- a/src/commands/models/auth-order.ts +++ b/src/commands/models/auth-order.ts @@ -1,6 +1,5 @@ /** Commands for viewing and editing per-agent provider auth profile order. */ import { normalizeStringEntries } from "@openclaw/normalization-core/string-normalization"; -import { resolveAgentDir, resolveDefaultAgentId } from "../../agents/agent-scope.js"; import { type AuthProfileStore, externalCliDiscoveryForProviderAuth, @@ -13,19 +12,7 @@ import { formatCliCommand } from "../../cli/command-format.js"; import { type RuntimeEnv, writeRuntimeJson } from "../../runtime.js"; import { shortenHomePath } from "../../utils.js"; import { loadModelsConfig } from "./load-config.js"; -import { resolveKnownAgentId } from "./shared.js"; - -function resolveTargetAgent( - cfg: Awaited>, - raw?: string, -): { - agentId: string; - agentDir: string; -} { - const agentId = resolveKnownAgentId({ cfg, rawAgentId: raw }) ?? resolveDefaultAgentId(cfg); - const agentDir = resolveAgentDir(cfg, agentId); - return { agentId, agentDir }; -} +import { resolveModelsTargetAgent } from "./shared.js"; function describeOrder(store: AuthProfileStore, provider: string): string[] { const providerKey = normalizeProviderId(provider); @@ -45,7 +32,7 @@ async function resolveAuthOrderContext( } const provider = normalizeProviderId(rawProvider); const cfg = await loadModelsConfig({ commandName: "models auth-order", runtime }); - const { agentId, agentDir } = resolveTargetAgent(cfg, opts.agent); + const { agentId, agentDir } = resolveModelsTargetAgent(cfg, opts.agent); return { cfg, agentId, agentDir, provider }; } diff --git a/src/commands/models/shared.ts b/src/commands/models/shared.ts index 96a357415952..40deec608696 100644 --- a/src/commands/models/shared.ts +++ b/src/commands/models/shared.ts @@ -1,5 +1,5 @@ /** Shared helpers for model commands that read or mutate model config. */ -import { listAgentIds } from "../../agents/agent-scope.js"; +import { resolveAgentDir, resolveDefaultAgentId, listAgentIds } from "../../agents/agent-scope.js"; import { DEFAULT_MODEL, DEFAULT_PROVIDER } from "../../agents/defaults.js"; import { buildModelAliasIndex, @@ -163,6 +163,19 @@ export function resolveKnownAgentId(params: { return agentId; } +/** Resolves the selected model-command agent and its profile directory. */ +export function resolveModelsTargetAgent( + cfg: OpenClawConfig, + rawAgentId?: string, +): { + agentId: string; + agentDir: string; +} { + const agentId = resolveKnownAgentId({ cfg, rawAgentId }) ?? resolveDefaultAgentId(cfg); + const agentDir = resolveAgentDir(cfg, agentId); + return { agentId, agentDir }; +} + /** Normalized primary/fallback config shape used by text and image defaults. */ export type PrimaryFallbackConfig = { primary?: string; fallbacks?: string[] };