openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
189 lines (188 loc) • 7.34 kB
JavaScript
import { s as normalizeNullableString } from "./string-coerce-CCIiB4H7.mjs";
import { n as isTruthyEnvValue } from "./env-HehwidXj.mjs";
import { a as resolveRequiredHomeDir } from "./home-dir-EKmgmdPG.mjs";
import { a as asOptionalRecord, l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { a as extractErrorCode, r as collectErrorGraphCandidates } from "./error-coercion-hDrHOSGy.mjs";
import { t as parseJsonWithJson5Fallback } from "./parse-json-compat-DoWUfD79.mjs";
import { i as resolveConfigEnvVars } from "./env-substitution-CgVeOqUd.mjs";
import { t as hasErrnoCode } from "./errno-DFcoqIT9.mjs";
import { c as readConfigIncludeFileWithGuards, i as INCLUDE_KEY, l as resolveConfigIncludeWritePath, o as hashConfigIncludeRaw, u as resolveConfigIncludes } from "./includes-wjVi5Ymt.mjs";
import { r as resolveStateDir } from "./state-dir-DiG0gXzD.mjs";
import { m as resolveConfigPath, x as resolveIncludeRoots } from "./paths-DAcAg5aV.mjs";
import "./errors-BKzCeTYb.mjs";
import { n as sha256Hex } from "./node-crypto-B6yuTjmm.mjs";
import "./crypto-digest-B2m8wIBj.mjs";
import { i as projectUpdateRehearsalDatabaseConfig } from "./update-rehearsal-paths-Dr1MbUA_.mjs";
import { r as cloneEnvWithPlatformSemantics, t as applyConfigEnvVars } from "./config-env-vars-ByeGN-9N.mjs";
import { s as createConfigResolutionFacts } from "./resolution-facts-xaIKuREs.mjs";
import { t as normalizeLegacyWorkboardDatabaseConfig } from "./legacy-workboard-database-C5tBV8hg.mjs";
import fs from "node:fs";
import path from "node:path";
import { createHash } from "node:crypto";
import os from "node:os";
import JSON5 from "json5";
//#region src/config/io.read-helpers.ts
function hashConfigRaw(e) {
return e === null ? hashConfigIncludeRaw(null) : sha256Hex(e);
}
function hashConfigRevision(e, t, n) {
const r = createHash("sha256").update(e);
for (const [e, i] of Object.entries(t)) r.update(JSON.stringify([
e,
n[e],
i
]));
return r.digest("hex");
}
function readConfigFileIfPresent(e, t) {
try {
return e.fs.readFileSync(t, "utf-8");
} catch (e) {
if (hasErrnoCode(e, "ENOENT")) return;
throw e;
}
}
function resolveConfigSnapshotHash(e) {
return normalizeNullableString(e.hash) ?? (typeof e.raw === "string" ? hashConfigRaw(e.raw) : null);
}
function coerceConfig(e) {
return asOptionalRecord(e) ?? {};
}
function hasConfigMeta(e) {
return isRecord(asOptionalRecord(e)?.meta);
}
function resolveGatewayMode(e) {
const t = asOptionalRecord(asOptionalRecord(e)?.gateway);
return normalizeNullableString(t?.mode);
}
function containsConfigIncludeDirective(e) {
const t = [e];
while (t.length > 0) {
const e = t.pop();
if (Array.isArray(e)) {
for (const n of e) t.push(n);
continue;
}
if (!isRecord(e)) continue;
if ("$include" in e) return true;
for (const n of Object.values(e)) t.push(n);
}
return false;
}
function resolveConfigPathForDeps(e) {
if (e.configPath) return e.configPath;
return resolveConfigPath(e.env, resolveStateDir(e.env, e.homedir));
}
function normalizeConfigIoDeps(e = {}) {
const t = e.env ?? process.env;
return {
fs: e.fs ?? fs,
env: t,
lowerPrecedenceEnv: e.lowerPrecedenceEnv ?? {},
homedir: e.homedir ?? (() => resolveRequiredHomeDir(t, os.homedir)),
configPath: e.configPath ?? "",
mapIncludePath: e.mapIncludePath ?? ((e) => e),
logger: e.logger ?? console,
measure: e.measure ?? (async (e, t) => await t()),
suppressFutureVersionWarning: e.suppressFutureVersionWarning ?? (isTruthyEnvValue(t.OPENCLAW_UPDATE_IN_PROGRESS) || isTruthyEnvValue(t.OPENCLAW_UPDATE_POST_CORE)),
observe: e.observe ?? true
};
}
function parseConfigJson5(e) {
try {
return {
ok: true,
parsed: parseJsonWithJson5Fallback(e, JSON5)
};
} catch (e) {
return {
ok: false,
error: String(e)
};
}
}
const TILDE_PATH_VALUE_RE = /^~(?=$|[\\/])/;
const PATH_LIKE_CONFIG_KEY_RE = /(dir|path|paths|file|root|workspace)$/i;
const PATH_LIKE_CONFIG_LIST_KEYS = /* @__PURE__ */ new Set(["paths", "pathPrepend"]);
function isPathLikeConfigKey(e) {
return Boolean(e && (PATH_LIKE_CONFIG_KEY_RE.test(e) || PATH_LIKE_CONFIG_LIST_KEYS.has(e)));
}
function restoreAuthoredTildePathsForWrite(e, t, n, r) {
if (typeof e === "string" && typeof t === "string" && isPathLikeConfigKey(n) && TILDE_PATH_VALUE_RE.test(t.trim()) && path.normalize(e) === path.join(r, t.trim().slice(2))) return t;
if (Array.isArray(e) && Array.isArray(t)) {
const i = isPathLikeConfigKey(n);
return e.map((e, a) => restoreAuthoredTildePathsForWrite(e, t[a], i ? n : void 0, r));
}
if (!isRecord(e) || !isRecord(t)) return e;
const i = { ...e };
for (const [e, n] of Object.entries(i)) if (Object.hasOwn(t, e)) i[e] = restoreAuthoredTildePathsForWrite(n, t[e], e, r);
return i;
}
function resolveConfigIncludesForRead(e, t, n, r, i, a, o) {
const s = resolveIncludeRoots(n.env, n.homedir);
const recordIncludeWatchPath = (e) => {
a?.add(path.normalize(e));
};
const recordIncludeTarget = (e, n) => {
if (!i) return;
const r = path.normalize(e);
try {
i[r] = path.normalize(n ?? resolveConfigIncludeWritePath({
configPath: t,
includePath: e,
allowedRoots: s
}));
} catch {}
};
return resolveConfigIncludes(e, t, {
readFile: (e) => n.fs.readFileSync(e, "utf-8"),
mapPath: n.mapIncludePath,
onLexicalPath: recordIncludeWatchPath,
onIncludeResolved: o,
readFileWithGuards: ({ includePath: e, resolvedPath: t, rootRealDir: i }) => {
try {
const a = readConfigIncludeFileWithGuards({
includePath: e,
resolvedPath: t,
rootRealDir: i,
ioFs: n.fs,
onResolvedPath: (e) => {
recordIncludeWatchPath(e);
recordIncludeTarget(t, e);
}
});
if (r) r[path.normalize(t)] = hashConfigIncludeRaw(a);
return a;
} catch (e) {
const n = collectErrorGraphCandidates(e, (e) => [e.cause]).some((e) => extractErrorCode(e) === "ENOENT");
if (r && n) r[path.normalize(t)] = hashConfigIncludeRaw(null);
if (n) recordIncludeTarget(t);
throw e;
}
},
parseJson: (e) => JSON5.parse(e)
}, { allowedRoots: s });
}
function resolveConfigForRead(e, t, n = {}) {
if (e && typeof e === "object" && "env" in e) applyConfigEnvVars(e, t, { lowerPrecedenceEnv: n });
const r = [];
const i = /* @__PURE__ */ new Map();
const a = /* @__PURE__ */ new Map();
const o = resolveConfigEnvVars(e, t, {
onMissing: (e) => r.push(e),
onPendingEnvSecretRef: (e, t) => i.set(t, e),
onResolvedEnvSecretRef: (e, t) => a.set(t, e)
});
return {
resolvedConfigRaw: normalizeLegacyWorkboardDatabaseConfig(projectUpdateRehearsalDatabaseConfig(o, t), t),
envSnapshotForRestore: cloneEnvWithPlatformSemantics(t),
envWarnings: r,
resolutionFacts: createConfigResolutionFacts(r, i, coerceConfig(o).secrets?.defaults?.env, a)
};
}
function replaceEnvSnapshot(e, t) {
for (const t of Object.keys(e)) delete e[t];
Object.assign(e, t);
}
//#endregion
export { hashConfigRevision as a, readConfigFileIfPresent as c, resolveConfigIncludesForRead as d, resolveConfigPathForDeps as f, restoreAuthoredTildePathsForWrite as h, hashConfigRaw as i, replaceEnvSnapshot as l, resolveGatewayMode as m, containsConfigIncludeDirective as n, normalizeConfigIoDeps as o, resolveConfigSnapshotHash as p, hasConfigMeta as r, parseConfigJson5 as s, coerceConfig as t, resolveConfigForRead as u };