cua-agent 0.3.2__py3-none-any.whl → 0.4.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.

Files changed (112) hide show
  1. agent/__init__.py +21 -12
  2. agent/__main__.py +21 -0
  3. agent/adapters/__init__.py +9 -0
  4. agent/adapters/huggingfacelocal_adapter.py +229 -0
  5. agent/agent.py +594 -0
  6. agent/callbacks/__init__.py +19 -0
  7. agent/callbacks/base.py +153 -0
  8. agent/callbacks/budget_manager.py +44 -0
  9. agent/callbacks/image_retention.py +139 -0
  10. agent/callbacks/logging.py +247 -0
  11. agent/callbacks/pii_anonymization.py +259 -0
  12. agent/callbacks/telemetry.py +210 -0
  13. agent/callbacks/trajectory_saver.py +305 -0
  14. agent/cli.py +297 -0
  15. agent/computer_handler.py +107 -0
  16. agent/decorators.py +90 -0
  17. agent/loops/__init__.py +11 -0
  18. agent/loops/anthropic.py +728 -0
  19. agent/loops/omniparser.py +339 -0
  20. agent/loops/openai.py +95 -0
  21. agent/loops/uitars.py +688 -0
  22. agent/responses.py +207 -0
  23. agent/telemetry.py +135 -14
  24. agent/types.py +79 -0
  25. agent/ui/__init__.py +7 -1
  26. agent/ui/__main__.py +2 -13
  27. agent/ui/gradio/__init__.py +6 -19
  28. agent/ui/gradio/app.py +94 -1313
  29. agent/ui/gradio/ui_components.py +721 -0
  30. cua_agent-0.4.0.dist-info/METADATA +424 -0
  31. cua_agent-0.4.0.dist-info/RECORD +33 -0
  32. agent/core/__init__.py +0 -27
  33. agent/core/agent.py +0 -210
  34. agent/core/base.py +0 -217
  35. agent/core/callbacks.py +0 -200
  36. agent/core/experiment.py +0 -249
  37. agent/core/factory.py +0 -122
  38. agent/core/messages.py +0 -332
  39. agent/core/provider_config.py +0 -21
  40. agent/core/telemetry.py +0 -142
  41. agent/core/tools/__init__.py +0 -21
  42. agent/core/tools/base.py +0 -74
  43. agent/core/tools/bash.py +0 -52
  44. agent/core/tools/collection.py +0 -46
  45. agent/core/tools/computer.py +0 -113
  46. agent/core/tools/edit.py +0 -67
  47. agent/core/tools/manager.py +0 -56
  48. agent/core/tools.py +0 -32
  49. agent/core/types.py +0 -88
  50. agent/core/visualization.py +0 -197
  51. agent/providers/__init__.py +0 -4
  52. agent/providers/anthropic/__init__.py +0 -6
  53. agent/providers/anthropic/api/client.py +0 -360
  54. agent/providers/anthropic/api/logging.py +0 -150
  55. agent/providers/anthropic/api_handler.py +0 -140
  56. agent/providers/anthropic/callbacks/__init__.py +0 -5
  57. agent/providers/anthropic/callbacks/manager.py +0 -65
  58. agent/providers/anthropic/loop.py +0 -568
  59. agent/providers/anthropic/prompts.py +0 -23
  60. agent/providers/anthropic/response_handler.py +0 -226
  61. agent/providers/anthropic/tools/__init__.py +0 -33
  62. agent/providers/anthropic/tools/base.py +0 -88
  63. agent/providers/anthropic/tools/bash.py +0 -66
  64. agent/providers/anthropic/tools/collection.py +0 -34
  65. agent/providers/anthropic/tools/computer.py +0 -396
  66. agent/providers/anthropic/tools/edit.py +0 -326
  67. agent/providers/anthropic/tools/manager.py +0 -54
  68. agent/providers/anthropic/tools/run.py +0 -42
  69. agent/providers/anthropic/types.py +0 -16
  70. agent/providers/anthropic/utils.py +0 -381
  71. agent/providers/omni/__init__.py +0 -8
  72. agent/providers/omni/api_handler.py +0 -42
  73. agent/providers/omni/clients/anthropic.py +0 -103
  74. agent/providers/omni/clients/base.py +0 -35
  75. agent/providers/omni/clients/oaicompat.py +0 -195
  76. agent/providers/omni/clients/ollama.py +0 -122
  77. agent/providers/omni/clients/openai.py +0 -155
  78. agent/providers/omni/clients/utils.py +0 -25
  79. agent/providers/omni/image_utils.py +0 -34
  80. agent/providers/omni/loop.py +0 -990
  81. agent/providers/omni/parser.py +0 -307
  82. agent/providers/omni/prompts.py +0 -64
  83. agent/providers/omni/tools/__init__.py +0 -30
  84. agent/providers/omni/tools/base.py +0 -29
  85. agent/providers/omni/tools/bash.py +0 -74
  86. agent/providers/omni/tools/computer.py +0 -179
  87. agent/providers/omni/tools/manager.py +0 -61
  88. agent/providers/omni/utils.py +0 -236
  89. agent/providers/openai/__init__.py +0 -6
  90. agent/providers/openai/api_handler.py +0 -456
  91. agent/providers/openai/loop.py +0 -472
  92. agent/providers/openai/response_handler.py +0 -205
  93. agent/providers/openai/tools/__init__.py +0 -15
  94. agent/providers/openai/tools/base.py +0 -79
  95. agent/providers/openai/tools/computer.py +0 -326
  96. agent/providers/openai/tools/manager.py +0 -106
  97. agent/providers/openai/types.py +0 -36
  98. agent/providers/openai/utils.py +0 -98
  99. agent/providers/uitars/__init__.py +0 -1
  100. agent/providers/uitars/clients/base.py +0 -35
  101. agent/providers/uitars/clients/mlxvlm.py +0 -263
  102. agent/providers/uitars/clients/oaicompat.py +0 -214
  103. agent/providers/uitars/loop.py +0 -660
  104. agent/providers/uitars/prompts.py +0 -63
  105. agent/providers/uitars/tools/__init__.py +0 -1
  106. agent/providers/uitars/tools/computer.py +0 -283
  107. agent/providers/uitars/tools/manager.py +0 -60
  108. agent/providers/uitars/utils.py +0 -264
  109. cua_agent-0.3.2.dist-info/METADATA +0 -295
  110. cua_agent-0.3.2.dist-info/RECORD +0 -87
  111. {cua_agent-0.3.2.dist-info → cua_agent-0.4.0.dist-info}/WHEEL +0 -0
  112. {cua_agent-0.3.2.dist-info → cua_agent-0.4.0.dist-info}/entry_points.txt +0 -0
@@ -1,326 +0,0 @@
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.stdout.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.stdout.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.stdout.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.stdout
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.stderr: # If there's stderr output
276
- raise ToolError(f"Error reading file: {result.stderr}")
277
- return result.stdout
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.stderr: # If there's stderr output
295
- raise ToolError(f"Error writing file: {result.stderr}")
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
- }
@@ -1,54 +0,0 @@
1
- from typing import Any, Dict, List, cast
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 cast(List[BetaToolUnionParam], 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)
@@ -1,42 +0,0 @@
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
@@ -1,16 +0,0 @@
1
- from enum import StrEnum
2
-
3
-
4
- class LLMProvider(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[LLMProvider, str] = {
13
- LLMProvider.ANTHROPIC: "claude-3-7-sonnet-20250219",
14
- LLMProvider.BEDROCK: "anthropic.claude-3-7-sonnet-20250219-v2:0",
15
- LLMProvider.VERTEX: "claude-3-5-sonnet-v2@20241022",
16
- }