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
|
from pathlib import Path
from typing import Optional, Callable, Dict, List
from dataclasses import dataclass
import subprocess
import re
from llm import AgentConfig, JSON
from web_utils import fetch_and_markdownify, FetchSuccess, FetchError
@dataclass
class Action:
"""Class for LLM actions.
Should only be called on JSON dicts where "action" -> `name`.
"""
name: str
description: str
handler: Callable[[JSON, AgentConfig], JSON]
required_keys: List[str]
formatter: Callable[[JSON], str] = lambda args: (
f"{args['action']} with "
+ ",".join([f"{arg} as {args[arg]}" for arg in args if arg != "action"])
)
def __call__(self, data: JSON, config: AgentConfig) -> JSON:
return self.handler(data, config)
def _path_checked(path: Path, cwd=Path.cwd()) -> Optional[Path]:
"""Resolves `path`, returning `None` if it's not a subdirectory of `cwd`."""
path = (path if path.is_absolute() else (cwd / path)).resolve()
cwd = cwd.resolve()
return path if path.is_relative_to(cwd) else None
def _path_helper(path_str: str, callback, cwd=Path.cwd()):
path = _path_checked(Path(path_str), cwd=cwd)
if path is None:
return {"status": "error", "reason": f"Permission denied: {path_str}"}
else:
try:
return {"status": "success", **callback(path)}
except Exception as e:
return {"status": "error", "reason": f"Failure: {e}"}
def _ls(args: JSON, config: AgentConfig) -> JSON:
'{"action": "ls", "path": PATH} - list files at PATH'
def inner(path: Path):
return {"content": list(map(lambda s: str(s), path.iterdir()))}
return _path_helper(args["path"], inner, cwd=config.workdir)
def _read(args: JSON, config: AgentConfig) -> JSON:
'{"action": "read", "path": PATH} - read file at PATH'
def inner(path: Path):
contents = path.read_text(encoding="utf-8")
return {"content": contents}
return _path_helper(args["path"], inner, cwd=config.workdir)
def _write(args: JSON, config: AgentConfig) -> JSON:
'{"action": "write", "path": PATH, "content": CONTENT} - write CONTENT into file at PATH'
def inner(path: Path, text: str):
n_bytes = path.write_text(text)
return {"bytes": n_bytes}
return _path_helper(
args["path"], lambda path: inner(path, args["content"]), cwd=config.workdir
)
def _tldr(args: JSON, config: AgentConfig) -> JSON:
'{"action": "tldr", "program": PROGRAM} - read tldr docs for PROGRAM'
try:
result = subprocess.run(
["tldr", args["program"]], capture_output=True, text=True, check=True
)
return {"status": "success", "content": result.stdout}
except subprocess.CalledProcessError:
return {"status": "error", "reason": f"Tldr for '{args['program']}' not found."}
except Exception as e:
return {"status": "error", "reason": f"An unexpected error occurred: {e}"}
def _shell(args: JSON, config: AgentConfig) -> JSON:
'{"action": "shell", "command": CMD} - Propose executing a shell command, but interrupts to ask for permission from the user.'
res = subprocess.run(
args["command"],
shell=True,
cwd=config.workdir,
capture_output=True,
text=True,
)
if res.returncode == 0:
return {"status": "success", "stdout": res.stdout}
else:
return {
"status": "error",
"reason": f"Shell command exited with code {res.returncode}",
"stdout": res.stdout,
"stderr": res.stderr,
}
def _mypy(args: JSON, config: AgentConfig) -> JSON:
'{"action": "mypy", "path": PATH} - run mypy type checking on PATH'
def inner(path: Path):
try:
res = subprocess.run(
["mypy", str(path)], cwd=config.workdir, capture_output=True, text=True
)
if res.returncode == 0:
return {"status": "success", "stdout": res.stdout}
else:
return {"status": "error", "stderr": res.stderr}
except Exception as e:
return {"status": "error", "reason": f"An unexpected error occurred: {e}"}
return _path_helper(args["path"], inner, cwd=config.workdir)
def _webgrep(args: JSON, config: AgentConfig) -> JSON:
'{"action": "webgrep", "url": URL, "keywords": KEYWORD_LIST} - find case-insensitive matches in markdownified content of URL for keywords in KEYWORD_LIST plus some context on both sides'
match fetch_and_markdownify(args["url"]):
case FetchError(e):
return {"status": "error", "reason": e}
case FetchSuccess(content):
matches = map(
lambda m: content[
max(0, m.start() - config.search_padding) : min(
len(content), m.end() + config.search_padding
)
],
re.finditer(
"|".join(map(lambda kw: re.escape(kw), args["keywords"])),
content,
re.IGNORECASE,
),
)
return {"status": "success", "matches": list(matches)}
ls = Action("ls", _ls.__doc__ or "", _ls, ["path"])
read = Action("read", _read.__doc__ or "", _read, ["path"])
write = Action(
"write",
_write.__doc__ or "",
_write,
["path", "content"],
formatter=lambda args: f"write {args['path']}",
)
tldr = Action("tldr", _tldr.__doc__ or "", _tldr, ["program"])
shell = Action(
"shell",
_shell.__doc__ or "",
_shell,
["command"],
formatter=lambda args: f"shell command `{args['command']}`",
)
mypy = Action(
"mypy",
_mypy.__doc__ or "",
_mypy,
["path"],
formatter=lambda args: f"type check {args['path']}",
)
webgrep = Action(
"webgrep",
_webgrep.__doc__ or "",
_webgrep,
["url", "keywords"],
formatter=lambda args: f"search {args['url']}",
)
|