11using System . CommandLine ;
2+ using System . ComponentModel ;
23using System . Reflection ;
34using Microsoft . Extensions . AI ;
45using ModelContextProtocol . Client ;
@@ -97,9 +98,63 @@ private static async Task HandleAsync(string input, string inputPath, string out
9798 inputText ,
9899 new ChatOptions
99100 {
100- Tools = [ .. aiTools . SelectMany ( x => x ) . ToArray ( ) ] ,
101+ Tools = [
102+ .. aiTools . SelectMany ( x => x ) . ToArray ( ) ,
103+ .. tools . Contains ( "filesystem" )
104+ ? new [ ] { AIFunctionFactory . Create (
105+ FindFilePathsByContent ,
106+ name : "FindFilePathsByContent" ,
107+ description : "Finds file paths by content." ) }
108+ : [ ] ,
109+ ] ,
101110 } ) . ConfigureAwait ( false ) ;
102111
103112 await Helpers . WriteOutputAsync ( response . Text , outputPath ) . ConfigureAwait ( false ) ;
113+
114+ return ;
115+
116+ [ Description ( "Finds file paths by content." ) ]
117+ static async Task < IList < string > > FindFilePathsByContent (
118+ [ Description ( "The directory in which the search will be performed. Includes all subdirectories" ) ] string directory ,
119+ [ Description ( "The content to search for in the files. Ignores case." ) ] string content )
120+ {
121+ var paths = new List < string > ( ) ;
122+
123+ Console . WriteLine ( $ "Searching for files in \" { directory } \" containing \" { content } \" ...") ;
124+
125+ foreach ( var path in Directory . EnumerateFiles ( directory , "*.*" , SearchOption . AllDirectories ) )
126+ {
127+ try
128+ {
129+ var extension = Path . GetExtension ( path ) ;
130+ if ( extension is not ".txt" and not ".md" and not ".json" and not ".cs" and not ".csproj" and not ".sln" and not ".sh" and not ".yml" and not ".yaml" )
131+ {
132+ continue ;
133+ }
134+
135+ //FileInfo info = new FileInfo(path);
136+ var text = await File . ReadAllTextAsync ( path ) . ConfigureAwait ( false ) ;
137+
138+ if ( text . Contains ( content , StringComparison . OrdinalIgnoreCase ) )
139+ {
140+ paths . Add ( path ) ;
141+ }
142+ }
143+ #pragma warning disable CA1031
144+ catch ( Exception )
145+ #pragma warning restore CA1031
146+ {
147+ // ignore
148+ }
149+ }
150+
151+ Console . WriteLine ( $ "Found { paths . Count } files:") ;
152+ foreach ( var path in paths )
153+ {
154+ Console . WriteLine ( path ) ;
155+ }
156+
157+ return paths ;
158+ }
104159 }
105160}
0 commit comments