openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
91 lines (90 loc) • 3.7 kB
JavaScript
import { c as isRecord } from "./record-coerce-DItp3I4t.js";
import { S as resolveConfigEnvVars, j as resolveConfigIncludes, k as readConfigIncludeFileWithGuards } from "./redact-BtvPPfTi.js";
import { t as parseJsonWithJson5Fallback } from "./parse-json-compat-Cc8PAOTi.js";
import { f as resolveConfigPath, v as resolveIncludeRoots } from "./paths-D2sRr1a_.js";
import { t as applyConfigEnvVars } from "./config-env-vars-DUfQlcAk.js";
import fs from "node:fs";
import path from "node:path";
//#region src/config/gateway-dispatch-config.ts
const GATEWAY_DISPATCH_SHELL_ENV_EXPECTED_KEYS = ["OPENCLAW_GATEWAY_TOKEN", "OPENCLAW_GATEWAY_PASSWORD"];
const GATEWAY_DISPATCH_TOP_LEVEL_KEYS = [
"agents",
"env",
"gateway",
"plugins",
"secrets",
"session"
];
function cloneConfigValue(value) {
if (Array.isArray(value)) return value.map((entry) => cloneConfigValue(entry));
if (!isRecord(value)) return value;
const out = {};
for (const [key, child] of Object.entries(value)) out[key] = cloneConfigValue(child);
return out;
}
function projectGatewayDispatchConfig(value) {
if (!isRecord(value)) return {};
const projected = {};
for (const key of GATEWAY_DISPATCH_TOP_LEVEL_KEYS) if (Object.hasOwn(value, key)) projected[key] = cloneConfigValue(value[key]);
return projected;
}
function applyGatewayDispatchSessionDefaults(config) {
if (config.session?.mainKey === void 0) return config;
return {
...config,
session: {
...config.session,
mainKey: "main"
}
};
}
function resolveIncludesForGatewayDispatch(parsed, configPath, env) {
return resolveConfigIncludes(parsed, configPath, {
readFile: (candidate) => fs.readFileSync(candidate, "utf-8"),
readFileWithGuards: ({ includePath, resolvedPath, rootRealDir }) => readConfigIncludeFileWithGuards({
includePath,
resolvedPath,
rootRealDir,
ioFs: fs
}),
parseJson: parseJsonWithJson5Fallback
}, { allowedRoots: resolveIncludeRoots(env) });
}
function resolveGatewayDispatchEnvVars(config, env) {
if (isRecord(config) && Object.hasOwn(config, "env")) applyConfigEnvVars(config, env);
return resolveConfigEnvVars(config, env, { onMissing: () => void 0 });
}
function readRawGatewayDispatchConfig(options = {}) {
const env = options.env ?? process.env;
const configPath = options.configPath ?? resolveConfigPath(env);
if (!fs.existsSync(configPath)) return {
config: {},
configPath
};
const raw = fs.readFileSync(configPath, "utf-8");
return {
config: applyGatewayDispatchSessionDefaults(projectGatewayDispatchConfig(resolveGatewayDispatchEnvVars(resolveIncludesForGatewayDispatch(parseJsonWithJson5Fallback(raw), configPath, env), env))),
configPath
};
}
function readGatewayDispatchConfig(options = {}) {
return readRawGatewayDispatchConfig(options).config;
}
async function readGatewayDispatchConfigWithShellEnvFallback(options = {}) {
const env = options.env ?? process.env;
const firstRead = readRawGatewayDispatchConfig(options);
const { loadShellEnvFallback, resolveShellEnvFallbackTimeoutMs, shouldDeferShellEnvFallback, shouldEnableShellEnvFallback } = await import("./shell-env-CLXu7ydM.js");
if ((shouldEnableShellEnvFallback(env) || firstRead.config.env?.shellEnv?.enabled === true) && !shouldDeferShellEnvFallback(env)) loadShellEnvFallback({
enabled: true,
env,
expectedKeys: [...GATEWAY_DISPATCH_SHELL_ENV_EXPECTED_KEYS],
logger: options.logger ?? console,
timeoutMs: firstRead.config.env?.shellEnv?.timeoutMs ?? resolveShellEnvFallbackTimeoutMs(env)
});
return readGatewayDispatchConfig({
...options,
configPath: path.resolve(firstRead.configPath)
});
}
//#endregion
export { readGatewayDispatchConfigWithShellEnvFallback as n, readGatewayDispatchConfig as t };