Repository navigation
Expand file tree
/
Copy pathstructural_editor.py
More file actions
57 lines (49 loc) · 2 KB
/
Copy pathstructural_editor.py
File metadata and controls
57 lines (49 loc) · 2 KB
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
# Agentic tool wrapper for ast-grep structural replace (#415). Rewrites are
# gated twice: dry_run defaults to a preview, and the tool requires approval
# before any invocation actually touches disk.
from __future__ import annotations
import asyncio
from pydantic_ai import Tool
from .. import constants as cs
from ..types_defs import StructuralReplaceChange
from ..utils.dependencies import has_ast_grep
from . import tool_descriptions as td
from .ast_grep_service import AstGrepService
def format_changes(changes: list[StructuralReplaceChange], dry_run: bool) -> str:
header = (
cs.AST_GREP_DRY_RUN_HEADER if dry_run else cs.AST_GREP_APPLIED_HEADER
).format(count=len(changes))
bodies = [f"{c['file']} ({c['matches']} match(es))\n{c['diff']}" for c in changes]
return "\n\n".join([header, *bodies])
def create_structural_editor_tool(service: AstGrepService) -> Tool:
async def structural_replace(
pattern: str,
rewrite: str,
language: str | None = None,
dry_run: bool = True,
) -> str:
if not has_ast_grep():
return cs.AST_GREP_NOT_AVAILABLE
try:
# offload to a thread: replace does blocking os.walk, file I/O, and
# CPU-bound AST parsing, which would stall the event loop.
changes = await asyncio.to_thread(
service.replace,
pattern,
rewrite,
language=language,
dry_run=dry_run,
)
# catch broadly: ast-grep-py's Rust bindings raise beyond ValueError
# (RuntimeError and others); report it rather than crash the turn.
except Exception as e:
return str(e)
if not changes:
return cs.AST_GREP_NO_MATCHES.format(pattern=pattern)
return format_changes(changes, dry_run)
return Tool(
function=structural_replace,
name=td.AgenticToolName.STRUCTURAL_REPLACE,
description=td.STRUCTURAL_EDITOR,
requires_approval=True,
)