openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
66 lines (65 loc) • 2.4 kB
JavaScript
//#region src/infra/exec-approval-policy-snapshot.ts
function compareUtf8(e, t) {
let n = 0;
let r = 0;
while (n < e.length && r < t.length) {
let i = e.codePointAt(n);
let a = t.codePointAt(r);
n += i > 65535 ? 2 : 1;
r += a > 65535 ? 2 : 1;
if (i >= 55296 && i <= 57343) i = 65533;
if (a >= 55296 && a <= 57343) a = 65533;
if (i !== a) return i - a;
}
return e.length - n - (t.length - r);
}
function compareOptionalUtf8(e, t) {
if (e === void 0) return t === void 0 ? 0 : -1;
if (t === void 0) return 1;
return compareUtf8(e, t);
}
/** Cross-runtime order: tuple fields, absent before present, UTF-8 byte lexicographic. */
function compareExecApprovalPolicyRules(e, t) {
return compareUtf8(e.pattern, t.pattern) || compareOptionalUtf8(e.argPattern, t.argPattern) || compareOptionalUtf8(e.source, t.source);
}
function buildExecApprovalPolicyRuleKey(e) {
return JSON.stringify([
e.pattern,
e.argPattern ?? null,
e.source ?? null
]);
}
function canonicalizeExecApprovalPolicyRules(e) {
return [...new Map(e.map((e) => [buildExecApprovalPolicyRuleKey(e), e])).values()].toSorted(compareExecApprovalPolicyRules);
}
function normalizeExecApprovalPolicySnapshot(e) {
if (e === void 0) return;
if (!e || typeof e !== "object" || Array.isArray(e)) return null;
const t = e;
const n = t.security;
const r = t.ask;
const i = t.askFallback;
const a = t.autoAllowSkills;
const o = t.allowlistRules;
if (n !== "deny" && n !== "allowlist" && n !== "full" || r !== "off" && r !== "on-miss" && r !== "always" || i !== "deny" && i !== "allowlist" && i !== "full" || typeof a !== "boolean" || !Array.isArray(o)) return null;
const s = [];
for (const e of o) {
if (!e || typeof e !== "object" || Array.isArray(e)) return null;
const t = e;
if (typeof t.pattern !== "string" || t.argPattern !== void 0 && typeof t.argPattern !== "string" || t.source !== void 0 && t.source !== "allow-always") return null;
s.push({
pattern: t.pattern,
...typeof t.argPattern === "string" ? { argPattern: t.argPattern } : {},
...t.source === "allow-always" ? { source: t.source } : {}
});
}
return {
security: n,
ask: r,
askFallback: i,
autoAllowSkills: a,
allowlistRules: canonicalizeExecApprovalPolicyRules(s)
};
}
//#endregion
export { canonicalizeExecApprovalPolicyRules as n, normalizeExecApprovalPolicySnapshot as r, buildExecApprovalPolicyRuleKey as t };