@@ -553,66 +553,46 @@ function createCachedDescriptorPluginTool(params: {
553553 loadOptions,
554554 onlyPluginIds : [ pluginId ] ,
555555 } ) ;
556- const candidates = registry ?. tools . filter (
557- ( candidate ) => candidate . pluginId === pluginId ,
558- ) ;
556+ const candidates = registry ?. tools . filter ( ( candidate ) => candidate . pluginId === pluginId ) ;
559557 if ( ! candidates || candidates . length === 0 ) {
560558 throw new Error ( `plugin tool runtime unavailable (${ pluginId } ): ${ toolName } ` ) ;
561559 }
562- // First try named candidates, then fall back to all candidates if no match
563- const namedCandidates = candidates . filter (
564- ( candidate ) => candidate . names . length > 0 ,
560+ const requestedToolName = normalizeToolName ( toolName ) ;
561+ const resolveCandidateTool = (
562+ candidate : PluginToolRegistration ,
563+ ) : AnyAgentTool | undefined => {
564+ const resolved = candidate . factory ( params . ctx ) ;
565+ const listRaw : unknown [ ] = Array . isArray ( resolved ) ? resolved : resolved ? [ resolved ] : [ ] ;
566+ for ( const toolRaw of listRaw ) {
567+ const malformedReason = describeMalformedPluginTool ( toolRaw ) ;
568+ if ( malformedReason ) {
569+ throw new Error ( `plugin tool is malformed (${ pluginId } ): ${ malformedReason } ` ) ;
570+ }
571+ const runtimeTool = toolRaw as AnyAgentTool ;
572+ if ( normalizeToolName ( runtimeTool . name ) === requestedToolName ) {
573+ return runtimeTool ;
574+ }
575+ }
576+ return undefined ;
577+ } ;
578+ const matchingNamedCandidates = candidates . filter (
579+ ( candidate ) =>
580+ candidate . names . length > 0 &&
581+ candidate . names . some ( ( name ) => normalizeToolName ( name ) === requestedToolName ) ,
565582 ) ;
566- // Try named candidates first
583+ const unnamedCandidates = candidates . filter ( ( candidate ) => candidate . names . length === 0 ) ;
567584 let matchedTool : AnyAgentTool | undefined ;
568- for ( const candidate of namedCandidates ) {
585+ for ( const candidate of [ ... matchingNamedCandidates , ... unnamedCandidates ] ) {
569586 try {
570- const resolved = candidate . factory ( params . ctx ) ;
571- const listRaw : unknown [ ] = Array . isArray ( resolved ) ? resolved : resolved ? [ resolved ] : [ ] ;
572- for ( const toolRaw of listRaw ) {
573- const malformedReason = describeMalformedPluginTool ( toolRaw ) ;
574- if ( malformedReason ) {
575- throw new Error ( `plugin tool is malformed (${ pluginId } ): ${ malformedReason } ` ) ;
576- }
577- const runtimeTool = toolRaw as AnyAgentTool ;
578- if ( normalizeToolName ( runtimeTool . name ) === normalizeToolName ( toolName ) ) {
579- matchedTool = runtimeTool ;
580- break ;
581- }
587+ matchedTool = resolveCandidateTool ( candidate ) ;
588+ if ( matchedTool ) {
589+ break ;
582590 }
583- if ( matchedTool ) { break ; }
584591 } catch {
585592 // Continue to next candidate if factory/materialization fails
586593 continue ;
587594 }
588595 }
589- // If no match in named candidates, try unnamed candidates only
590- if ( ! matchedTool ) {
591- const unnamedCandidates = candidates . filter (
592- ( candidate ) => candidate . names . length === 0 ,
593- ) ;
594- for ( const candidate of unnamedCandidates ) {
595- try {
596- const resolved = candidate . factory ( params . ctx ) ;
597- const listRaw : unknown [ ] = Array . isArray ( resolved ) ? resolved : resolved ? [ resolved ] : [ ] ;
598- for ( const toolRaw of listRaw ) {
599- const malformedReason = describeMalformedPluginTool ( toolRaw ) ;
600- if ( malformedReason ) {
601- throw new Error ( `plugin tool is malformed (${ pluginId } ): ${ malformedReason } ` ) ;
602- }
603- const runtimeTool = toolRaw as AnyAgentTool ;
604- if ( normalizeToolName ( runtimeTool . name ) === normalizeToolName ( toolName ) ) {
605- matchedTool = runtimeTool ;
606- break ;
607- }
608- }
609- if ( matchedTool ) { break ; }
610- } catch {
611- // Continue to next candidate if factory/materialization fails
612- continue ;
613- }
614- }
615- }
616596 if ( matchedTool ) {
617597 // Return outside try blocks to preserve tool execution errors
618598 return matchedTool . execute ( toolCallId , executeParams , signal , onUpdate ) ;
@@ -844,13 +824,13 @@ function resolvePluginToolLoadState(params: {
844824 env ?: NodeJS . ProcessEnv ;
845825} ) :
846826 | {
847- context : ReturnType < typeof resolvePluginRuntimeLoadContext > ;
848- env : NodeJS . ProcessEnv ;
849- loadOptions : PluginLoadOptions ;
850- onlyPluginIds : string [ ] ;
851- runtimeOptions : PluginLoadOptions [ "runtimeOptions" ] ;
852- snapshot : PluginMetadataManifestView ;
853- }
827+ context : ReturnType < typeof resolvePluginRuntimeLoadContext > ;
828+ env : NodeJS . ProcessEnv ;
829+ loadOptions : PluginLoadOptions ;
830+ onlyPluginIds : string [ ] ;
831+ runtimeOptions : PluginLoadOptions [ "runtimeOptions" ] ;
832+ snapshot : PluginMetadataManifestView ;
833+ }
854834 | undefined {
855835 const env = params . env ?? process . env ;
856836 const baseConfig = applyTestPluginDefaults ( params . context . config ?? { } , env ) ;
@@ -988,7 +968,8 @@ export function resolvePluginTools(params: {
988968 } ) ;
989969 } catch ( error ) {
990970 context . logger . error (
991- `failed to cold-load plugin tool registry for plugin ids [${ runtimePluginIds . join ( ", " ) } ]: ${ error instanceof Error ? error . message : String ( error )
971+ `failed to cold-load plugin tool registry for plugin ids [${ runtimePluginIds . join ( ", " ) } ]: ${
972+ error instanceof Error ? error . message : String ( error )
992973 } `,
993974 ) ;
994975 throw error ;
@@ -1057,19 +1038,19 @@ export function resolvePluginTools(params: {
10571038 declaredNames . length > 0 ? declaredNames : ( entry . declaredNames ?? [ ] ) ;
10581039 const allowlistNames = manifestPlugin
10591040 ? filterManifestToolNamesForAvailability ( {
1060- plugin : manifestPlugin ,
1061- toolNames : availabilityNames ,
1062- config : params . context . runtimeConfig ?? context . config ,
1063- env,
1064- hasAuthForProvider : params . hasAuthForProvider ,
1065- } ) . filter (
1066- ( toolName ) =>
1067- ! denylistBlocksPluginTool ( {
1068- pluginId : entry . pluginId ,
1069- toolName,
1070- denylist,
1071- } ) ,
1072- )
1041+ plugin : manifestPlugin ,
1042+ toolNames : availabilityNames ,
1043+ config : params . context . runtimeConfig ?? context . config ,
1044+ env,
1045+ hasAuthForProvider : params . hasAuthForProvider ,
1046+ } ) . filter (
1047+ ( toolName ) =>
1048+ ! denylistBlocksPluginTool ( {
1049+ pluginId : entry . pluginId ,
1050+ toolName,
1051+ denylist,
1052+ } ) ,
1053+ )
10731054 : declaredNames ;
10741055 if ( manifestPlugin && availabilityNames . length > 0 && allowlistNames . length === 0 ) {
10751056 continue ;
@@ -1115,33 +1096,33 @@ export function resolvePluginTools(params: {
11151096 : undefined ;
11161097 const availableList = manifestPlugin
11171098 ? listRaw . filter ( ( tool ) => {
1118- const toolName = readPluginToolName ( tool ) ;
1119- const normalizedToolName = normalizeToolName ( toolName ) ;
1120- if (
1121- isManifestToolOptional ( manifestPlugin , toolName ) &&
1122- ! isOptionalToolAllowed ( {
1099+ const toolName = readPluginToolName ( tool ) ;
1100+ const normalizedToolName = normalizeToolName ( toolName ) ;
1101+ if (
1102+ isManifestToolOptional ( manifestPlugin , toolName ) &&
1103+ ! isOptionalToolAllowed ( {
1104+ toolName,
1105+ pluginId : entry . pluginId ,
1106+ allowlist,
1107+ } )
1108+ ) {
1109+ return false ;
1110+ }
1111+ if (
1112+ selectedManifestToolNames &&
1113+ manifestContractToolNames ?. has ( normalizedToolName ) &&
1114+ ! selectedManifestToolNames . has ( normalizedToolName )
1115+ ) {
1116+ return false ;
1117+ }
1118+ return isManifestToolNameAvailable ( {
1119+ plugin : manifestPlugin ,
11231120 toolName,
1124- pluginId : entry . pluginId ,
1125- allowlist,
1126- } )
1127- ) {
1128- return false ;
1129- }
1130- if (
1131- selectedManifestToolNames &&
1132- manifestContractToolNames ?. has ( normalizedToolName ) &&
1133- ! selectedManifestToolNames . has ( normalizedToolName )
1134- ) {
1135- return false ;
1136- }
1137- return isManifestToolNameAvailable ( {
1138- plugin : manifestPlugin ,
1139- toolName,
1140- config : params . context . runtimeConfig ?? context . config ,
1141- env,
1142- hasAuthForProvider : params . hasAuthForProvider ,
1143- } ) ;
1144- } )
1121+ config : params . context . runtimeConfig ?? context . config ,
1122+ env,
1123+ hasAuthForProvider : params . hasAuthForProvider ,
1124+ } ) ;
1125+ } )
11451126 : listRaw ;
11461127 const policyAvailableList = availableList . filter (
11471128 ( tool ) =>
@@ -1153,12 +1134,12 @@ export function resolvePluginTools(params: {
11531134 ) ;
11541135 const list = entry . optional
11551136 ? policyAvailableList . filter ( ( tool ) =>
1156- isOptionalToolAllowed ( {
1157- toolName : readPluginToolName ( tool ) ,
1158- pluginId : entry . pluginId ,
1159- allowlist,
1160- } ) ,
1161- )
1137+ isOptionalToolAllowed ( {
1138+ toolName : readPluginToolName ( tool ) ,
1139+ pluginId : entry . pluginId ,
1140+ allowlist,
1141+ } ) ,
1142+ )
11621143 : policyAvailableList ;
11631144 if ( list . length === 0 ) {
11641145 continue ;
@@ -1182,9 +1163,9 @@ export function resolvePluginTools(params: {
11821163 const tool = toolRaw as AnyAgentTool ;
11831164 const undeclared = entry . declaredNames
11841165 ? findUndeclaredPluginToolNames ( {
1185- declaredNames : entry . declaredNames ,
1186- toolNames : [ tool . name ] ,
1187- } )
1166+ declaredNames : entry . declaredNames ,
1167+ toolNames : [ tool . name ] ,
1168+ } )
11881169 : [ ] ;
11891170 if ( undeclared . length > 0 ) {
11901171 const message = `plugin tool is undeclared (${ entry . pluginId } ): ${ undeclared . join ( ", " ) } ` ;
@@ -1287,8 +1268,3 @@ export function resolvePluginTools(params: {
12871268
12881269 return tools ;
12891270}
1290-
1291-
1292-
1293-
1294-
0 commit comments