UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

126 lines (125 loc) • 5.01 kB
import { o as resolveRequiredOsHomeDir } from "./home-dir-EKmgmdPG.mjs"; import { r as normalizeProviderId } from "./provider-id-b5XGSf9C.mjs"; import { F as EXTERNAL_CLI_SYNC_TTL_MS, R as authProfilesLog } from "./user-model-account-authority-CRj924IP.mjs"; import { r as MINIMAX_CLI_PROFILE_ID } from "./profile-ids-B9fFWPvP.mjs"; import { a as isSafeToAdoptBootstrapOAuthIdentity } from "./oauth-shared-gvo_ItCc.mjs"; import { i as isOAuthRefreshFence } from "./oauth-refresh-marker-Bm1Qb0ET.mjs"; import { r as hasUsableOAuthCredential } from "./credential-state-BiAE74F8.mjs"; import { s as readMiniMaxCliCredentialsCached } from "./cli-credentials-BaPg7qWx.mjs"; //#region src/agents/auth-profiles/external-cli-sync.ts /** MiniMax CLI bootstrap and persisted ownership; provider plugins own other external auth. */ const PERSISTED_EXTERNAL_CLI_AUTH_FLOW = "external-cli"; const MINIMAX_PROVIDER = "minimax-portal"; const MINIMAX_PROVIDER_IDS = [ MINIMAX_PROVIDER, "minimax", "minimax-cli" ]; function isMiniMaxCliProfile(e) { return e.profileId === "minimax-portal:minimax-cli" && (!e.credential || MINIMAX_PROVIDER_IDS.includes(e.credential.provider)); } function readMiniMaxCredential(e, t) { const n = readMiniMaxCliCredentialsCached({ ttlMs: EXTERNAL_CLI_SYNC_TTL_MS, ...e ? { homeDir: resolveRequiredOsHomeDir(e) } : {} }); return n && t !== void 0 ? { ...n, provider: t } : n; } /** True when durable metadata assigns this stored profile to an external CLI owner. */ function isPersistedExternalCliAuthProfile(e) { if (!isMiniMaxCliProfile(e)) return false; return e.credential.authFlow === PERSISTED_EXTERNAL_CLI_AUTH_FLOW; } function markPersistedExternalCliCredential(e) { return { ...e, authFlow: PERSISTED_EXTERNAL_CLI_AUTH_FLOW }; } /** Provider ids whose external CLI credentials can be refreshed by this owner. */ function listExternalCliSyncProviderIds() { return [...MINIMAX_PROVIDER_IDS]; } /** Read a CLI credential only for safe bootstrap of an unusable local profile. */ function readExternalCliBootstrapCredential(e) { if (!isMiniMaxCliProfile(e)) return null; const t = readMiniMaxCredential(void 0, e.credential.provider); if (t && isOAuthRefreshFence(e.credential)) return null; return t; } function normalizeProviderScope(e) { return e === void 0 ? void 0 : new Set(Array.from(e, normalizeProviderId).filter(Boolean)); } function isMiniMaxCliInScope(e) { const { options: t, store: n } = e; const r = normalizeProviderScope(t?.providerIds); if (r === void 0 && t?.profileIds === void 0) { const e = n.profiles[MINIMAX_CLI_PROFILE_ID]; return Object.hasOwn(n.profiles, "minimax-portal:minimax-cli") && e?.type === "oauth" && MINIMAX_PROVIDER_IDS.includes(e.provider); } if (Array.from(t?.profileIds ?? []).some((e) => "minimax-portal:minimax-cli" === e.trim())) return true; return MINIMAX_PROVIDER_IDS.some((e) => r?.has(e)); } /** True when the built-in CLI owns a refreshable profile slot. */ function isExternalCliAuthProfileInScope(e) { return e.profileId === "minimax-portal:minimax-cli" && isMiniMaxCliInScope({ store: e.store }); } function backfillExternalCliIdentity(e) { const t = readMiniMaxCredential(e.env); if (!(t?.refresh === e.existingOAuth.refresh)) return null; const n = markPersistedExternalCliCredential({ ...e.existingOAuth, ...e.existingOAuth.email || !t.email ? {} : { email: t.email } }); return n.authFlow === e.existingOAuth.authFlow && n.email === e.existingOAuth.email ? null : n; } /** Resolve scoped external CLI auth profiles available to overlay or persist. */ function resolveExternalCliAuthProfiles(e, t) { const n = Date.now(); if (!isMiniMaxCliInScope({ store: e, options: t })) return []; const r = MINIMAX_CLI_PROFILE_ID; const i = e.profiles[r]; const a = i?.type === "oauth" && MINIMAX_PROVIDER_IDS.includes(i.provider) ? i : void 0; if (i && !a) return []; if (a && hasUsableOAuthCredential(a, { now: n })) { const e = backfillExternalCliIdentity({ existingOAuth: a, env: t?.env }); return e ? [{ profileId: r, credential: e, persistence: "persisted" }] : []; } const o = readMiniMaxCredential(t?.env, a?.provider ?? MINIMAX_PROVIDER); if (!o) return []; if (a && isOAuthRefreshFence(a)) { authProfilesLog.warn("refused unordered external cli oauth recovery for a fenced profile", { profileId: r, provider: MINIMAX_PROVIDER }); return []; } if (a && !isSafeToAdoptBootstrapOAuthIdentity(a, o)) { authProfilesLog.warn("refused external cli oauth bootstrap: identity mismatch", { profileId: r, provider: MINIMAX_PROVIDER }); return []; } if (!hasUsableOAuthCredential(o, { now: n })) return []; return [{ profileId: r, credential: markPersistedExternalCliCredential(o), persistence: "persisted" }]; } //#endregion export { resolveExternalCliAuthProfiles as a, readExternalCliBootstrapCredential as i, isPersistedExternalCliAuthProfile as n, listExternalCliSyncProviderIds as r, isExternalCliAuthProfileInScope as t };