openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
104 lines (103 loc) • 5.36 kB
JavaScript
import { t as isPlainObject } from "./plain-object-CGY5w7fM.mjs";
import { n as resolveIdentityPathViaExistingAncestorSync } from "./boundary-path-C2gXEiXe.mjs";
import { p as shortenHomePath } from "./utils-C8YECn2Q.mjs";
import { m as resolveConfigPath } from "./paths-DAcAg5aV.mjs";
import { n as getDatabaseEngineSelection } from "./context-DE59wyBs.mjs";
import { n as formatConfigPatchPath, t as collectBaseArrayPaths } from "./patch-replace-paths-GzVnRbCn.mjs";
import { n as createMergePatch, t as applyMergePatch } from "./merge-patch-D-7aajuy.mjs";
import { t as prepareConfigWriteTopology } from "./io.write-topology-B__9tFVE.mjs";
import { isDeepStrictEqual } from "node:util";
//#region src/cli/config-cli-owner-patch.ts
const MAX_REPLACE_PATHS = 256;
function collectReplacePaths(e, t, n) {
if (!isPlainObject(e)) return [];
return Object.entries(e).flatMap(([e, r]) => {
const i = formatConfigPatchPath(n, e);
const a = isPlainObject(t) ? t[e] : void 0;
if (isPlainObject(r)) return collectReplacePaths(r, a, i);
const o = collectBaseArrayPaths(a, i);
return Array.isArray(r) && !o.includes(i) ? [i].concat(o) : o;
});
}
function createOwnerMergePatch(e, t) {
const n = createMergePatch(e, t);
const r = collectReplacePaths(n, e);
const apply = (e) => {
return applyMergePatch(e, n, {
mergeObjectArraysById: true,
replaceArrayPaths: new Set(r)
});
};
return {
patch: n,
replacePaths: r,
apply
};
}
/**
* Prepare the authored config the local writer would save for this change, including the
* ownership it derives from the command's explicit and unset paths. config.patch carries no
* such paths, so the Gateway's writer derives ownership and `${VAR}` references from the
* merged result alone; a change whose result would differ there is refused, not approximated.
*/
async function prepareConfigOwnerChange(e) {
const { previous: t, explicitSetPaths: n, unsetPaths: r, ...i } = e;
const a = await prepareConfigWriteTopology({
...i,
nextConfig: e.next,
options: { explicitSetPaths: n },
unsetPaths: r
});
const refusal = (e) => new Error("The running Gateway would save a different result for this change: it cannot see which paths this command set, so it would recompute environment references or agent, auth or session-store ownership differently. Nothing was sent and no settings were saved. Split the change into separate commands (for example set an owner path after the roster or store change), or stop the Gateway through its service owner, wait for ownership to release, then rerun this exact command.", e === void 0 ? void 0 : { cause: e });
const o = await prepareConfigWriteTopology({
...i,
nextConfig: createOwnerMergePatch(t, a.authoredConfig).apply(e.snapshot.sourceConfig),
options: {},
unsetPaths: []
}).catch((e) => {
throw refusal(e);
});
if (!isDeepStrictEqual(o.authoredConfig, a.authoredConfig)) throw refusal();
return {
previous: t,
next: a.authoredConfig,
clearedSessionStoreOwner: a.clearedSessionStoreOwner
};
}
/**
* Express a prepared CLI change as one Gateway config.patch request against `baseHash`. Arrays replace or
* delete exactly, as the local writer would; a change the Gateway's merge cannot reproduce
* (for example an authored null) is refused instead of being approximated.
*/
function buildConfigOwnerPatch(e, t) {
const { patch: n, replacePaths: r, apply: i } = createOwnerMergePatch(e.previous, e.next);
if (r.length > MAX_REPLACE_PATHS || !isDeepStrictEqual(i(e.previous), e.next)) throw new Error("This change cannot be expressed as a Gateway config patch, so it was not sent to the running Gateway. No settings were saved. To apply it, stop the Gateway through its service owner, wait for ownership to release, then rerun this exact command.");
return {
raw: JSON.stringify(n),
...typeof t === "string" ? { baseHash: t } : {},
...r.length > 0 ? { replacePaths: r } : {}
};
}
/**
* Bind a routed config change to the file the running Gateway writes. The change is prepared
* from this command's own read; the returned commit sends it as one config.patch against the
* owner's revision and returns the restart hint, if any, plus the owner's billing-route warnings.
*/
async function bindConfigOwner(e, t) {
const n = await e("config.get", {});
const r = resolveConfigPath(t);
if (typeof n.path !== "string" || resolveIdentityPathViaExistingAncestorSync(n.path) !== resolveIdentityPathViaExistingAncestorSync(r)) throw new Error(`The running Gateway owns this state directory but uses config file ${typeof n.path === "string" ? shortenHomePath(n.path) : "<unknown>"}, not ${shortenHomePath(r)}. Nothing was sent and no settings were saved. Rerun with the Gateway's config path, or stop the Gateway through its service owner and rerun this exact command.`);
if (!getDatabaseEngineSelection()) {
const { readConfigSnapshotForEngineSelection: e } = await import("./config-preflight-snapshot-DXY8QhxC.mjs");
await e();
}
return async (t) => {
const r = await e("config.patch", buildConfigOwnerPatch(t, n.hash));
return {
hint: r.restart || r.restartPending ? "The Gateway is restarting to apply it." : void 0,
warnings: Array.isArray(r.billingRouteWarnings) ? r.billingRouteWarnings.filter((e) => typeof e === "string") : []
};
};
}
//#endregion
export { bindConfigOwner, prepareConfigOwnerChange };