mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 20:18:28 +00:00
refactor(tools): dedupe alsoAllow merge
This commit is contained in:
@@ -52,6 +52,7 @@ import {
|
|||||||
import {
|
import {
|
||||||
applyOwnerOnlyToolPolicy,
|
applyOwnerOnlyToolPolicy,
|
||||||
collectExplicitAllowlist,
|
collectExplicitAllowlist,
|
||||||
|
mergeAlsoAllowPolicy,
|
||||||
resolveToolProfilePolicy,
|
resolveToolProfilePolicy,
|
||||||
} from "./tool-policy.js";
|
} from "./tool-policy.js";
|
||||||
import { resolveWorkspaceRoot } from "./workspace-dir.js";
|
import { resolveWorkspaceRoot } from "./workspace-dir.js";
|
||||||
@@ -219,15 +220,8 @@ export function createOpenClawCodingTools(options?: {
|
|||||||
const profilePolicy = resolveToolProfilePolicy(profile);
|
const profilePolicy = resolveToolProfilePolicy(profile);
|
||||||
const providerProfilePolicy = resolveToolProfilePolicy(providerProfile);
|
const providerProfilePolicy = resolveToolProfilePolicy(providerProfile);
|
||||||
|
|
||||||
const mergeAlsoAllow = (policy: typeof profilePolicy, alsoAllow?: string[]) => {
|
const profilePolicyWithAlsoAllow = mergeAlsoAllowPolicy(profilePolicy, profileAlsoAllow);
|
||||||
if (!policy?.allow || !Array.isArray(alsoAllow) || alsoAllow.length === 0) {
|
const providerProfilePolicyWithAlsoAllow = mergeAlsoAllowPolicy(
|
||||||
return policy;
|
|
||||||
}
|
|
||||||
return { ...policy, allow: Array.from(new Set([...policy.allow, ...alsoAllow])) };
|
|
||||||
};
|
|
||||||
|
|
||||||
const profilePolicyWithAlsoAllow = mergeAlsoAllow(profilePolicy, profileAlsoAllow);
|
|
||||||
const providerProfilePolicyWithAlsoAllow = mergeAlsoAllow(
|
|
||||||
providerProfilePolicy,
|
providerProfilePolicy,
|
||||||
providerProfileAlsoAllow,
|
providerProfileAlsoAllow,
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -291,3 +291,13 @@ export function resolveToolProfilePolicy(profile?: string): ToolProfilePolicy |
|
|||||||
deny: resolved.deny ? [...resolved.deny] : undefined,
|
deny: resolved.deny ? [...resolved.deny] : undefined,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function mergeAlsoAllowPolicy<TPolicy extends { allow?: string[] }>(
|
||||||
|
policy: TPolicy | undefined,
|
||||||
|
alsoAllow?: string[],
|
||||||
|
): TPolicy | undefined {
|
||||||
|
if (!policy?.allow || !Array.isArray(alsoAllow) || alsoAllow.length === 0) {
|
||||||
|
return policy;
|
||||||
|
}
|
||||||
|
return { ...policy, allow: Array.from(new Set([...policy.allow, ...alsoAllow])) };
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,7 +10,11 @@ import {
|
|||||||
applyToolPolicyPipeline,
|
applyToolPolicyPipeline,
|
||||||
buildDefaultToolPolicyPipelineSteps,
|
buildDefaultToolPolicyPipelineSteps,
|
||||||
} from "../agents/tool-policy-pipeline.js";
|
} from "../agents/tool-policy-pipeline.js";
|
||||||
import { collectExplicitAllowlist, resolveToolProfilePolicy } from "../agents/tool-policy.js";
|
import {
|
||||||
|
collectExplicitAllowlist,
|
||||||
|
mergeAlsoAllowPolicy,
|
||||||
|
resolveToolProfilePolicy,
|
||||||
|
} from "../agents/tool-policy.js";
|
||||||
import { ToolInputError } from "../agents/tools/common.js";
|
import { ToolInputError } from "../agents/tools/common.js";
|
||||||
import { loadConfig } from "../config/config.js";
|
import { loadConfig } from "../config/config.js";
|
||||||
import { resolveMainSessionKey } from "../config/sessions.js";
|
import { resolveMainSessionKey } from "../config/sessions.js";
|
||||||
@@ -215,15 +219,8 @@ export async function handleToolsInvokeHttpRequest(
|
|||||||
const profilePolicy = resolveToolProfilePolicy(profile);
|
const profilePolicy = resolveToolProfilePolicy(profile);
|
||||||
const providerProfilePolicy = resolveToolProfilePolicy(providerProfile);
|
const providerProfilePolicy = resolveToolProfilePolicy(providerProfile);
|
||||||
|
|
||||||
const mergeAlsoAllow = (policy: typeof profilePolicy, alsoAllow?: string[]) => {
|
const profilePolicyWithAlsoAllow = mergeAlsoAllowPolicy(profilePolicy, profileAlsoAllow);
|
||||||
if (!policy?.allow || !Array.isArray(alsoAllow) || alsoAllow.length === 0) {
|
const providerProfilePolicyWithAlsoAllow = mergeAlsoAllowPolicy(
|
||||||
return policy;
|
|
||||||
}
|
|
||||||
return { ...policy, allow: Array.from(new Set([...policy.allow, ...alsoAllow])) };
|
|
||||||
};
|
|
||||||
|
|
||||||
const profilePolicyWithAlsoAllow = mergeAlsoAllow(profilePolicy, profileAlsoAllow);
|
|
||||||
const providerProfilePolicyWithAlsoAllow = mergeAlsoAllow(
|
|
||||||
providerProfilePolicy,
|
providerProfilePolicy,
|
||||||
providerProfileAlsoAllow,
|
providerProfileAlsoAllow,
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user