support context for ripgrep

This commit is contained in:
AI Christianson 2025-02-27 10:04:59 -05:00
parent a8030104fc
commit 6d2b0a148d
2 changed files with 18 additions and 6 deletions

View File

@ -169,6 +169,7 @@ You must:
Do so by incrementally and systematically exploring the filesystem with careful directory listing tool calls.
You can use fuzzy file search to quickly find relevant files matching a search pattern.
Use ripgrep_search extensively to do *exhaustive* searches for all references to anything that might be changed as part of the base level task.
Prefer to use ripgrep_search with context params rather than reading whole files in order to preserve context tokens.
You must not:

View File

@ -71,6 +71,8 @@ FILE_TYPE_MAP = {
def ripgrep_search(
pattern: str,
*,
before_context_lines: int = None,
after_context_lines: int = None,
file_type: str = None,
case_sensitive: bool = True,
include_hidden: bool = False,
@ -79,23 +81,27 @@ def ripgrep_search(
) -> Dict[str, Union[str, int, bool]]:
"""Execute a ripgrep (rg) search with formatting and common options.
Prefer to use this with after_context_lines and/or before_context_lines over reading entire file contents, to conserve tokens and resources.
Args:
pattern: Search pattern to find
before_context_lines: Number of lines to show before each match (default: None)
after_context_lines: Number of lines to show after each match (default: None)
file_type: Optional file type to filter results (e.g. 'py' for Python files)
case_sensitive: Whether to do case-sensitive search (default: True)
include_hidden: Whether to search hidden files and directories (default: False)
follow_links: Whether to follow symbolic links (default: False)
exclude_dirs: Additional directories to exclude (combines with defaults)
Returns:
Dict containing:
- output: The formatted search results
- return_code: Process return code (0 means success)
- success: Boolean indicating if search succeeded
"""
# Build rg command with options
cmd = ["rg", "--color", "always"]
if before_context_lines is not None:
cmd.extend(["-B", str(before_context_lines)])
if after_context_lines is not None:
cmd.extend(["-A", str(after_context_lines)])
if not case_sensitive:
cmd.append("-i")
@ -128,6 +134,11 @@ def ripgrep_search(
f"**Case Sensitive**: {case_sensitive}",
f"**File Type**: {file_type or 'all'}",
]
if before_context_lines is not None:
params.append(f"**Before Context Lines**: {before_context_lines}")
if after_context_lines is not None:
params.append(f"**After Context Lines**: {after_context_lines}")
if include_hidden:
params.append("**Including Hidden Files**: yes")
if follow_links: