refactor(models): dedupe auth order context

This commit is contained in:
Peter Steinberger
2026-02-15 16:32:12 +00:00
parent f4782e1e73
commit d238483337

View File

@@ -28,18 +28,22 @@ function describeOrder(store: AuthProfileStore, provider: string): string[] {
return Array.isArray(order) ? order : []; return Array.isArray(order) ? order : [];
} }
export async function modelsAuthOrderGetCommand( function resolveAuthOrderContext(opts: { provider: string; agent?: string }) {
opts: { provider: string; agent?: string; json?: boolean },
runtime: RuntimeEnv,
) {
const rawProvider = opts.provider?.trim(); const rawProvider = opts.provider?.trim();
if (!rawProvider) { if (!rawProvider) {
throw new Error("Missing --provider."); throw new Error("Missing --provider.");
} }
const provider = normalizeProviderId(rawProvider); const provider = normalizeProviderId(rawProvider);
const cfg = loadConfig(); const cfg = loadConfig();
const { agentId, agentDir } = resolveTargetAgent(cfg, opts.agent); const { agentId, agentDir } = resolveTargetAgent(cfg, opts.agent);
return { cfg, agentId, agentDir, provider };
}
export async function modelsAuthOrderGetCommand(
opts: { provider: string; agent?: string; json?: boolean },
runtime: RuntimeEnv,
) {
const { agentId, agentDir, provider } = resolveAuthOrderContext(opts);
const store = ensureAuthProfileStore(agentDir, { const store = ensureAuthProfileStore(agentDir, {
allowKeychainPrompt: false, allowKeychainPrompt: false,
}); });
@@ -72,14 +76,7 @@ export async function modelsAuthOrderClearCommand(
opts: { provider: string; agent?: string }, opts: { provider: string; agent?: string },
runtime: RuntimeEnv, runtime: RuntimeEnv,
) { ) {
const rawProvider = opts.provider?.trim(); const { agentId, agentDir, provider } = resolveAuthOrderContext(opts);
if (!rawProvider) {
throw new Error("Missing --provider.");
}
const provider = normalizeProviderId(rawProvider);
const cfg = loadConfig();
const { agentId, agentDir } = resolveTargetAgent(cfg, opts.agent);
const updated = await setAuthProfileOrder({ const updated = await setAuthProfileOrder({
agentDir, agentDir,
provider, provider,
@@ -98,19 +95,12 @@ export async function modelsAuthOrderSetCommand(
opts: { provider: string; agent?: string; order: string[] }, opts: { provider: string; agent?: string; order: string[] },
runtime: RuntimeEnv, runtime: RuntimeEnv,
) { ) {
const rawProvider = opts.provider?.trim(); const { agentId, agentDir, provider } = resolveAuthOrderContext(opts);
if (!rawProvider) {
throw new Error("Missing --provider.");
}
const provider = normalizeProviderId(rawProvider);
const cfg = loadConfig();
const { agentId, agentDir } = resolveTargetAgent(cfg, opts.agent);
const store = ensureAuthProfileStore(agentDir, { const store = ensureAuthProfileStore(agentDir, {
allowKeychainPrompt: false, allowKeychainPrompt: false,
}); });
const providerKey = normalizeProviderId(provider); const providerKey = provider;
const requested = (opts.order ?? []).map((entry) => String(entry).trim()).filter(Boolean); const requested = (opts.order ?? []).map((entry) => String(entry).trim()).filter(Boolean);
if (requested.length === 0) { if (requested.length === 0) {
throw new Error("Missing profile ids. Provide one or more profile ids."); throw new Error("Missing profile ids. Provide one or more profile ids.");