openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
100 lines (99 loc) • 4.9 kB
JavaScript
import { f as normalizeStringEntries } from "./string-normalization-DLmfYZXI.mjs";
import { o as writeRuntimeJson } from "./runtime-B-SLYP-m.mjs";
import { p as shortenHomePath } from "./utils-C8YECn2Q.mjs";
import { t as formatCliCommand } from "./command-format-CqdwUgPk.mjs";
import { r as normalizeProviderId } from "./provider-id-b5XGSf9C.mjs";
import { i as resolveProviderIdForAuth } from "./provider-auth-aliases-CgGlzANu.mjs";
import { t as findNormalizedProviderValue } from "./model-selection-normalize-DIZCRGVF.mjs";
import "./model-selection-C2A4Wyix.mjs";
import { n as externalCliDiscoveryForProviderAuth } from "./external-cli-discovery-DZ419koV.mjs";
import { i as ensureAuthProfileStoreAsync } from "./store-runtime-dW0pXLJI.mjs";
import { t as resolveAuthStatePathForDisplay } from "./paths-BSElhsGi.mjs";
import { o as setAuthProfileOrder } from "./profiles-DbLHYG1P.mjs";
import "./auth-profiles-VRLhewIH.mjs";
import { l as resolveModelsTargetAgent } from "./shared-jZuoMzdG.mjs";
import { t as loadModelsConfig } from "./load-config-CTxoGQxg.mjs";
import { n as refreshRunningGatewayAuthState } from "./auth-refresh-CoYY69-l.mjs";
//#region src/commands/models/auth-order.ts
function describeOrder(e, t, n) {
const r = resolveProviderIdForAuth(t, { config: n });
const i = findNormalizedProviderValue(e.order, r);
if (i !== void 0) return i;
return Object.entries(e.order ?? {}).filter(([e]) => resolveProviderIdForAuth(e, { config: n }) === r).toSorted(([e], [t]) => e.localeCompare(t))[0]?.[1] ?? [];
}
function describeOrderFallback(e, t) {
const n = resolveProviderIdForAuth(t, { config: e });
const r = findNormalizedProviderValue(e.auth?.order, n) ?? findNormalizedProviderValue(e.auth?.order, t);
if (r === void 0) return "selecting automatically";
return r.length > 0 ? `using order from config: ${r.join(", ")}` : "config selects no profiles";
}
async function resolveAuthOrderContext(e, t, n) {
const r = e.provider?.trim();
if (!r) throw new Error(`Missing --provider. Run ${formatCliCommand("openclaw models auth list")} to see saved provider profiles.`);
const i = normalizeProviderId(r);
const a = await loadModelsConfig({
commandName: "models auth-order",
runtime: t
});
const { agentId: o, agentDir: s } = resolveModelsTargetAgent(a, e.agent, { kind: n });
return {
cfg: a,
agentId: o,
agentDir: s,
provider: i
};
}
async function modelsAuthOrderGetCommand(e, t) {
const { cfg: n, agentId: r, agentDir: i, provider: a } = await resolveAuthOrderContext(e, t, "read");
const o = describeOrder(await ensureAuthProfileStoreAsync(i, { externalCli: externalCliDiscoveryForProviderAuth({
cfg: n,
provider: a
}) }), a, n);
if (e.json) {
writeRuntimeJson(t, {
agentId: r,
agentDir: i,
provider: a,
authStatePath: shortenHomePath(resolveAuthStatePathForDisplay(i)),
order: o.length > 0 ? o : null
});
return;
}
t.log(`Agent: ${r}`);
t.log(`Provider: ${a}`);
t.log(`Auth state store: ${shortenHomePath(resolveAuthStatePathForDisplay(i))}`);
t.log(o.length > 0 ? `Auth profile order override: ${o.join(", ")}` : `Auth profile order override: none (${describeOrderFallback(n, a)})`);
}
async function modelsAuthOrderUpdateCommand(e, t) {
const { cfg: n, agentId: r, agentDir: i, provider: a } = await resolveAuthOrderContext(e, t, "mutation");
const o = e.order === null ? null : await ensureAuthProfileStoreAsync(i, { externalCli: externalCliDiscoveryForProviderAuth({
cfg: n,
provider: a
}) });
const s = resolveProviderIdForAuth(a, { config: n });
const c = e.order === null ? null : normalizeStringEntries(e.order ?? []);
if (c?.length === 0) throw new Error(`Missing profile ids. Run ${formatCliCommand("openclaw models auth list --provider " + a)} to choose one or more profile ids.`);
for (const e of c ?? []) {
const t = o?.profiles[e];
if (!t) throw new Error(`Auth profile "${e}" not found in ${shortenHomePath(i)}. Run ${formatCliCommand("openclaw models auth list --provider " + a)} to see saved profiles.`);
if (resolveProviderIdForAuth(t.provider, {
config: n,
storedCredential: true
}) !== s) throw new Error(`Auth profile "${e}" is for ${t.provider}, not ${a}.`);
}
const l = await setAuthProfileOrder({
agentDir: i,
provider: s,
order: c
});
if (!l) {
const e = c ? `openclaw models auth order set --provider ${a} <profileIds...>` : `openclaw models auth order clear --provider ${a}`;
throw new Error(`Failed to update auth state; the auth state lock may be busy. Wait a moment and rerun ${formatCliCommand(e)}.`);
}
t.log(`Agent: ${r}`);
t.log(`Provider: ${a}`);
t.log(c ? `Auth profile order override: ${describeOrder(l, a, n).join(", ")}` : `Auth profile order override cleared; ${describeOrderFallback(n, a)}.`);
await refreshRunningGatewayAuthState(r, "update", t);
}
//#endregion
export { modelsAuthOrderGetCommand, modelsAuthOrderUpdateCommand };