openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
156 lines (155 loc) • 7.01 kB
JavaScript
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { t as parseJsonWithJson5Fallback } from "./parse-json-compat-DoWUfD79.mjs";
import { c as readConfigIncludeFileWithGuards, l as resolveConfigIncludeWritePath, o as hashConfigIncludeRaw } from "./includes-wjVi5Ymt.mjs";
import { i as formatInvalidConfigDetails, r as createInvalidConfigError } from "./io.invalid-config-DK-BYZ4Y.mjs";
import { n as containsConfigIncludeDirective } from "./io.read-helpers-qAcqsRC0.mjs";
import { t as ConfigMutationConflictError } from "./mutation-conflict-Bs0nsNWn.mjs";
import { n as composeConfigWriteAssertions } from "./write-authority-l37tp55r.mjs";
import fs from "node:fs";
import path from "node:path";
import { isDeepStrictEqual } from "node:util";
//#region src/plugins/install-config-mutation.ts
const CONFIG_MUTATION_ALLOWED = { mode: "allowed" };
function supportsInstallConfigSingleTopLevelIncludeShape(e) {
if (!containsConfigIncludeDirective(e)) return true;
return isRecord(e) && Object.keys(e).length === 1 && typeof e.$include === "string";
}
function resolveSingleTopLevelIncludePath(e, t, n) {
const r = e[n];
if (!isRecord(r) || Object.keys(r).length !== 1 || typeof r.$include !== "string") return null;
return path.normalize(path.isAbsolute(r.$include) ? r.$include : path.resolve(path.dirname(t), r.$include));
}
function resolveConfigMutationPreflight(e) {
if (Object.hasOwn(e.parsed, "$include")) return {
mode: "blocked",
scope: "config",
reason: `Config ${e.section} are stored through an unsupported $include shape at the root; edit the included file directly or move ${e.section} into the root config before installing.`
};
if (!supportsInstallConfigSingleTopLevelIncludeShape(e.parsed[e.section])) return {
mode: "blocked",
scope: e.section,
reason: `Config ${e.section} are stored through an unsupported $include shape; edit the included file directly or move ${e.section} to a single-file top-level include before installing.`
};
const t = resolveSingleTopLevelIncludePath(e.parsed, e.snapshotPath, e.section);
if (!t) return CONFIG_MUTATION_ALLOWED;
const n = e.writeOptions.includeFileTargetsForWrite?.[t];
let r = null;
try {
r = resolveConfigIncludeWritePath({
configPath: e.snapshotPath,
includePath: t,
allowedRoots: []
});
} catch {}
if (n && r && path.normalize(n) === path.normalize(r)) {
const n = e.writeOptions.includeFileHashesForWrite?.[t];
try {
const i = readConfigIncludeFileWithGuards({
includePath: t,
resolvedPath: r,
rootRealDir: fs.realpathSync(path.dirname(e.snapshotPath))
});
if (n !== hashConfigIncludeRaw(i)) return {
mode: "blocked",
scope: e.section,
reason: `Config ${e.section} include changed since the config was read; rerun the install after reloading the config.`
};
if (containsConfigIncludeDirective(parseJsonWithJson5Fallback(i))) return {
mode: "blocked",
scope: e.section,
reason: `Config ${e.section} are stored through a nested $include; edit the included file directly or remove the nested $include before installing.`
};
return CONFIG_MUTATION_ALLOWED;
} catch {
return {
mode: "blocked",
scope: e.section,
reason: `Config ${e.section} include could not be inspected at its snapshot target; rerun the install after repairing or reloading the config.`
};
}
}
return {
mode: "blocked",
scope: e.section,
reason: `Config ${e.section} are stored in an external or unresolved top-level $include; edit the included file directly or move it under the config directory before installing.`
};
}
function resolveInstallConfigMutationPreflights(e) {
const t = resolveConfigMutationPreflight({
...e,
section: "plugins"
});
const n = resolveConfigMutationPreflight({
...e,
section: "hooks"
});
const r = resolveSingleTopLevelIncludePath(e.parsed, e.snapshotPath, "plugins");
const i = resolveSingleTopLevelIncludePath(e.parsed, e.snapshotPath, "hooks");
const a = r ? e.writeOptions.includeFileTargetsForWrite?.[r] : void 0;
const o = i ? e.writeOptions.includeFileTargetsForWrite?.[i] : void 0;
if (a && o && path.normalize(a) === path.normalize(o)) {
const e = {
mode: "blocked",
scope: "config",
reason: "Config plugins and hooks share the same top-level $include target; split them into separate include files before installing."
};
return {
hookMutation: e,
pluginMutation: e
};
}
return {
hookMutation: n,
pluginMutation: t
};
}
function resolveCombinedPluginAndHookConfigMutationPreflight(e) {
const t = resolveSingleTopLevelIncludePath(e.parsed, e.snapshotPath, "plugins");
const n = resolveSingleTopLevelIncludePath(e.parsed, e.snapshotPath, "hooks");
if (!t && !n) return CONFIG_MUTATION_ALLOWED;
return {
mode: "blocked",
scope: "config",
reason: "Config plugins and hooks cannot be updated together while either section uses a top-level $include; update them separately."
};
}
function selectInstallMutationWriteOptions(e, t) {
return {
inputBase: "source",
auditOrigin: "plugin-install",
assertConfigPathForWrite: t ? composeConfigWriteAssertions(e.assertConfigPathForWrite, t) : e.assertConfigPathForWrite,
expectedConfigPath: e.expectedConfigPath,
ownedConfigPathForWrite: e.ownedConfigPathForWrite,
envSnapshotForRestore: e.envSnapshotForRestore,
includeFileHashesForWrite: e.includeFileHashesForWrite,
includeFileTargetsForWrite: e.includeFileTargetsForWrite
};
}
function assertWriteOptionRecordFresh(e) {
if (!isDeepStrictEqual(e.current ?? {}, e.expected ?? {})) throw new ConfigMutationConflictError(e.message);
}
async function assertRecordsOnlyUpdateConfigFresh(e) {
const { readConfigFileSnapshotForWrite: t } = await import("./config/config.js");
const n = await t(e.writeOptions);
const r = {
...n.writeOptions,
...e.writeOptions
};
const i = n.snapshot.hash ?? null;
r.assertConfigPathForWrite?.();
if (r.expectedConfigPath !== void 0 && r.expectedConfigPath !== n.snapshot.path) throw new ConfigMutationConflictError("config path changed since last load", { retryable: false });
if (e.baseHash !== void 0 && e.baseHash !== i) throw new ConfigMutationConflictError("config changed since last load");
assertWriteOptionRecordFresh({
current: n.writeOptions.includeFileTargetsForWrite,
expected: e.writeOptions?.includeFileTargetsForWrite,
message: "included config target changed since last load"
});
assertWriteOptionRecordFresh({
current: n.writeOptions.includeFileHashesForWrite,
expected: e.writeOptions?.includeFileHashesForWrite,
message: "included config changed since last load"
});
if (!n.snapshot.valid) throw createInvalidConfigError(n.snapshot.path, formatInvalidConfigDetails(n.snapshot.issues));
}
//#endregion
export { supportsInstallConfigSingleTopLevelIncludeShape as a, selectInstallMutationWriteOptions as i, resolveCombinedPluginAndHookConfigMutationPreflight as n, resolveInstallConfigMutationPreflights as r, assertRecordsOnlyUpdateConfigFresh as t };