openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
99 lines (98 loc) • 5.47 kB
JavaScript
import { d as normalizeStringEntries } from "./string-normalization-D6DKCpwE.mjs";
import { a as writeRuntimeJson } from "./runtime-BRg2NubB.mjs";
import { p as shortenHomePath } from "./utils-CitNzLyF.mjs";
import { t as formatCliCommand } from "./command-format-DwEdrDKL.mjs";
import { r as normalizeProviderId } from "./provider-id-D1VImdKk.mjs";
import { i as resolveProviderIdForAuth } from "./provider-auth-aliases-CAzJGfWN.mjs";
import { t as findNormalizedProviderValue } from "./model-selection-normalize-B-Anaau2.mjs";
import { b as resolveAuthStatePathForDisplay, o as setAuthProfileOrder } from "./profiles-6K3k_v-S.mjs";
import { n as externalCliDiscoveryForProviderAuth } from "./external-cli-discovery-C550HCjn.mjs";
import { n as ensureAuthProfileStore } from "./store-runtime-DPTEKEq5.mjs";
import "./model-selection-Cpoq0B2R.mjs";
import "./auth-profiles-DpzfW6rH.mjs";
import { c as resolveModelsTargetAgent } from "./shared-DqbOZ4lY.mjs";
import { t as loadModelsConfig } from "./load-config-DC0PCN-v.mjs";
import { n as refreshRunningGatewayAuthState } from "./auth-refresh-DEMbRFL6.mjs";
//#region src/commands/models/auth-order.ts
function describeOrder(store, provider, cfg) {
const authProvider = resolveProviderIdForAuth(provider, { config: cfg });
const canonical = findNormalizedProviderValue(store.order, authProvider);
if (canonical !== void 0) return canonical;
return Object.entries(store.order ?? {}).filter(([key]) => resolveProviderIdForAuth(key, { config: cfg }) === authProvider).toSorted(([left], [right]) => left.localeCompare(right))[0]?.[1] ?? [];
}
function describeOrderFallback(cfg, provider) {
const authProvider = resolveProviderIdForAuth(provider, { config: cfg });
const configuredOrder = findNormalizedProviderValue(cfg.auth?.order, authProvider) ?? findNormalizedProviderValue(cfg.auth?.order, provider);
if (configuredOrder === void 0) return "selecting automatically";
return configuredOrder.length > 0 ? `using order from config: ${configuredOrder.join(", ")}` : "config selects no profiles";
}
async function resolveAuthOrderContext(opts, runtime, kind) {
const rawProvider = opts.provider?.trim();
if (!rawProvider) throw new Error(`Missing --provider. Run ${formatCliCommand("openclaw models auth list")} to see saved provider profiles.`);
const provider = normalizeProviderId(rawProvider);
const cfg = await loadModelsConfig({
commandName: "models auth-order",
runtime
});
const { agentId, agentDir } = resolveModelsTargetAgent(cfg, opts.agent, { kind });
return {
cfg,
agentId,
agentDir,
provider
};
}
async function modelsAuthOrderGetCommand(opts, runtime) {
const { cfg, agentId, agentDir, provider } = await resolveAuthOrderContext(opts, runtime, "read");
const order = describeOrder(ensureAuthProfileStore(agentDir, { externalCli: externalCliDiscoveryForProviderAuth({
cfg,
provider
}) }), provider, cfg);
if (opts.json) {
writeRuntimeJson(runtime, {
agentId,
agentDir,
provider,
authStatePath: shortenHomePath(resolveAuthStatePathForDisplay(agentDir)),
order: order.length > 0 ? order : null
});
return;
}
runtime.log(`Agent: ${agentId}`);
runtime.log(`Provider: ${provider}`);
runtime.log(`Auth state store: ${shortenHomePath(resolveAuthStatePathForDisplay(agentDir))}`);
runtime.log(order.length > 0 ? `Auth profile order override: ${order.join(", ")}` : `Auth profile order override: none (${describeOrderFallback(cfg, provider)})`);
}
async function modelsAuthOrderUpdateCommand(opts, runtime) {
const { cfg, agentId, agentDir, provider } = await resolveAuthOrderContext(opts, runtime, "mutation");
const store = opts.order === null ? null : ensureAuthProfileStore(agentDir, { externalCli: externalCliDiscoveryForProviderAuth({
cfg,
provider
}) });
const providerKey = resolveProviderIdForAuth(provider, { config: cfg });
const requested = opts.order === null ? null : normalizeStringEntries(opts.order ?? []);
if (requested?.length === 0) throw new Error(`Missing profile ids. Run ${formatCliCommand("openclaw models auth list --provider " + provider)} to choose 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 ${shortenHomePath(agentDir)}. Run ${formatCliCommand("openclaw models auth list --provider " + provider)} to see saved profiles.`);
if (resolveProviderIdForAuth(cred.provider, {
config: cfg,
storedCredential: true
}) !== providerKey) throw new Error(`Auth profile "${profileId}" is for ${cred.provider}, not ${provider}.`);
}
const updated = await setAuthProfileOrder({
agentDir,
provider: providerKey,
order: requested
});
if (!updated) {
const command = requested ? `openclaw models auth order set --provider ${provider} <profileIds...>` : `openclaw models auth order clear --provider ${provider}`;
throw new Error(`Failed to update auth state; the auth state lock may be busy. Wait a moment and rerun ${formatCliCommand(command)}.`);
}
runtime.log(`Agent: ${agentId}`);
runtime.log(`Provider: ${provider}`);
runtime.log(requested ? `Auth profile order override: ${describeOrder(updated, provider, cfg).join(", ")}` : `Auth profile order override cleared; ${describeOrderFallback(cfg, provider)}.`);
await refreshRunningGatewayAuthState(agentId, "update", runtime);
}
//#endregion
export { modelsAuthOrderGetCommand, modelsAuthOrderUpdateCommand };