1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# frozen_string_literal: true
module Dispatch
module Tool
module Files
SEARCH_FILES = Dispatch::Tools::Definition.new(
name: "search_files",
description: "Search for text in files. Returns matching lines with file paths and line numbers.",
parameters: {
type: "object",
properties: {
query: {
type: "string",
description: "The text or regex pattern to search for."
},
path: {
type: "string",
description: "Directory to search within, relative to the worktree root. Defaults to root."
},
pattern: {
type: "string",
description: "Glob pattern to filter which files to search (e.g. '**/*.rb')."
},
is_regex: {
type: "boolean",
description: "Whether the query is a regular expression. Defaults to false."
}
},
required: %w[query],
additionalProperties: false
}
) do |params, context|
worktree_path = context[:worktree_path]
query = params[:query]
path = params.fetch(:path, ".")
file_pattern = params[:pattern]
is_regex = params.fetch(:is_regex, false)
resolved = begin
Sandbox.resolve_path(path, worktree_path:)
rescue SandboxError => e
next Dispatch::Tools::Result.failure(error: e.message)
end
regex = if is_regex
begin
Regexp.new(query)
rescue RegexpError => e
next Dispatch::Tools::Result.failure(error: "Invalid regex: #{e.message}")
end
else
Regexp.new(Regexp.escape(query))
end
# Build file list
glob = if file_pattern
File.join(resolved, file_pattern)
else
File.join(resolved, "**", "*")
end
files = Dir.glob(glob).select { |f| File.file?(f) }.sort
worktree_real = File.realpath(worktree_path)
max_results = 100
matches = []
files.each do |file_path|
# Skip binary files
sample = File.binread(file_path, 8192)
next if sample&.include?("\x00")
relative = file_path.delete_prefix("#{worktree_real}/")
File.readlines(file_path).each_with_index do |line, idx|
if regex.match?(line)
matches << "#{relative}:#{idx + 1}: #{line.chomp}"
break if matches.length >= max_results
end
end
break if matches.length >= max_results
end
Dispatch::Tools::Result.success(output: matches.join("\n"))
end
end
end
end
|