openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 4.44 kB
JavaScript
import{f as normalizeStringEntries}from"./string-normalization-D_oBUpO3.mjs";import{o as writeRuntimeJson}from"./runtime-DIcLIHcp.mjs";import{f as shortenHomePath}from"./utils-Dlk0IYAa.mjs";import{t as formatCliCommand}from"./command-format-D7KhNLP0.mjs";import{r as normalizeProviderId}from"./provider-id-A-8WTtLA.mjs";import{i as resolveProviderIdForAuth}from"./provider-auth-aliases-BRA1A5Vm.mjs";import{t as findNormalizedProviderValue}from"./model-selection-normalize-DU37Sfvg.mjs";import"./model-selection-CNHinoVN.mjs";import{a as resolveAuthProfileOrder}from"./order-B8e_5ABM.mjs";import{n as externalCliDiscoveryForProviderAuth}from"./external-cli-discovery-CHmIqgrd.mjs";import{i as ensureAuthProfileStoreAsync}from"./store-runtime-GNz-2GjY.mjs";import{t as resolveAuthStatePathForDisplay}from"./paths-BDkFWGjX.mjs";import{o as setAuthProfileOrder}from"./profiles-Bp0-JifZ.mjs";import"./auth-profiles-nMUErGRQ.mjs";import{l as resolveModelsTargetAgent}from"./shared-Sc7lnGKV.mjs";import{t as loadModelsConfig}from"./load-config-BZk4Hyvt.mjs";import{n as refreshRunningGatewayAuthState}from"./auth-refresh-B35YNJt_.mjs";
//!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 describeEffectiveOrder(e,t,n){const r=resolveAuthProfileOrder({cfg:t,store:e,provider:n,readinessMode:`read-only`});return`effective order: ${r.length>0?r.join(`, `):`none`}`}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=await ensureAuthProfileStoreAsync(i,{externalCli:externalCliDiscoveryForProviderAuth({cfg:n,provider:a})});const s=describeOrder(o,a,n);if(e.json){writeRuntimeJson(t,{agentId:r,agentDir:i,provider:a,authStatePath:shortenHomePath(resolveAuthStatePathForDisplay(i)),order:s.length>0?s:null});return}t.log(`Agent: ${r}`);t.log(`Provider: ${a}`);t.log(`Auth state store: ${shortenHomePath(resolveAuthStatePathForDisplay(i))}`);t.log(s.length>0?`Auth profile order override: ${s.join(`, `)}`:`Auth profile order override: none (${describeEffectiveOrder(o,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}`);const u=c?l:await ensureAuthProfileStoreAsync(i,{externalCli:externalCliDiscoveryForProviderAuth({cfg:n,provider:a})});t.log(c?`Auth profile order override: ${describeOrder(l,a,n).join(`, `)}`:`Auth profile order override cleared; ${describeEffectiveOrder(u,n,a)}.`);await refreshRunningGatewayAuthState(r,`update`,t)}export{modelsAuthOrderGetCommand,modelsAuthOrderUpdateCommand};