mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 09:51:22 +00:00
refactor: centralize message-provider tool filtering
This commit is contained in:
@@ -67,6 +67,31 @@ function isOpenAIProvider(provider?: string) {
|
||||
return normalized === "openai" || normalized === "openai-codex";
|
||||
}
|
||||
|
||||
const TOOL_DENY_BY_MESSAGE_PROVIDER: Readonly<Record<string, readonly string[]>> = {
|
||||
voice: ["tts"],
|
||||
};
|
||||
|
||||
function normalizeMessageProvider(messageProvider?: string): string | undefined {
|
||||
const normalized = messageProvider?.trim().toLowerCase();
|
||||
return normalized && normalized.length > 0 ? normalized : undefined;
|
||||
}
|
||||
|
||||
function applyMessageProviderToolPolicy(
|
||||
tools: AnyAgentTool[],
|
||||
messageProvider?: string,
|
||||
): AnyAgentTool[] {
|
||||
const normalizedProvider = normalizeMessageProvider(messageProvider);
|
||||
if (!normalizedProvider) {
|
||||
return tools;
|
||||
}
|
||||
const deniedTools = TOOL_DENY_BY_MESSAGE_PROVIDER[normalizedProvider];
|
||||
if (!deniedTools || deniedTools.length === 0) {
|
||||
return tools;
|
||||
}
|
||||
const deniedSet = new Set(deniedTools);
|
||||
return tools.filter((tool) => !deniedSet.has(tool.name));
|
||||
}
|
||||
|
||||
function isApplyPatchAllowedForModel(params: {
|
||||
modelProvider?: string;
|
||||
modelId?: string;
|
||||
@@ -217,8 +242,6 @@ export function createOpenClawCodingTools(options?: {
|
||||
/** Whether the sender is an owner (required for owner-only tools). */
|
||||
senderIsOwner?: boolean;
|
||||
}): AnyAgentTool[] {
|
||||
const rawMessageProvider = options?.messageProvider?.trim().toLowerCase();
|
||||
const isVoiceMessageProvider = rawMessageProvider === "voice";
|
||||
const execToolName = "exec";
|
||||
const sandbox = options?.sandbox?.enabled ? options.sandbox : undefined;
|
||||
const {
|
||||
@@ -482,9 +505,7 @@ export function createOpenClawCodingTools(options?: {
|
||||
senderIsOwner: options?.senderIsOwner,
|
||||
}),
|
||||
];
|
||||
const toolsForMessageProvider = isVoiceMessageProvider
|
||||
? tools.filter((tool) => tool.name !== "tts")
|
||||
: tools;
|
||||
const toolsForMessageProvider = applyMessageProviderToolPolicy(tools, options?.messageProvider);
|
||||
// Security: treat unknown/undefined as unauthorized (opt-in, not opt-out)
|
||||
const senderIsOwner = options?.senderIsOwner === true;
|
||||
const toolsByAuthorization = applyOwnerOnlyToolPolicy(toolsForMessageProvider, senderIsOwner);
|
||||
|
||||
Reference in New Issue
Block a user