lm-deluge 0.0.15__py3-none-any.whl → 0.0.17__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of lm-deluge might be problematic. Click here for more details.
- lm_deluge/api_requests/__init__.py +0 -2
- lm_deluge/api_requests/anthropic.py +58 -84
- lm_deluge/api_requests/base.py +43 -229
- lm_deluge/api_requests/bedrock.py +173 -195
- lm_deluge/api_requests/gemini.py +18 -44
- lm_deluge/api_requests/mistral.py +30 -60
- lm_deluge/api_requests/openai.py +147 -148
- lm_deluge/api_requests/response.py +2 -1
- lm_deluge/batches.py +1 -1
- lm_deluge/{computer_use/anthropic_tools.py → built_in_tools/anthropic/__init__.py} +58 -5
- lm_deluge/built_in_tools/anthropic/bash.py +0 -0
- lm_deluge/built_in_tools/anthropic/computer_use.py +0 -0
- lm_deluge/built_in_tools/anthropic/editor.py +559 -0
- lm_deluge/built_in_tools/base.py +9 -0
- lm_deluge/built_in_tools/openai.py +28 -0
- lm_deluge/client.py +304 -150
- lm_deluge/image.py +13 -8
- lm_deluge/llm_tools/extract.py +23 -4
- lm_deluge/llm_tools/ocr.py +1 -0
- lm_deluge/models.py +39 -2
- lm_deluge/prompt.py +43 -27
- lm_deluge/request_context.py +75 -0
- lm_deluge/tool.py +97 -15
- lm_deluge/tracker.py +1 -0
- {lm_deluge-0.0.15.dist-info → lm_deluge-0.0.17.dist-info}/METADATA +35 -1
- lm_deluge-0.0.17.dist-info/RECORD +52 -0
- lm_deluge-0.0.15.dist-info/RECORD +0 -45
- {lm_deluge-0.0.15.dist-info → lm_deluge-0.0.17.dist-info}/WHEEL +0 -0
- {lm_deluge-0.0.15.dist-info → lm_deluge-0.0.17.dist-info}/licenses/LICENSE +0 -0
- {lm_deluge-0.0.15.dist-info → lm_deluge-0.0.17.dist-info}/top_level.txt +0 -0
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
from typing import Literal
|
|
2
2
|
|
|
3
|
+
# from lm_deluge.prompt import ToolCall
|
|
4
|
+
|
|
3
5
|
ToolVersion = Literal["2024-10-22", "2025-01-24", "2025-04-29"]
|
|
4
6
|
ToolType = Literal["bash", "computer", "editor"]
|
|
5
7
|
|
|
@@ -11,14 +13,16 @@ def model_to_version(model: str) -> ToolVersion:
|
|
|
11
13
|
return "2025-04-29"
|
|
12
14
|
elif "3.7" in model:
|
|
13
15
|
return "2025-01-24"
|
|
14
|
-
|
|
16
|
+
elif "3.6" in model:
|
|
15
17
|
return "2024-10-22"
|
|
18
|
+
else:
|
|
19
|
+
raise ValueError("unsupported model for anthropic CUA")
|
|
16
20
|
|
|
17
21
|
|
|
18
22
|
def get_anthropic_cu_tools(
|
|
19
23
|
model: str,
|
|
20
|
-
display_width: int,
|
|
21
|
-
display_height: int,
|
|
24
|
+
display_width: int = 1024,
|
|
25
|
+
display_height: int = 768,
|
|
22
26
|
exclude_tools: list[ToolType] | None = None,
|
|
23
27
|
):
|
|
24
28
|
version = model_to_version(model)
|
|
@@ -31,8 +35,8 @@ def get_anthropic_cu_tools(
|
|
|
31
35
|
"display_height_px": display_height,
|
|
32
36
|
"display_number": None,
|
|
33
37
|
},
|
|
34
|
-
{"name": "str_replace_editor", "type": "
|
|
35
|
-
{"
|
|
38
|
+
{"name": "str_replace_editor", "type": "text_editor_20241022"},
|
|
39
|
+
{"name": "bash", "type": "bash_20241022"},
|
|
36
40
|
]
|
|
37
41
|
elif version == "2025-01-24":
|
|
38
42
|
result = [
|
|
@@ -73,3 +77,52 @@ def get_anthropic_cu_tools(
|
|
|
73
77
|
if "computer" in exclude_tools:
|
|
74
78
|
result = [x for x in result if "computer" not in x["name"]]
|
|
75
79
|
return result
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def bash_tool(model: str = "claude-4-sonnet"):
|
|
83
|
+
# Claude Sonnet 3.5 requires the computer-use-2024-10-22 beta header when using the bash tool.
|
|
84
|
+
# The bash tool is generally available in Claude 4 and Sonnet 3.7.
|
|
85
|
+
if "claude-4" in model:
|
|
86
|
+
return {"type": "text_editor_20250429", "name": "str_replace_based_edit_tool"}
|
|
87
|
+
elif "3.7" in model:
|
|
88
|
+
return {"type": "text_editor_20250124", "name": "str_replace_editor"}
|
|
89
|
+
else:
|
|
90
|
+
return {"type": "text_editor_20241022", "name": "str_replace_editor"}
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def text_editor_tool(model: str = "claude-4-sonnet"):
|
|
94
|
+
if "claude-4" in model:
|
|
95
|
+
return {"type": "bash_20250124", "name": "bash"}
|
|
96
|
+
elif "3.7" in model:
|
|
97
|
+
return {"type": "bash_20250124", "name": "bash"}
|
|
98
|
+
else:
|
|
99
|
+
return {"type": "bash_20241022", "name": "bash"}
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def web_search_tool(max_uses: int = 5):
|
|
103
|
+
res = {
|
|
104
|
+
"type": "web_search_20250305",
|
|
105
|
+
"name": "web_search",
|
|
106
|
+
# Optional: Limit the number of searches per request
|
|
107
|
+
"max_uses": max_uses,
|
|
108
|
+
# You can use either allowed_domains or blocked_domains, but not both in the same request.
|
|
109
|
+
# Optional: Only include results from these domains
|
|
110
|
+
# "allowed_domains": ["example.com", "trusteddomain.org"],
|
|
111
|
+
# Optional: Never include results from these domains
|
|
112
|
+
# "blocked_domains": ["untrustedsource.com"],
|
|
113
|
+
# Optional: Localize search results
|
|
114
|
+
# "user_location": {
|
|
115
|
+
# "type": "approximate",
|
|
116
|
+
# "city": "San Francisco",
|
|
117
|
+
# "region": "California",
|
|
118
|
+
# "country": "US",
|
|
119
|
+
# "timezone": "America/Los_Angeles"
|
|
120
|
+
# }
|
|
121
|
+
}
|
|
122
|
+
return res
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def code_execution_tool():
|
|
126
|
+
# The code execution tool is currently in beta.
|
|
127
|
+
# This feature requires the beta header: "anthropic-beta": "code-execution-2025-05-22"
|
|
128
|
+
return {"type": "code_execution_20250522", "name": "code_execution"}
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,559 @@
|
|
|
1
|
+
# from collections import defaultdict
|
|
2
|
+
# from pathlib import Path
|
|
3
|
+
# from typing import Any, Literal, get_args
|
|
4
|
+
|
|
5
|
+
# Command_20250124 = Literal[
|
|
6
|
+
# "view",
|
|
7
|
+
# "create",
|
|
8
|
+
# "str_replace",
|
|
9
|
+
# "insert",
|
|
10
|
+
# "undo_edit",
|
|
11
|
+
# ]
|
|
12
|
+
|
|
13
|
+
# Command_20250429 = Literal[
|
|
14
|
+
# "view",
|
|
15
|
+
# "create",
|
|
16
|
+
# "str_replace",
|
|
17
|
+
# "insert",
|
|
18
|
+
# ]
|
|
19
|
+
# SNIPPET_LINES: int = 4
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
# class EditTool:
|
|
23
|
+
# """
|
|
24
|
+
# An filesystem editor tool that allows the agent to view,
|
|
25
|
+
# create, and edit files. The tool parameters are defined by
|
|
26
|
+
# Anthropic and are not editable.
|
|
27
|
+
# """
|
|
28
|
+
|
|
29
|
+
# api_type: Literal["text_editor_20250124"] = "text_editor_20250124"
|
|
30
|
+
# name: Literal["str_replace_editor"] = "str_replace_editor"
|
|
31
|
+
|
|
32
|
+
# _file_history: dict[Path, list[str]]
|
|
33
|
+
|
|
34
|
+
# def __init__(self):
|
|
35
|
+
# self._file_history = defaultdict(list)
|
|
36
|
+
# super().__init__()
|
|
37
|
+
|
|
38
|
+
# def to_params(self) -> Any:
|
|
39
|
+
# return {
|
|
40
|
+
# "name": self.name,
|
|
41
|
+
# "type": self.api_type,
|
|
42
|
+
# }
|
|
43
|
+
|
|
44
|
+
# async def __call__(
|
|
45
|
+
# self,
|
|
46
|
+
# *,
|
|
47
|
+
# command: Command_20250124,
|
|
48
|
+
# path: str,
|
|
49
|
+
# file_text: str | None = None,
|
|
50
|
+
# view_range: list[int] | None = None,
|
|
51
|
+
# old_str: str | None = None,
|
|
52
|
+
# new_str: str | None = None,
|
|
53
|
+
# insert_line: int | None = None,
|
|
54
|
+
# **kwargs,
|
|
55
|
+
# ):
|
|
56
|
+
# _path = Path(path)
|
|
57
|
+
# self.validate_path(command, _path)
|
|
58
|
+
# if command == "view":
|
|
59
|
+
# return await self.view(_path, view_range)
|
|
60
|
+
# elif command == "create":
|
|
61
|
+
# if file_text is None:
|
|
62
|
+
# raise ToolError("Parameter `file_text` is required for command: create")
|
|
63
|
+
# self.write_file(_path, file_text)
|
|
64
|
+
# self._file_history[_path].append(file_text)
|
|
65
|
+
# return ToolResult(output=f"File created successfully at: {_path}")
|
|
66
|
+
# elif command == "str_replace":
|
|
67
|
+
# if old_str is None:
|
|
68
|
+
# raise ToolError(
|
|
69
|
+
# "Parameter `old_str` is required for command: str_replace"
|
|
70
|
+
# )
|
|
71
|
+
# return self.str_replace(_path, old_str, new_str)
|
|
72
|
+
# elif command == "insert":
|
|
73
|
+
# if insert_line is None:
|
|
74
|
+
# raise ToolError(
|
|
75
|
+
# "Parameter `insert_line` is required for command: insert"
|
|
76
|
+
# )
|
|
77
|
+
# if new_str is None:
|
|
78
|
+
# raise ToolError("Parameter `new_str` is required for command: insert")
|
|
79
|
+
# return self.insert(_path, insert_line, new_str)
|
|
80
|
+
# elif command == "undo_edit":
|
|
81
|
+
# return self.undo_edit(_path)
|
|
82
|
+
# raise ToolError(
|
|
83
|
+
# f"Unrecognized command {command}. The allowed commands for the {self.name} tool are: {', '.join(get_args(Command_20250124))}"
|
|
84
|
+
# )
|
|
85
|
+
|
|
86
|
+
# def validate_path(self, command: str, path: Path):
|
|
87
|
+
# """
|
|
88
|
+
# Check that the path/command combination is valid.
|
|
89
|
+
# """
|
|
90
|
+
# # Check if its an absolute path
|
|
91
|
+
# if not path.is_absolute():
|
|
92
|
+
# suggested_path = Path("") / path
|
|
93
|
+
# raise ToolError(
|
|
94
|
+
# f"The path {path} is not an absolute path, it should start with `/`. Maybe you meant {suggested_path}?"
|
|
95
|
+
# )
|
|
96
|
+
# # Check if path exists
|
|
97
|
+
# if not path.exists() and command != "create":
|
|
98
|
+
# raise ToolError(
|
|
99
|
+
# f"The path {path} does not exist. Please provide a valid path."
|
|
100
|
+
# )
|
|
101
|
+
# if path.exists() and command == "create":
|
|
102
|
+
# raise ToolError(
|
|
103
|
+
# f"File already exists at: {path}. Cannot overwrite files using command `create`."
|
|
104
|
+
# )
|
|
105
|
+
# # Check if the path points to a directory
|
|
106
|
+
# if path.is_dir():
|
|
107
|
+
# if command != "view":
|
|
108
|
+
# raise ToolError(
|
|
109
|
+
# f"The path {path} is a directory and only the `view` command can be used on directories"
|
|
110
|
+
# )
|
|
111
|
+
|
|
112
|
+
# async def view(self, path: Path, view_range: list[int] | None = None):
|
|
113
|
+
# """Implement the view command"""
|
|
114
|
+
# if path.is_dir():
|
|
115
|
+
# if view_range:
|
|
116
|
+
# raise ToolError(
|
|
117
|
+
# "The `view_range` parameter is not allowed when `path` points to a directory."
|
|
118
|
+
# )
|
|
119
|
+
|
|
120
|
+
# _, stdout, stderr = await run(
|
|
121
|
+
# rf"find {path} -maxdepth 2 -not -path '*/\.*'"
|
|
122
|
+
# )
|
|
123
|
+
# if not stderr:
|
|
124
|
+
# stdout = f"Here's the files and directories up to 2 levels deep in {path}, excluding hidden items:\n{stdout}\n"
|
|
125
|
+
# return CLIResult(output=stdout, error=stderr)
|
|
126
|
+
|
|
127
|
+
# file_content = self.read_file(path)
|
|
128
|
+
# init_line = 1
|
|
129
|
+
# if view_range:
|
|
130
|
+
# if len(view_range) != 2 or not all(isinstance(i, int) for i in view_range):
|
|
131
|
+
# raise ToolError(
|
|
132
|
+
# "Invalid `view_range`. It should be a list of two integers."
|
|
133
|
+
# )
|
|
134
|
+
# file_lines = file_content.split("\n")
|
|
135
|
+
# n_lines_file = len(file_lines)
|
|
136
|
+
# init_line, final_line = view_range
|
|
137
|
+
# if init_line < 1 or init_line > n_lines_file:
|
|
138
|
+
# raise ToolError(
|
|
139
|
+
# f"Invalid `view_range`: {view_range}. Its first element `{init_line}` should be within the range of lines of the file: {[1, n_lines_file]}"
|
|
140
|
+
# )
|
|
141
|
+
# if final_line > n_lines_file:
|
|
142
|
+
# raise ToolError(
|
|
143
|
+
# f"Invalid `view_range`: {view_range}. Its second element `{final_line}` should be smaller than the number of lines in the file: `{n_lines_file}`"
|
|
144
|
+
# )
|
|
145
|
+
# if final_line != -1 and final_line < init_line:
|
|
146
|
+
# raise ToolError(
|
|
147
|
+
# f"Invalid `view_range`: {view_range}. Its second element `{final_line}` should be larger or equal than its first `{init_line}`"
|
|
148
|
+
# )
|
|
149
|
+
|
|
150
|
+
# if final_line == -1:
|
|
151
|
+
# file_content = "\n".join(file_lines[init_line - 1 :])
|
|
152
|
+
# else:
|
|
153
|
+
# file_content = "\n".join(file_lines[init_line - 1 : final_line])
|
|
154
|
+
|
|
155
|
+
# return CLIResult(
|
|
156
|
+
# output=self._make_output(file_content, str(path), init_line=init_line)
|
|
157
|
+
# )
|
|
158
|
+
|
|
159
|
+
# def str_replace(self, path: Path, old_str: str, new_str: str | None):
|
|
160
|
+
# """Implement the str_replace command, which replaces old_str with new_str in the file content"""
|
|
161
|
+
# # Read the file content
|
|
162
|
+
# file_content = self.read_file(path).expandtabs()
|
|
163
|
+
# old_str = old_str.expandtabs()
|
|
164
|
+
# new_str = new_str.expandtabs() if new_str is not None else ""
|
|
165
|
+
|
|
166
|
+
# # Check if old_str is unique in the file
|
|
167
|
+
# occurrences = file_content.count(old_str)
|
|
168
|
+
# if occurrences == 0:
|
|
169
|
+
# raise ToolError(
|
|
170
|
+
# f"No replacement was performed, old_str `{old_str}` did not appear verbatim in {path}."
|
|
171
|
+
# )
|
|
172
|
+
# elif occurrences > 1:
|
|
173
|
+
# file_content_lines = file_content.split("\n")
|
|
174
|
+
# lines = [
|
|
175
|
+
# idx + 1
|
|
176
|
+
# for idx, line in enumerate(file_content_lines)
|
|
177
|
+
# if old_str in line
|
|
178
|
+
# ]
|
|
179
|
+
# raise ToolError(
|
|
180
|
+
# f"No replacement was performed. Multiple occurrences of old_str `{old_str}` in lines {lines}. Please ensure it is unique"
|
|
181
|
+
# )
|
|
182
|
+
|
|
183
|
+
# # Replace old_str with new_str
|
|
184
|
+
# new_file_content = file_content.replace(old_str, new_str)
|
|
185
|
+
|
|
186
|
+
# # Write the new content to the file
|
|
187
|
+
# self.write_file(path, new_file_content)
|
|
188
|
+
|
|
189
|
+
# # Save the content to history
|
|
190
|
+
# self._file_history[path].append(file_content)
|
|
191
|
+
|
|
192
|
+
# # Create a snippet of the edited section
|
|
193
|
+
# replacement_line = file_content.split(old_str)[0].count("\n")
|
|
194
|
+
# start_line = max(0, replacement_line - SNIPPET_LINES)
|
|
195
|
+
# end_line = replacement_line + SNIPPET_LINES + new_str.count("\n")
|
|
196
|
+
# snippet = "\n".join(new_file_content.split("\n")[start_line : end_line + 1])
|
|
197
|
+
|
|
198
|
+
# # Prepare the success message
|
|
199
|
+
# success_msg = f"The file {path} has been edited. "
|
|
200
|
+
# success_msg += self._make_output(
|
|
201
|
+
# snippet, f"a snippet of {path}", start_line + 1
|
|
202
|
+
# )
|
|
203
|
+
# success_msg += "Review the changes and make sure they are as expected. Edit the file again if necessary."
|
|
204
|
+
|
|
205
|
+
# return CLIResult(output=success_msg)
|
|
206
|
+
|
|
207
|
+
# def insert(self, path: Path, insert_line: int, new_str: str):
|
|
208
|
+
# """Implement the insert command, which inserts new_str at the specified line in the file content."""
|
|
209
|
+
# file_text = self.read_file(path).expandtabs()
|
|
210
|
+
# new_str = new_str.expandtabs()
|
|
211
|
+
# file_text_lines = file_text.split("\n")
|
|
212
|
+
# n_lines_file = len(file_text_lines)
|
|
213
|
+
|
|
214
|
+
# if insert_line < 0 or insert_line > n_lines_file:
|
|
215
|
+
# raise ToolError(
|
|
216
|
+
# f"Invalid `insert_line` parameter: {insert_line}. It should be within the range of lines of the file: {[0, n_lines_file]}"
|
|
217
|
+
# )
|
|
218
|
+
|
|
219
|
+
# new_str_lines = new_str.split("\n")
|
|
220
|
+
# new_file_text_lines = (
|
|
221
|
+
# file_text_lines[:insert_line]
|
|
222
|
+
# + new_str_lines
|
|
223
|
+
# + file_text_lines[insert_line:]
|
|
224
|
+
# )
|
|
225
|
+
# snippet_lines = (
|
|
226
|
+
# file_text_lines[max(0, insert_line - SNIPPET_LINES) : insert_line]
|
|
227
|
+
# + new_str_lines
|
|
228
|
+
# + file_text_lines[insert_line : insert_line + SNIPPET_LINES]
|
|
229
|
+
# )
|
|
230
|
+
|
|
231
|
+
# new_file_text = "\n".join(new_file_text_lines)
|
|
232
|
+
# snippet = "\n".join(snippet_lines)
|
|
233
|
+
|
|
234
|
+
# self.write_file(path, new_file_text)
|
|
235
|
+
# self._file_history[path].append(file_text)
|
|
236
|
+
|
|
237
|
+
# success_msg = f"The file {path} has been edited. "
|
|
238
|
+
# success_msg += self._make_output(
|
|
239
|
+
# snippet,
|
|
240
|
+
# "a snippet of the edited file",
|
|
241
|
+
# max(1, insert_line - SNIPPET_LINES + 1),
|
|
242
|
+
# )
|
|
243
|
+
# success_msg += "Review the changes and make sure they are as expected (correct indentation, no duplicate lines, etc). Edit the file again if necessary."
|
|
244
|
+
# return CLIResult(output=success_msg)
|
|
245
|
+
|
|
246
|
+
# def undo_edit(self, path: Path):
|
|
247
|
+
# """Implement the undo_edit command."""
|
|
248
|
+
# if not self._file_history[path]:
|
|
249
|
+
# raise ToolError(f"No edit history found for {path}.")
|
|
250
|
+
|
|
251
|
+
# old_text = self._file_history[path].pop()
|
|
252
|
+
# self.write_file(path, old_text)
|
|
253
|
+
|
|
254
|
+
# return CLIResult(
|
|
255
|
+
# output=f"Last edit to {path} undone successfully. {self._make_output(old_text, str(path))}"
|
|
256
|
+
# )
|
|
257
|
+
|
|
258
|
+
# def read_file(self, path: Path):
|
|
259
|
+
# """Read the content of a file from a given path; raise a ToolError if an error occurs."""
|
|
260
|
+
# try:
|
|
261
|
+
# return path.read_text()
|
|
262
|
+
# except Exception as e:
|
|
263
|
+
# raise ToolError(f"Ran into {e} while trying to read {path}") from None
|
|
264
|
+
|
|
265
|
+
# def write_file(self, path: Path, file: str):
|
|
266
|
+
# """Write the content of a file to a given path; raise a ToolError if an error occurs."""
|
|
267
|
+
# try:
|
|
268
|
+
# path.write_text(file)
|
|
269
|
+
# except Exception as e:
|
|
270
|
+
# raise ToolError(f"Ran into {e} while trying to write to {path}") from None
|
|
271
|
+
|
|
272
|
+
# def _make_output(
|
|
273
|
+
# self,
|
|
274
|
+
# file_content: str,
|
|
275
|
+
# file_descriptor: str,
|
|
276
|
+
# init_line: int = 1,
|
|
277
|
+
# expand_tabs: bool = True,
|
|
278
|
+
# ):
|
|
279
|
+
# """Generate output for the CLI based on the content of a file."""
|
|
280
|
+
# file_content = maybe_truncate(file_content)
|
|
281
|
+
# if expand_tabs:
|
|
282
|
+
# file_content = file_content.expandtabs()
|
|
283
|
+
# file_content = "\n".join(
|
|
284
|
+
# [
|
|
285
|
+
# f"{i + init_line:6}\t{line}"
|
|
286
|
+
# for i, line in enumerate(file_content.split("\n"))
|
|
287
|
+
# ]
|
|
288
|
+
# )
|
|
289
|
+
# return (
|
|
290
|
+
# f"Here's the result of running `cat -n` on {file_descriptor}:\n"
|
|
291
|
+
# + file_content
|
|
292
|
+
# + "\n"
|
|
293
|
+
# )
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
# class EditTool20250429(BaseAnthropicTool):
|
|
297
|
+
# """
|
|
298
|
+
# An filesystem editor tool that allows the agent to view, create, and edit files.
|
|
299
|
+
# The tool parameters are defined by Anthropic and are not editable.
|
|
300
|
+
# """
|
|
301
|
+
|
|
302
|
+
# api_type: Literal["str_replace_based_edit_tool"] = "str_replace_based_edit_tool"
|
|
303
|
+
# name: Literal["str_replace_based_edit_tool"] = "str_replace_based_edit_tool"
|
|
304
|
+
|
|
305
|
+
# _file_history: dict[Path, list[str]]
|
|
306
|
+
|
|
307
|
+
# def __init__(self):
|
|
308
|
+
# self._file_history = defaultdict(list)
|
|
309
|
+
# super().__init__()
|
|
310
|
+
|
|
311
|
+
# def to_params(self) -> Any:
|
|
312
|
+
# return {
|
|
313
|
+
# "name": self.name,
|
|
314
|
+
# "type": self.api_type,
|
|
315
|
+
# }
|
|
316
|
+
|
|
317
|
+
# async def __call__(
|
|
318
|
+
# self,
|
|
319
|
+
# *,
|
|
320
|
+
# command: Command_20250429,
|
|
321
|
+
# path: str,
|
|
322
|
+
# file_text: str | None = None,
|
|
323
|
+
# view_range: list[int] | None = None,
|
|
324
|
+
# old_str: str | None = None,
|
|
325
|
+
# new_str: str | None = None,
|
|
326
|
+
# insert_line: int | None = None,
|
|
327
|
+
# **kwargs,
|
|
328
|
+
# ):
|
|
329
|
+
# _path = Path(path)
|
|
330
|
+
# self.validate_path(command, _path)
|
|
331
|
+
# if command == "view":
|
|
332
|
+
# return await self.view(_path, view_range)
|
|
333
|
+
# elif command == "create":
|
|
334
|
+
# if file_text is None:
|
|
335
|
+
# raise ToolError("Parameter `file_text` is required for command: create")
|
|
336
|
+
# self.write_file(_path, file_text)
|
|
337
|
+
# self._file_history[_path].append(file_text)
|
|
338
|
+
# return ToolResult(output=f"File created successfully at: {_path}")
|
|
339
|
+
# elif command == "str_replace":
|
|
340
|
+
# if old_str is None:
|
|
341
|
+
# raise ToolError(
|
|
342
|
+
# "Parameter `old_str` is required for command: str_replace"
|
|
343
|
+
# )
|
|
344
|
+
# return self.str_replace(_path, old_str, new_str)
|
|
345
|
+
# elif command == "insert":
|
|
346
|
+
# if insert_line is None:
|
|
347
|
+
# raise ToolError(
|
|
348
|
+
# "Parameter `insert_line` is required for command: insert"
|
|
349
|
+
# )
|
|
350
|
+
# if new_str is None:
|
|
351
|
+
# raise ToolError("Parameter `new_str` is required for command: insert")
|
|
352
|
+
# return self.insert(_path, insert_line, new_str)
|
|
353
|
+
# # Note: undo_edit command was removed in this version
|
|
354
|
+
# raise ToolError(
|
|
355
|
+
# f"Unrecognized command {command}. The allowed commands for the {self.name} tool are: {', '.join(get_args(Command_20250429))}"
|
|
356
|
+
# )
|
|
357
|
+
|
|
358
|
+
# def validate_path(self, command: str, path: Path):
|
|
359
|
+
# """
|
|
360
|
+
# Check that the path/command combination is valid.
|
|
361
|
+
# """
|
|
362
|
+
# # Check if its an absolute path
|
|
363
|
+
# if not path.is_absolute():
|
|
364
|
+
# suggested_path = Path("") / path
|
|
365
|
+
# raise ToolError(
|
|
366
|
+
# f"The path {path} is not an absolute path, it should start with `/`. Maybe you meant {suggested_path}?"
|
|
367
|
+
# )
|
|
368
|
+
# # Check if path exists
|
|
369
|
+
# if not path.exists() and command != "create":
|
|
370
|
+
# raise ToolError(
|
|
371
|
+
# f"The path {path} does not exist. Please provide a valid path."
|
|
372
|
+
# )
|
|
373
|
+
# if path.exists() and command == "create":
|
|
374
|
+
# raise ToolError(
|
|
375
|
+
# f"File already exists at: {path}. Cannot overwrite files using command `create`."
|
|
376
|
+
# )
|
|
377
|
+
# # Check if the path points to a directory
|
|
378
|
+
# if path.is_dir():
|
|
379
|
+
# if command != "view":
|
|
380
|
+
# raise ToolError(
|
|
381
|
+
# f"The path {path} is a directory and only the `view` command can be used on directories"
|
|
382
|
+
# )
|
|
383
|
+
|
|
384
|
+
# async def view(self, path: Path, view_range: list[int] | None = None):
|
|
385
|
+
# """Implement the view command"""
|
|
386
|
+
# if path.is_dir():
|
|
387
|
+
# if view_range:
|
|
388
|
+
# raise ToolError(
|
|
389
|
+
# "The `view_range` parameter is not allowed when `path` points to a directory."
|
|
390
|
+
# )
|
|
391
|
+
|
|
392
|
+
# _, stdout, stderr = await run(
|
|
393
|
+
# rf"find {path} -maxdepth 2 -not -path '*/\.*'"
|
|
394
|
+
# )
|
|
395
|
+
# if not stderr:
|
|
396
|
+
# stdout = f"Here's the files and directories up to 2 levels deep in {path}, excluding hidden items:\n{stdout}\n"
|
|
397
|
+
# return CLIResult(output=stdout, error=stderr)
|
|
398
|
+
|
|
399
|
+
# file_content = self.read_file(path)
|
|
400
|
+
# init_line = 1
|
|
401
|
+
# if view_range:
|
|
402
|
+
# if len(view_range) != 2 or not all(isinstance(i, int) for i in view_range):
|
|
403
|
+
# raise ToolError(
|
|
404
|
+
# "Invalid `view_range`. It should be a list of two integers."
|
|
405
|
+
# )
|
|
406
|
+
# file_lines = file_content.split("\n")
|
|
407
|
+
# n_lines_file = len(file_lines)
|
|
408
|
+
# init_line, final_line = view_range
|
|
409
|
+
# if init_line < 1 or init_line > n_lines_file:
|
|
410
|
+
# raise ToolError(
|
|
411
|
+
# f"Invalid `view_range`: {view_range}. Its first element `{init_line}` should be within the range of lines of the file: {[1, n_lines_file]}"
|
|
412
|
+
# )
|
|
413
|
+
# if final_line > n_lines_file:
|
|
414
|
+
# raise ToolError(
|
|
415
|
+
# f"Invalid `view_range`: {view_range}. Its second element `{final_line}` should be smaller than the number of lines in the file: `{n_lines_file}`"
|
|
416
|
+
# )
|
|
417
|
+
# if final_line != -1 and final_line < init_line:
|
|
418
|
+
# raise ToolError(
|
|
419
|
+
# f"Invalid `view_range`: {view_range}. Its second element `{final_line}` should be larger or equal than its first `{init_line}`"
|
|
420
|
+
# )
|
|
421
|
+
|
|
422
|
+
# if final_line == -1:
|
|
423
|
+
# file_content = "\n".join(file_lines[init_line - 1 :])
|
|
424
|
+
# else:
|
|
425
|
+
# file_content = "\n".join(file_lines[init_line - 1 : final_line])
|
|
426
|
+
|
|
427
|
+
# return CLIResult(
|
|
428
|
+
# output=self._make_output(file_content, str(path), init_line=init_line)
|
|
429
|
+
# )
|
|
430
|
+
|
|
431
|
+
# def str_replace(self, path: Path, old_str: str, new_str: str | None):
|
|
432
|
+
# """Implement the str_replace command, which replaces old_str with new_str in the file content"""
|
|
433
|
+
# # Read the file content
|
|
434
|
+
# file_content = self.read_file(path).expandtabs()
|
|
435
|
+
# old_str = old_str.expandtabs()
|
|
436
|
+
# new_str = new_str.expandtabs() if new_str is not None else ""
|
|
437
|
+
|
|
438
|
+
# # Check if old_str is unique in the file
|
|
439
|
+
# occurrences = file_content.count(old_str)
|
|
440
|
+
# if occurrences == 0:
|
|
441
|
+
# raise ToolError(
|
|
442
|
+
# f"No replacement was performed, old_str `{old_str}` did not appear verbatim in {path}."
|
|
443
|
+
# )
|
|
444
|
+
# elif occurrences > 1:
|
|
445
|
+
# file_content_lines = file_content.split("\n")
|
|
446
|
+
# lines = [
|
|
447
|
+
# idx + 1
|
|
448
|
+
# for idx, line in enumerate(file_content_lines)
|
|
449
|
+
# if old_str in line
|
|
450
|
+
# ]
|
|
451
|
+
# raise ToolError(
|
|
452
|
+
# f"No replacement was performed. Multiple occurrences of old_str `{old_str}` in lines {lines}. Please ensure it is unique"
|
|
453
|
+
# )
|
|
454
|
+
|
|
455
|
+
# # Replace old_str with new_str
|
|
456
|
+
# new_file_content = file_content.replace(old_str, new_str)
|
|
457
|
+
|
|
458
|
+
# # Write the new content to the file
|
|
459
|
+
# self.write_file(path, new_file_content)
|
|
460
|
+
|
|
461
|
+
# # Save the content to history
|
|
462
|
+
# self._file_history[path].append(file_content)
|
|
463
|
+
|
|
464
|
+
# # Create a snippet of the edited section
|
|
465
|
+
# replacement_line = file_content.split(old_str)[0].count("\n")
|
|
466
|
+
# start_line = max(0, replacement_line - SNIPPET_LINES)
|
|
467
|
+
# end_line = replacement_line + SNIPPET_LINES + new_str.count("\n")
|
|
468
|
+
# snippet = "\n".join(new_file_content.split("\n")[start_line : end_line + 1])
|
|
469
|
+
|
|
470
|
+
# # Prepare the success message
|
|
471
|
+
# success_msg = f"The file {path} has been edited. "
|
|
472
|
+
# success_msg += self._make_output(
|
|
473
|
+
# snippet, f"a snippet of {path}", start_line + 1
|
|
474
|
+
# )
|
|
475
|
+
# success_msg += "Review the changes and make sure they are as expected. Edit the file again if necessary."
|
|
476
|
+
|
|
477
|
+
# return CLIResult(output=success_msg)
|
|
478
|
+
|
|
479
|
+
# def insert(self, path: Path, insert_line: int, new_str: str):
|
|
480
|
+
# """Implement the insert command, which inserts new_str at the specified line in the file content."""
|
|
481
|
+
# file_text = self.read_file(path).expandtabs()
|
|
482
|
+
# new_str = new_str.expandtabs()
|
|
483
|
+
# file_text_lines = file_text.split("\n")
|
|
484
|
+
# n_lines_file = len(file_text_lines)
|
|
485
|
+
|
|
486
|
+
# if insert_line < 0 or insert_line > n_lines_file:
|
|
487
|
+
# raise ToolError(
|
|
488
|
+
# f"Invalid `insert_line` parameter: {insert_line}. It should be within the range of lines of the file: {[0, n_lines_file]}"
|
|
489
|
+
# )
|
|
490
|
+
|
|
491
|
+
# new_str_lines = new_str.split("\n")
|
|
492
|
+
# new_file_text_lines = (
|
|
493
|
+
# file_text_lines[:insert_line]
|
|
494
|
+
# + new_str_lines
|
|
495
|
+
# + file_text_lines[insert_line:]
|
|
496
|
+
# )
|
|
497
|
+
# snippet_lines = (
|
|
498
|
+
# file_text_lines[max(0, insert_line - SNIPPET_LINES) : insert_line]
|
|
499
|
+
# + new_str_lines
|
|
500
|
+
# + file_text_lines[insert_line : insert_line + SNIPPET_LINES]
|
|
501
|
+
# )
|
|
502
|
+
|
|
503
|
+
# new_file_text = "\n".join(new_file_text_lines)
|
|
504
|
+
# snippet = "\n".join(snippet_lines)
|
|
505
|
+
|
|
506
|
+
# self.write_file(path, new_file_text)
|
|
507
|
+
# self._file_history[path].append(file_text)
|
|
508
|
+
|
|
509
|
+
# success_msg = f"The file {path} has been edited. "
|
|
510
|
+
# success_msg += self._make_output(
|
|
511
|
+
# snippet,
|
|
512
|
+
# "a snippet of the edited file",
|
|
513
|
+
# max(1, insert_line - SNIPPET_LINES + 1),
|
|
514
|
+
# )
|
|
515
|
+
# success_msg += "Review the changes and make sure they are as expected (correct indentation, no duplicate lines, etc). Edit the file again if necessary."
|
|
516
|
+
# return CLIResult(output=success_msg)
|
|
517
|
+
|
|
518
|
+
# # Note: undo_edit method is not implemented in this version as it was removed
|
|
519
|
+
|
|
520
|
+
# def read_file(self, path: Path):
|
|
521
|
+
# """Read the content of a file from a given path; raise a ToolError if an error occurs."""
|
|
522
|
+
# try:
|
|
523
|
+
# return path.read_text()
|
|
524
|
+
# except Exception as e:
|
|
525
|
+
# raise ToolError(f"Ran into {e} while trying to read {path}") from None
|
|
526
|
+
|
|
527
|
+
# def write_file(self, path: Path, file: str):
|
|
528
|
+
# """Write the content of a file to a given path; raise a ToolError if an error occurs."""
|
|
529
|
+
# try:
|
|
530
|
+
# path.write_text(file)
|
|
531
|
+
# except Exception as e:
|
|
532
|
+
# raise ToolError(f"Ran into {e} while trying to write to {path}") from None
|
|
533
|
+
|
|
534
|
+
# def _make_output(
|
|
535
|
+
# self,
|
|
536
|
+
# file_content: str,
|
|
537
|
+
# file_descriptor: str,
|
|
538
|
+
# init_line: int = 1,
|
|
539
|
+
# expand_tabs: bool = True,
|
|
540
|
+
# ):
|
|
541
|
+
# """Generate output for the CLI based on the content of a file."""
|
|
542
|
+
# file_content = maybe_truncate(file_content)
|
|
543
|
+
# if expand_tabs:
|
|
544
|
+
# file_content = file_content.expandtabs()
|
|
545
|
+
# file_content = "\n".join(
|
|
546
|
+
# [
|
|
547
|
+
# f"{i + init_line:6}\t{line}"
|
|
548
|
+
# for i, line in enumerate(file_content.split("\n"))
|
|
549
|
+
# ]
|
|
550
|
+
# )
|
|
551
|
+
# return (
|
|
552
|
+
# f"Here's the result of running `cat -n` on {file_descriptor}:\n"
|
|
553
|
+
# + file_content
|
|
554
|
+
# + "\n"
|
|
555
|
+
# )
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
# class EditTool20241022(EditTool20250124):
|
|
559
|
+
# api_type: Literal["text_editor_20250429"] = "text_editor_20250429" # pyright: ignore[reportIncompatibleVariableOverride]
|