#!/usr/bin/env python3 """ Utilities for generating unified diffs from Edit tool operations. """ import difflib from typing import Dict, Any def generate_unified_diff(old_string: str, new_string: str, file_path: str = "") -> str: """ Generate a unified diff from old and new strings. Args: old_string: Original content new_string: Modified content file_path: Optional file path for context Returns: Unified diff string with line numbers and +/- markers """ old_lines = old_string.splitlines(keepends=True) new_lines = new_string.splitlines(keepends=True) # Generate unified diff diff = difflib.unified_diff( old_lines, new_lines, fromfile=f"a/{file_path}" if file_path else "a/file", tofile=f"b/{file_path}" if file_path else "b/file", lineterm="", ) return "".join(diff) def format_edit_as_diff(tool_input: Dict[str, Any]) -> str: """ Format an Edit tool's input as a syntax-highlighted diff with CSS classes. Args: tool_input: The input dict from an Edit tool_use Returns: HTML-formatted diff block with red/green highlighting """ file_path = tool_input.get("file_path", "") old_string = tool_input.get("old_string", "") new_string = tool_input.get("new_string", "") replace_all = tool_input.get("replace_all", False) # Get relative path for display display_path = file_path if file_path.startswith("/"): # Try to shorten to just the filename and parent dir parts = file_path.split("/") if len(parts) > 2: display_path = "/".join(parts[-2:]) # Generate diff diff_output = generate_unified_diff(old_string, new_string, display_path) # Build output with custom HTML wrapper for styling output = f"**📝 Edit File:** `{display_path}`" if replace_all: output += " _(replace all occurrences)_" output += "\n\n" # Output clean diff syntax wrapped in div for CSS targeting output += '