Skip to content

Commit 588f256

Browse files
committed
feat: Added FindFilePathsByContent tool.
1 parent d753da2 commit 588f256

File tree

2 files changed

+56
-64
lines changed

2 files changed

+56
-64
lines changed

src/Cli/src/Commands/DoCommand.cs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.CommandLine;
2+
using System.ComponentModel;
23
using System.Reflection;
34
using Microsoft.Extensions.AI;
45
using 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
}

src/Cli/src/Commands/FileSystemService.cs

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)