mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-09 07:23:58 +00:00
50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
/**
|
|
* Shared tool-call name validation helpers.
|
|
* Keeps model-supplied tool names compact, normalized, and policy-checked
|
|
* before routing them to any tool execution surface.
|
|
*/
|
|
import { normalizeLowercaseStringOrEmpty } from "@openclaw/normalization-core/string-coerce";
|
|
|
|
const TOOL_CALL_NAME_MAX_CHARS = 64;
|
|
const TOOL_CALL_NAME_RE = /^[A-Za-z0-9_:.-]+$/;
|
|
|
|
/** Normalize an optional iterable of allowed tool names for lookup. */
|
|
export function normalizeAllowedToolNames(allowedToolNames?: Iterable<string>): Set<string> | null {
|
|
if (!allowedToolNames) {
|
|
return null;
|
|
}
|
|
const normalized = new Set<string>();
|
|
for (const name of allowedToolNames) {
|
|
if (typeof name !== "string") {
|
|
continue;
|
|
}
|
|
const trimmed = name.trim();
|
|
if (!trimmed) {
|
|
continue;
|
|
}
|
|
normalized.add(normalizeLowercaseStringOrEmpty(trimmed));
|
|
}
|
|
return normalized.size > 0 ? normalized : null;
|
|
}
|
|
|
|
/** Return whether a model-supplied tool call name is syntactically and policy allowed. */
|
|
export function isAllowedToolCallName(
|
|
name: unknown,
|
|
allowedToolNames: Set<string> | null,
|
|
): boolean {
|
|
if (typeof name !== "string") {
|
|
return false;
|
|
}
|
|
const trimmed = name.trim();
|
|
if (!trimmed) {
|
|
return false;
|
|
}
|
|
if (trimmed.length > TOOL_CALL_NAME_MAX_CHARS || !TOOL_CALL_NAME_RE.test(trimmed)) {
|
|
return false;
|
|
}
|
|
if (!allowedToolNames) {
|
|
return true;
|
|
}
|
|
return allowedToolNames.has(normalizeLowercaseStringOrEmpty(trimmed));
|
|
}
|