openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
83 lines (82 loc) • 2.36 kB
JavaScript
import { t as isPlainObject } from "./plain-object-CGY5w7fM.mjs";
import { t as isBlockedObjectKey } from "./prototype-keys-CLGi5xDj.mjs";
import "./utils-C8YECn2Q.mjs";
import { t as normalizeConfigModelSelectionParent } from "./model-input-normalization-BSpz0kXV.mjs";
//#region src/config/config-paths.ts
function setOwnConfigProperty(e, t, n) {
if (Object.hasOwn(e, t)) {
e[t] = n;
return;
}
Object.defineProperty(e, t, {
configurable: true,
enumerable: true,
value: n,
writable: true
});
}
/** Parses CLI/config dot-notation paths and rejects unsafe object-key segments. */
function parseConfigPath(e) {
const t = e.trim().split(".").map((e) => e.trim());
if (t.some((e) => !e)) return {
ok: false,
error: "Invalid path. Use dot notation (e.g. foo.bar)."
};
if (t.some((e) => isBlockedObjectKey(e))) return {
ok: false,
error: "Invalid path segment."
};
return {
ok: true,
path: t
};
}
function setConfigValueAtPath(e, t, n) {
const r = t.at(-1);
if (r === void 0) throw new Error("Config path must contain at least one segment");
let i = e;
for (const [e, n] of t.slice(0, -1).entries()) {
const r = Object.hasOwn(i, n) ? i[n] : void 0;
const a = isPlainObject(r) ? r : normalizeConfigModelSelectionParent(r, t, e) ?? {};
if (a !== r) setOwnConfigProperty(i, n, a);
i = a;
}
setOwnConfigProperty(i, r, n);
}
/** Removes a value at a config path and prunes empty parent objects created by setters. */
function unsetConfigValueAtPath(e, t, n) {
const r = t.at(-1);
if (r === void 0) return false;
const i = [];
let a = e;
for (const e of t.slice(0, -1)) {
if (!Object.hasOwn(a, e)) return false;
const t = a[e];
if (!isPlainObject(t)) return false;
i.push({
node: a,
key: e
});
a = t;
}
if (!Object.hasOwn(a, r)) return false;
delete a[r];
for (const { node: e, key: r } of i.toReversed()) {
const a = e[r];
if (isPlainObject(a) && Object.keys(a).length === 0 && (!n || getConfigValueAtPath(n, t.slice(0, i.length)) === void 0)) {
delete e[r];
i.pop();
} else break;
}
return true;
}
function getConfigValueAtPath(e, t) {
let n = e;
for (const e of t) {
if (!isPlainObject(n) || !Object.hasOwn(n, e)) return;
n = n[e];
}
return n;
}
//#endregion
export { unsetConfigValueAtPath as i, parseConfigPath as n, setConfigValueAtPath as r, getConfigValueAtPath as t };