Searches inside files with grep, skipping vendor and node_modules.
New file · app/Ai/Tools/SearchFiles.php
<?php
namespace App\Ai\Tools;
use Symfony\Component\Process\Process;
class SearchFiles implements Tool
{
public function definition(): array
{
return [
'type' => 'function',
'name' => 'search_files',
'description' => 'Search project files for a piece of text. Returns matching lines with file names and line numbers.',
'parameters' => [
'type' => 'object',
'properties' => [
'query' => ['type' => 'string', 'description' => 'The text to search for.'],
'directory' => ['type' => 'string', 'description' => 'Folder to search in, relative to the project root. Use "." for everything.'],
],
'required' => ['query', 'directory'],
'additionalProperties' => false,
],
'strict' => true,
];
}
public function use(array $arguments = []): string
{
$process = new Process(
['grep', '-rn', '--exclude-dir=vendor', '--exclude-dir=node_modules', '-F', $arguments['query'], $arguments['directory']],
base_path(),
timeout: 30,
);
$process->run();
return $process->getOutput() ?: 'No matches.';
}
}