mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-24 09:41:14 +00:00
refactor(models): share auth command agent resolution
This commit is contained in:
@@ -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 () => {
|
||||
|
||||
@@ -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<ReturnType<typeof loadModelsConfig>>,
|
||||
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,
|
||||
|
||||
@@ -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<ReturnType<typeof loadModelsConfig>>,
|
||||
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 };
|
||||
}
|
||||
|
||||
|
||||
@@ -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[] };
|
||||
|
||||
|
||||
Reference in New Issue
Block a user