@@ -35,7 +35,7 @@ import {
3535 resolveSkillTelemetrySource ,
3636 resolveSkillTelemetrySourceValue ,
3737} from "../skills/loading/source.js" ;
38- import type { SkillSnapshot , SkillTelemetrySource , SkillUsagePath } from "../skills/types.js" ;
38+ import type { SkillSnapshot , SkillTelemetrySource } from "../skills/types.js" ;
3939import { isPlainObject , truncateUtf16Safe } from "../utils.js" ;
4040import { buildAdjustedParamsKey } from "./agent-tools.before-tool-call.state.js" ;
4141import type {
@@ -334,51 +334,31 @@ function resolveRelativeToolPath(candidate: string, ctx?: HookContext): string |
334334 return base ? path . resolve ( base , trimmed ) : undefined ;
335335}
336336
337- function readToolPathCandidates ( params : unknown , ctx ?: HookContext ) : string [ ] {
338- if ( ! isPlainObject ( params ) ) {
339- return [ ] ;
340- }
341- const candidates = typeof params . path === "string" ? [ params . path ] : [ ] ;
342- return candidates
343- . map ( ( candidate ) => resolveRelativeToolPath ( normalizeFileToolPathParam ( candidate ) , ctx ) )
344- . filter ( ( candidate ) : candidate is string => Boolean ( candidate ) ) ;
337+ function readToolPathCandidate ( params : unknown , ctx ?: HookContext ) : string | undefined {
338+ return isPlainObject ( params ) && typeof params . path === "string"
339+ ? resolveRelativeToolPath ( normalizeFileToolPathParam ( params . path ) , ctx )
340+ : undefined ;
345341}
346342
347- function skillInstructionPaths ( snapshot : SkillSnapshot | undefined ) : Map < string , SkillUsageMatch > {
348- const matches = new Map < string , SkillUsageMatch > ( ) ;
349- for ( const skill of snapshot ?. resolvedSkills ?? [ ] ) {
350- const skillName = typeof skill . name === "string" ? skill . name . trim ( ) : "" ;
351- if ( ! skillName ) {
352- continue ;
353- }
354- const match = resolvedSkillUsageMatch ( { activation : "read" , skill } ) ;
355- const filePath = typeof skill . filePath === "string" ? skill . filePath . trim ( ) : "" ;
356- if ( filePath ) {
357- if ( filePath . startsWith ( "node://" ) ) {
358- matches . set ( filePath , match ) ;
359- } else if ( path . isAbsolute ( filePath ) ) {
360- matches . set ( path . resolve ( filePath ) , match ) ;
361- }
343+ function findSkillInstructionMatch (
344+ snapshot : SkillSnapshot ,
345+ candidate : string ,
346+ ) : SkillUsageMatch | undefined {
347+ const skill = snapshot . resolvedSkills ?. findLast ( ( entry ) => {
348+ if ( typeof entry . name !== "string" || ! entry . name . trim ( ) ) {
349+ return false ;
362350 }
363- const baseDir = typeof skill . baseDir === "string" ? skill . baseDir . trim ( ) : "" ;
364- if ( baseDir && path . isAbsolute ( baseDir ) ) {
365- matches . set ( path . resolve ( baseDir , "SKILL.md" ) , match ) ;
366- }
367- }
368- return matches ;
369- }
370-
371- function materializedSkillInstructionPaths ( paths : SkillUsagePath [ ] | undefined ) {
372- const matches = new Map < string , SkillUsageMatch > ( ) ;
373- for ( const entry of paths ?? [ ] ) {
374- matches . set ( path . resolve ( entry . readPath ) , {
375- skillFile : entry . skillFile ,
376- skillName : entry . skillName ,
377- skillSource : entry . skillSource ,
378- activation : "read" ,
379- } ) ;
380- }
381- return matches ;
351+ const filePath = typeof entry . filePath === "string" ? entry . filePath . trim ( ) : "" ;
352+ const baseDir = typeof entry . baseDir === "string" ? entry . baseDir . trim ( ) : "" ;
353+ return (
354+ ( filePath &&
355+ ( filePath . startsWith ( "node://" )
356+ ? filePath === candidate
357+ : path . isAbsolute ( filePath ) && path . resolve ( filePath ) === candidate ) ) ||
358+ ( baseDir && path . isAbsolute ( baseDir ) && path . resolve ( baseDir , "SKILL.md" ) === candidate )
359+ ) ;
360+ } ) ;
361+ return skill ? resolvedSkillUsageMatch ( { activation : "read" , skill } ) : undefined ;
382362}
383363
384364export function findSkillUsageMatch ( params : {
@@ -410,16 +390,24 @@ export function findSkillUsageMatch(params: {
410390 if ( params . toolName !== "read" ) {
411391 return undefined ;
412392 }
413- const skillPaths = params . ctx ?. skillsSnapshot ?. resolvedSkills ?. length
414- ? skillInstructionPaths ( params . ctx . skillsSnapshot )
415- : materializedSkillInstructionPaths ( params . ctx ?. skillUsagePaths ) ;
416- for ( const candidate of readToolPathCandidates ( params . toolParams , params . ctx ) ) {
417- const match = skillPaths . get ( candidate ) ;
418- if ( match ) {
419- return match ;
420- }
393+ const candidate = readToolPathCandidate ( params . toolParams , params . ctx ) ;
394+ if ( ! candidate ) {
395+ return undefined ;
421396 }
422- return undefined ;
397+ if ( params . ctx ?. skillsSnapshot ?. resolvedSkills ?. length ) {
398+ return findSkillInstructionMatch ( params . ctx . skillsSnapshot , candidate ) ;
399+ }
400+ const match = params . ctx ?. skillUsagePaths ?. findLast (
401+ ( entry ) => path . resolve ( entry . readPath ) === candidate ,
402+ ) ;
403+ return match
404+ ? {
405+ skillFile : match . skillFile ,
406+ skillName : match . skillName ,
407+ skillSource : match . skillSource ,
408+ activation : "read" ,
409+ }
410+ : undefined ;
423411}
424412
425413export function emitSkillUsedDiagnostic ( params : {
0 commit comments