UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

46 lines (45 loc) 1.62 kB
import { y as resolveStateDir } from "./paths-mvMm5bYV.js"; import { m as writeJson } from "./json-files-2umMHm0W.js"; import path from "node:path"; import fs from "node:fs/promises"; import crypto from "node:crypto"; //#region src/node-host/config.ts /** Configuration defaults and env parsing for the node-host runner. */ const NODE_HOST_FILE = "node.json"; function resolveNodeHostConfigPath() { return path.join(resolveStateDir(), NODE_HOST_FILE); } function normalizeConfig(config) { const base = { version: 1, nodeId: "", token: config?.token, displayName: config?.displayName, gateway: config?.gateway }; if (config?.version === 1 && typeof config.nodeId === "string") base.nodeId = config.nodeId.trim(); if (!base.nodeId) base.nodeId = crypto.randomUUID(); return base; } /** Load and normalize the node-host config, or null when no readable config exists. */ async function loadNodeHostConfig() { const filePath = resolveNodeHostConfigPath(); try { const raw = await fs.readFile(filePath, "utf8"); return normalizeConfig(JSON.parse(raw)); } catch { return null; } } /** Save node-host config with private file permissions. */ async function saveNodeHostConfig(config) { await writeJson(resolveNodeHostConfigPath(), config, { mode: 384 }); } /** Load or create a node-host config with a stable generated node id. */ async function ensureNodeHostConfig() { const normalized = normalizeConfig(await loadNodeHostConfig()); await saveNodeHostConfig(normalized); return normalized; } //#endregion export { loadNodeHostConfig as n, saveNodeHostConfig as r, ensureNodeHostConfig as t };