refactor: improve sanitization and formatting in buildFunctionCallToo… (#6152)

refactor: improve sanitization and formatting in buildFunctionCallToolName function
This commit is contained in:
LiuVaayne 2025-05-19 11:20:38 +08:00 committed by GitHub
parent eec83da19c
commit 2acebf1353

View File

@ -1,8 +1,11 @@
export function buildFunctionCallToolName(serverName: string, toolName: string) { export function buildFunctionCallToolName(serverName: string, toolName: string) {
const sanitizedServer = serverName.trim().replace(/-/g, '_')
const sanitizedTool = toolName.trim().replace(/-/g, '_')
// Combine server name and tool name // Combine server name and tool name
let name = toolName let name = sanitizedTool
if (!toolName.includes(serverName.slice(0, 7))) { if (!sanitizedTool.includes(sanitizedServer.slice(0, 7))) {
name = `${serverName.slice(0, 7) || ''}_${toolName || ''}` name = `${sanitizedServer.slice(0, 7) || ''}-${sanitizedTool || ''}`
} }
// Replace invalid characters with underscores or dashes // Replace invalid characters with underscores or dashes
@ -11,7 +14,7 @@ export function buildFunctionCallToolName(serverName: string, toolName: string)
// Ensure name starts with a letter or underscore (for valid JavaScript identifier) // Ensure name starts with a letter or underscore (for valid JavaScript identifier)
if (!/^[a-zA-Z]/.test(name)) { if (!/^[a-zA-Z]/.test(name)) {
name = `tool_${name}` name = `tool-${name}`
} }
// Remove consecutive underscores/dashes (optional improvement) // Remove consecutive underscores/dashes (optional improvement)