-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtools.py
More file actions
1271 lines (1131 loc) · 47.8 KB
/
tools.py
File metadata and controls
1271 lines (1131 loc) · 47.8 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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Tool definitions and implementations for CheetahClaws."""
import json
import os
import re
import glob as _glob
import difflib
import subprocess
import threading
from pathlib import Path
from typing import Callable, Optional
from tool_registry import ToolDef, register_tool
from tool_registry import execute_tool as _registry_execute
# ── AskUserQuestion state ──────────────────────────────────────────────────────
# The main REPL loop drains _pending_questions and fills _question_answers.
_pending_questions: list[dict] = [] # [{id, question, options, allow_freetext, event, result_holder}]
_ask_lock = threading.Lock()
# ── Telegram turn detection (thread-local) ─────────────────────────────────
# Using thread-local storage instead of a shared config key prevents race
# conditions when slash commands run in their own daemon threads while the
# Telegram poll loop and the main REPL loop continue on other threads.
_tg_thread_local = threading.local()
def _is_in_tg_turn(config: dict) -> bool:
"""Return True if the *current thread* is handling a Telegram interaction.
Checks the thread-local flag first (set by the slash-command runner thread),
then falls back to the config key (set by the main REPL for _bg_runner turns).
"""
return getattr(_tg_thread_local, "active", False) or bool(config.get("_in_telegram_turn", False))
# ── Tool JSON schemas (sent to Claude API) ─────────────────────────────────
TOOL_SCHEMAS = [
{
"name": "Read",
"description": (
"Read a file's contents. Returns content with line numbers "
"(format: 'N\\tline'). Use limit/offset to read large files in chunks."
),
"input_schema": {
"type": "object",
"properties": {
"file_path": {"type": "string", "description": "Absolute file path"},
"limit": {"type": "integer", "description": "Max lines to read"},
"offset": {"type": "integer", "description": "Start line (0-indexed)"},
},
"required": ["file_path"],
},
},
{
"name": "Write",
"description": "Write content to a file, creating parent directories as needed.",
"input_schema": {
"type": "object",
"properties": {
"file_path": {"type": "string"},
"content": {"type": "string"},
},
"required": ["file_path", "content"],
},
},
{
"name": "Edit",
"description": (
"Replace exact text in a file. old_string must match exactly (including whitespace). "
"If old_string appears multiple times, use replace_all=true or add more context."
),
"input_schema": {
"type": "object",
"properties": {
"file_path": {"type": "string"},
"old_string": {"type": "string", "description": "Exact text to replace"},
"new_string": {"type": "string", "description": "Replacement text"},
"replace_all": {"type": "boolean", "description": "Replace all occurrences"},
},
"required": ["file_path", "old_string", "new_string"],
},
},
{
"name": "Bash",
"description": "Execute a shell command. Returns stdout+stderr. Stateless (no cd persistence).",
"input_schema": {
"type": "object",
"properties": {
"command": {"type": "string"},
"timeout": {"type": "integer", "description": "Seconds before timeout (default 30). Use 120-300 for package installs (npm, pip, npx), builds, and long-running commands."},
},
"required": ["command"],
},
},
{
"name": "Glob",
"description": "Find files matching a glob pattern. Returns sorted list of matching paths.",
"input_schema": {
"type": "object",
"properties": {
"pattern": {"type": "string", "description": "Glob pattern e.g. **/*.py"},
"path": {"type": "string", "description": "Base directory (default: cwd)"},
},
"required": ["pattern"],
},
},
{
"name": "Grep",
"description": "Search file contents with regex using ripgrep (falls back to grep).",
"input_schema": {
"type": "object",
"properties": {
"pattern": {"type": "string", "description": "Regex pattern"},
"path": {"type": "string", "description": "File or directory to search"},
"glob": {"type": "string", "description": "File filter e.g. *.py"},
"output_mode": {
"type": "string",
"enum": ["content", "files_with_matches", "count"],
"description": "content=matching lines, files_with_matches=file paths, count=match counts",
},
"case_insensitive": {"type": "boolean"},
"context": {"type": "integer", "description": "Lines of context around matches"},
},
"required": ["pattern"],
},
},
{
"name": "WebFetch",
"description": "Fetch a URL and return its text content (HTML stripped).",
"input_schema": {
"type": "object",
"properties": {
"url": {"type": "string"},
"prompt": {"type": "string", "description": "Hint for what to extract"},
},
"required": ["url"],
},
},
{
"name": "WebSearch",
"description": "Search the web via DuckDuckGo and return top results.",
"input_schema": {
"type": "object",
"properties": {
"query": {"type": "string"},
},
"required": ["query"],
},
},
# ── Task tools (schemas also listed here for Claude's tool list) ──────────
{
"name": "TaskCreate",
"description": (
"Create a new task in the task list. "
"Use this to track work items, to-dos, and multi-step plans."
),
"input_schema": {
"type": "object",
"properties": {
"subject": {"type": "string", "description": "Brief title"},
"description": {"type": "string", "description": "What needs to be done"},
"active_form": {"type": "string", "description": "Present-continuous label while in_progress"},
"metadata": {"type": "object", "description": "Arbitrary metadata"},
},
"required": ["subject", "description"],
},
},
{
"name": "TaskUpdate",
"description": (
"Update a task: change status, subject, description, owner, "
"dependency edges, or metadata. "
"Set status='deleted' to remove. "
"Statuses: pending, in_progress, completed, cancelled, deleted."
),
"input_schema": {
"type": "object",
"properties": {
"task_id": {"type": "string"},
"subject": {"type": "string"},
"description": {"type": "string"},
"status": {"type": "string", "enum": ["pending","in_progress","completed","cancelled","deleted"]},
"active_form": {"type": "string"},
"owner": {"type": "string"},
"add_blocks": {"type": "array", "items": {"type": "string"}},
"add_blocked_by":{"type": "array", "items": {"type": "string"}},
"metadata": {"type": "object"},
},
"required": ["task_id"],
},
},
{
"name": "TaskGet",
"description": "Retrieve full details of a single task by ID.",
"input_schema": {
"type": "object",
"properties": {
"task_id": {"type": "string", "description": "Task ID to retrieve"},
},
"required": ["task_id"],
},
},
{
"name": "TaskList",
"description": "List all tasks with their status, owner, and pending blockers.",
"input_schema": {
"type": "object",
"properties": {},
"required": [],
},
},
{
"name": "NotebookEdit",
"description": (
"Edit a Jupyter notebook (.ipynb) cell. "
"Supports replace (modify existing cell), insert (add new cell after cell_id), "
"and delete (remove cell) operations. "
"Read the notebook with the Read tool first to see cell IDs."
),
"input_schema": {
"type": "object",
"properties": {
"notebook_path": {
"type": "string",
"description": "Absolute path to the .ipynb notebook file",
},
"new_source": {
"type": "string",
"description": "New source code/text for the cell",
},
"cell_id": {
"type": "string",
"description": (
"ID of the cell to edit. For insert, the new cell is inserted after this cell "
"(or at the beginning if omitted). Use 'cell-N' (0-indexed) if no IDs are set."
),
},
"cell_type": {
"type": "string",
"enum": ["code", "markdown"],
"description": "Cell type. Required for insert; defaults to current type for replace.",
},
"edit_mode": {
"type": "string",
"enum": ["replace", "insert", "delete"],
"description": "replace (default) / insert / delete",
},
},
"required": ["notebook_path", "new_source"],
},
},
{
"name": "GetDiagnostics",
"description": (
"Get LSP-style diagnostics (errors, warnings, hints) for a source file. "
"Uses pyright/mypy/flake8 for Python, tsc for TypeScript/JavaScript, "
"and shellcheck for shell scripts. Returns structured diagnostic output."
),
"input_schema": {
"type": "object",
"properties": {
"file_path": {
"type": "string",
"description": "Absolute or relative path to the file to diagnose",
},
"language": {
"type": "string",
"description": (
"Override auto-detected language: python, javascript, typescript, "
"shellscript. Omit to auto-detect from file extension."
),
},
},
"required": ["file_path"],
},
},
{
"name": "AskUserQuestion",
"description": (
"Pause execution and ask the user a clarifying question. "
"Use this when you need a decision from the user before proceeding. "
"Returns the user's answer as a string."
),
"input_schema": {
"type": "object",
"properties": {
"question": {
"type": "string",
"description": "The question to ask the user.",
},
"options": {
"type": "array",
"description": "Optional list of choices. Each item: {label, description}.",
"items": {
"type": "object",
"properties": {
"label": {"type": "string"},
"description": {"type": "string"},
},
"required": ["label"],
},
},
"allow_freetext": {
"type": "boolean",
"description": "If true (default), user may type a free-text answer instead of selecting an option.",
},
},
"required": ["question"],
},
},
{
"name": "SleepTimer",
"description": (
"Schedule a silent background timer. When the timer finishes, it injects an automated prompt into the chat history: "
"'(System Automated Event): The timer has finished...' so you can seamlessly wake up and execute deferred monitoring tasks."
),
"input_schema": {
"type": "object",
"properties": {
"seconds": {"type": "integer", "description": "Number of seconds to sleep before waking up."}
},
"required": ["seconds"],
},
},
]
# ── Safe bash commands (never ask permission) ───────────────────────────────
_SAFE_PREFIXES = (
"ls", "cat", "head", "tail", "wc", "pwd", "echo", "printf", "date",
"which", "type", "env", "printenv", "uname", "whoami", "id",
"git log", "git status", "git diff", "git show", "git branch",
"git remote", "git stash list", "git tag",
"find ", "grep ", "rg ", "ag ", "fd ",
"python ", "python3 ", "node ", "ruby ", "perl ",
"pip show", "pip list", "npm list", "cargo metadata",
"df ", "du ", "free ", "top -bn", "ps ",
"curl -I", "curl --head",
)
def _is_safe_bash(cmd: str) -> bool:
c = cmd.strip()
return any(c.startswith(p) for p in _SAFE_PREFIXES)
# ── Diff helpers ──────────────────────────────────────────────────────────
def generate_unified_diff(old, new, filename, context_lines=3):
old_lines = old.splitlines(keepends=True)
new_lines = new.splitlines(keepends=True)
diff = difflib.unified_diff(old_lines, new_lines,
fromfile=f"a/{filename}", tofile=f"b/{filename}", n=context_lines)
return "".join(diff)
def maybe_truncate_diff(diff_text, max_lines=80):
lines = diff_text.splitlines()
if len(lines) <= max_lines:
return diff_text
shown = lines[:max_lines]
remaining = len(lines) - max_lines
return "\n".join(shown) + f"\n\n[... {remaining} more lines ...]"
# ── Tool implementations ───────────────────────────────────────────────────
def _read(file_path: str, limit: int = None, offset: int = None) -> str:
p = Path(file_path)
if not p.exists():
return f"Error: file not found: {file_path}"
if p.is_dir():
return f"Error: {file_path} is a directory"
try:
# Explicitly use utf-8 and newline="" to avoid encoding/line-ending mismatches
lines = p.read_text(encoding="utf-8", errors="replace", newline="").splitlines(keepends=True)
start = offset or 0
chunk = lines[start:start + limit] if limit else lines[start:]
if not chunk:
return "(empty file)"
# Use standard 6-char padding for line numbers, matching Claude's expected format
return "".join(f"{start + i + 1:6}\t{l}" for i, l in enumerate(chunk))
except Exception as e:
return f"Error: {e}"
def _write(file_path: str, content: str) -> str:
p = Path(file_path)
try:
is_new = not p.exists()
# Ensure utf-8 and newline="" for reading existing content to generate diff
old_content = "" if is_new else p.read_text(encoding="utf-8", errors="replace", newline="")
p.parent.mkdir(parents=True, exist_ok=True)
# Always write as utf-8 with newline="" to prevent double CRLF on Windows
p.write_text(content, encoding="utf-8", newline="")
if is_new:
lc = content.count("\n") + (1 if content and not content.endswith("\n") else 0)
return f"Created {file_path} ({lc} lines)"
filename = p.name
diff = generate_unified_diff(old_content, content, filename)
if not diff:
return f"No changes in {file_path}"
truncated = maybe_truncate_diff(diff)
return f"File updated — {file_path}:\n\n{truncated}"
except Exception as e:
return f"Error: {e}"
def _edit(file_path: str, old_string: str, new_string: str, replace_all: bool = False) -> str:
p = Path(file_path)
if not p.exists():
return f"Error: file not found: {file_path}"
try:
# Read with newline="" to get original line endings
content = p.read_text(encoding="utf-8", errors="replace", newline="")
# Detect original line endings: only treat as pure CRLF if every \n is part of \r\n
crlf_count = content.count("\r\n")
lf_count = content.count("\n")
is_pure_crlf = crlf_count > 0 and crlf_count == lf_count
# Normalize line endings to avoid \r\n vs \n mismatch during matching
content_norm = content.replace("\r\n", "\n")
old_norm = old_string.replace("\r\n", "\n")
new_norm = new_string.replace("\r\n", "\n")
count = content_norm.count(old_norm)
if count == 0:
return "Error: old_string not found in file. Please ensure EXACT match, including all exact leading spaces/indentation and trailing newlines."
if count > 1 and not replace_all:
return (f"Error: old_string appears {count} times. "
"Provide more context to make it unique, or use replace_all=true.")
old_content_norm = content_norm
new_content_norm = content_norm.replace(old_norm, new_norm) if replace_all else \
content_norm.replace(old_norm, new_norm, 1)
# Restore CRLF only for pure-CRLF files; mixed or LF-only files stay as LF
if is_pure_crlf:
final_content = new_content_norm.replace("\n", "\r\n")
old_content_final = content
else:
final_content = new_content_norm
old_content_final = content_norm
# Write with newline="" to prevent double CRLF translation on Windows
p.write_text(final_content, encoding="utf-8", newline="")
filename = p.name
diff = generate_unified_diff(old_content_final, final_content, filename)
return f"Changes applied to {filename}:\n\n{diff}"
except Exception as e:
return f"Error: {e}"
def _kill_proc_tree(pid: int):
"""Kill a process and all its children."""
import sys as _sys
if _sys.platform == "win32":
# taskkill /T kills the entire process tree on Windows
subprocess.run(["taskkill", "/F", "/T", "/PID", str(pid)],
capture_output=True)
else:
import signal
try:
os.killpg(os.getpgid(pid), signal.SIGKILL)
except (ProcessLookupError, PermissionError):
try:
os.kill(pid, signal.SIGKILL)
except (ProcessLookupError, PermissionError):
pass
def _bash(command: str, timeout: int = 30) -> str:
import sys as _sys
kwargs = dict(
shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
text=True, cwd=os.getcwd(),
)
# On Unix, create a process group so we can kill the whole tree.
# start_new_session=True is equivalent to setsid but safe in multithreaded code
# (preexec_fn=os.setsid can deadlock when other threads hold locks at fork time).
if _sys.platform != "win32":
kwargs["start_new_session"] = True
try:
proc = subprocess.Popen(command, **kwargs)
try:
stdout, stderr = proc.communicate(timeout=timeout)
except subprocess.TimeoutExpired:
_kill_proc_tree(proc.pid)
proc.wait()
return f"Error: timed out after {timeout}s (process killed)"
out = stdout
if stderr:
out += ("\n" if out else "") + "[stderr]\n" + stderr
return out.strip() or "(no output)"
except Exception as e:
return f"Error: {e}"
def _glob(pattern: str, path: str = None) -> str:
base = Path(path) if path else Path.cwd()
try:
matches = sorted(base.glob(pattern))
if not matches:
return "No files matched"
return "\n".join(str(m) for m in matches[:500])
except Exception as e:
return f"Error: {e}"
def _has_rg() -> bool:
try:
subprocess.run(["rg", "--version"], capture_output=True, check=True)
return True
except Exception:
return False
def _grep(pattern: str, path: str = None, glob: str = None,
output_mode: str = "files_with_matches",
case_insensitive: bool = False, context: int = 0) -> str:
use_rg = _has_rg()
cmd = ["rg" if use_rg else "grep", "--no-heading"]
if case_insensitive:
cmd.append("-i")
if output_mode == "files_with_matches":
cmd.append("-l")
elif output_mode == "count":
cmd.append("-c")
else:
cmd.append("-n")
if context:
cmd += ["-C", str(context)]
if glob:
cmd += (["--glob", glob] if use_rg else ["--include", glob])
cmd.append(pattern)
cmd.append(path or str(Path.cwd()))
try:
r = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
out = r.stdout.strip()
return out[:20000] if out else "No matches found"
except Exception as e:
return f"Error: {e}"
def _webfetch(url: str, prompt: str = None) -> str:
try:
import httpx
r = httpx.get(url, headers={"User-Agent": "NanoClaude/1.0"},
timeout=30, follow_redirects=True)
r.raise_for_status()
ct = r.headers.get("content-type", "")
if "html" in ct:
text = re.sub(r"<script[^>]*>.*?</script>", "", r.text,
flags=re.DOTALL | re.IGNORECASE)
text = re.sub(r"<style[^>]*>.*?</style>", "", text,
flags=re.DOTALL | re.IGNORECASE)
text = re.sub(r"<[^>]+>", " ", text)
text = re.sub(r"\s+", " ", text).strip()
else:
text = r.text
return text[:25000]
except ImportError:
return "Error: httpx not installed — run: pip install httpx"
except Exception as e:
return f"Error: {e}"
def _websearch(query: str) -> str:
try:
import httpx
url = "https://html.duckduckgo.com/html/"
r = httpx.get(url, params={"q": query},
headers={"User-Agent": "Mozilla/5.0 (compatible)"},
timeout=30, follow_redirects=True)
titles = re.findall(r'class="result__title"[^>]*>.*?<a[^>]*href="([^"]+)"[^>]*>(.*?)</a>',
r.text, re.DOTALL)
snippets = re.findall(r'class="result__snippet"[^>]*>(.*?)</div>', r.text, re.DOTALL)
results = []
for i, (link, title) in enumerate(titles[:8]):
t = re.sub(r"<[^>]+>", "", title).strip()
s = re.sub(r"<[^>]+>", "", snippets[i]).strip() if i < len(snippets) else ""
results.append(f"**{t}**\n{link}\n{s}")
return "\n\n".join(results) if results else "No results found"
except ImportError:
return "Error: httpx not installed — run: pip install httpx"
except Exception as e:
return f"Error: {e}"
# ── NotebookEdit implementation ────────────────────────────────────────────
def _parse_cell_id(cell_id: str) -> int | None:
"""Convert 'cell-N' shorthand to integer index; return None if not that form."""
m = re.fullmatch(r"cell-(\d+)", cell_id)
return int(m.group(1)) if m else None
def _notebook_edit(
notebook_path: str,
new_source: str,
cell_id: str = None,
cell_type: str = None,
edit_mode: str = "replace",
) -> str:
p = Path(notebook_path)
if p.suffix != ".ipynb":
return "Error: file must be a Jupyter notebook (.ipynb)"
if not p.exists():
return f"Error: notebook not found: {notebook_path}"
try:
nb = json.loads(p.read_text(encoding="utf-8"))
except json.JSONDecodeError as e:
return f"Error: notebook is not valid JSON: {e}"
cells = nb.get("cells", [])
# Resolve cell index
def _resolve_index(cid: str) -> int | None:
# Try exact id match first
for i, c in enumerate(cells):
if c.get("id") == cid:
return i
# Fallback: cell-N
idx = _parse_cell_id(cid)
if idx is not None and 0 <= idx < len(cells):
return idx
return None
if edit_mode == "replace":
if not cell_id:
return "Error: cell_id is required for replace"
idx = _resolve_index(cell_id)
if idx is None:
return f"Error: cell '{cell_id}' not found"
target = cells[idx]
target["source"] = new_source
if cell_type and cell_type != target.get("cell_type"):
target["cell_type"] = cell_type
if target.get("cell_type") == "code":
target["execution_count"] = None
target["outputs"] = []
elif edit_mode == "insert":
if not cell_type:
return "Error: cell_type is required for insert ('code' or 'markdown')"
# Determine nb format for cell ids
nbformat = nb.get("nbformat", 4)
nbformat_minor = nb.get("nbformat_minor", 0)
use_ids = nbformat > 4 or (nbformat == 4 and nbformat_minor >= 5)
new_id = None
if use_ids:
import random, string
new_id = "".join(random.choices(string.ascii_lowercase + string.digits, k=8))
if cell_type == "markdown":
new_cell = {"cell_type": "markdown", "source": new_source, "metadata": {}}
else:
new_cell = {
"cell_type": "code",
"source": new_source,
"metadata": {},
"execution_count": None,
"outputs": [],
}
if use_ids and new_id:
new_cell["id"] = new_id
if cell_id:
idx = _resolve_index(cell_id)
if idx is None:
return f"Error: cell '{cell_id}' not found"
cells.insert(idx + 1, new_cell)
else:
cells.insert(0, new_cell)
nb["cells"] = cells
cell_id = new_id or cell_id
elif edit_mode == "delete":
if not cell_id:
return "Error: cell_id is required for delete"
idx = _resolve_index(cell_id)
if idx is None:
return f"Error: cell '{cell_id}' not found"
cells.pop(idx)
nb["cells"] = cells
p.write_text(json.dumps(nb, indent=1, ensure_ascii=False), encoding="utf-8")
return f"Deleted cell '{cell_id}' from {notebook_path}"
else:
return f"Error: unknown edit_mode '{edit_mode}' — use replace, insert, or delete"
nb["cells"] = cells
p.write_text(json.dumps(nb, indent=1, ensure_ascii=False), encoding="utf-8")
return f"NotebookEdit({edit_mode}) applied to cell '{cell_id}' in {notebook_path}"
# ── GetDiagnostics implementation ──────────────────────────────────────────
def _detect_language(file_path: str) -> str:
ext = Path(file_path).suffix.lower()
return {
".py": "python",
".js": "javascript",
".mjs": "javascript",
".cjs": "javascript",
".ts": "typescript",
".tsx": "typescript",
".sh": "shellscript",
".bash": "shellscript",
".zsh": "shellscript",
}.get(ext, "unknown")
def _run_quietly(cmd: list[str], cwd: str | None = None, timeout: int = 30) -> tuple[int, str]:
"""Run a command, return (returncode, combined_output)."""
try:
r = subprocess.run(
cmd, capture_output=True, text=True, timeout=timeout,
cwd=cwd or os.getcwd(),
)
out = (r.stdout + ("\n" + r.stderr if r.stderr else "")).strip()
return r.returncode, out
except FileNotFoundError:
return -1, f"(command not found: {cmd[0]})"
except subprocess.TimeoutExpired:
return -1, f"(timed out after {timeout}s)"
except Exception as e:
return -1, f"(error: {e})"
def _get_diagnostics(file_path: str, language: str = None) -> str:
p = Path(file_path)
if not p.exists():
return f"Error: file not found: {file_path}"
lang = language or _detect_language(file_path)
abs_path = str(p.resolve())
results: list[str] = []
if lang == "python":
# Try pyright first (most comprehensive)
rc, out = _run_quietly(["pyright", "--outputjson", abs_path])
if rc != -1:
try:
data = json.loads(out)
diags = data.get("generalDiagnostics", [])
if not diags:
results.append("pyright: no diagnostics")
else:
lines = [f"pyright ({len(diags)} issue(s)):"]
for d in diags[:50]:
rng = d.get("range", {}).get("start", {})
ln = rng.get("line", 0) + 1
ch = rng.get("character", 0) + 1
sev = d.get("severity", "error")
msg = d.get("message", "")
rule = d.get("rule", "")
lines.append(f" {ln}:{ch} [{sev}] {msg}" + (f" ({rule})" if rule else ""))
results.append("\n".join(lines))
except json.JSONDecodeError:
if out:
results.append(f"pyright:\n{out[:3000]}")
else:
# Try mypy
rc2, out2 = _run_quietly(["mypy", "--no-error-summary", abs_path])
if rc2 != -1:
results.append(f"mypy:\n{out2[:3000]}" if out2 else "mypy: no diagnostics")
else:
# Fall back to flake8
rc3, out3 = _run_quietly(["flake8", abs_path])
if rc3 != -1:
results.append(f"flake8:\n{out3[:3000]}" if out3 else "flake8: no diagnostics")
else:
# Last resort: py_compile syntax check
rc4, out4 = _run_quietly(["python3", "-m", "py_compile", abs_path])
if out4:
results.append(f"py_compile (syntax check):\n{out4}")
else:
results.append("py_compile: syntax OK (no further tools available)")
elif lang in ("javascript", "typescript"):
# Try tsc
rc, out = _run_quietly(["tsc", "--noEmit", "--strict", abs_path])
if rc != -1:
results.append(f"tsc:\n{out[:3000]}" if out else "tsc: no errors")
else:
# Try eslint
rc2, out2 = _run_quietly(["eslint", abs_path])
if rc2 != -1:
results.append(f"eslint:\n{out2[:3000]}" if out2 else "eslint: no issues")
else:
results.append("No TypeScript/JavaScript checker found (install tsc or eslint)")
elif lang == "shellscript":
rc, out = _run_quietly(["shellcheck", abs_path])
if rc != -1:
results.append(f"shellcheck:\n{out[:3000]}" if out else "shellcheck: no issues")
else:
# Basic bash syntax check
rc2, out2 = _run_quietly(["bash", "-n", abs_path])
results.append(f"bash -n (syntax check):\n{out2}" if out2 else "bash -n: syntax OK")
else:
results.append(f"No diagnostic tool available for language: {lang or 'unknown'} (ext: {Path(file_path).suffix})")
return "\n\n".join(results) if results else "(no diagnostics output)"
# ── AskUserQuestion implementation ────────────────────────────────────────
def _ask_user_question(
question: str,
options: list[dict] | None = None,
allow_freetext: bool = True,
) -> str:
"""
Block the agent loop and surface a question to the user in the terminal.
The REPL loop (cheetahclaws.py) periodically calls drain_pending_questions()
to render any questions and collect answers. We use a threading.Event to
block this call until the user responds.
"""
event = threading.Event()
result_holder: list[str] = []
entry = {
"question": question,
"options": options or [],
"allow_freetext": allow_freetext,
"event": event,
"result": result_holder,
}
with _ask_lock:
_pending_questions.append(entry)
# Block until the REPL answers us
event.wait(timeout=300) # 5-minute max wait
if result_holder:
return result_holder[0]
return "(no answer — timeout)"
def ask_input_interactive(prompt: str, config: dict, menu_text: str = None) -> str:
"""Prompt the user for input, routing to Telegram if in a Telegram turn.
If menu_text is provided, it is sent ahead of the prompt."""
is_tg = _is_in_tg_turn(config)
if is_tg and "_tg_send_callback" in config:
token = config.get("telegram_token")
chat_id = config.get("telegram_chat_id")
import re, threading
clean_prompt = re.sub(r'\x1b\[[0-9;]*m', '', prompt).strip()
payload = ""
if menu_text:
clean_menu = re.sub(r'\x1b\[[0-9;]*m', '', menu_text).strip()
payload += f"{clean_menu}\n\n"
payload += f"❓ *Input Required*\n{clean_prompt}"
config["_tg_send_callback"](token, chat_id, payload)
evt = threading.Event()
config["_tg_input_event"] = evt
evt.wait()
text = config.pop("_tg_input_value", "").strip()
config.pop("_tg_input_event", None)
return text
else:
try:
return input(prompt)
except (KeyboardInterrupt, EOFError):
print()
return ""
def drain_pending_questions(config: dict) -> bool:
"""
Called by the REPL loop after each streaming turn.
Renders pending questions and collects user input.
Returns True if any questions were answered.
"""
with _ask_lock:
pending = list(_pending_questions)
_pending_questions.clear()
if not pending:
return False
for entry in pending:
question = entry["question"]
options = entry["options"]
allow_ft = entry["allow_freetext"]
event = entry["event"]
result = entry["result"]
print()
print("\033[1;35m❓ Question from assistant:\033[0m")
print(f" {question}")
if options:
print()
for i, opt in enumerate(options, 1):
label = opt.get("label", "")
desc = opt.get("description", "")
line = f" [{i}] {label}"
if desc:
line += f" — {desc}"
print(line)
if allow_ft:
print(" [0] Type a custom answer")
print()
while True:
raw = ask_input_interactive("Your choice (number or text): ", config).strip()
if not raw:
break
if raw.isdigit():
idx = int(raw)
if 1 <= idx <= len(options):
raw = options[idx - 1]["label"]
break
elif idx == 0 and allow_ft:
raw = ask_input_interactive("Your answer: ", config).strip()
break
else:
print(f"Invalid option: {idx}")
raw = ""
continue
elif allow_ft:
break # accept free text directly
else:
# Free-text only
print()
raw = ask_input_interactive("Your answer: ", config).strip()
result.append(raw)
event.set()
return True
def _sleeptimer(seconds: int, config: dict) -> str:
import threading
cb = config.get("_run_query_callback")
if not cb:
return "Error: Internal callback missing, cheetahclaws did not provide _run_query_callback"
def worker():
import time
time.sleep(seconds)
cb("(System Automated Event): The timer has finished. Please wake up, perform any pending monitoring checks and report to the user now.")
t = threading.Thread(target=worker, daemon=True)
t.start()
return f"Timer successfully scheduled for {seconds} seconds. You can output your final thoughts and end your turn. You will be automatically awakened."
# ── Dispatcher (backward-compatible wrapper) ──────────────────────────────
def execute_tool(
name: str,
inputs: dict,
permission_mode: str = "auto",
ask_permission: Optional[Callable[[str], bool]] = None,
config: dict = None,
) -> str:
"""Dispatch tool execution; ask permission for write/destructive ops.
Permission checking is done here, then delegation goes to the registry.
The config dict is forwarded to tool functions so they can access
runtime context like _depth, _system_prompt, model, etc.
"""
cfg = config or {}
def _check(desc: str) -> bool:
"""Return True if action is allowed."""
if permission_mode == "accept-all":
return True
if ask_permission:
return ask_permission(desc)
return True # headless: allow everything
# --- permission gate ---
if name == "Write":
if not _check(f"Write to {inputs['file_path']}"):
return "Denied: user rejected write operation"
elif name == "Edit":
if not _check(f"Edit {inputs['file_path']}"):
return "Denied: user rejected edit operation"
elif name == "Bash":
cmd = inputs["command"]
if permission_mode != "accept-all" and not _is_safe_bash(cmd):
if not _check(f"Bash: {cmd}"):
return "Denied: user rejected bash command"
elif name == "NotebookEdit":
if not _check(f"Edit notebook {inputs['notebook_path']}"):
return "Denied: user rejected notebook edit operation"
return _registry_execute(name, inputs, cfg)