UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

140 lines (139 loc) • 5.71 kB
import { o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs"; import { o as asDateTimestampMs } from "./number-coercion-D6u-4YD8.mjs"; import { n as sliceUtf16Safe } from "./utf16-slice-CuDyEXgS.mjs"; import { g as parseSecretRef } from "./types.secrets-BmNLLnHp.mjs"; import { p as shortenHomePath } from "./utils-C8YECn2Q.mjs"; import { r as normalizeProviderId } from "./provider-id-b5XGSf9C.mjs"; import { A as resolveProfileUnusableUntilForDisplay } from "./user-model-account-authority-CRj924IP.mjs"; import { t as findNormalizedProviderValue } from "./model-selection-normalize-DIZCRGVF.mjs"; import { l as cloneAuthProfileStore } from "./oauth-shared-gvo_ItCc.mjs"; import { a as resolveAuthProfileOrder, t as isConfiguredAwsSdkAuthProfileForProvider } from "./order-jOvee2J6.mjs"; import { t as resolveEnvApiKey } from "./model-auth-env-kbpGo8fU.mjs"; import { b as resolveUsableCustomProviderApiKey } from "./model-auth-provider-config-Bnc4upRS.mjs"; import { t as formatAuthProfileOAuthLabel } from "./display-DA_6g711.mjs"; import { r as formatRemainingShort } from "./auth-health-sVoXFBMP.mjs"; //#region src/security/secret-mask.ts /** Masks credential-like values without splitting UTF-16 surrogate pairs at the edges. */ function maskApiKey(e) { const t = stripControlCharacters(e).trim(); if (!t) return "missing"; if (t.length <= 6) return `${sliceUtf16Safe(t, 0, 1)}...${sliceUtf16Safe(t, -1)}`; if (t.length <= 16) return `${sliceUtf16Safe(t, 0, 2)}...${sliceUtf16Safe(t, -2)}`; return `${sliceUtf16Safe(t, 0, 8)}...${sliceUtf16Safe(t, -8)}`; } function stripControlCharacters(e) { let t = ""; for (const n of e) { const e = n.charCodeAt(0); if (!(e >= 0 && e <= 31 || e >= 127 && e <= 159)) t += n; } return t; } //#endregion //#region src/agents/model-catalog-auth-labels.ts function resolveStoredCredentialLabel(e) { const t = maskApiKey(typeof e.value === "string" ? e.value : ""); if (t !== "missing") return t; if (parseSecretRef(e.refValue)) return "ref"; return "missing"; } /** Reuses time-dependent ordering against captured facts without reopening auth sources. */ function formatModelCatalogAuthLabel(e, t) { if (typeof e === "string") return e; const { cfg: n, metadataSnapshot: r } = t; const i = cloneAuthProfileStore(t.store); const a = Date.now(); const o = resolveAuthProfileOrder({ cfg: n, store: i, provider: e.provider, authAliasLookupParams: { metadataSnapshot: r } }).filter((t) => { if (!e.apiKeyOnly) return true; const r = i.profiles[t]?.type ?? n.auth?.profiles?.[t]?.mode; return r !== "oauth" && r !== "token"; }); if (!o.length) return e.fallback; const s = findNormalizedProviderValue(i.lastGood, e.provider); return `${o.map((t, n) => { const r = []; if (n === 0) r.push("next"); if (s === t) r.push("lastGood"); const o = resolveProfileUnusableUntilForDisplay(i, t); if (o && o > a) r.push(`cooldown ${formatRemainingShort(o - a, { underMinuteLabel: "soon" })}`); const c = i.profiles[t]; const l = c && c.type !== "api_key" ? asDateTimestampMs(c.expires) : void 0; if (l !== void 0 && l > 0) r.push(l <= a ? "expired" : ` exp ${formatRemainingShort(l - a, { underMinuteLabel: "soon" })}`); return `${e.profiles[t]}${r.length ? ` (${r.join(", ")})` : ""}`; }).join(", ")} (${e.source})`; } function captureProfileLabel(e, t, n) { const r = n.profiles[e]; const i = t.auth?.profiles?.[e]; if (!r || i?.provider && i.provider !== r.provider || i?.mode && i.mode !== r.type && !(i.mode === "oauth" && r.type === "token")) return `${e}=missing`; if (r.type === "api_key") return `${e}=${resolveStoredCredentialLabel({ value: r.key, refValue: r.keyRef })}`; if (r.type === "token") return `${e}=token:${resolveStoredCredentialLabel({ value: r.token, refValue: r.tokenRef })}`; return formatAuthProfileOAuthLabel({ cfg: t, store: n, profileId: e }); } function captureFallbackLabel(e, t, n, r, i) { const a = resolveEnvApiKey(e, r, { config: t, workspaceDir: i }); if (a) { const e = a.source.includes("ANTHROPIC_OAUTH_TOKEN") || normalizeLowercaseStringOrEmpty(a.source).includes("oauth") ? "OAuth (env)" : maskApiKey(a.apiKey); return a.source && a.source !== e && a.source !== "missing" ? `${e} (${a.source})` : e; } const o = resolveUsableCustomProviderApiKey({ cfg: t, provider: e })?.apiKey; if (o) return `${maskApiKey(o)} (models.json: ${shortenHomePath(`${n}/models.json`)})`; return "missing"; } /** Captures display labels from the generation's auth store and workspace environment. */ function prepareModelCatalogAuthLabels(e) { const t = /* @__PURE__ */ new Map(); const n = /* @__PURE__ */ new Set([ ...Object.keys(e.store.profiles), ...Object.keys(e.config.auth?.profiles ?? {}), ...Object.values(e.store.order ?? {}).flat(), ...Object.values(e.config.auth?.order ?? {}).flat() ]); const r = new Set([...e.providers].map(normalizeProviderId)); if (r.size === 0) return t; const i = [...n].map((t) => [t, captureProfileLabel(t, e.config, e.store)]); for (const n of r) { const r = { provider: n, profiles: Object.fromEntries(i.map(([t, r]) => [t, !e.store.profiles[t] && isConfiguredAwsSdkAuthProfileForProvider({ cfg: e.config, provider: n, profileId: t }) ? `${t}=aws-sdk` : r])), source: `auth profile store: ${shortenHomePath(e.authStorePath)}`, fallback: captureFallbackLabel(n, e.config, e.agentDir, e.env, e.workspaceDir), apiKeyOnly: false }; t.set(n, { all: r, apiKey: n === "openai" ? { ...r, apiKeyOnly: true } : r }); } return t; } //#endregion export { prepareModelCatalogAuthLabels as n, maskApiKey as r, formatModelCatalogAuthLabel as t };