openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
147 lines (146 loc) • 4.76 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { s as redactSensitiveText } from "./redact-Mj7pppxC.mjs";
import { _ as secretRefKey } from "./ref-contract-b8L-9DrQ.mjs";
import { g as parseSecretRef } from "./types.secrets-BmNLLnHp.mjs";
import { n as sanitizeForLog } from "./ansi-CZa2t0Hf.mjs";
import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs";
import { i as resolveProviderIdForAuth } from "./provider-auth-aliases-CgGlzANu.mjs";
import { r as hasUsableOAuthCredential } from "./credential-state-BiAE74F8.mjs";
import { n as SecretSurfaceUnavailableError } from "./runtime-degraded-state-BSnwk5PQ.mjs";
import { n as resolveAuthProfileSecretOwnerId } from "./runtime-auth-profile-owner-BEs73a-p.mjs";
//#region src/agents/models-config.providers.discovery-auth.runtime.ts
const unavailableDiscoveryAuthProfiles = /* @__PURE__ */ new WeakMap();
function throwUnavailableDiscoveryAuthProfile(e, t) {
if (typeof t === "object" && t !== null) unavailableDiscoveryAuthProfiles.set(t, e);
throw t;
}
function resolveUnavailableDiscoveryAuthProfileId(e) {
return typeof e === "object" && e !== null ? unavailableDiscoveryAuthProfiles.get(e) : void 0;
}
/** Prepares transient auth facts without changing synchronous catalog callback contracts. */
async function prepareProviderDiscoveryAuth({ agentDir: e, authStore: t, env: n, resolveProviderApiKey: r, resolveProviderAuth: i }, a) {
const o = /* @__PURE__ */ new Map();
for (const [r, i] of Object.entries(t.profiles)) {
const s = i.type === "api_key" ? "key" : "token";
const c = parseSecretRef(i.type === "api_key" ? i.keyRef : i.type === "token" ? i.tokenRef : void 0, a?.secrets?.defaults);
if (!c) continue;
const l = c.source === "env" ? normalizeOptionalString(n[c.id.trim()]) : void 0;
if (l) {
o.set(r, () => l);
continue;
}
const { resolveApiKeyForProfile: u } = await import("./oauth-I5nvh-hv.mjs");
try {
const n = await u({
cfg: a,
store: t,
profileId: r,
agentDir: e,
allowProfileFallback: false
});
if (!n) throw new SecretSurfaceUnavailableError({
ownerKind: "account",
ownerId: resolveAuthProfileSecretOwnerId({
agentDir: e,
profileId: r
}),
state: "unavailable",
paths: [`auth-profiles.${r}.${s}`],
refKeys: [secretRefKey(c)],
reason: "resolved secret value was invalid"
});
o.set(r, () => n.apiKey);
} catch (e) {
o.set(r, () => throwUnavailableDiscoveryAuthProfile(r, e));
}
}
const enrich = (e) => {
const t = e.profileId ? o.get(e.profileId) : void 0;
return t ? {
...e,
discoveryApiKey: t()
} : e;
};
return {
resolveProviderApiKey: (e) => enrich(r(e)),
resolveProviderAuth: (e, t) => enrich(i(e, t))
};
}
/** Excludes only failed expiring OAuth candidates for one live catalog hook. */
async function prepareProviderCatalogOAuthAuth({ agentDir: e, authStore: t, env: n, provider: r, resolveProviderAuth: i, isActive: a, onPreparationFailure: o }, s) {
const c = [];
const l = [];
let u;
let d;
while (a()) {
let n;
try {
n = i(r, { excludeProfileIds: c });
} catch {
break;
}
if (!n.profileId || n.mode !== "oauth") break;
const o = t.profiles[n.profileId];
if (o?.type !== "oauth" || o.oauthRef || hasUsableOAuthCredential(o)) break;
const { resolveApiKeyForProfile: f } = await import("./oauth-I5nvh-hv.mjs");
if (!a()) break;
let p = "No OAuth credential was returned";
try {
const r = await f({
cfg: s,
store: t,
profileId: n.profileId,
agentDir: e,
allowProfileFallback: false
});
if (r?.apiKey) {
d = {
profileId: n.profileId,
apiKey: r.apiKey,
authFlow: r.credential?.type === "oauth" ? r.credential.authFlow : void 0
};
break;
}
} catch (e) {
p = sanitizeForLog(redactSensitiveText(formatErrorMessage(e), { mode: "tools" })).replace(/\s+/gu, " ").slice(0, 500);
}
c.push(n.profileId);
l.push({
profileId: n.profileId,
message: p
});
u = n.authFlow;
}
const resolvePreparedProviderAuth = (e, t) => {
const a = e?.trim() || r;
const l = i(a, {
...t,
excludeProfileIds: c
});
if (l.mode === "none" && c.length > 0 && resolveProviderIdForAuth(a, {
config: s,
env: n
}) === resolveProviderIdForAuth(r, {
config: s,
env: n
})) {
o(c);
return {
...l,
preparationFailed: true,
...u ? { authFlow: u } : {}
};
}
return d && l.profileId === d.profileId ? {
...l,
discoveryApiKey: d.apiKey,
authFlow: d.authFlow
} : l;
};
return {
resolveProviderAuth: resolvePreparedProviderAuth,
failures: l
};
}
//#endregion
export { prepareProviderCatalogOAuthAuth, prepareProviderDiscoveryAuth, resolveUnavailableDiscoveryAuthProfileId };