UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

107 lines (106 loc) • 4.46 kB
import { n as parseBooleanValue } from "./boolean-2vRuVafy.mjs"; //#region src/infra/fs-safe-env.ts const nativeModeKey = "OPENCLAW_FS_SAFE_NATIVE_MODE"; const legacyModeKeys = ["FS_SAFE_PYTHON_MODE", "OPENCLAW_FS_SAFE_PYTHON_MODE"]; const legacyKeys = [ ...legacyModeKeys, "FS_SAFE_PYTHON", "OPENCLAW_FS_SAFE_PYTHON", "OPENCLAW_PINNED_PYTHON", "OPENCLAW_PINNED_WRITE_PYTHON" ]; const fallbacks = /* @__PURE__ */ new WeakMap(); let warned = false; function nativeModeIsValid(e) { const t = e?.trim().toLowerCase() ?? ""; return /^(?:0|false|off|never|1|true|on|auto|required|require)$/u.test(t); } function currentFallback(e) { const t = fallbacks.get(e); return t && e[t.key] === t.value ? t : void 0; } /** Read the operator/config inputs without promoting a derived native fallback. */ function fsSafeEnvInput(e) { const t = currentFallback(e); if (!t) return e; const n = { ...e, [t.key]: t.previous }; if (!t.existed) delete n[t.key]; return n; } /** Retire our unchanged projection before an environment owner writes real inputs. */ function clearFsSafeEnvFallback(e) { const t = currentFallback(e); fallbacks.delete(e); if (!t) return; if (!t.existed) delete e[t.key]; else e[t.key] = t.previous; } /** Preserve OpenClaw's retired Python mode env contract below both native names. */ function normalizeFsSafeNativeEnv(e = process.env) { clearFsSafeEnvFallback(e); const t = legacyKeys.filter((t) => e[t] !== void 0); if (t.length && e === process.env && !warned) { warned = true; process.emitWarning(`${t.join(", ")} is deprecated. OpenClaw maps legacy mode values to native mode only when neither FS_SAFE_NATIVE_MODE nor OPENCLAW_FS_SAFE_NATIVE_MODE selects a mode. Replace Python mode variables with native mode variables; interpreter path settings are ignored.`, { code: "FS_SAFE_PYTHON_DEPRECATED", type: "DeprecationWarning" }); } if (nativeModeIsValid(e.FS_SAFE_NATIVE_MODE) || nativeModeIsValid(e[nativeModeKey])) return; const n = legacyModeKeys.map((t) => e[t]).find((e) => e !== void 0); if (n === void 0) return; const r = Object.keys(e); const i = !r.includes(nativeModeKey) && Object.hasOwn(e, nativeModeKey) ? r.find((e) => e.toUpperCase() === nativeModeKey) ?? nativeModeKey : nativeModeKey; const a = Object.hasOwn(e, i); const o = e[i]; e[i] = n; fallbacks.set(e, { key: i, value: n, previous: o, existed: a }); } //#endregion //#region src/infra/test-runtime-env.ts /** Detects Vitest/test execution from the env shape used by local and worker processes. */ function isVitestRuntimeEnv(e = process.env) { const t = e.VITEST; return t === "true" || t === "1" || e.VITEST_POOL_ID !== void 0 || e.VITEST_WORKER_ID !== void 0 || e.NODE_ENV === "test"; } /** Enables the shared fast-test shortcuts only inside a detected test runtime. */ function isFastTestRuntimeEnv(e = process.env) { return e.OPENCLAW_TEST_FAST === "1" && (isVitestRuntimeEnv(e) || e !== process.env && isVitestRuntimeEnv(process.env)); } //#endregion //#region src/infra/env.ts const ENV_NORMALIZATION_KEY_GROUPS = [["ZAI_API_KEY", "Z_AI_API_KEY"]]; /** Normalizes the legacy Z_AI_API_KEY spelling into the canonical ZAI_API_KEY env var. */ function normalizeZaiEnv(e = process.env) { if (!e.ZAI_API_KEY?.trim() && e.Z_AI_API_KEY?.trim()) e.ZAI_API_KEY = e.Z_AI_API_KEY; } /** Expands env keys to include aliases that process-wide normalization treats as equivalent. */ function expandEnvNormalizationKeys(e) { const t = /* @__PURE__ */ new Set(); for (const n of e) for (const e of resolveEnvNormalizationKeys(n)) t.add(e); return t; } /** Resolves one env key to its canonical-first runtime normalization group. */ function resolveEnvNormalizationKeys(e) { const t = process.platform === "win32" ? e.toUpperCase() : e; return ENV_NORMALIZATION_KEY_GROUPS.find((e) => e.some((e) => e === t)) ?? [t]; } /** Interprets common human/operator truthy env strings. */ function isTruthyEnvValue(e) { return parseBooleanValue(e) === true; } /** Applies process-wide env normalization before runtime configuration is read. */ function normalizeEnv() { normalizeZaiEnv(process.env); normalizeFsSafeNativeEnv(process.env); } //#endregion export { resolveEnvNormalizationKeys as a, clearFsSafeEnvFallback as c, normalizeZaiEnv as i, fsSafeEnvInput as l, isTruthyEnvValue as n, isFastTestRuntimeEnv as o, normalizeEnv as r, isVitestRuntimeEnv as s, expandEnvNormalizationKeys as t, normalizeFsSafeNativeEnv as u };