mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-13 09:50:36 +00:00
chore: Enable "curly" rule to avoid single-statement if confusion/errors.
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import type { ContextPruningToolMatch } from "./settings.js";
|
||||
|
||||
function normalizePatterns(patterns?: string[]): string[] {
|
||||
if (!Array.isArray(patterns)) return [];
|
||||
if (!Array.isArray(patterns)) {
|
||||
return [];
|
||||
}
|
||||
return patterns
|
||||
.map((p) =>
|
||||
String(p ?? "")
|
||||
@@ -17,8 +19,12 @@ type CompiledPattern =
|
||||
| { kind: "regex"; value: RegExp };
|
||||
|
||||
function compilePattern(pattern: string): CompiledPattern {
|
||||
if (pattern === "*") return { kind: "all" };
|
||||
if (!pattern.includes("*")) return { kind: "exact", value: pattern };
|
||||
if (pattern === "*") {
|
||||
return { kind: "all" };
|
||||
}
|
||||
if (!pattern.includes("*")) {
|
||||
return { kind: "exact", value: pattern };
|
||||
}
|
||||
|
||||
const escaped = pattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
const re = new RegExp(`^${escaped.replaceAll("\\*", ".*")}$`);
|
||||
@@ -31,9 +37,15 @@ function compilePatterns(patterns?: string[]): CompiledPattern[] {
|
||||
|
||||
function matchesAny(toolName: string, patterns: CompiledPattern[]): boolean {
|
||||
for (const p of patterns) {
|
||||
if (p.kind === "all") return true;
|
||||
if (p.kind === "exact" && toolName === p.value) return true;
|
||||
if (p.kind === "regex" && p.value.test(toolName)) return true;
|
||||
if (p.kind === "all") {
|
||||
return true;
|
||||
}
|
||||
if (p.kind === "exact" && toolName === p.value) {
|
||||
return true;
|
||||
}
|
||||
if (p.kind === "regex" && p.value.test(toolName)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -46,8 +58,12 @@ export function makeToolPrunablePredicate(
|
||||
|
||||
return (toolName: string) => {
|
||||
const normalized = toolName.trim().toLowerCase();
|
||||
if (matchesAny(normalized, deny)) return false;
|
||||
if (allow.length === 0) return true;
|
||||
if (matchesAny(normalized, deny)) {
|
||||
return false;
|
||||
}
|
||||
if (allow.length === 0) {
|
||||
return true;
|
||||
}
|
||||
return matchesAny(normalized, allow);
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user