hud-python 0.2.10__py3-none-any.whl → 0.3.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 hud-python might be problematic. Click here for more details.

Files changed (64) hide show
  1. hud/__init__.py +14 -5
  2. hud/env/docker_client.py +1 -1
  3. hud/env/environment.py +10 -7
  4. hud/env/local_docker_client.py +1 -1
  5. hud/env/remote_client.py +1 -1
  6. hud/env/remote_docker_client.py +2 -2
  7. hud/exceptions.py +2 -1
  8. hud/mcp_agent/__init__.py +15 -0
  9. hud/mcp_agent/base.py +723 -0
  10. hud/mcp_agent/claude.py +316 -0
  11. hud/mcp_agent/langchain.py +231 -0
  12. hud/mcp_agent/openai.py +318 -0
  13. hud/mcp_agent/tests/__init__.py +1 -0
  14. hud/mcp_agent/tests/test_base.py +437 -0
  15. hud/settings.py +14 -2
  16. hud/task.py +4 -0
  17. hud/telemetry/__init__.py +11 -7
  18. hud/telemetry/_trace.py +82 -71
  19. hud/telemetry/context.py +9 -27
  20. hud/telemetry/exporter.py +6 -5
  21. hud/telemetry/instrumentation/mcp.py +174 -410
  22. hud/telemetry/mcp_models.py +13 -74
  23. hud/telemetry/tests/test_context.py +9 -6
  24. hud/telemetry/tests/test_trace.py +92 -61
  25. hud/tools/__init__.py +21 -0
  26. hud/tools/base.py +65 -0
  27. hud/tools/bash.py +137 -0
  28. hud/tools/computer/__init__.py +13 -0
  29. hud/tools/computer/anthropic.py +411 -0
  30. hud/tools/computer/hud.py +315 -0
  31. hud/tools/computer/openai.py +283 -0
  32. hud/tools/edit.py +290 -0
  33. hud/tools/executors/__init__.py +13 -0
  34. hud/tools/executors/base.py +331 -0
  35. hud/tools/executors/pyautogui.py +585 -0
  36. hud/tools/executors/tests/__init__.py +1 -0
  37. hud/tools/executors/tests/test_base_executor.py +338 -0
  38. hud/tools/executors/tests/test_pyautogui_executor.py +162 -0
  39. hud/tools/executors/xdo.py +503 -0
  40. hud/tools/helper/README.md +56 -0
  41. hud/tools/helper/__init__.py +9 -0
  42. hud/tools/helper/mcp_server.py +78 -0
  43. hud/tools/helper/server_initialization.py +115 -0
  44. hud/tools/helper/utils.py +58 -0
  45. hud/tools/playwright_tool.py +373 -0
  46. hud/tools/tests/__init__.py +3 -0
  47. hud/tools/tests/test_bash.py +152 -0
  48. hud/tools/tests/test_computer.py +52 -0
  49. hud/tools/tests/test_computer_actions.py +34 -0
  50. hud/tools/tests/test_edit.py +233 -0
  51. hud/tools/tests/test_init.py +27 -0
  52. hud/tools/tests/test_playwright_tool.py +183 -0
  53. hud/tools/tests/test_tools.py +154 -0
  54. hud/tools/tests/test_utils.py +156 -0
  55. hud/tools/utils.py +50 -0
  56. hud/types.py +10 -1
  57. hud/utils/tests/test_init.py +21 -0
  58. hud/utils/tests/test_version.py +1 -1
  59. hud/version.py +1 -1
  60. {hud_python-0.2.10.dist-info → hud_python-0.3.0.dist-info}/METADATA +9 -6
  61. hud_python-0.3.0.dist-info/RECORD +124 -0
  62. hud_python-0.2.10.dist-info/RECORD +0 -85
  63. {hud_python-0.2.10.dist-info → hud_python-0.3.0.dist-info}/WHEEL +0 -0
  64. {hud_python-0.2.10.dist-info → hud_python-0.3.0.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,411 @@
1
+ # flake8: noqa: B008
2
+ from __future__ import annotations
3
+
4
+ import logging
5
+ from typing import TYPE_CHECKING, Literal, cast
6
+
7
+ from mcp import ErrorData, McpError
8
+ from mcp.types import INTERNAL_ERROR, INVALID_PARAMS, ImageContent, TextContent
9
+ from pydantic import Field
10
+
11
+ from hud.tools.base import ToolResult, tool_result_to_content_blocks
12
+
13
+ from .hud import HudComputerTool
14
+
15
+ if TYPE_CHECKING:
16
+ from anthropic.types.beta import BetaToolComputerUse20250124Param
17
+
18
+ logger = logging.getLogger(__name__)
19
+
20
+ # Map Anthropic key names to CLA standard keys
21
+ ANTHROPIC_TO_CLA_KEYS = {
22
+ # Common variations
23
+ "Return": "enter",
24
+ "Escape": "escape",
25
+ "ArrowUp": "up",
26
+ "ArrowDown": "down",
27
+ "ArrowLeft": "left",
28
+ "ArrowRight": "right",
29
+ "Backspace": "backspace",
30
+ "Delete": "delete",
31
+ "Tab": "tab",
32
+ "Space": "space",
33
+ "Control": "ctrl",
34
+ "Alt": "alt",
35
+ "Shift": "shift",
36
+ "Meta": "win", # Windows key
37
+ "Command": "cmd", # macOS
38
+ "Super": "win", # Linux
39
+ "PageUp": "pageup",
40
+ "PageDown": "pagedown",
41
+ "Home": "home",
42
+ "End": "end",
43
+ "Insert": "insert",
44
+ "F1": "f1",
45
+ "F2": "f2",
46
+ "F3": "f3",
47
+ "F4": "f4",
48
+ "F5": "f5",
49
+ "F6": "f6",
50
+ "F7": "f7",
51
+ "F8": "f8",
52
+ "F9": "f9",
53
+ "F10": "f10",
54
+ "F11": "f11",
55
+ "F12": "f12",
56
+ }
57
+
58
+
59
+ class AnthropicComputerTool(HudComputerTool):
60
+ """
61
+ Anthropic Computer Use tool for interacting with the computer.
62
+ """
63
+
64
+ name: str = "computer"
65
+ api_type: str = "computer_20250124"
66
+
67
+ def __init__(
68
+ self,
69
+ width: int = 1400,
70
+ height: int = 850,
71
+ display_num: int | None = None,
72
+ platform_type: Literal["auto", "xdo", "pyautogui"] = "auto",
73
+ rescale_images: bool = False,
74
+ ) -> None:
75
+ """
76
+ Initialize with Anthropic's default dimensions.
77
+
78
+ Args:
79
+ width: Target width for rescaling (default: 1024 for Anthropic)
80
+ height: Target height for rescaling (default: 768 for Anthropic)
81
+ display_num: X display number
82
+ platform_type: Which executor to use:
83
+ - "auto": Automatically detect based on platform
84
+ - "xdo": Use XDOExecutor (Linux/X11 only)
85
+ - "pyautogui": Use PyAutoGUIExecutor (cross-platform)
86
+ rescale_images: If True, rescale screenshots. If False, only rescale action coordinates
87
+ """
88
+ super().__init__(
89
+ width=width,
90
+ height=height,
91
+ display_num=display_num,
92
+ platform_type=platform_type,
93
+ rescale_images=rescale_images,
94
+ )
95
+
96
+ def to_params(self) -> BetaToolComputerUse20250124Param:
97
+ """Convert to Anthropic tool parameters."""
98
+ return cast(
99
+ "BetaToolComputerUse20250124Param",
100
+ {
101
+ "type": self.api_type,
102
+ "name": self.name,
103
+ "display_width_px": self.width,
104
+ "display_height_px": self.height,
105
+ },
106
+ )
107
+
108
+ def _map_anthropic_key_to_cla(self, key: str) -> str:
109
+ """Map Anthropic key name to CLA standard key."""
110
+ # Handle key combinations like "ctrl+a"
111
+ if "+" in key:
112
+ parts = key.split("+")
113
+ mapped_parts = []
114
+ for part in parts:
115
+ # Try exact match first, then case-insensitive
116
+ mapped = ANTHROPIC_TO_CLA_KEYS.get(
117
+ part, ANTHROPIC_TO_CLA_KEYS.get(part.capitalize(), part.lower())
118
+ )
119
+ mapped_parts.append(mapped)
120
+ return "+".join(mapped_parts)
121
+ else:
122
+ # Single key - try exact match first, then case-insensitive
123
+ return ANTHROPIC_TO_CLA_KEYS.get(
124
+ key, ANTHROPIC_TO_CLA_KEYS.get(key.capitalize(), key.lower())
125
+ )
126
+
127
+ async def __call__(
128
+ self,
129
+ action: str = Field(..., description="The action to perform on the computer"),
130
+ coordinate: list[int] | tuple[int, int] | None = Field(
131
+ None, description="The coordinate to interact with on the computer [x, y]"
132
+ ),
133
+ text: str | None = Field(
134
+ None, description="The text to type on the computer or key to press"
135
+ ),
136
+ start_coordinate: list[int] | tuple[int, int] | None = Field(
137
+ None, description="The starting coordinate for drag actions [x, y]"
138
+ ),
139
+ scroll_direction: str | None = Field(
140
+ None, description="The direction to scroll (up, down, left, right)"
141
+ ),
142
+ scroll_amount: int | None = Field(None, description="The amount to scroll"),
143
+ duration: float | None = Field(None, description="The duration of the action in seconds"),
144
+ take_screenshot_on_click: bool = Field(
145
+ True, description="Whether to take a screenshot after clicking"
146
+ ),
147
+ ) -> list[ImageContent | TextContent]:
148
+ """
149
+ Handle Anthropic Computer Use API calls.
150
+
151
+ This converts Anthropic's action format to HudComputerTool's format.
152
+
153
+ Returns:
154
+ List of MCP content blocks
155
+ """
156
+ logger.info("AnthropicComputerTool received action: %s", action)
157
+
158
+ # Convert lists to tuples if needed
159
+ coord_tuple = None
160
+ if coordinate:
161
+ coord_tuple = tuple(coordinate) if isinstance(coordinate, list) else coordinate
162
+
163
+ start_coord_tuple = None
164
+ if start_coordinate:
165
+ start_coord_tuple = (
166
+ tuple(start_coordinate) if isinstance(start_coordinate, list) else start_coordinate
167
+ )
168
+
169
+ # Map Anthropic actions to HudComputerTool actions
170
+ if action == "screenshot":
171
+ screenshot_base64 = await self.executor.screenshot()
172
+ if screenshot_base64:
173
+ # Rescale screenshot if requested
174
+ screenshot_base64 = await self._rescale_screenshot(screenshot_base64)
175
+ result = ToolResult(base64_image=screenshot_base64)
176
+ else:
177
+ result = ToolResult(error="Failed to take screenshot")
178
+
179
+ elif action == "left_click" or action == "click":
180
+ if coord_tuple and len(coord_tuple) >= 2:
181
+ scaled_x, scaled_y = self._scale_coordinates(coord_tuple[0], coord_tuple[1])
182
+ logger.info("Scaled coordinates: %s, %s", scaled_x, scaled_y)
183
+ result = await self.executor.click(x=scaled_x, y=scaled_y)
184
+ else:
185
+ result = await self.executor.click()
186
+
187
+ elif action == "double_click":
188
+ if coord_tuple and len(coord_tuple) >= 2:
189
+ # Use pattern for double-click
190
+ scaled_x, scaled_y = self._scale_coordinates(coord_tuple[0], coord_tuple[1])
191
+ result = await self.executor.click(x=scaled_x, y=scaled_y, pattern=[100])
192
+ else:
193
+ result = await self.executor.click(pattern=[100])
194
+
195
+ elif action == "triple_click":
196
+ if coord_tuple and len(coord_tuple) >= 2:
197
+ # Use pattern for triple-click
198
+ scaled_x, scaled_y = self._scale_coordinates(coord_tuple[0], coord_tuple[1])
199
+ result = await self.executor.click(x=scaled_x, y=scaled_y, pattern=[100, 100])
200
+ else:
201
+ result = await self.executor.click(pattern=[100, 100])
202
+
203
+ elif action == "right_click":
204
+ if coord_tuple and len(coord_tuple) >= 2:
205
+ scaled_x, scaled_y = self._scale_coordinates(coord_tuple[0], coord_tuple[1])
206
+ result = await self.executor.click(x=scaled_x, y=scaled_y, button="right")
207
+ else:
208
+ result = await self.executor.click(button="right")
209
+
210
+ elif action == "middle_click":
211
+ if coord_tuple and len(coord_tuple) >= 2:
212
+ scaled_x, scaled_y = self._scale_coordinates(coord_tuple[0], coord_tuple[1])
213
+ result = await self.executor.click(x=scaled_x, y=scaled_y, button="middle")
214
+ else:
215
+ result = await self.executor.click(button="middle")
216
+
217
+ elif action == "mouse_move" or action == "move":
218
+ if coord_tuple and len(coord_tuple) >= 2:
219
+ scaled_x, scaled_y = self._scale_coordinates(coord_tuple[0], coord_tuple[1])
220
+ result = await self.executor.move(x=scaled_x, y=scaled_y)
221
+ else:
222
+ raise McpError(
223
+ ErrorData(code=INVALID_PARAMS, message="coordinate is required for mouse_move")
224
+ )
225
+
226
+ elif action == "type":
227
+ if text:
228
+ result = await self.executor.type(text=text)
229
+ else:
230
+ raise McpError(ErrorData(code=INVALID_PARAMS, message="text is required for type"))
231
+
232
+ elif action == "key":
233
+ if text:
234
+ # Anthropic sends single key or combo like "ctrl+a"
235
+ # Map to CLA standard key format
236
+ mapped_key = self._map_anthropic_key_to_cla(text)
237
+ result = await self.executor.press(keys=[mapped_key])
238
+ else:
239
+ raise McpError(ErrorData(code=INVALID_PARAMS, message="text is required for key"))
240
+
241
+ elif action == "scroll":
242
+ # Original implementation validates scroll_direction and scroll_amount
243
+ if scroll_direction not in ["up", "down", "left", "right"]:
244
+ raise McpError(
245
+ ErrorData(
246
+ code=INVALID_PARAMS,
247
+ message="scroll_direction must be 'up', 'down', 'left', or 'right'",
248
+ )
249
+ )
250
+
251
+ if scroll_amount is None or scroll_amount < 0:
252
+ raise McpError(
253
+ ErrorData(
254
+ code=INVALID_PARAMS, message="scroll_amount must be a non-negative int"
255
+ )
256
+ )
257
+
258
+ # Convert direction to scroll amounts
259
+ scroll_x = None
260
+ scroll_y = None
261
+ if scroll_direction == "down":
262
+ scroll_y = scroll_amount
263
+ elif scroll_direction == "up":
264
+ scroll_y = -scroll_amount
265
+ elif scroll_direction == "right":
266
+ scroll_x = scroll_amount
267
+ elif scroll_direction == "left":
268
+ scroll_x = -scroll_amount
269
+
270
+ if coord_tuple and len(coord_tuple) >= 2:
271
+ scaled_x, scaled_y = self._scale_coordinates(coord_tuple[0], coord_tuple[1])
272
+ result = await self.executor.scroll(
273
+ x=scaled_x, y=scaled_y, scroll_x=scroll_x, scroll_y=scroll_y
274
+ )
275
+ else:
276
+ result = await self.executor.scroll(scroll_x=scroll_x, scroll_y=scroll_y)
277
+
278
+ elif action == "left_click_drag" or action == "drag":
279
+ # Anthropic sends drag with start and end coordinates
280
+ if coord_tuple and len(coord_tuple) >= 2:
281
+ if start_coord_tuple and len(start_coord_tuple) >= 2:
282
+ # Full drag path
283
+ path = [
284
+ (start_coord_tuple[0], start_coord_tuple[1]),
285
+ (coord_tuple[0], coord_tuple[1]),
286
+ ]
287
+ scaled_path = self._scale_path(path)
288
+ result = await self.executor.drag(path=scaled_path)
289
+ else:
290
+ # Just end coordinate, drag from current position
291
+ # Original spec allows this
292
+ current_pos = [(0, 0), (coord_tuple[0], coord_tuple[1])] # Simplified
293
+ scaled_path = self._scale_path(current_pos)
294
+ result = await self.executor.drag(path=scaled_path)
295
+ else:
296
+ raise McpError(
297
+ ErrorData(
298
+ code=INVALID_PARAMS, message="coordinate is required for left_click_drag"
299
+ )
300
+ )
301
+
302
+ elif action == "wait":
303
+ # Original spec expects duration in seconds
304
+ if duration is None:
305
+ raise McpError(
306
+ ErrorData(code=INVALID_PARAMS, message="duration is required for wait")
307
+ )
308
+ if duration < 0:
309
+ raise McpError(
310
+ ErrorData(code=INVALID_PARAMS, message="duration must be non-negative")
311
+ )
312
+ if duration > 100:
313
+ raise McpError(ErrorData(code=INVALID_PARAMS, message="duration is too long"))
314
+
315
+ # Convert seconds to milliseconds for HudComputerTool
316
+ result = await self.executor.wait(time=int(duration * 1000))
317
+
318
+ elif action == "hold_key":
319
+ # Original spec has hold_key action
320
+ if text is None:
321
+ raise McpError(
322
+ ErrorData(code=INVALID_PARAMS, message="text is required for hold_key")
323
+ )
324
+ if duration is None:
325
+ raise McpError(
326
+ ErrorData(code=INVALID_PARAMS, message="duration is required for hold_key")
327
+ )
328
+ if duration < 0:
329
+ raise McpError(
330
+ ErrorData(code=INVALID_PARAMS, message="duration must be non-negative")
331
+ )
332
+ if duration > 100:
333
+ raise McpError(ErrorData(code=INVALID_PARAMS, message="duration is too long"))
334
+
335
+ # Hold key action
336
+ result = await self.executor.hold_key(key=text, duration=duration)
337
+
338
+ elif action == "left_mouse_down":
339
+ # These don't accept coordinates in original spec
340
+ if coord_tuple is not None:
341
+ raise McpError(
342
+ ErrorData(
343
+ code=INVALID_PARAMS,
344
+ message="coordinate is not accepted for left_mouse_down",
345
+ )
346
+ )
347
+ # Use generic mouse_down method
348
+ result = await self.executor.mouse_down(button="left")
349
+
350
+ elif action == "left_mouse_up":
351
+ # These don't accept coordinates in original spec
352
+ if coord_tuple is not None:
353
+ raise McpError(
354
+ ErrorData(
355
+ code=INVALID_PARAMS, message="coordinate is not accepted for left_mouse_up"
356
+ )
357
+ )
358
+ # Use generic mouse_up method
359
+ result = await self.executor.mouse_up(button="left")
360
+
361
+ elif action == "cursor_position":
362
+ result = await self.executor.position()
363
+
364
+ else:
365
+ # Unknown action
366
+ raise McpError(ErrorData(code=INTERNAL_ERROR, message=f"Invalid action: {action}"))
367
+
368
+ # Rescale screenshot in result if present
369
+ if isinstance(result, ToolResult) and result.base64_image and self.rescale_images:
370
+ rescaled_image = await self._rescale_screenshot(result.base64_image)
371
+ result = result.replace(base64_image=rescaled_image)
372
+
373
+ # Handle screenshot for actions that need it
374
+ screenshot_actions = {
375
+ "screenshot",
376
+ "left_click",
377
+ "click",
378
+ "double_click",
379
+ "triple_click",
380
+ "right_click",
381
+ "middle_click",
382
+ "mouse_move",
383
+ "move",
384
+ "type",
385
+ "key",
386
+ "scroll",
387
+ "left_click_drag",
388
+ "drag",
389
+ "wait",
390
+ "hold_key",
391
+ "left_mouse_down",
392
+ "left_mouse_up",
393
+ }
394
+
395
+ if (
396
+ action in screenshot_actions
397
+ and action != "screenshot"
398
+ and take_screenshot_on_click
399
+ and isinstance(result, ToolResult)
400
+ and not result.base64_image
401
+ ):
402
+ screenshot_base64 = await self.executor.screenshot()
403
+ if screenshot_base64:
404
+ # Rescale screenshot if requested
405
+ screenshot_base64 = await self._rescale_screenshot(screenshot_base64)
406
+ result = ToolResult(
407
+ output=result.output, error=result.error, base64_image=screenshot_base64
408
+ )
409
+
410
+ # Convert to content blocks
411
+ return tool_result_to_content_blocks(result)