Skip to content

Commit 5b605da

Browse files
authored
fix(ui): keep agent tool policy out of the initial bundle (#132124)
1 parent 6e6a0e5 commit 5b605da

3 files changed

Lines changed: 100 additions & 102 deletions

File tree

‎ui/src/lib/agents/display.ts‎

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ import {
55
normalizeOptionalString,
66
} from "@openclaw/normalization-core/string-coerce";
77
import { normalizeStringEntries } from "@openclaw/normalization-core/string-normalization";
8-
import {
9-
expandToolGroups,
10-
normalizeToolPolicyName,
11-
resolveToolProfilePolicy,
12-
} from "../../../../src/agents/tool-policy-shared.js";
138
import type {
149
AgentIdentityResult,
1510
AgentsFilesListResult,
@@ -201,11 +196,6 @@ export function resolveToolProfileOptions(
201196
}));
202197
}
203198

204-
type ToolPolicy = {
205-
allow?: string[];
206-
deny?: string[];
207-
};
208-
209199
type GitHubIdentityConfigValue = {
210200
profileId?: string;
211201
gitAuthor?: { name?: string; email?: string };
@@ -592,90 +582,3 @@ export function buildModelOptions(
592582

593583
return options;
594584
}
595-
596-
type CompiledPattern =
597-
| { kind: "all" }
598-
| { kind: "exact"; value: string }
599-
| { kind: "regex"; value: RegExp };
600-
601-
function compilePattern(pattern: string): CompiledPattern {
602-
const normalized = normalizeToolPolicyName(pattern);
603-
if (!normalized) {
604-
return { kind: "exact", value: "" };
605-
}
606-
if (normalized === "*") {
607-
return { kind: "all" };
608-
}
609-
if (!normalized.includes("*")) {
610-
return { kind: "exact", value: normalized };
611-
}
612-
const escaped = normalized.replace(/[.*+?^${}()|[\\]\\]/g, "\\$&");
613-
return { kind: "regex", value: new RegExp(`^${escaped.replaceAll("\\*", ".*")}$`) };
614-
}
615-
616-
function compilePatterns(patterns?: string[]): CompiledPattern[] {
617-
if (!Array.isArray(patterns)) {
618-
return [];
619-
}
620-
return expandToolGroups(patterns)
621-
.map(compilePattern)
622-
.filter((pattern) => {
623-
return pattern.kind !== "exact" || pattern.value.length > 0;
624-
});
625-
}
626-
627-
function matchesAny(name: string, patterns: CompiledPattern[]) {
628-
for (const pattern of patterns) {
629-
if (pattern.kind === "all") {
630-
return true;
631-
}
632-
if (pattern.kind === "exact" && name === pattern.value) {
633-
return true;
634-
}
635-
if (pattern.kind === "regex" && pattern.value.test(name)) {
636-
return true;
637-
}
638-
}
639-
return false;
640-
}
641-
642-
export function isAllowedByPolicy(name: string, policy?: ToolPolicy) {
643-
if (!policy) {
644-
return true;
645-
}
646-
const normalized = normalizeToolPolicyName(name);
647-
const deny = compilePatterns(policy.deny);
648-
if (matchesAny(normalized, deny)) {
649-
return false;
650-
}
651-
const allow = compilePatterns(policy.allow);
652-
if (allow.length === 0) {
653-
return true;
654-
}
655-
if (matchesAny(normalized, allow)) {
656-
return true;
657-
}
658-
if (normalized === "apply_patch" && matchesAny("exec", allow)) {
659-
return true;
660-
}
661-
return false;
662-
}
663-
664-
export function matchesList(name: string, list?: string[]) {
665-
if (!Array.isArray(list) || list.length === 0) {
666-
return false;
667-
}
668-
const normalized = normalizeToolPolicyName(name);
669-
const patterns = compilePatterns(list);
670-
if (matchesAny(normalized, patterns)) {
671-
return true;
672-
}
673-
if (normalized === "apply_patch" && matchesAny("exec", patterns)) {
674-
return true;
675-
}
676-
return false;
677-
}
678-
679-
export function resolveToolProfile(profile: string) {
680-
return resolveToolProfilePolicy(profile) ?? undefined;
681-
}

‎ui/src/pages/agents/panels-tools-skills.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { normalizeLowercaseStringOrEmpty } from "@openclaw/normalization-core/st
22
import { normalizeStringEntries } from "@openclaw/normalization-core/string-normalization";
33
// Control UI view renders agents panels tools skills screen content.
44
import { html, nothing } from "lit";
5-
import { normalizeToolPolicyName } from "../../../../src/agents/tool-policy-shared.js";
5+
import {
6+
normalizeToolPolicyName,
7+
resolveToolProfilePolicy,
8+
} from "../../../../src/agents/tool-policy-shared.js";
69
import type {
710
SkillStatusEntry,
811
SkillStatusReport,
@@ -20,12 +23,9 @@ import { t } from "../../i18n/index.ts";
2023
import {
2124
type AgentToolEntry,
2225
type AgentToolSection,
23-
isAllowedByPolicy,
24-
matchesList,
2526
resolveAgentConfig,
2627
resolveAgentSkillsFilter,
2728
resolveToolProfileOptions,
28-
resolveToolProfile,
2929
resolveToolSections,
3030
} from "../../lib/agents/display.ts";
3131
import { formatUiExternalText } from "../../lib/format-error.ts";
@@ -39,6 +39,7 @@ import {
3939
} from "../../lib/skills-shared.ts";
4040
import type { GitHubIdentityController } from "./github-identity-controller.ts";
4141
import { renderGitHubIdentity } from "./github-identity-view.ts";
42+
import { isAllowedByPolicy, matchesList } from "./tool-policy.ts";
4243

4344
function renderToolMetaBadges(labels: string[]) {
4445
if (labels.length === 0) {
@@ -272,7 +273,7 @@ export function renderAgentTools(params: {
272273
const deny = hasAgentAllow ? [] : Array.isArray(agentTools.deny) ? agentTools.deny : [];
273274
const basePolicy = hasAgentAllow
274275
? { allow: agentTools.allow ?? [], deny: agentTools.deny ?? [] }
275-
: (resolveToolProfile(profile) ?? undefined);
276+
: resolveToolProfilePolicy(profile);
276277
const toolIds = toolSections.flatMap((section) => section.tools.map((tool) => tool.id));
277278

278279
const resolveAllowed = (toolId: string) => {

‎ui/src/pages/agents/tool-policy.ts‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Keep catalog-backed policy evaluation behind the Agents page's lazy boundary;
2+
// importing it from shared agent display helpers loads tool descriptions at startup.
3+
import {
4+
expandToolGroups,
5+
normalizeToolPolicyName,
6+
} from "../../../../src/agents/tool-policy-shared.js";
7+
8+
type ToolPolicy = {
9+
allow?: string[];
10+
deny?: string[];
11+
};
12+
13+
type CompiledPattern =
14+
| { kind: "all" }
15+
| { kind: "exact"; value: string }
16+
| { kind: "regex"; value: RegExp };
17+
18+
function compilePattern(pattern: string): CompiledPattern {
19+
const normalized = normalizeToolPolicyName(pattern);
20+
if (!normalized) {
21+
return { kind: "exact", value: "" };
22+
}
23+
if (normalized === "*") {
24+
return { kind: "all" };
25+
}
26+
if (!normalized.includes("*")) {
27+
return { kind: "exact", value: normalized };
28+
}
29+
const escaped = normalized.replace(/[.*+?^${}()|[\\]\\]/g, "\\$&");
30+
return { kind: "regex", value: new RegExp(`^${escaped.replaceAll("\\*", ".*")}$`) };
31+
}
32+
33+
function compilePatterns(patterns?: string[]): CompiledPattern[] {
34+
if (!Array.isArray(patterns)) {
35+
return [];
36+
}
37+
return expandToolGroups(patterns)
38+
.map(compilePattern)
39+
.filter((pattern) => {
40+
return pattern.kind !== "exact" || pattern.value.length > 0;
41+
});
42+
}
43+
44+
function matchesAny(name: string, patterns: CompiledPattern[]) {
45+
for (const pattern of patterns) {
46+
if (pattern.kind === "all") {
47+
return true;
48+
}
49+
if (pattern.kind === "exact" && name === pattern.value) {
50+
return true;
51+
}
52+
if (pattern.kind === "regex" && pattern.value.test(name)) {
53+
return true;
54+
}
55+
}
56+
return false;
57+
}
58+
59+
export function isAllowedByPolicy(name: string, policy?: ToolPolicy) {
60+
if (!policy) {
61+
return true;
62+
}
63+
const normalized = normalizeToolPolicyName(name);
64+
const deny = compilePatterns(policy.deny);
65+
if (matchesAny(normalized, deny)) {
66+
return false;
67+
}
68+
const allow = compilePatterns(policy.allow);
69+
if (allow.length === 0) {
70+
return true;
71+
}
72+
if (matchesAny(normalized, allow)) {
73+
return true;
74+
}
75+
if (normalized === "apply_patch" && matchesAny("exec", allow)) {
76+
return true;
77+
}
78+
return false;
79+
}
80+
81+
export function matchesList(name: string, list?: string[]) {
82+
if (!Array.isArray(list) || list.length === 0) {
83+
return false;
84+
}
85+
const normalized = normalizeToolPolicyName(name);
86+
const patterns = compilePatterns(list);
87+
if (matchesAny(normalized, patterns)) {
88+
return true;
89+
}
90+
if (normalized === "apply_patch" && matchesAny("exec", patterns)) {
91+
return true;
92+
}
93+
return false;
94+
}

0 commit comments

Comments
 (0)