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