Repository navigation
Expand file tree
/
Copy pathscratch_tools.py
More file actions
607 lines (505 loc) · 22.4 KB
/
Copy pathscratch_tools.py
File metadata and controls
607 lines (505 loc) · 22.4 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
import os
import time
import subprocess
import difflib
from pathlib import Path
from typing import List
from langchain.tools import tool
# ── Global Caches and Constants ───────────────────────────────────────────────
IGNORE_DIRS = {
".git", ".next", "node_modules", "dist", "build", "out",
"coverage", ".turbo", ".cache", "__pycache__", ".venv", "venv"
}
BINARY_EXTENSIONS = {
".pyc", ".png", ".jpg", ".jpeg", ".gif", ".webp", ".ico", ".pdf",
".mp4", ".mp3", ".wav", ".avi", ".mov", ".exe", ".dll", ".so", ".dylib",
".zip", ".tar", ".gz", ".bz2", ".7z", ".rar", ".iso", ".bin", ".wasm"
}
MAX_FILE_SIZE = 2 * 1024 * 1024 # 2 MB
FILE_CACHE = {}
DIR_CACHE = {}
BLOCKED_COMMANDS = [
"npm run dev", "npm start", "npm run start",
"yarn dev", "yarn start",
"pnpm dev", "pnpm start",
"python manage.py runserver",
"python -m flask run", "flask run",
"uvicorn", "gunicorn",
"next dev", "next start",
"vite", "vite dev",
"ng serve",
"rails server", "rails s",
"grep -r", "find .", "find -exec", "ls -r", "tree"
]
BLOCKED_PATHS_IN_CMD = [
".next", "node_modules", "dist", "build", "coverage", ".git"
]
# ── ANSI colors ───────────────────────────────────────────────────────────────
class C:
RESET = "\033[0m"
BOLD = "\033[1m"
RED = "\033[91m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
BLUE = "\033[94m"
MAGENTA = "\033[95m"
CYAN = "\033[96m"
WHITE = "\033[97m"
DIM = "\033[2m"
# ── Helpers ───────────────────────────────────────────────────────────────────
def _get_agent_dir() -> Path:
return Path(os.environ.get("AGENT_WORKING_DIR", ".")).resolve()
def _validate_path(path: str) -> Path:
"""
Resolve and validate path.
Rejects paths in IGNORE_DIRS, outside AGENT_WORKING_DIR, or traversal attempts.
"""
base = _get_agent_dir()
p = Path(path)
if p.is_absolute():
resolved = p.resolve()
else:
resolved = (base / p).resolve()
try:
resolved.relative_to(base)
except ValueError:
raise ValueError(f"Path '{resolved}' escapes agent working directory '{base}'.")
rel_parts = resolved.relative_to(base).parts
for part in rel_parts:
if part in IGNORE_DIRS:
raise ValueError(f"Path accesses ignored directory: '{part}'")
return resolved
def _is_binary(path: Path) -> bool:
return path.suffix.lower() in BINARY_EXTENSIONS
def _invalidate_file_cache(path: Path):
FILE_CACHE.pop(str(path), None)
DIR_CACHE.clear()
def _is_blocked(command: str) -> bool:
cmd = command.strip().lower()
for blocked in BLOCKED_COMMANDS:
if blocked in cmd:
return True
if "rg " in cmd and "--glob" not in cmd and "-g" not in cmd and "--ignore" not in cmd:
return True
if "cat " in cmd:
return True
for blocked in BLOCKED_PATHS_IN_CMD:
if f" {blocked}" in cmd or f"/{blocked}" in cmd or f"'{blocked}'" in cmd or f'"{blocked}"' in cmd:
return True
return False
def _banner(action: str, color: str, path: str = None, elapsed: float = None):
label = f"{color}{C.BOLD}[{action}]{C.RESET}"
loc = f"{C.DIM}{path}{C.RESET}" if path else ""
time_str = f" {C.DIM}({elapsed:.2f}s){C.RESET}" if elapsed is not None else ""
print(f"\n{label} {loc}{time_str}")
def _divider(color: str = C.DIM):
print(f"{color}{'─' * 52}{C.RESET}")
def _approve(action: str, path: str, reason: str = "") -> bool:
"""Ask user to confirm a destructive operation. Returns True if approved."""
_divider(C.YELLOW)
print(f"{C.YELLOW}{C.BOLD}⚠ Approval Required{C.RESET}\n")
print(f"{C.BOLD}Action:{C.RESET} {C.CYAN}{action}{C.RESET}")
print(f"{C.BOLD}Target:{C.RESET} {C.WHITE}{path}{C.RESET}")
if reason:
print(f"{C.BOLD}Reason:{C.RESET} {reason}")
print("")
print(f"{C.GREEN}[y] Yes{C.RESET}")
print(f"{C.RED}[n] No{C.RESET}")
_divider(C.YELLOW)
answer = input(f"{C.YELLOW}Proceed? [y/n]: {C.RESET}").strip().lower()
approved = answer == "y"
if approved:
print(f"{C.GREEN}✔ Approved{C.RESET}")
else:
print(f"{C.RED}✘ Cancelled by user{C.RESET}")
return approved
# ── File Operations ────────────────────────────────────────────────────────────
@tool
def read_file(path: str) -> str:
"""Read and return the contents of a file."""
start_time = time.time()
try:
abs_path = _validate_path(path)
_banner("READ", C.BLUE, abs_path)
if _is_binary(abs_path):
msg = f"ERROR: '{abs_path}' is a binary file. Reading binary files is not allowed."
print(f"{C.RED}✘ {msg}{C.RESET}")
return msg
if not abs_path.is_file():
msg = f"ERROR: File '{abs_path}' does not exist."
print(f"{C.RED}✘ {msg}{C.RESET}")
return msg
file_size = abs_path.stat().st_size
if file_size > MAX_FILE_SIZE:
msg = f"ERROR: File '{abs_path}' exceeds 2MB limit. Use search_in_files or read partial lines if supported."
print(f"{C.RED}✘ {msg}{C.RESET}")
return msg
mtime = abs_path.stat().st_mtime
cache_key = str(abs_path)
if cache_key in FILE_CACHE and FILE_CACHE[cache_key]['mtime'] == mtime:
print(f"{C.DIM} (Cache hit){C.RESET}")
content = FILE_CACHE[cache_key]['content']
else:
content = abs_path.read_text(encoding="utf-8", errors="replace")
FILE_CACHE[cache_key] = {'mtime': mtime, 'content': content}
lines = content.splitlines()
truncated = False
if len(lines) > 500:
lines = lines[:500]
lines.append("\n... truncated ...")
truncated = True
result_content = "\n".join(lines)
size = len(lines) - (1 if truncated else 0)
print(f"{C.DIM} {size} lines read{C.RESET}")
elapsed = time.time() - start_time
print(f"{C.GREEN}✔ Done ({elapsed:.2f}s){C.RESET}")
return result_content
except Exception as e:
print(f"{C.RED}✘ Error: {e}{C.RESET}")
return f"ERROR reading {path}: {e}"
@tool
def create_file(path: str, content: str) -> str:
"""Create a new file with the given content."""
start_time = time.time()
try:
abs_path = _validate_path(path)
_banner("WRITE", C.GREEN, abs_path)
if abs_path.exists():
return f"ERROR: File '{abs_path}' already exists. Use overwrite_file or edit_file instead."
abs_path.parent.mkdir(parents=True, exist_ok=True)
abs_path.write_text(content, encoding="utf-8")
_invalidate_file_cache(abs_path)
elapsed = time.time() - start_time
print(f"{C.GREEN}✔ Created ({elapsed:.2f}s){C.RESET}")
return f"Created {abs_path}"
except Exception as e:
print(f"{C.RED}✘ Error: {e}{C.RESET}")
return f"ERROR creating {path}: {e}"
@tool
def overwrite_file(path: str, content: str) -> str:
"""Completely overwrite an existing file with new content."""
start_time = time.time()
try:
abs_path = _validate_path(path)
_banner("WRITE", C.MAGENTA, abs_path)
if not _approve("Overwrite file", str(abs_path), "Completely replacing file contents."):
return f"Cancelled — {abs_path} was not overwritten."
abs_path.parent.mkdir(parents=True, exist_ok=True)
abs_path.write_text(content, encoding="utf-8")
_invalidate_file_cache(abs_path)
elapsed = time.time() - start_time
print(f"{C.MAGENTA}✔ Overwritten ({elapsed:.2f}s){C.RESET}")
return f"Updated {abs_path}"
except Exception as e:
print(f"{C.RED}✘ Error: {e}{C.RESET}")
return f"ERROR overwriting {path}: {e}"
@tool
def edit_file(path: str, old: str, new: str) -> str:
"""Replace the first occurrence of 'old' with 'new' in a file."""
start_time = time.time()
try:
abs_path = _validate_path(path)
_banner("EDIT", C.CYAN, abs_path)
if not abs_path.is_file():
msg = f"ERROR: File '{abs_path}' does not exist."
print(f"{C.RED}✘ {msg}{C.RESET}")
return msg
content = abs_path.read_text(encoding="utf-8", errors="replace")
if old not in content:
old_first_line = old.strip().splitlines()[0].strip() if old.strip() else ""
hint_lines = [
line for line in content.splitlines()
if old_first_line[:20] in line
]
hint = f" Closest match in file: {hint_lines[0]!r}" if hint_lines else ""
msg = f"ERROR: Target text not found in {abs_path}.{hint}"
print(f"{C.RED}✘ {msg}{C.RESET}")
return msg
# Show a mini diff preview
diff = list(difflib.unified_diff(
old.splitlines(), new.splitlines(),
fromfile="before", tofile="after", lineterm=""
))
if diff:
print(f"{C.DIM} Preview:{C.RESET}")
for line in diff[:12]:
if line.startswith("+"):
print(f" {C.GREEN}{line}{C.RESET}")
elif line.startswith("-"):
print(f" {C.RED}{line}{C.RESET}")
else:
print(f" {C.DIM}{line}{C.RESET}")
if len(diff) > 12:
print(f" {C.DIM}... ({len(diff) - 12} more lines){C.RESET}")
content = content.replace(old, new, 1)
abs_path.write_text(content, encoding="utf-8")
_invalidate_file_cache(abs_path)
elapsed = time.time() - start_time
print(f"{C.CYAN}✔ Edited ({elapsed:.2f}s){C.RESET}")
return f"Edited {abs_path}"
except Exception as e:
print(f"{C.RED}✘ Error: {e}{C.RESET}")
return f"ERROR editing {path}: {e}"
@tool
def delete_file(path: str) -> str:
"""Delete a file."""
start_time = time.time()
try:
abs_path = _validate_path(path)
_banner("DELETE", C.RED, abs_path)
# Prevent deleting the project root
if abs_path == _get_agent_dir():
msg = "ERROR: Cannot delete the project root."
print(f"{C.RED}✘ {msg}{C.RESET}")
return msg
if not abs_path.exists():
return f"ERROR: {abs_path} does not exist."
if not _approve("Delete file", str(abs_path), "Removing file from disk."):
return f"Cancelled — {abs_path} was not deleted."
abs_path.unlink(missing_ok=True)
_invalidate_file_cache(abs_path)
elapsed = time.time() - start_time
print(f"{C.RED}✔ Deleted ({elapsed:.2f}s){C.RESET}")
return f"Deleted {abs_path}"
except Exception as e:
print(f"{C.RED}✘ Error: {e}{C.RESET}")
return f"ERROR deleting {path}: {e}"
# ── Directory Operations ───────────────────────────────────────────────────────
@tool
def list_dir(path: str = ".") -> List[str]:
"""List files and directories."""
start_time = time.time()
try:
abs_path = _validate_path(path)
_banner("READ", C.BLUE, abs_path)
mtime = abs_path.stat().st_mtime if abs_path.exists() else 0
cache_key = f"list_{abs_path}"
if cache_key in DIR_CACHE and DIR_CACHE[cache_key]['mtime'] == mtime:
print(f"{C.DIM} (Cache hit){C.RESET}")
entries = DIR_CACHE[cache_key]['entries']
else:
entries = []
if abs_path.is_dir():
for p in abs_path.iterdir():
if p.name not in IGNORE_DIRS:
entries.append(str(p))
entries.sort()
DIR_CACHE[cache_key] = {'mtime': mtime, 'entries': entries}
for e in entries:
icon = "📁" if Path(e).is_dir() else "📄"
print(f" {icon} {C.DIM}{e}{C.RESET}")
elapsed = time.time() - start_time
print(f"{C.GREEN}✔ Done ({elapsed:.2f}s){C.RESET}")
return entries
except Exception as e:
print(f"{C.RED}✘ Error: {e}{C.RESET}")
return [f"ERROR listing {path}: {e}"]
@tool
def find_files(pattern: str, root: str = ".") -> List[str]:
"""Find files matching a glob pattern."""
start_time = time.time()
try:
abs_root = _validate_path(root)
_banner("SEARCH", C.BLUE, abs_root)
print(f" {C.DIM}Pattern: {pattern}{C.RESET}")
mtime = abs_root.stat().st_mtime if abs_root.exists() else 0
cache_key = f"find_{abs_root}_{pattern}"
if cache_key in DIR_CACHE and DIR_CACHE[cache_key]['mtime'] == mtime:
print(f"{C.DIM} (Cache hit){C.RESET}")
results = DIR_CACHE[cache_key]['results']
else:
results = []
for p in abs_root.rglob(pattern):
# Check against ignore dirs
rel_parts = p.relative_to(abs_root).parts
if not any(part in IGNORE_DIRS for part in rel_parts):
results.append(str(p))
results.sort()
DIR_CACHE[cache_key] = {'mtime': mtime, 'results': results}
print(f" {C.GREEN}{len(results)} match(es) found{C.RESET}")
for r in results:
print(f" {C.DIM}→ {r}{C.RESET}")
elapsed = time.time() - start_time
print(f"{C.GREEN}✔ Done ({elapsed:.2f}s){C.RESET}")
return results
except Exception as e:
print(f"{C.RED}✘ Error: {e}{C.RESET}")
return [f"ERROR finding files in {root}: {e}"]
# ── Search ─────────────────────────────────────────────────────────────────────
@tool
def search_in_files(query: str, root: str = ".") -> List[str]:
"""Search for text inside files. Returns matching file paths."""
start_time = time.time()
try:
abs_root = _validate_path(root)
_banner("SEARCH", C.CYAN, abs_root)
print(f" {C.DIM}Query: \"{query}\"{C.RESET}")
matches = []
for file in abs_root.rglob("*"):
if not file.is_file():
continue
rel_parts = file.relative_to(_get_agent_dir()).parts
if any(part in IGNORE_DIRS for part in rel_parts):
continue
if _is_binary(file):
continue
if file.stat().st_size > MAX_FILE_SIZE:
continue
try:
text = file.read_text(encoding="utf-8", errors="ignore")
if query in text:
matches.append(str(file))
print(f" {C.GREEN}✔ {file}{C.RESET}")
if len(matches) >= 100:
print(f" {C.YELLOW}⚠ Reached 100 matches limit.{C.RESET}")
break
except Exception:
pass
if not matches:
print(f" {C.YELLOW}No matches found{C.RESET}")
elapsed = time.time() - start_time
print(f"{C.GREEN}✔ Done ({elapsed:.2f}s){C.RESET}")
return matches
except Exception as e:
print(f"{C.RED}✘ Error: {e}{C.RESET}")
return [f"ERROR searching in {root}: {e}"]
# ── Terminal ───────────────────────────────────────────────────────────────────
@tool
def run_command(command: str, cwd: str = ".") -> str:
"""Execute a shell command."""
start_time = time.time()
try:
abs_cwd = _validate_path(cwd)
_banner("RUN", C.MAGENTA, abs_cwd)
if _is_blocked(command):
msg = (
f"BLOCKED: '{command}' contains blocked patterns and cannot "
f"be run by the agent."
)
print(f"{C.RED}✘ {msg}{C.RESET}")
return msg
print(f" {C.BOLD}$ {command}{C.RESET}")
_divider(C.DIM)
result = subprocess.run(
command,
shell=True,
cwd=abs_cwd,
capture_output=True,
text=True,
input="y\n",
timeout=60,
)
stdout_lines = result.stdout.strip().splitlines()
stderr_lines = result.stderr.strip().splitlines()
truncated = False
if len(stdout_lines) > 200:
stdout_lines = stdout_lines[:200]
stdout_lines.append("... OUTPUT TRUNCATED ...")
truncated = True
if len(stderr_lines) > 200:
stderr_lines = stderr_lines[:200]
stderr_lines.append("... OUTPUT TRUNCATED ...")
truncated = True
stdout_text = "\n".join(stdout_lines)
stderr_text = "\n".join(stderr_lines)
if stdout_text:
print(f"{C.DIM}{stdout_text}{C.RESET}")
if stderr_text:
print(f"{C.RED}{stderr_text}{C.RESET}")
color = C.GREEN if result.returncode == 0 else C.RED
print(f"{color} Exit code: {result.returncode}{C.RESET}")
elapsed = time.time() - start_time
print(f"{color}✔ Done ({elapsed:.2f}s){C.RESET}")
return (
f"Exit Code: {result.returncode}\n\n"
f"STDOUT:\n{stdout_text}\n\n"
f"STDERR:\n{stderr_text}"
)
except subprocess.TimeoutExpired:
elapsed = time.time() - start_time
msg = f"TimeoutExpired: Command killed after 60s."
print(f"{C.YELLOW} ⚠ {msg} ({elapsed:.2f}s){C.RESET}")
return msg
except Exception as e:
print(f"{C.RED}✘ Error: {e}{C.RESET}")
return f"ERROR running command '{command}': {e}"
# ── Git ────────────────────────────────────────────────────────────────────────
@tool
def git_diff(cwd: str = ".") -> str:
"""Return current git diff."""
return run_command.invoke({"command": "git diff", "cwd": cwd})
# ── Testing ────────────────────────────────────────────────────────────────────
@tool
def run_tests(command: str = "pytest", cwd: str = ".") -> str:
"""Run project tests."""
return run_command.invoke({"command": command, "cwd": cwd})
# ── Diff Preview ───────────────────────────────────────────────────────────────
@tool
def preview_changes(old: str, new: str) -> str:
"""Generate a unified diff without modifying files."""
start_time = time.time()
_banner("PREVIEW", C.YELLOW)
diff = list(difflib.unified_diff(
old.splitlines(), new.splitlines(),
fromfile="before", tofile="after", lineterm=""
))
for line in diff:
if line.startswith("+"):
print(f"{C.GREEN}{line}{C.RESET}")
elif line.startswith("-"):
print(f"{C.RED}{line}{C.RESET}")
else:
print(f"{C.DIM}{line}{C.RESET}")
elapsed = time.time() - start_time
print(f"{C.GREEN}✔ Done ({elapsed:.2f}s){C.RESET}")
return "\n".join(diff)
# ── Web Search ─────────────────────────────────────────────────────────────────
from serpapi import GoogleSearch
@tool
def search_web(query: str, num_results: int = 5) -> str:
"""Search Google using SerpAPI and return formatted results."""
start_time = time.time()
try:
_banner("SEARCH", C.CYAN)
print(f" {C.DIM}Query: \"{query}\"{C.RESET}")
api_key = os.getenv("SERPAPI_API_KEY")
if not api_key:
return "ERROR: SERPAPI_API_KEY environment variable not found."
search = GoogleSearch({
"q": query,
"api_key": api_key,
"engine": "google",
"num": num_results,
})
results = search.get_dict()
organic_results = results.get("organic_results", [])
if not organic_results:
print(f" {C.YELLOW}No results found{C.RESET}")
return "No search results found."
output = []
for i, result in enumerate(organic_results, 1):
title = result.get("title", "")
link = result.get("link", "")
snippet = result.get("snippet", "")
print(f" {C.BOLD}{i}. {title}{C.RESET}")
print(f" {C.DIM}{link}{C.RESET}")
output.append(f"{i}. {title}\nURL: {link}\nSnippet: {snippet}\n")
elapsed = time.time() - start_time
print(f"{C.GREEN}✔ Done ({elapsed:.2f}s){C.RESET}")
return "\n".join(output)
except Exception as e:
print(f"{C.RED}✘ Error: {e}{C.RESET}")
return f"ERROR during web search: {e}"
# ── Ask User ───────────────────────────────────────────────────────────────────
@tool
def ask_user(question: str) -> str:
"""Ask the user for clarification and wait for a response."""
_divider(C.CYAN)
print(f"{C.CYAN}{C.BOLD}🤖 Agent needs your input{C.RESET}")
_divider(C.CYAN)
print(f"{C.WHITE}{question}{C.RESET}")
_divider(C.DIM)
response = input(f"{C.CYAN}Your response: {C.RESET}").strip()
while not response:
response = input(f"{C.YELLOW}Please enter a response: {C.RESET}").strip()
return response