UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

114 lines (113 loc) 5.18 kB
import { s as coerceSecretRef, t as DEFAULT_SECRET_PROVIDER_ALIAS } from "./types.secrets-kC0nOetj.js"; //#region src/config/resolution-facts.ts const configResolutionFacts = /* @__PURE__ */ new WeakMap(); const envSecretRefsByFacts = /* @__PURE__ */ new WeakMap(); function createConfigResolutionFacts(warnings, pendingEnvSecretRefs = /* @__PURE__ */ new Map(), envProvider = DEFAULT_SECRET_PROVIDER_ALIAS, resolvedEnvSecretRefs = /* @__PURE__ */ new Map()) { const facts = new Set(warnings.map(({ configPath }) => configPath)); const provider = envProvider?.trim() || "default"; if (pendingEnvSecretRefs.size > 0 || resolvedEnvSecretRefs.size > 0) { const envSecretRefs = /* @__PURE__ */ new Map(); for (const [path, id] of pendingEnvSecretRefs) envSecretRefs.set(path, { ref: { source: "env", provider, id }, state: "pending" }); for (const [path, id] of resolvedEnvSecretRefs) envSecretRefs.set(path, { ref: { source: "env", provider, id }, state: "resolved" }); envSecretRefsByFacts.set(facts, envSecretRefs); } return facts; } function setConfigResolutionFacts(target, facts) { if (!target || typeof target !== "object") return; if (facts === null) { configResolutionFacts.delete(target); return; } configResolutionFacts.set(target, facts); } function getConfigResolutionFacts(target) { return target && typeof target === "object" ? configResolutionFacts.get(target) ?? null : null; } function copyConfigResolutionFacts(source, target) { setConfigResolutionFacts(target, getConfigResolutionFacts(source)); } function cloneConfigWithResolutionFacts(value) { const cloned = structuredClone(value); copyConfigResolutionFacts(value, cloned); return cloned; } function copyConfigResolutionFactsExcept(source, target, paths) { const facts = getConfigResolutionFacts(source); if (facts === null) { setConfigResolutionFacts(target, null); return; } const envSecretRefs = envSecretRefsByFacts.get(facts); if (paths.length === 0 || !paths.some((path) => facts.has(path) || envSecretRefs?.has(path) === true)) { setConfigResolutionFacts(target, facts); return; } const remaining = new Set(facts); paths.forEach((path) => remaining.delete(path)); if (envSecretRefs) { const remainingEnvSecretRefs = new Map(envSecretRefs); paths.forEach((path) => remainingEnvSecretRefs.delete(path)); if (remainingEnvSecretRefs.size > 0) envSecretRefsByFacts.set(remaining, remainingEnvSecretRefs); } setConfigResolutionFacts(target, remaining); } /** Captures loader provenance as deterministic data for a prepared worker generation. */ function serializeConfigResolutionFacts(target) { const facts = getConfigResolutionFacts(target); return facts === null ? null : { unresolvedPaths: [...facts].toSorted(), envSecretRefs: [...envSecretRefsByFacts.get(facts) ?? []].toSorted(([left], [right]) => left.localeCompare(right)) }; } /** Restores known-empty facts too: absence would reparse decoded literals as references. */ function restoreConfigResolutionFacts(target, data) { if (data === null) { setConfigResolutionFacts(target, null); return; } const facts = new Set(data.unresolvedPaths); if (data.envSecretRefs.length > 0) envSecretRefsByFacts.set(facts, new Map(data.envSecretRefs)); setConfigResolutionFacts(target, facts); } function hasUnresolvedConfigPath(target, path) { return getConfigResolutionFacts(target)?.has(path) === true; } /** Returns only a still-pending reference recorded from the authored config source. */ function getAuthoredConfigSecretRef(target, path) { const facts = getConfigResolutionFacts(target); const fact = facts ? envSecretRefsByFacts.get(facts)?.get(path) : void 0; return fact?.state === "pending" ? fact.ref : null; } /** Returns the env source of a value that config substitution already materialized. */ function getResolvedConfigEnvSecretRef(target, path) { const facts = getConfigResolutionFacts(target); const fact = facts ? envSecretRefsByFacts.get(facts)?.get(path) : void 0; return fact?.state === "resolved" ? fact.ref : null; } /** Reads inline references from authored facts and structured references from their values. */ function resolveConfigSecretRef(params) { return typeof params.value === "string" && getConfigResolutionFacts(params.config) !== null ? getAuthoredConfigSecretRef(params.config, params.path) : coerceSecretRef(params.value, params.defaults); } function hasUnresolvedConfigPathInSubtree(target, path) { const facts = getConfigResolutionFacts(target); if (facts === null) return false; for (const candidate of facts) if (candidate === path || candidate.startsWith(`${path}.`) || candidate.startsWith(`${path}[`)) return true; return false; } //#endregion export { getAuthoredConfigSecretRef as a, hasUnresolvedConfigPath as c, restoreConfigResolutionFacts as d, serializeConfigResolutionFacts as f, createConfigResolutionFacts as i, hasUnresolvedConfigPathInSubtree as l, copyConfigResolutionFacts as n, getConfigResolutionFacts as o, setConfigResolutionFacts as p, copyConfigResolutionFactsExcept as r, getResolvedConfigEnvSecretRef as s, cloneConfigWithResolutionFacts as t, resolveConfigSecretRef as u };