openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
164 lines (163 loc) • 6.05 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { n as getShellEnvAppliedKeys } from "./shell-env-Dbjy7T84.mjs";
import { i as normalizeProviderIdForAuth } from "./provider-id-b5XGSf9C.mjs";
import { t as normalizeOptionalSecretInput } from "./normalize-secret-input-Cxkx67Fv.mjs";
import { s as resolveProviderAuthLookupMaps } from "./provider-env-vars-DPvuIf4v.mjs";
import { r as resolvePluginSetupProviderCore } from "./setup-registry-CgyZCdrJ.mjs";
import { r as GCP_VERTEX_CREDENTIALS_MARKER } from "./model-auth-markers-4YSUG71a.mjs";
import fs from "node:fs";
import os from "node:os";
//#region src/secrets/provider-auth-evidence.ts
/** Resolves cheap, secret-free local credential evidence declared by provider manifests. */
function expandAuthEvidencePath(e, t) {
const n = e.trim();
if (!n) return;
let r = false;
let i = false;
const a = /\$\{([A-Za-z_][A-Za-z0-9_]*)\}/gu;
const o = n.replace(a, "").includes("${");
const s = n.replace(a, (e, n) => {
const a = n === "HOME" ? normalizeOptionalString(t.HOME) ?? os.homedir() : normalizeOptionalString(t[n]);
if (!a) {
r = true;
return "";
}
if (n !== "HOME" && n !== "APPDATA") i = true;
return a;
});
return r || o ? void 0 : {
path: s,
explicitOverride: i
};
}
function hasRequiredAuthEvidenceEnv(e, t) {
const hasEnv = (e) => Boolean(normalizeOptionalSecretInput(t[e]));
if (e.requiresAnyEnv?.length && !e.requiresAnyEnv.some(hasEnv)) return false;
if (e.requiresAllEnv?.length && !e.requiresAllEnv.every(hasEnv)) return false;
return true;
}
function hasLocalFileAuthEvidence(e, t) {
if (e.fileEnvVar) {
const n = normalizeOptionalString(t[e.fileEnvVar]);
if (n) return fs.existsSync(n);
}
for (const n of e.fallbackPaths ?? []) {
const e = expandAuthEvidencePath(n, t);
if (!e) continue;
if (fs.existsSync(e.path)) return true;
if (e.explicitOverride) return false;
}
return false;
}
function resolveLocalProviderAuthEvidence(e, t) {
for (const n of e ?? []) {
if (n.type !== "local-file-with-env" || !hasRequiredAuthEvidenceEnv(n, t) || !hasLocalFileAuthEvidence(n, t)) continue;
return {
credentialMarker: n.credentialMarker,
source: n.source ?? "local auth evidence"
};
}
return null;
}
//#endregion
//#region src/agents/model-auth-env-vars.ts
/** Lists every provider key represented by either env candidates or auth evidence. */
function listProviderEnvAuthLookupKeys(e) {
return Array.from(/* @__PURE__ */ new Set([...Object.keys(e.envCandidateMap), ...Object.keys(e.authEvidenceMap)])).toSorted((e, t) => e.localeCompare(t));
}
//#endregion
//#region src/agents/model-auth-env.ts
function prepareEnvAuthLookupMaps(e, t, n = false) {
const r = !t.aliasMap || !t.candidateMap || !t.authEvidenceMap || n && !t.setupProviderFallbackRefs ? resolveProviderAuthLookupMaps({
config: t.config,
workspaceDir: t.workspaceDir,
env: e,
...n ? { metadataSnapshot: t.metadataSnapshot } : {}
}) : void 0;
return {
aliasMap: t.aliasMap ?? r?.aliasMap ?? {},
candidateMap: t.candidateMap ?? r?.envCandidateMap ?? {},
authEvidenceMap: t.authEvidenceMap ?? r?.authEvidenceMap ?? {},
lookupMaps: r
};
}
/** Reports env/local auth presence without returning or resolving credential material. */
function resolveProviderEnvAuthEvidence(e, t = process.env, n = {}) {
const r = normalizeProviderIdForAuth(e);
const { aliasMap: i, candidateMap: a, authEvidenceMap: o } = prepareEnvAuthLookupMaps(t, n);
const s = i[r] ?? r;
const c = new Set(getShellEnvAppliedKeys());
for (const e of a[s] ?? []) {
if (!normalizeOptionalSecretInput(t[e])) continue;
return {
mode: s === "amazon-bedrock" && e.startsWith("AWS_") ? "aws-sdk" : e.includes("OAUTH_TOKEN") ? "oauth" : "api-key",
source: c.has(e) ? `shell env: ${e}` : `env: ${e}`
};
}
const l = resolveLocalProviderAuthEvidence(o[s], t);
if (l) return {
mode: s === "amazon-bedrock" ? "aws-sdk" : "api-key",
source: l.source
};
return null;
}
/**
* Plans direct auth without loading a provider runtime or resolving credential material.
* Setup-provider refs are deferred evidence only; runtime lookup still decides availability.
*/
function resolveProviderDirectAuthPlanningEvidence(e, t = process.env, n = {}) {
const { aliasMap: r, candidateMap: i, authEvidenceMap: a, lookupMaps: o } = prepareEnvAuthLookupMaps(t, n, true);
const s = resolveProviderEnvAuthEvidence(e, t, {
aliasMap: r,
candidateMap: i,
authEvidenceMap: a
});
if (s) return {
kind: "environment",
...s
};
const c = normalizeProviderIdForAuth(e);
const l = r[c] ?? c;
return (n.setupProviderFallbackRefs ?? o?.setupProviderFallbackRefs ?? []).some((e) => normalizeProviderIdForAuth(e) === l) ? {
kind: "setup-provider",
mode: "api-key",
source: "setup provider"
} : null;
}
function resolveEnvApiKey(e, t = process.env, n = {}) {
const r = normalizeProviderIdForAuth(e);
const { aliasMap: i, candidateMap: a, authEvidenceMap: o } = prepareEnvAuthLookupMaps(t, n);
const s = i[r] ?? r;
const c = new Set(getShellEnvAppliedKeys());
const l = Object.hasOwn(a, s) ? a[s] : void 0;
if (l !== void 0) for (const e of l) {
const n = normalizeOptionalSecretInput(t[e]);
if (n) return {
apiKey: n,
source: c.has(e) ? `shell env: ${e}` : `env: ${e}`
};
}
const u = Object.hasOwn(o, s) ? o[s] : void 0;
const d = resolveLocalProviderAuthEvidence(u, t);
if (d) return {
apiKey: d.credentialMarker,
source: d.source
};
if (l !== void 0 || n.skipSetupProviderFallback === true) return null;
const f = resolvePluginSetupProviderCore({
provider: s,
config: n.config,
workspaceDir: n.workspaceDir,
env: t
})?.resolveConfigApiKey?.({
provider: s,
env: t
});
if (f?.trim()) return {
apiKey: f,
source: f === "gcp-vertex-credentials" ? "gcloud adc" : "env"
};
return null;
}
//#endregion
export { resolveLocalProviderAuthEvidence as a, listProviderEnvAuthLookupKeys as i, resolveProviderDirectAuthPlanningEvidence as n, resolveProviderEnvAuthEvidence as r, resolveEnvApiKey as t };