cua-agent 0.1.0__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 cua-agent might be problematic. Click here for more details.
- agent/README.md +63 -0
- agent/__init__.py +10 -0
- agent/core/README.md +101 -0
- agent/core/__init__.py +34 -0
- agent/core/agent.py +284 -0
- agent/core/base_agent.py +164 -0
- agent/core/callbacks.py +147 -0
- agent/core/computer_agent.py +69 -0
- agent/core/experiment.py +222 -0
- agent/core/factory.py +102 -0
- agent/core/loop.py +244 -0
- agent/core/messages.py +230 -0
- agent/core/tools/__init__.py +21 -0
- agent/core/tools/base.py +74 -0
- agent/core/tools/bash.py +52 -0
- agent/core/tools/collection.py +46 -0
- agent/core/tools/computer.py +113 -0
- agent/core/tools/edit.py +67 -0
- agent/core/tools/manager.py +56 -0
- agent/providers/__init__.py +4 -0
- agent/providers/anthropic/__init__.py +6 -0
- agent/providers/anthropic/api/client.py +222 -0
- agent/providers/anthropic/api/logging.py +150 -0
- agent/providers/anthropic/callbacks/manager.py +55 -0
- agent/providers/anthropic/loop.py +521 -0
- agent/providers/anthropic/messages/manager.py +110 -0
- agent/providers/anthropic/prompts.py +20 -0
- agent/providers/anthropic/tools/__init__.py +33 -0
- agent/providers/anthropic/tools/base.py +88 -0
- agent/providers/anthropic/tools/bash.py +163 -0
- agent/providers/anthropic/tools/collection.py +34 -0
- agent/providers/anthropic/tools/computer.py +550 -0
- agent/providers/anthropic/tools/edit.py +326 -0
- agent/providers/anthropic/tools/manager.py +54 -0
- agent/providers/anthropic/tools/run.py +42 -0
- agent/providers/anthropic/types.py +16 -0
- agent/providers/omni/__init__.py +27 -0
- agent/providers/omni/callbacks.py +78 -0
- agent/providers/omni/clients/anthropic.py +99 -0
- agent/providers/omni/clients/base.py +44 -0
- agent/providers/omni/clients/groq.py +101 -0
- agent/providers/omni/clients/openai.py +159 -0
- agent/providers/omni/clients/utils.py +25 -0
- agent/providers/omni/experiment.py +273 -0
- agent/providers/omni/image_utils.py +106 -0
- agent/providers/omni/loop.py +961 -0
- agent/providers/omni/messages.py +168 -0
- agent/providers/omni/parser.py +252 -0
- agent/providers/omni/prompts.py +78 -0
- agent/providers/omni/tool_manager.py +91 -0
- agent/providers/omni/tools/__init__.py +13 -0
- agent/providers/omni/tools/bash.py +69 -0
- agent/providers/omni/tools/computer.py +216 -0
- agent/providers/omni/tools/manager.py +83 -0
- agent/providers/omni/types.py +30 -0
- agent/providers/omni/utils.py +155 -0
- agent/providers/omni/visualization.py +130 -0
- agent/types/__init__.py +26 -0
- agent/types/base.py +52 -0
- agent/types/messages.py +36 -0
- agent/types/tools.py +32 -0
- cua_agent-0.1.0.dist-info/METADATA +44 -0
- cua_agent-0.1.0.dist-info/RECORD +65 -0
- cua_agent-0.1.0.dist-info/WHEEL +4 -0
- cua_agent-0.1.0.dist-info/entry_points.txt +4 -0
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
from collections import defaultdict
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from typing import Literal, get_args, Dict, Any
|
|
4
|
+
from computer.computer import Computer
|
|
5
|
+
|
|
6
|
+
from .base import BaseAnthropicTool, CLIResult, ToolError, ToolResult
|
|
7
|
+
from ....core.tools.edit import BaseEditTool
|
|
8
|
+
from .run import maybe_truncate
|
|
9
|
+
|
|
10
|
+
Command = Literal[
|
|
11
|
+
"view",
|
|
12
|
+
"create",
|
|
13
|
+
"str_replace",
|
|
14
|
+
"insert",
|
|
15
|
+
"undo_edit",
|
|
16
|
+
]
|
|
17
|
+
SNIPPET_LINES: int = 4
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class EditTool(BaseEditTool, BaseAnthropicTool):
|
|
21
|
+
"""
|
|
22
|
+
An filesystem editor tool that allows the agent to view, create, and edit files.
|
|
23
|
+
The tool parameters are defined by Anthropic and are not editable.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
api_type: Literal["text_editor_20250124"] = "text_editor_20250124"
|
|
27
|
+
name: Literal["str_replace_editor"] = "str_replace_editor"
|
|
28
|
+
_timeout: float = 30.0 # seconds
|
|
29
|
+
|
|
30
|
+
def __init__(self, computer: Computer):
|
|
31
|
+
"""Initialize the edit tool.
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
computer: Computer instance for file operations
|
|
35
|
+
"""
|
|
36
|
+
# Initialize the base edit tool first
|
|
37
|
+
BaseEditTool.__init__(self, computer)
|
|
38
|
+
# Then initialize the Anthropic tool
|
|
39
|
+
BaseAnthropicTool.__init__(self)
|
|
40
|
+
|
|
41
|
+
# Edit history for the current session
|
|
42
|
+
self.edit_history = defaultdict(list)
|
|
43
|
+
|
|
44
|
+
async def __call__(
|
|
45
|
+
self,
|
|
46
|
+
*,
|
|
47
|
+
command: Command,
|
|
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
|
+
await self.validate_path(command, _path)
|
|
58
|
+
|
|
59
|
+
if command == "view":
|
|
60
|
+
return await self.view(_path, view_range)
|
|
61
|
+
elif command == "create":
|
|
62
|
+
if file_text is None:
|
|
63
|
+
raise ToolError("Parameter `file_text` is required for command: create")
|
|
64
|
+
await self.write_file(_path, file_text)
|
|
65
|
+
self.edit_history[_path].append(file_text)
|
|
66
|
+
return ToolResult(output=f"File created successfully at: {_path}")
|
|
67
|
+
elif command == "str_replace":
|
|
68
|
+
if old_str is None:
|
|
69
|
+
raise ToolError("Parameter `old_str` is required for command: str_replace")
|
|
70
|
+
return await self.str_replace(_path, old_str, new_str)
|
|
71
|
+
elif command == "insert":
|
|
72
|
+
if insert_line is None:
|
|
73
|
+
raise ToolError("Parameter `insert_line` is required for command: insert")
|
|
74
|
+
if new_str is None:
|
|
75
|
+
raise ToolError("Parameter `new_str` is required for command: insert")
|
|
76
|
+
return await self.insert(_path, insert_line, new_str)
|
|
77
|
+
elif command == "undo_edit":
|
|
78
|
+
return await self.undo_edit(_path)
|
|
79
|
+
|
|
80
|
+
raise ToolError(
|
|
81
|
+
f'Unrecognized command {command}. The allowed commands for the {self.name} tool are: {", ".join(get_args(Command))}'
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
async def validate_path(self, command: str, path: Path):
|
|
85
|
+
"""Check that the path/command combination is valid."""
|
|
86
|
+
# Check if its an absolute path
|
|
87
|
+
if not path.is_absolute():
|
|
88
|
+
suggested_path = Path("") / path
|
|
89
|
+
raise ToolError(
|
|
90
|
+
f"The path {path} is not an absolute path, it should start with `/`. Maybe you meant {suggested_path}?"
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
# Check if path exists using bash commands
|
|
94
|
+
try:
|
|
95
|
+
result = await self.computer.interface.run_command(
|
|
96
|
+
f'[ -e "{str(path)}" ] && echo "exists" || echo "not exists"'
|
|
97
|
+
)
|
|
98
|
+
exists = result[0].strip() == "exists"
|
|
99
|
+
|
|
100
|
+
if exists:
|
|
101
|
+
result = await self.computer.interface.run_command(
|
|
102
|
+
f'[ -d "{str(path)}" ] && echo "dir" || echo "file"'
|
|
103
|
+
)
|
|
104
|
+
is_dir = result[0].strip() == "dir"
|
|
105
|
+
else:
|
|
106
|
+
is_dir = False
|
|
107
|
+
|
|
108
|
+
# Check path validity
|
|
109
|
+
if not exists and command != "create":
|
|
110
|
+
raise ToolError(f"The path {path} does not exist. Please provide a valid path.")
|
|
111
|
+
if exists and command == "create":
|
|
112
|
+
raise ToolError(
|
|
113
|
+
f"File already exists at: {path}. Cannot overwrite files using command `create`."
|
|
114
|
+
)
|
|
115
|
+
if is_dir and command != "view":
|
|
116
|
+
raise ToolError(
|
|
117
|
+
f"The path {path} is a directory and only the `view` command can be used on directories"
|
|
118
|
+
)
|
|
119
|
+
except Exception as e:
|
|
120
|
+
raise ToolError(f"Failed to validate path: {str(e)}")
|
|
121
|
+
|
|
122
|
+
async def view(self, path: Path, view_range: list[int] | None = None):
|
|
123
|
+
"""Implement the view command"""
|
|
124
|
+
try:
|
|
125
|
+
# Check if path is a directory
|
|
126
|
+
result = await self.computer.interface.run_command(
|
|
127
|
+
f'[ -d "{str(path)}" ] && echo "dir" || echo "file"'
|
|
128
|
+
)
|
|
129
|
+
is_dir = result[0].strip() == "dir"
|
|
130
|
+
|
|
131
|
+
if is_dir:
|
|
132
|
+
if view_range:
|
|
133
|
+
raise ToolError(
|
|
134
|
+
"The `view_range` parameter is not allowed when `path` points to a directory."
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
# List directory contents using ls
|
|
138
|
+
result = await self.computer.interface.run_command(f'ls -la "{str(path)}"')
|
|
139
|
+
contents = result[0]
|
|
140
|
+
if contents:
|
|
141
|
+
stdout = f"Here's the files and directories in {path}:\n{contents}\n"
|
|
142
|
+
else:
|
|
143
|
+
stdout = f"Directory {path} is empty\n"
|
|
144
|
+
return CLIResult(output=stdout)
|
|
145
|
+
|
|
146
|
+
# Read file content using cat
|
|
147
|
+
file_content = await self.read_file(path)
|
|
148
|
+
init_line = 1
|
|
149
|
+
|
|
150
|
+
if view_range:
|
|
151
|
+
if len(view_range) != 2 or not all(isinstance(i, int) for i in view_range):
|
|
152
|
+
raise ToolError("Invalid `view_range`. It should be a list of two integers.")
|
|
153
|
+
|
|
154
|
+
file_lines = file_content.split("\n")
|
|
155
|
+
n_lines_file = len(file_lines)
|
|
156
|
+
init_line, final_line = view_range
|
|
157
|
+
|
|
158
|
+
if init_line < 1 or init_line > n_lines_file:
|
|
159
|
+
raise ToolError(
|
|
160
|
+
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]}"
|
|
161
|
+
)
|
|
162
|
+
if final_line > n_lines_file:
|
|
163
|
+
raise ToolError(
|
|
164
|
+
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}`"
|
|
165
|
+
)
|
|
166
|
+
if final_line != -1 and final_line < init_line:
|
|
167
|
+
raise ToolError(
|
|
168
|
+
f"Invalid `view_range`: {view_range}. Its second element `{final_line}` should be larger or equal than its first `{init_line}`"
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
if final_line == -1:
|
|
172
|
+
file_content = "\n".join(file_lines[init_line - 1 :])
|
|
173
|
+
else:
|
|
174
|
+
file_content = "\n".join(file_lines[init_line - 1 : final_line])
|
|
175
|
+
|
|
176
|
+
return CLIResult(output=self._make_output(file_content, str(path), init_line=init_line))
|
|
177
|
+
except Exception as e:
|
|
178
|
+
raise ToolError(f"Failed to view path: {str(e)}")
|
|
179
|
+
|
|
180
|
+
async def str_replace(self, path: Path, old_str: str, new_str: str | None):
|
|
181
|
+
"""Implement the str_replace command"""
|
|
182
|
+
# Read the file content
|
|
183
|
+
file_content = await self.read_file(path)
|
|
184
|
+
file_content = file_content.expandtabs()
|
|
185
|
+
old_str = old_str.expandtabs()
|
|
186
|
+
new_str = new_str.expandtabs() if new_str is not None else ""
|
|
187
|
+
|
|
188
|
+
# Check if old_str is unique in the file
|
|
189
|
+
occurrences = file_content.count(old_str)
|
|
190
|
+
if occurrences == 0:
|
|
191
|
+
raise ToolError(
|
|
192
|
+
f"No replacement was performed, old_str `{old_str}` did not appear verbatim in {path}."
|
|
193
|
+
)
|
|
194
|
+
elif occurrences > 1:
|
|
195
|
+
file_content_lines = file_content.split("\n")
|
|
196
|
+
lines = [idx + 1 for idx, line in enumerate(file_content_lines) if old_str in line]
|
|
197
|
+
raise ToolError(
|
|
198
|
+
f"No replacement was performed. Multiple occurrences of old_str `{old_str}` in lines {lines}. Please ensure it is unique"
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
# Replace old_str with new_str
|
|
202
|
+
new_file_content = file_content.replace(old_str, new_str)
|
|
203
|
+
|
|
204
|
+
# Write the new content to the file
|
|
205
|
+
await self.write_file(path, new_file_content)
|
|
206
|
+
|
|
207
|
+
# Save the content to history
|
|
208
|
+
self.edit_history[path].append(file_content)
|
|
209
|
+
|
|
210
|
+
# Create a snippet of the edited section
|
|
211
|
+
replacement_line = file_content.split(old_str)[0].count("\n")
|
|
212
|
+
start_line = max(0, replacement_line - SNIPPET_LINES)
|
|
213
|
+
end_line = replacement_line + SNIPPET_LINES + new_str.count("\n")
|
|
214
|
+
snippet = "\n".join(new_file_content.split("\n")[start_line : end_line + 1])
|
|
215
|
+
|
|
216
|
+
# Prepare the success message
|
|
217
|
+
success_msg = f"The file {path} has been edited. "
|
|
218
|
+
success_msg += self._make_output(snippet, f"a snippet of {path}", start_line + 1)
|
|
219
|
+
success_msg += "Review the changes and make sure they are as expected. Edit the file again if necessary."
|
|
220
|
+
|
|
221
|
+
return CLIResult(output=success_msg)
|
|
222
|
+
|
|
223
|
+
async def insert(self, path: Path, insert_line: int, new_str: str):
|
|
224
|
+
"""Implement the insert command"""
|
|
225
|
+
file_text = await self.read_file(path)
|
|
226
|
+
file_text = file_text.expandtabs()
|
|
227
|
+
new_str = new_str.expandtabs()
|
|
228
|
+
file_text_lines = file_text.split("\n")
|
|
229
|
+
n_lines_file = len(file_text_lines)
|
|
230
|
+
|
|
231
|
+
if insert_line < 0 or insert_line > n_lines_file:
|
|
232
|
+
raise ToolError(
|
|
233
|
+
f"Invalid `insert_line` parameter: {insert_line}. It should be within the range of lines of the file: {[0, n_lines_file]}"
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
new_str_lines = new_str.split("\n")
|
|
237
|
+
new_file_text_lines = (
|
|
238
|
+
file_text_lines[:insert_line] + new_str_lines + file_text_lines[insert_line:]
|
|
239
|
+
)
|
|
240
|
+
snippet_lines = (
|
|
241
|
+
file_text_lines[max(0, insert_line - SNIPPET_LINES) : insert_line]
|
|
242
|
+
+ new_str_lines
|
|
243
|
+
+ file_text_lines[insert_line : insert_line + SNIPPET_LINES]
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
new_file_text = "\n".join(new_file_text_lines)
|
|
247
|
+
snippet = "\n".join(snippet_lines)
|
|
248
|
+
|
|
249
|
+
await self.write_file(path, new_file_text)
|
|
250
|
+
self.edit_history[path].append(file_text)
|
|
251
|
+
|
|
252
|
+
success_msg = f"The file {path} has been edited. "
|
|
253
|
+
success_msg += self._make_output(
|
|
254
|
+
snippet, "a snippet of the edited file", max(1, insert_line - SNIPPET_LINES + 1)
|
|
255
|
+
)
|
|
256
|
+
success_msg += "Review the changes and make sure they are as expected (correct indentation, no duplicate lines, etc). Edit the file again if necessary."
|
|
257
|
+
return CLIResult(output=success_msg)
|
|
258
|
+
|
|
259
|
+
async def undo_edit(self, path: Path):
|
|
260
|
+
"""Implement the undo_edit command"""
|
|
261
|
+
if not self.edit_history[path]:
|
|
262
|
+
raise ToolError(f"No edit history found for {path}.")
|
|
263
|
+
|
|
264
|
+
old_text = self.edit_history[path].pop()
|
|
265
|
+
await self.write_file(path, old_text)
|
|
266
|
+
|
|
267
|
+
return CLIResult(
|
|
268
|
+
output=f"Last edit to {path} undone successfully. {self._make_output(old_text, str(path))}"
|
|
269
|
+
)
|
|
270
|
+
|
|
271
|
+
async def read_file(self, path: Path) -> str:
|
|
272
|
+
"""Read the content of a file using cat command."""
|
|
273
|
+
try:
|
|
274
|
+
result = await self.computer.interface.run_command(f'cat "{str(path)}"')
|
|
275
|
+
if result[1]: # If there's stderr output
|
|
276
|
+
raise ToolError(f"Error reading file: {result[1]}")
|
|
277
|
+
return result[0]
|
|
278
|
+
except Exception as e:
|
|
279
|
+
raise ToolError(f"Failed to read {path}: {str(e)}")
|
|
280
|
+
|
|
281
|
+
async def write_file(self, path: Path, content: str):
|
|
282
|
+
"""Write content to a file using echo and redirection."""
|
|
283
|
+
try:
|
|
284
|
+
# Create parent directories if they don't exist
|
|
285
|
+
parent = path.parent
|
|
286
|
+
if parent != Path("/"):
|
|
287
|
+
await self.computer.interface.run_command(f'mkdir -p "{str(parent)}"')
|
|
288
|
+
|
|
289
|
+
# Write content to file using echo and heredoc to preserve formatting
|
|
290
|
+
cmd = f"""cat > "{str(path)}" << 'EOFCUA'
|
|
291
|
+
{content}
|
|
292
|
+
EOFCUA"""
|
|
293
|
+
result = await self.computer.interface.run_command(cmd)
|
|
294
|
+
if result[1]: # If there's stderr output
|
|
295
|
+
raise ToolError(f"Error writing file: {result[1]}")
|
|
296
|
+
except Exception as e:
|
|
297
|
+
raise ToolError(f"Failed to write to {path}: {str(e)}")
|
|
298
|
+
|
|
299
|
+
def _make_output(
|
|
300
|
+
self,
|
|
301
|
+
file_content: str,
|
|
302
|
+
file_descriptor: str,
|
|
303
|
+
init_line: int = 1,
|
|
304
|
+
expand_tabs: bool = True,
|
|
305
|
+
) -> str:
|
|
306
|
+
"""Generate output for the CLI based on the content of a file."""
|
|
307
|
+
file_content = maybe_truncate(file_content)
|
|
308
|
+
if expand_tabs:
|
|
309
|
+
file_content = file_content.expandtabs()
|
|
310
|
+
file_content = "\n".join(
|
|
311
|
+
[f"{i + init_line:6}\t{line}" for i, line in enumerate(file_content.split("\n"))]
|
|
312
|
+
)
|
|
313
|
+
return (
|
|
314
|
+
f"Here's the result of running `cat -n` on {file_descriptor}:\n" + file_content + "\n"
|
|
315
|
+
)
|
|
316
|
+
|
|
317
|
+
def to_params(self) -> Dict[str, Any]:
|
|
318
|
+
"""Convert tool to API parameters.
|
|
319
|
+
|
|
320
|
+
Returns:
|
|
321
|
+
Dictionary with tool parameters
|
|
322
|
+
"""
|
|
323
|
+
return {
|
|
324
|
+
"name": self.name,
|
|
325
|
+
"type": self.api_type,
|
|
326
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
from typing import Any, Dict, List
|
|
2
|
+
from anthropic.types.beta import BetaToolUnionParam
|
|
3
|
+
from computer.computer import Computer
|
|
4
|
+
|
|
5
|
+
from ....core.tools import BaseToolManager, ToolResult
|
|
6
|
+
from ....core.tools.collection import ToolCollection
|
|
7
|
+
|
|
8
|
+
from .bash import BashTool
|
|
9
|
+
from .computer import ComputerTool
|
|
10
|
+
from .edit import EditTool
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class ToolManager(BaseToolManager):
|
|
14
|
+
"""Manages Anthropic-specific tool initialization and execution."""
|
|
15
|
+
|
|
16
|
+
def __init__(self, computer: Computer):
|
|
17
|
+
"""Initialize the tool manager.
|
|
18
|
+
|
|
19
|
+
Args:
|
|
20
|
+
computer: Computer instance for computer-related tools
|
|
21
|
+
"""
|
|
22
|
+
super().__init__(computer)
|
|
23
|
+
# Initialize Anthropic-specific tools
|
|
24
|
+
self.computer_tool = ComputerTool(self.computer)
|
|
25
|
+
self.bash_tool = BashTool(self.computer)
|
|
26
|
+
self.edit_tool = EditTool(self.computer)
|
|
27
|
+
|
|
28
|
+
def _initialize_tools(self) -> ToolCollection:
|
|
29
|
+
"""Initialize all available tools."""
|
|
30
|
+
return ToolCollection(self.computer_tool, self.bash_tool, self.edit_tool)
|
|
31
|
+
|
|
32
|
+
async def _initialize_tools_specific(self) -> None:
|
|
33
|
+
"""Initialize Anthropic-specific tool requirements."""
|
|
34
|
+
await self.computer_tool.initialize_dimensions()
|
|
35
|
+
|
|
36
|
+
def get_tool_params(self) -> List[BetaToolUnionParam]:
|
|
37
|
+
"""Get tool parameters for Anthropic API calls."""
|
|
38
|
+
if self.tools is None:
|
|
39
|
+
raise RuntimeError("Tools not initialized. Call initialize() first.")
|
|
40
|
+
return self.tools.to_params()
|
|
41
|
+
|
|
42
|
+
async def execute_tool(self, name: str, tool_input: dict[str, Any]) -> ToolResult:
|
|
43
|
+
"""Execute a tool with the given input.
|
|
44
|
+
|
|
45
|
+
Args:
|
|
46
|
+
name: Name of the tool to execute
|
|
47
|
+
tool_input: Input parameters for the tool
|
|
48
|
+
|
|
49
|
+
Returns:
|
|
50
|
+
Result of the tool execution
|
|
51
|
+
"""
|
|
52
|
+
if self.tools is None:
|
|
53
|
+
raise RuntimeError("Tools not initialized. Call initialize() first.")
|
|
54
|
+
return await self.tools.run(name=name, tool_input=tool_input)
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""Utility to run shell commands asynchronously with a timeout."""
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
|
|
5
|
+
TRUNCATED_MESSAGE: str = "<response clipped><NOTE>To save on context only part of this file has been shown to you. You should retry this tool after you have searched inside the file with `grep -n` in order to find the line numbers of what you are looking for.</NOTE>"
|
|
6
|
+
MAX_RESPONSE_LEN: int = 16000
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def maybe_truncate(content: str, truncate_after: int | None = MAX_RESPONSE_LEN):
|
|
10
|
+
"""Truncate content and append a notice if content exceeds the specified length."""
|
|
11
|
+
return (
|
|
12
|
+
content
|
|
13
|
+
if not truncate_after or len(content) <= truncate_after
|
|
14
|
+
else content[:truncate_after] + TRUNCATED_MESSAGE
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
async def run(
|
|
19
|
+
cmd: str,
|
|
20
|
+
timeout: float | None = 120.0, # seconds
|
|
21
|
+
truncate_after: int | None = MAX_RESPONSE_LEN,
|
|
22
|
+
):
|
|
23
|
+
"""Run a shell command asynchronously with a timeout."""
|
|
24
|
+
process = await asyncio.create_subprocess_shell(
|
|
25
|
+
cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
try:
|
|
29
|
+
stdout, stderr = await asyncio.wait_for(process.communicate(), timeout=timeout)
|
|
30
|
+
return (
|
|
31
|
+
process.returncode or 0,
|
|
32
|
+
maybe_truncate(stdout.decode(), truncate_after=truncate_after),
|
|
33
|
+
maybe_truncate(stderr.decode(), truncate_after=truncate_after),
|
|
34
|
+
)
|
|
35
|
+
except asyncio.TimeoutError as exc:
|
|
36
|
+
try:
|
|
37
|
+
process.kill()
|
|
38
|
+
except ProcessLookupError:
|
|
39
|
+
pass
|
|
40
|
+
raise TimeoutError(
|
|
41
|
+
f"Command '{cmd}' timed out after {timeout} seconds"
|
|
42
|
+
) from exc
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from enum import StrEnum
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class APIProvider(StrEnum):
|
|
5
|
+
"""Enum for supported API providers."""
|
|
6
|
+
|
|
7
|
+
ANTHROPIC = "anthropic"
|
|
8
|
+
BEDROCK = "bedrock"
|
|
9
|
+
VERTEX = "vertex"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
PROVIDER_TO_DEFAULT_MODEL_NAME: dict[APIProvider, str] = {
|
|
13
|
+
APIProvider.ANTHROPIC: "claude-3-7-sonnet-20250219",
|
|
14
|
+
APIProvider.BEDROCK: "anthropic.claude-3-7-sonnet-20250219-v2:0",
|
|
15
|
+
APIProvider.VERTEX: "claude-3-5-sonnet-v2@20241022",
|
|
16
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""Omni provider implementation."""
|
|
2
|
+
|
|
3
|
+
# The OmniComputerAgent has been replaced by the unified ComputerAgent
|
|
4
|
+
# which can be found in agent.core.agent
|
|
5
|
+
from .types import APIProvider
|
|
6
|
+
from .experiment import ExperimentManager
|
|
7
|
+
from .visualization import visualize_click, visualize_scroll, calculate_element_center
|
|
8
|
+
from .image_utils import (
|
|
9
|
+
decode_base64_image,
|
|
10
|
+
encode_image_base64,
|
|
11
|
+
clean_base64_data,
|
|
12
|
+
extract_base64_from_text,
|
|
13
|
+
get_image_dimensions,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
__all__ = [
|
|
17
|
+
"APIProvider",
|
|
18
|
+
"ExperimentManager",
|
|
19
|
+
"visualize_click",
|
|
20
|
+
"visualize_scroll",
|
|
21
|
+
"calculate_element_center",
|
|
22
|
+
"decode_base64_image",
|
|
23
|
+
"encode_image_base64",
|
|
24
|
+
"clean_base64_data",
|
|
25
|
+
"extract_base64_from_text",
|
|
26
|
+
"get_image_dimensions",
|
|
27
|
+
]
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""Omni callback manager implementation."""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
from typing import Any, Dict, Optional, Set
|
|
5
|
+
|
|
6
|
+
from ...core.callbacks import BaseCallbackManager, ContentCallback, ToolCallback, APICallback
|
|
7
|
+
from ...types.tools import ToolResult
|
|
8
|
+
|
|
9
|
+
logger = logging.getLogger(__name__)
|
|
10
|
+
|
|
11
|
+
class OmniCallbackManager(BaseCallbackManager):
|
|
12
|
+
"""Callback manager for multi-provider support."""
|
|
13
|
+
|
|
14
|
+
def __init__(
|
|
15
|
+
self,
|
|
16
|
+
content_callback: ContentCallback,
|
|
17
|
+
tool_callback: ToolCallback,
|
|
18
|
+
api_callback: APICallback,
|
|
19
|
+
):
|
|
20
|
+
"""Initialize Omni callback manager.
|
|
21
|
+
|
|
22
|
+
Args:
|
|
23
|
+
content_callback: Callback for content updates
|
|
24
|
+
tool_callback: Callback for tool execution results
|
|
25
|
+
api_callback: Callback for API interactions
|
|
26
|
+
"""
|
|
27
|
+
super().__init__(
|
|
28
|
+
content_callback=content_callback,
|
|
29
|
+
tool_callback=tool_callback,
|
|
30
|
+
api_callback=api_callback
|
|
31
|
+
)
|
|
32
|
+
self._active_tools: Set[str] = set()
|
|
33
|
+
|
|
34
|
+
def on_content(self, content: Any) -> None:
|
|
35
|
+
"""Handle content updates.
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
content: Content update data
|
|
39
|
+
"""
|
|
40
|
+
logger.debug(f"Content update: {content}")
|
|
41
|
+
self.content_callback(content)
|
|
42
|
+
|
|
43
|
+
def on_tool_result(self, result: ToolResult, tool_id: str) -> None:
|
|
44
|
+
"""Handle tool execution results.
|
|
45
|
+
|
|
46
|
+
Args:
|
|
47
|
+
result: Tool execution result
|
|
48
|
+
tool_id: ID of the tool
|
|
49
|
+
"""
|
|
50
|
+
logger.debug(f"Tool result for {tool_id}: {result}")
|
|
51
|
+
self.tool_callback(result, tool_id)
|
|
52
|
+
|
|
53
|
+
def on_api_interaction(
|
|
54
|
+
self,
|
|
55
|
+
request: Any,
|
|
56
|
+
response: Any,
|
|
57
|
+
error: Optional[Exception] = None
|
|
58
|
+
) -> None:
|
|
59
|
+
"""Handle API interactions.
|
|
60
|
+
|
|
61
|
+
Args:
|
|
62
|
+
request: API request data
|
|
63
|
+
response: API response data
|
|
64
|
+
error: Optional error that occurred
|
|
65
|
+
"""
|
|
66
|
+
if error:
|
|
67
|
+
logger.error(f"API error: {str(error)}")
|
|
68
|
+
else:
|
|
69
|
+
logger.debug(f"API interaction - Request: {request}, Response: {response}")
|
|
70
|
+
self.api_callback(request, response, error)
|
|
71
|
+
|
|
72
|
+
def get_active_tools(self) -> Set[str]:
|
|
73
|
+
"""Get currently active tools.
|
|
74
|
+
|
|
75
|
+
Returns:
|
|
76
|
+
Set of active tool names
|
|
77
|
+
"""
|
|
78
|
+
return self._active_tools.copy()
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"""Anthropic API client implementation."""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
from typing import Any, Dict, List, Optional, Tuple, cast
|
|
5
|
+
import asyncio
|
|
6
|
+
from httpx import ConnectError, ReadTimeout
|
|
7
|
+
|
|
8
|
+
from anthropic import AsyncAnthropic, Anthropic
|
|
9
|
+
from anthropic.types import MessageParam
|
|
10
|
+
from .base import BaseOmniClient
|
|
11
|
+
|
|
12
|
+
logger = logging.getLogger(__name__)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class AnthropicClient(BaseOmniClient):
|
|
16
|
+
"""Client for making calls to Anthropic API."""
|
|
17
|
+
|
|
18
|
+
def __init__(self, api_key: str, model: str, max_retries: int = 3, retry_delay: float = 1.0):
|
|
19
|
+
"""Initialize the Anthropic client.
|
|
20
|
+
|
|
21
|
+
Args:
|
|
22
|
+
api_key: Anthropic API key
|
|
23
|
+
model: Anthropic model name (e.g. "claude-3-opus-20240229")
|
|
24
|
+
max_retries: Maximum number of retries for API calls
|
|
25
|
+
retry_delay: Base delay between retries in seconds
|
|
26
|
+
"""
|
|
27
|
+
if not model:
|
|
28
|
+
raise ValueError("Model name must be provided")
|
|
29
|
+
|
|
30
|
+
self.client = AsyncAnthropic(api_key=api_key)
|
|
31
|
+
self.model: str = model # Add explicit type annotation
|
|
32
|
+
self.max_retries = max_retries
|
|
33
|
+
self.retry_delay = retry_delay
|
|
34
|
+
|
|
35
|
+
def _convert_message_format(self, messages: List[Dict[str, Any]]) -> List[MessageParam]:
|
|
36
|
+
"""Convert messages from standard format to Anthropic format.
|
|
37
|
+
|
|
38
|
+
Args:
|
|
39
|
+
messages: Messages in standard format
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
Messages in Anthropic format
|
|
43
|
+
"""
|
|
44
|
+
anthropic_messages = []
|
|
45
|
+
|
|
46
|
+
for message in messages:
|
|
47
|
+
if message["role"] == "user":
|
|
48
|
+
anthropic_messages.append({"role": "user", "content": message["content"]})
|
|
49
|
+
elif message["role"] == "assistant":
|
|
50
|
+
anthropic_messages.append({"role": "assistant", "content": message["content"]})
|
|
51
|
+
|
|
52
|
+
# Cast the list to the correct type expected by Anthropic
|
|
53
|
+
return cast(List[MessageParam], anthropic_messages)
|
|
54
|
+
|
|
55
|
+
async def run_interleaved(
|
|
56
|
+
self, messages: List[Dict[str, Any]], system: str, max_tokens: int
|
|
57
|
+
) -> Any:
|
|
58
|
+
"""Run model with interleaved conversation format.
|
|
59
|
+
|
|
60
|
+
Args:
|
|
61
|
+
messages: List of messages to process
|
|
62
|
+
system: System prompt
|
|
63
|
+
max_tokens: Maximum tokens to generate
|
|
64
|
+
|
|
65
|
+
Returns:
|
|
66
|
+
Model response
|
|
67
|
+
"""
|
|
68
|
+
last_error = None
|
|
69
|
+
|
|
70
|
+
for attempt in range(self.max_retries):
|
|
71
|
+
try:
|
|
72
|
+
# Convert messages to Anthropic format
|
|
73
|
+
anthropic_messages = self._convert_message_format(messages)
|
|
74
|
+
|
|
75
|
+
response = await self.client.messages.create(
|
|
76
|
+
model=self.model,
|
|
77
|
+
max_tokens=max_tokens,
|
|
78
|
+
temperature=0,
|
|
79
|
+
system=system,
|
|
80
|
+
messages=anthropic_messages,
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
return response
|
|
84
|
+
|
|
85
|
+
except (ConnectError, ReadTimeout) as e:
|
|
86
|
+
last_error = e
|
|
87
|
+
logger.warning(
|
|
88
|
+
f"Connection error on attempt {attempt + 1}/{self.max_retries}: {str(e)}"
|
|
89
|
+
)
|
|
90
|
+
if attempt < self.max_retries - 1:
|
|
91
|
+
await asyncio.sleep(self.retry_delay * (attempt + 1)) # Exponential backoff
|
|
92
|
+
continue
|
|
93
|
+
|
|
94
|
+
except Exception as e:
|
|
95
|
+
logger.error(f"Unexpected error in Anthropic API call: {str(e)}")
|
|
96
|
+
raise RuntimeError(f"Anthropic API call failed: {str(e)}")
|
|
97
|
+
|
|
98
|
+
# If we get here, all retries failed
|
|
99
|
+
raise RuntimeError(f"Connection error after {self.max_retries} retries: {str(last_error)}")
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""Base client implementation for Omni providers."""
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import logging
|
|
5
|
+
from typing import Dict, List, Optional, Any, Tuple
|
|
6
|
+
import aiohttp
|
|
7
|
+
import json
|
|
8
|
+
|
|
9
|
+
logger = logging.getLogger(__name__)
|
|
10
|
+
|
|
11
|
+
class BaseOmniClient:
|
|
12
|
+
"""Base class for provider-specific clients."""
|
|
13
|
+
|
|
14
|
+
def __init__(
|
|
15
|
+
self,
|
|
16
|
+
api_key: Optional[str] = None,
|
|
17
|
+
model: Optional[str] = None
|
|
18
|
+
):
|
|
19
|
+
"""Initialize base client.
|
|
20
|
+
|
|
21
|
+
Args:
|
|
22
|
+
api_key: Optional API key
|
|
23
|
+
model: Optional model name
|
|
24
|
+
"""
|
|
25
|
+
self.api_key = api_key
|
|
26
|
+
self.model = model
|
|
27
|
+
|
|
28
|
+
async def run_interleaved(
|
|
29
|
+
self,
|
|
30
|
+
messages: List[Dict[str, Any]],
|
|
31
|
+
system: str,
|
|
32
|
+
max_tokens: Optional[int] = None
|
|
33
|
+
) -> Dict[str, Any]:
|
|
34
|
+
"""Run interleaved chat completion.
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
messages: List of message dicts
|
|
38
|
+
system: System prompt
|
|
39
|
+
max_tokens: Optional max tokens override
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
Response dict
|
|
43
|
+
"""
|
|
44
|
+
raise NotImplementedError
|