openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
47 lines (46 loc) • 1.86 kB
JavaScript
import { A as compileGlobPatterns, U as expandToolGroups, W as normalizeToolName, j as matchesAnyGlobPattern } from "./docker-Bhjg8g2t.js";
//#region src/agents/sandbox-tool-policy.ts
function unionAllow(base, extra) {
if (!Array.isArray(extra) || extra.length === 0) return base;
if (!Array.isArray(base) || base.length === 0) return Array.from(new Set(["*", ...extra]));
return Array.from(new Set([...base, ...extra]));
}
function pickSandboxToolPolicy(config) {
if (!config) return;
const allow = Array.isArray(config.allow) ? unionAllow(config.allow, config.alsoAllow) : Array.isArray(config.alsoAllow) && config.alsoAllow.length > 0 ? unionAllow(void 0, config.alsoAllow) : void 0;
const deny = Array.isArray(config.deny) ? config.deny : void 0;
if (!allow && !deny) return;
return {
allow,
deny
};
}
//#endregion
//#region src/agents/tool-policy-match.ts
function makeToolPolicyMatcher(policy) {
const deny = compileGlobPatterns({
raw: expandToolGroups(policy.deny ?? []),
normalize: normalizeToolName
});
const allow = compileGlobPatterns({
raw: expandToolGroups(policy.allow ?? []),
normalize: normalizeToolName
});
return (name) => {
const normalized = normalizeToolName(name);
if (matchesAnyGlobPattern(normalized, deny)) return false;
if (allow.length === 0) return true;
if (matchesAnyGlobPattern(normalized, allow)) return true;
if (normalized === "apply_patch" && matchesAnyGlobPattern("exec", allow)) return true;
return false;
};
}
function isToolAllowedByPolicyName(name, policy) {
if (!policy) return true;
return makeToolPolicyMatcher(policy)(name);
}
function isToolAllowedByPolicies(name, policies) {
return policies.every((policy) => isToolAllowedByPolicyName(name, policy));
}
//#endregion
export { isToolAllowedByPolicyName as n, pickSandboxToolPolicy as r, isToolAllowedByPolicies as t };