Skip to content

Commit 8d96039

Browse files
refactor: simplify grep tool error handling and messaging
- Remove group_id generation and direct emit_error/emit_warning calls from grep function - Consolidate error handling to collect error messages in a single variable - Move UI message emission to the end of the function to ensure single emission point - Add error field to GrepOutput model to return error information to caller - Remove unused imports for emit_error, emit_warning, and generate_group_id - Restructure exception handling to set error_message instead of immediate emission - Clean up the order of operations to prepare data first, then emit messages consistently
1 parent a7519b0 commit 8d96039

1 file changed

Lines changed: 33 additions & 45 deletions

File tree

code_puppy/tools/file_operations.py

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@
1818
FileListingMessage,
1919
GrepMatch,
2020
GrepResultMessage,
21-
emit_error,
22-
emit_warning,
2321
get_message_bus,
2422
)
25-
from code_puppy.tools.common import generate_group_id
2623

2724

2825
# Pydantic models for tool return types
@@ -53,6 +50,7 @@ class MatchInfo(BaseModel):
5350

5451
class GrepOutput(BaseModel):
5552
matches: List[MatchInfo]
53+
error: str | None = None
5654

5755

5856
def is_likely_home_directory(directory):
@@ -582,9 +580,7 @@ def _grep(context: RunContext, search_string: str, directory: str = ".") -> Grep
582580

583581
directory = os.path.abspath(os.path.expanduser(directory))
584582
matches: List[MatchInfo] = []
585-
586-
# Generate group_id for this tool execution
587-
group_id = generate_group_id("grep", f"{directory}_{search_string}")
583+
error_message: str | None = None
588584

589585
# Create a temporary ignore file with our ignore patterns
590586
ignore_file = None
@@ -616,11 +612,10 @@ def _grep(context: RunContext, search_string: str, directory: str = ".") -> Grep
616612
break
617613

618614
if not rg_path:
619-
emit_error(
620-
"ripgrep (rg) not found. Please install ripgrep to use this tool.",
621-
message_group=group_id,
615+
error_message = (
616+
"ripgrep (rg) not found. Please install ripgrep to use this tool."
622617
)
623-
return GrepOutput(matches=[])
618+
return GrepOutput(matches=[], error=error_message)
624619

625620
cmd = [
626621
rg_path,
@@ -688,50 +683,43 @@ def _grep(context: RunContext, search_string: str, directory: str = ".") -> Grep
688683
# Skip lines that aren't valid JSON
689684
continue
690685

691-
# Build structured GrepMatch objects for the UI
692-
grep_matches = [
693-
GrepMatch(
694-
file_path=m.file_path or "",
695-
line_number=m.line_number or 1,
696-
line_content=m.line_content or "",
697-
)
698-
for m in matches
699-
]
700-
701-
# Count unique files searched (approximation based on matches)
702-
unique_files = len(set(m.file_path for m in matches)) if matches else 0
703-
704-
# Emit structured message for the UI
705-
grep_result_msg = GrepResultMessage(
706-
search_term=search_string,
707-
directory=directory,
708-
matches=grep_matches,
709-
total_matches=len(matches),
710-
files_searched=unique_files,
711-
)
712-
get_message_bus().emit(grep_result_msg)
713-
714-
if not matches:
715-
emit_warning(
716-
f"No matches found for '{search_string}' in {directory}",
717-
message_group=group_id,
718-
)
719-
720686
except subprocess.TimeoutExpired:
721-
emit_error("Grep command timed out after 30 seconds", message_group=group_id)
687+
error_message = "Grep command timed out after 30 seconds"
722688
except FileNotFoundError:
723-
emit_error(
724-
"ripgrep (rg) not found. Please install ripgrep to use this tool.",
725-
message_group=group_id,
689+
error_message = (
690+
"ripgrep (rg) not found. Please install ripgrep to use this tool."
726691
)
727692
except Exception as e:
728-
emit_error(f"Error during grep operation: {e}", message_group=group_id)
693+
error_message = f"Error during grep operation: {e}"
729694
finally:
730695
# Clean up the temporary ignore file
731696
if ignore_file and os.path.exists(ignore_file):
732697
os.unlink(ignore_file)
733698

734-
return GrepOutput(matches=matches)
699+
# Build structured GrepMatch objects for the UI
700+
grep_matches = [
701+
GrepMatch(
702+
file_path=m.file_path or "",
703+
line_number=m.line_number or 1,
704+
line_content=m.line_content or "",
705+
)
706+
for m in matches
707+
]
708+
709+
# Count unique files searched (approximation based on matches)
710+
unique_files = len(set(m.file_path for m in matches)) if matches else 0
711+
712+
# Emit structured message for the UI (only once, at the end)
713+
grep_result_msg = GrepResultMessage(
714+
search_term=search_string,
715+
directory=directory,
716+
matches=grep_matches,
717+
total_matches=len(matches),
718+
files_searched=unique_files,
719+
)
720+
get_message_bus().emit(grep_result_msg)
721+
722+
return GrepOutput(matches=matches, error=error_message)
735723

736724

737725
def register_list_files(agent):

0 commit comments

Comments
 (0)