UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

178 lines (177 loc) • 6.35 kB
import { o as asDateTimestampMs } from "./number-coercion-D6u-4YD8.mjs"; import { g as normalizeUniqueStringEntries } from "./string-normalization-DLmfYZXI.mjs"; import { p as normalizeSecretInputString } from "./types.secrets-BmNLLnHp.mjs"; import { r as normalizeProviderId } from "./provider-id-b5XGSf9C.mjs"; import { i as resolveProviderIdForAuth } from "./provider-auth-aliases-CgGlzANu.mjs"; import { a as resolveTokenExpiryState, n as evaluateStoredCredentialEligibility, t as DEFAULT_OAUTH_REFRESH_MARGIN_MS } from "./credential-state-BiAE74F8.mjs"; import { a as resolveAuthProfileOrder, m as resolveExplicitAuthOrderSelection } from "./order-jOvee2J6.mjs"; import { n as resolveAuthProfileDisplayLabel } from "./display-DA_6g711.mjs"; import { t as resolveEffectiveOAuthCredential } from "./effective-oauth-BmGrO0m8.mjs"; //#region src/agents/auth-health.ts /** * Auth profile health summarization. * Classifies stored and runtime credentials into profile/provider rollups for * status commands and doctor output without prompting keychain access. */ const DEFAULT_OAUTH_WARN_MS = 864e5; /** Format a remaining-duration value for compact auth status displays. */ function formatRemainingShort(e, t) { if (e === void 0 || Number.isNaN(e)) return "unknown"; if (e <= 0) return "0m"; const n = Math.round(e / 6e4); if (n < 1) return t?.underMinuteLabel ?? "1m"; if (n < 60) return `${n}m`; const r = Math.round(n / 60); if (r < 48) return `${r}h`; return `${Math.round(r / 24)}d`; } function resolveOAuthStatus(e, t, n) { const r = asDateTimestampMs(e); if (r === void 0 || r <= 0) return { status: "missing" }; const i = r - t; const a = resolveTokenExpiryState(r, t, { expiringWithinMs: n }); if (a === "invalid_expires" || a === "missing") return { status: "missing" }; return { status: a === "valid" ? "ok" : a, expiresAt: r, remainingMs: i }; } /** Build profile and provider auth health rollups from an auth profile store. */ function buildAuthHealthSummary(e) { const t = Date.now(); const n = e.warnAfterMs ?? 864e5; const r = e.providers ? new Set(normalizeUniqueStringEntries(e.providers.map((e) => normalizeProviderId(e)))) : null; const i = Object.entries(e.store.profiles).filter(([, e]) => r ? r.has(normalizeProviderId(e.provider)) : true).map(([n, r]) => { const i = e.runtimeCredentialsByProvider?.get(normalizeProviderId(r.provider)); const a = e.store; const o = e.cfg; const s = e.warnAfterMs; const c = resolveAuthProfileDisplayLabel({ cfg: o, store: a, profileId: n }); const l = i ?? r; const u = { profileId: n, provider: normalizeProviderId(l.provider), type: l.type, source: "store", label: c }; const expiringProfileHealth = (e, n, r = false) => { const { status: i, expiresAt: a, remainingMs: o } = resolveOAuthStatus(e, t, n); return { ...u, status: i, ...r ? { reasonCode: i === "expired" ? "expired" : void 0 } : {}, expiresAt: a, remainingMs: o }; }; if (r.setup?.replacement) return Object.assign({}, u, { type: r.type, status: "missing", reasonCode: "setup_inactive", label: `${c} (saved, inactive)` }); const d = evaluateStoredCredentialEligibility({ credential: l, now: t }); if (!d.eligible && (l.type !== "oauth" || d.reasonCode === "unresolved_ref")) return Object.assign({}, u, { status: d.reasonCode === "expired" ? "expired" : "missing", reasonCode: d.reasonCode }); if (l.type === "api_key") return Object.assign({}, u, { status: "static" }); if (l.type === "token") { const e = resolveTokenExpiryState(l.expires, t) === "valid" ? l.expires : void 0; if (!e) return Object.assign({}, u, { status: "static" }); return expiringProfileHealth(e, s ?? 864e5, true); } const f = resolveEffectiveOAuthCredential({ profileId: n, credential: l }); const p = evaluateStoredCredentialEligibility({ credential: f, now: t }); if (!p.eligible) return Object.assign({}, u, { status: p.reasonCode === "expired" ? "expired" : "missing", reasonCode: p.reasonCode }); const m = Math.max(s ?? (normalizeSecretInputString(f.refresh) ? 0 : 864e5), DEFAULT_OAUTH_REFRESH_MARGIN_MS); return expiringProfileHealth(f.expires, m); }).toSorted((e, t) => { if (e.provider !== t.provider) return e.provider.localeCompare(t.provider); return e.profileId.localeCompare(t.profileId); }); const a = /* @__PURE__ */ new Map(); const providerHealth = (e) => { const t = a.get(e) ?? { provider: e, status: "missing", profiles: [] }; a.set(e, t); return t; }; for (const e of i) providerHealth(e.provider).profiles.push(e); for (const e of r ?? []) providerHealth(e); const resolveProviderStatusProfiles = (t) => { const { order: n } = resolveExplicitAuthOrderSelection({ storeOrder: e.store.order, configuredOrder: e.cfg?.auth?.order, providerKey: t.provider, providerAuthKey: resolveProviderIdForAuth(t.provider, { config: e.cfg, ...e.authAliasLookupParams, storedCredential: true }) }); if (n && n.length === 0) return []; const r = resolveAuthProfileOrder({ cfg: e.cfg, store: e.store, provider: t.provider, authAliasLookupParams: e.authAliasLookupParams }); const profilesForOrder = (e) => e.flatMap((e) => t.profiles.find((t) => t.profileId === e) ?? []); const i = profilesForOrder(r); if (i.length > 0) return i; return n ? profilesForOrder(n) : t.profiles; }; for (const e of a.values()) { const n = resolveProviderStatusProfiles(e); e.effectiveProfiles = n; if (n.length === 0) { e.status = "missing"; e.expiresAt = void 0; e.remainingMs = void 0; continue; } const r = n.filter((e) => e.type !== "api_key"); const i = new Set(n.map((e) => e.status)); e.status = [ "expired", "missing", "expiring" ].find((e) => i.has(e)) ?? (r.length > 0 ? "ok" : "static"); let a; for (const e of r) if (e.expiresAt !== void 0) a = a === void 0 ? e.expiresAt : Math.min(a, e.expiresAt); if (a !== void 0) { e.expiresAt = a; e.remainingMs = e.expiresAt - t; } } return { now: t, warnAfterMs: n, profiles: i, providers: Array.from(a.values()).toSorted((e, t) => e.provider.localeCompare(t.provider)) }; } //#endregion export { buildAuthHealthSummary as n, formatRemainingShort as r, DEFAULT_OAUTH_WARN_MS as t };