@@ -27,6 +27,23 @@ for (const [alias, canonical] of Object.entries(TOOL_ALIASES)) {
2727 CANONICAL_TO_ALIASES . set ( canonical , existing )
2828}
2929
30+ /**
31+ * Pre-computed alias groups map - maps any tool name (canonical or alias) to its full group.
32+ * Built once at module load for O(1) lookup.
33+ */
34+ const ALIAS_GROUPS : Map < string , readonly string [ ] > = new Map ( )
35+
36+ // Build alias groups for all tools
37+ for ( const [ canonical , aliases ] of CANONICAL_TO_ALIASES . entries ( ) ) {
38+ const group = Object . freeze ( [ canonical , ...aliases ] )
39+ // Map canonical to group
40+ ALIAS_GROUPS . set ( canonical , group )
41+ // Map each alias to the same group
42+ for ( const alias of aliases ) {
43+ ALIAS_GROUPS . set ( alias , group )
44+ }
45+ }
46+
3047/**
3148 * Resolves a tool name to its canonical name.
3249 * If the tool name is an alias, returns the canonical tool name.
@@ -60,21 +77,13 @@ export function applyToolAliases(allowedTools: Set<string>): Set<string> {
6077
6178/**
6279 * Gets all tools in an alias group (including the canonical tool).
80+ * Uses pre-computed ALIAS_GROUPS map for O(1) lookup.
6381 *
6482 * @param toolName - Any tool name in the alias group
6583 * @returns Array of all tool names in the alias group, or just the tool if not aliased
6684 */
67- export function getToolAliasGroup ( toolName : string ) : string [ ] {
68- // Check if it's a canonical tool with aliases
69- if ( CANONICAL_TO_ALIASES . has ( toolName ) ) {
70- return [ toolName , ...CANONICAL_TO_ALIASES . get ( toolName ) ! ]
71- }
72- // Check if it's an alias
73- const canonical = ALIAS_TO_CANONICAL . get ( toolName )
74- if ( canonical ) {
75- return [ canonical , ...CANONICAL_TO_ALIASES . get ( canonical ) ! ]
76- }
77- return [ toolName ]
85+ export function getToolAliasGroup ( toolName : string ) : readonly string [ ] {
86+ return ALIAS_GROUPS . get ( toolName ) ?? [ toolName ]
7887}
7988
8089/**
@@ -218,9 +227,6 @@ export function filterNativeToolsForMode(
218227 )
219228 allowedToolNames = customizedTools
220229
221- // Resolve any aliases to canonical tool names for execution-time filtering
222- allowedToolNames = applyToolAliases ( allowedToolNames )
223-
224230 // Register alias renames for tools that are allowed and explicitly requested as aliases
225231 if ( modelInfo ?. includedTools && modelInfo . includedTools . length > 0 ) {
226232 for ( const includedTool of modelInfo . includedTools ) {
@@ -357,17 +363,15 @@ export function isToolAllowedInMode(
357363 }
358364
359365 // Check if the tool is allowed by the mode's groups
360- // Also check if any tool in the alias group is allowed
361- const aliasGroup = getToolAliasGroup ( toolName )
362- return aliasGroup . some ( ( aliasedTool ) =>
363- isToolAllowedForMode (
364- aliasedTool as ToolName ,
365- modeSlug ,
366- customModes ?? [ ] ,
367- undefined ,
368- undefined ,
369- experiments ?? { } ,
370- ) ,
366+ // Resolve to canonical name and check that single value
367+ const canonicalTool = resolveToolAlias ( toolName )
368+ return isToolAllowedForMode (
369+ canonicalTool as ToolName ,
370+ modeSlug ,
371+ customModes ?? [ ] ,
372+ undefined ,
373+ undefined ,
374+ experiments ?? { } ,
371375 )
372376}
373377
0 commit comments