mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-07 05:40:17 +00:00
Make regex search tool results look more like grep results
This commit is contained in:
@@ -3023,6 +3023,7 @@ async def grep_files(
|
||||
file_matches = await FileObjectAdapters.aget_file_objects_by_regex(user, db_pattern, path_prefix)
|
||||
|
||||
line_matches = []
|
||||
line_matches_count = 0
|
||||
for file_object in file_matches:
|
||||
lines = file_object.raw_text.split("\n")
|
||||
matched_line_numbers = []
|
||||
@@ -3031,6 +3032,7 @@ async def grep_files(
|
||||
for i, line in enumerate(lines, 1):
|
||||
if regex.search(line):
|
||||
matched_line_numbers.append(i)
|
||||
line_matches_count += len(matched_line_numbers)
|
||||
|
||||
# Build context for each match
|
||||
for line_num in matched_line_numbers:
|
||||
@@ -3047,10 +3049,10 @@ async def grep_files(
|
||||
|
||||
if current_line_num == line_num:
|
||||
# This is the matching line, mark it
|
||||
context_lines.append(f"{file_object.file_name}:{current_line_num}:> {line_content}")
|
||||
context_lines.append(f"{file_object.file_name}:{current_line_num}: {line_content}")
|
||||
else:
|
||||
# This is a context line
|
||||
context_lines.append(f"{file_object.file_name}:{current_line_num}: {line_content}")
|
||||
context_lines.append(f"{file_object.file_name}-{current_line_num}- {line_content}")
|
||||
|
||||
# Add separator between matches if showing context
|
||||
if lines_before > 0 or lines_after > 0:
|
||||
@@ -3065,7 +3067,7 @@ async def grep_files(
|
||||
# Check if no results found
|
||||
max_results = 1000
|
||||
query = _generate_query(
|
||||
len([m for m in line_matches if ":>" in m]),
|
||||
line_matches_count,
|
||||
len(file_matches),
|
||||
path_prefix,
|
||||
regex_pattern,
|
||||
|
||||
@@ -613,9 +613,12 @@ tools_for_research_llm = {
|
||||
Helpful to answer questions for which all relevant notes or documents are needed to complete the search. Example: "Notes that mention Tom".
|
||||
You need to know all the correct keywords or regex patterns for this tool to be useful.
|
||||
|
||||
REMEMBER:
|
||||
IMPORTANT:
|
||||
- The regex pattern will ONLY match content on a single line. Multi-line matches are NOT supported (even if you use \\n).
|
||||
|
||||
TIPS:
|
||||
- The output follows a grep-like format. Matches are prefixed with the file path and line number. Useful to combine with viewing file around specific line numbers.
|
||||
|
||||
An optional path prefix can restrict search to specific files/directories.
|
||||
Use lines_before, lines_after to show context around matches.
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user