Spaces:
Running
Running
File size: 4,329 Bytes
fb4d8fe | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | import type { RuntimeEnv } from "../../runtime.js";
import { resolveAgentDir, resolveDefaultAgentId } from "../../agents/agent-scope.js";
import {
type AuthProfileStore,
ensureAuthProfileStore,
setAuthProfileOrder,
} from "../../agents/auth-profiles.js";
import { normalizeProviderId } from "../../agents/model-selection.js";
import { loadConfig } from "../../config/config.js";
import { shortenHomePath } from "../../utils.js";
import { resolveKnownAgentId } from "./shared.js";
function resolveTargetAgent(
cfg: ReturnType<typeof loadConfig>,
raw?: string,
): {
agentId: string;
agentDir: string;
} {
const agentId = resolveKnownAgentId({ cfg, rawAgentId: raw }) ?? resolveDefaultAgentId(cfg);
const agentDir = resolveAgentDir(cfg, agentId);
return { agentId, agentDir };
}
function describeOrder(store: AuthProfileStore, provider: string): string[] {
const providerKey = normalizeProviderId(provider);
const order = store.order?.[providerKey];
return Array.isArray(order) ? order : [];
}
export async function modelsAuthOrderGetCommand(
opts: { provider: string; agent?: string; json?: boolean },
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 store = ensureAuthProfileStore(agentDir, {
allowKeychainPrompt: false,
});
const order = describeOrder(store, provider);
if (opts.json) {
runtime.log(
JSON.stringify(
{
agentId,
agentDir,
provider,
authStorePath: shortenHomePath(`${agentDir}/auth-profiles.json`),
order: order.length > 0 ? order : null,
},
null,
2,
),
);
return;
}
runtime.log(`Agent: ${agentId}`);
runtime.log(`Provider: ${provider}`);
runtime.log(`Auth file: ${shortenHomePath(`${agentDir}/auth-profiles.json`)}`);
runtime.log(order.length > 0 ? `Order override: ${order.join(", ")}` : "Order override: (none)");
}
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 updated = await setAuthProfileOrder({
agentDir,
provider,
order: null,
});
if (!updated) {
throw new Error("Failed to update auth-profiles.json (lock busy?).");
}
runtime.log(`Agent: ${agentId}`);
runtime.log(`Provider: ${provider}`);
runtime.log("Cleared per-agent order override.");
}
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 store = ensureAuthProfileStore(agentDir, {
allowKeychainPrompt: false,
});
const providerKey = normalizeProviderId(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.");
}
for (const profileId of requested) {
const cred = store.profiles[profileId];
if (!cred) {
throw new Error(`Auth profile "${profileId}" not found in ${agentDir}.`);
}
if (normalizeProviderId(cred.provider) !== providerKey) {
throw new Error(`Auth profile "${profileId}" is for ${cred.provider}, not ${provider}.`);
}
}
const updated = await setAuthProfileOrder({
agentDir,
provider,
order: requested,
});
if (!updated) {
throw new Error("Failed to update auth-profiles.json (lock busy?).");
}
runtime.log(`Agent: ${agentId}`);
runtime.log(`Provider: ${provider}`);
runtime.log(`Order override: ${describeOrder(updated, provider).join(", ")}`);
}
|