oagi 0.3.0__tar.gz → 0.4.1__tar.gz

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 oagi might be problematic. Click here for more details.

Files changed (58) hide show
  1. {oagi-0.3.0 → oagi-0.4.1}/PKG-INFO +43 -1
  2. {oagi-0.3.0 → oagi-0.4.1}/README.md +42 -0
  3. oagi-0.4.1/examples/async_google_weather.py +23 -0
  4. {oagi-0.3.0 → oagi-0.4.1}/examples/execute_task_auto.py +20 -1
  5. {oagi-0.3.0 → oagi-0.4.1}/examples/execute_task_manual.py +48 -1
  6. {oagi-0.3.0 → oagi-0.4.1}/examples/single_step.py +6 -4
  7. {oagi-0.3.0 → oagi-0.4.1}/pyproject.toml +2 -1
  8. {oagi-0.3.0 → oagi-0.4.1}/src/oagi/__init__.py +25 -3
  9. oagi-0.4.1/src/oagi/async_client.py +239 -0
  10. oagi-0.4.1/src/oagi/async_pyautogui_action_handler.py +44 -0
  11. oagi-0.4.1/src/oagi/async_screenshot_maker.py +47 -0
  12. oagi-0.4.1/src/oagi/async_short_task.py +56 -0
  13. oagi-0.4.1/src/oagi/async_single_step.py +83 -0
  14. oagi-0.4.1/src/oagi/async_task.py +117 -0
  15. {oagi-0.3.0 → oagi-0.4.1}/src/oagi/pyautogui_action_handler.py +86 -51
  16. {oagi-0.3.0 → oagi-0.4.1}/src/oagi/types/__init__.py +4 -0
  17. oagi-0.4.1/src/oagi/types/async_action_handler.py +30 -0
  18. oagi-0.4.1/src/oagi/types/async_image_provider.py +37 -0
  19. {oagi-0.3.0 → oagi-0.4.1}/src/oagi/types/models/action.py +1 -0
  20. oagi-0.4.1/tests/test_async_client.py +193 -0
  21. oagi-0.4.1/tests/test_async_handlers.py +222 -0
  22. oagi-0.4.1/tests/test_async_task.py +217 -0
  23. oagi-0.4.1/tests/test_pyautogui_action_handler.py +144 -0
  24. {oagi-0.3.0 → oagi-0.4.1}/tests/test_short_task.py +6 -2
  25. {oagi-0.3.0 → oagi-0.4.1}/uv.lock +25 -1
  26. oagi-0.3.0/tests/test_pyautogui_action_handler.py +0 -147
  27. {oagi-0.3.0 → oagi-0.4.1}/.github/workflows/ci.yml +0 -0
  28. {oagi-0.3.0 → oagi-0.4.1}/.github/workflows/release.yml +0 -0
  29. {oagi-0.3.0 → oagi-0.4.1}/.gitignore +0 -0
  30. {oagi-0.3.0 → oagi-0.4.1}/.python-version +0 -0
  31. {oagi-0.3.0 → oagi-0.4.1}/CONTRIBUTING.md +0 -0
  32. {oagi-0.3.0 → oagi-0.4.1}/LICENSE +0 -0
  33. {oagi-0.3.0 → oagi-0.4.1}/Makefile +0 -0
  34. {oagi-0.3.0 → oagi-0.4.1}/examples/google_weather.py +0 -0
  35. {oagi-0.3.0 → oagi-0.4.1}/examples/hotel_booking.py +0 -0
  36. {oagi-0.3.0 → oagi-0.4.1}/examples/screenshot_with_config.py +0 -0
  37. {oagi-0.3.0 → oagi-0.4.1}/src/oagi/exceptions.py +0 -0
  38. {oagi-0.3.0 → oagi-0.4.1}/src/oagi/logging.py +0 -0
  39. {oagi-0.3.0 → oagi-0.4.1}/src/oagi/pil_image.py +0 -0
  40. {oagi-0.3.0 → oagi-0.4.1}/src/oagi/screenshot_maker.py +0 -0
  41. {oagi-0.3.0 → oagi-0.4.1}/src/oagi/short_task.py +3 -3
  42. {oagi-0.3.0 → oagi-0.4.1}/src/oagi/single_step.py +0 -0
  43. {oagi-0.3.0 → oagi-0.4.1}/src/oagi/sync_client.py +0 -0
  44. {oagi-0.3.0 → oagi-0.4.1}/src/oagi/task.py +0 -0
  45. {oagi-0.3.0 → oagi-0.4.1}/src/oagi/types/action_handler.py +0 -0
  46. {oagi-0.3.0 → oagi-0.4.1}/src/oagi/types/image.py +0 -0
  47. {oagi-0.3.0 → oagi-0.4.1}/src/oagi/types/image_provider.py +0 -0
  48. {oagi-0.3.0 → oagi-0.4.1}/src/oagi/types/models/__init__.py +0 -0
  49. {oagi-0.3.0 → oagi-0.4.1}/src/oagi/types/models/image_config.py +0 -0
  50. {oagi-0.3.0 → oagi-0.4.1}/src/oagi/types/models/step.py +0 -0
  51. {oagi-0.3.0 → oagi-0.4.1}/tests/__init__.py +0 -0
  52. {oagi-0.3.0 → oagi-0.4.1}/tests/conftest.py +0 -0
  53. {oagi-0.3.0 → oagi-0.4.1}/tests/test_logging.py +0 -0
  54. {oagi-0.3.0 → oagi-0.4.1}/tests/test_pil_image.py +0 -0
  55. {oagi-0.3.0 → oagi-0.4.1}/tests/test_screenshot_maker.py +0 -0
  56. {oagi-0.3.0 → oagi-0.4.1}/tests/test_single_step.py +0 -0
  57. {oagi-0.3.0 → oagi-0.4.1}/tests/test_sync_client.py +0 -0
  58. {oagi-0.3.0 → oagi-0.4.1}/tests/test_task.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: oagi
3
- Version: 0.3.0
3
+ Version: 0.4.1
4
4
  Summary: Official API of OpenAGI Foundation
5
5
  Project-URL: Homepage, https://github.com/agiopen-org/oagi
6
6
  Author-email: OpenAGI Foundation <contact@agiopen.org>
@@ -82,6 +82,23 @@ completed = task.auto_mode(
82
82
  )
83
83
  ```
84
84
 
85
+ Configure PyAutoGUI behavior with custom settings:
86
+
87
+ ```python
88
+ from oagi import PyautoguiActionHandler, PyautoguiConfig
89
+
90
+ # Customize action behavior
91
+ config = PyautoguiConfig(
92
+ drag_duration=1.0, # Slower drags for precision (default: 0.5)
93
+ scroll_amount=50, # Larger scroll steps (default: 30)
94
+ wait_duration=2.0, # Longer waits (default: 1.0)
95
+ action_pause=0.2, # More pause between actions (default: 0.1)
96
+ )
97
+
98
+ executor = PyautoguiActionHandler(config=config)
99
+ task.auto_mode("Complete form", executor=executor, image_provider=ScreenshotMaker())
100
+ ```
101
+
85
102
  ### Image Processing
86
103
 
87
104
  Process and optimize images before sending to API:
@@ -103,6 +120,31 @@ compressed = image.transform(config)
103
120
  step = single_step("Click button", screenshot=compressed)
104
121
  ```
105
122
 
123
+ ### Async Support
124
+
125
+ Use async client for non-blocking operations and better concurrency:
126
+
127
+ ```python
128
+ import asyncio
129
+ from oagi import async_single_step, AsyncShortTask
130
+
131
+ async def main():
132
+ # Single-step async analysis
133
+ step = await async_single_step(
134
+ "Find the search bar",
135
+ screenshot="screenshot.png"
136
+ )
137
+ print(f"Found {len(step.actions)} actions")
138
+
139
+ # Async task automation
140
+ task = AsyncShortTask()
141
+ async with task:
142
+ await task.init_task("Complete the form")
143
+ # ... continue with async operations
144
+
145
+ asyncio.run(main())
146
+ ```
147
+
106
148
  ## Examples
107
149
 
108
150
  See the [`examples/`](examples/) directory for more usage patterns:
@@ -48,6 +48,23 @@ completed = task.auto_mode(
48
48
  )
49
49
  ```
50
50
 
51
+ Configure PyAutoGUI behavior with custom settings:
52
+
53
+ ```python
54
+ from oagi import PyautoguiActionHandler, PyautoguiConfig
55
+
56
+ # Customize action behavior
57
+ config = PyautoguiConfig(
58
+ drag_duration=1.0, # Slower drags for precision (default: 0.5)
59
+ scroll_amount=50, # Larger scroll steps (default: 30)
60
+ wait_duration=2.0, # Longer waits (default: 1.0)
61
+ action_pause=0.2, # More pause between actions (default: 0.1)
62
+ )
63
+
64
+ executor = PyautoguiActionHandler(config=config)
65
+ task.auto_mode("Complete form", executor=executor, image_provider=ScreenshotMaker())
66
+ ```
67
+
51
68
  ### Image Processing
52
69
 
53
70
  Process and optimize images before sending to API:
@@ -69,6 +86,31 @@ compressed = image.transform(config)
69
86
  step = single_step("Click button", screenshot=compressed)
70
87
  ```
71
88
 
89
+ ### Async Support
90
+
91
+ Use async client for non-blocking operations and better concurrency:
92
+
93
+ ```python
94
+ import asyncio
95
+ from oagi import async_single_step, AsyncShortTask
96
+
97
+ async def main():
98
+ # Single-step async analysis
99
+ step = await async_single_step(
100
+ "Find the search bar",
101
+ screenshot="screenshot.png"
102
+ )
103
+ print(f"Found {len(step.actions)} actions")
104
+
105
+ # Async task automation
106
+ task = AsyncShortTask()
107
+ async with task:
108
+ await task.init_task("Complete the form")
109
+ # ... continue with async operations
110
+
111
+ asyncio.run(main())
112
+ ```
113
+
72
114
  ## Examples
73
115
 
74
116
  See the [`examples/`](examples/) directory for more usage patterns:
@@ -0,0 +1,23 @@
1
+ # -----------------------------------------------------------------------------
2
+ # Copyright (c) OpenAGI Foundation
3
+ # All rights reserved.
4
+ #
5
+ # This file is part of the official API project.
6
+ # Licensed under the MIT License.
7
+ # -----------------------------------------------------------------------------
8
+
9
+ import asyncio
10
+
11
+ from examples.execute_task_manual import async_execute_task_manual
12
+
13
+
14
+ async def main():
15
+ is_completed, screenshot = await async_execute_task_manual(
16
+ desc := "Search weather with Google", max_steps=5
17
+ )
18
+
19
+ print(f"is_completed: {is_completed}, desc: {desc}")
20
+
21
+
22
+ if __name__ == "__main__":
23
+ asyncio.run(main())
@@ -5,7 +5,14 @@
5
5
  # This file is part of the official API project.
6
6
  # Licensed under the MIT License.
7
7
  # -----------------------------------------------------------------------------
8
- from oagi import PyautoguiActionHandler, ScreenshotMaker, ShortTask
8
+ from oagi import (
9
+ AsyncPyautoguiActionHandler,
10
+ AsyncScreenshotMaker,
11
+ AsyncShortTask,
12
+ PyautoguiActionHandler,
13
+ ScreenshotMaker,
14
+ ShortTask,
15
+ )
9
16
 
10
17
 
11
18
  def execute_task_auto(task_desc, max_steps=5):
@@ -21,3 +28,15 @@ def execute_task_auto(task_desc, max_steps=5):
21
28
  )
22
29
 
23
30
  return is_completed, sm.last_image()
31
+
32
+
33
+ async def async_execute_task_auto(task_desc, max_steps=5):
34
+ async with AsyncShortTask() as async_short_task:
35
+ is_completed = await async_short_task.auto_mode(
36
+ task_desc,
37
+ max_steps=max_steps,
38
+ executor=AsyncPyautoguiActionHandler(),
39
+ image_provider=(sm := AsyncScreenshotMaker()),
40
+ )
41
+
42
+ return is_completed, await sm.last_image()
@@ -6,7 +6,14 @@
6
6
  # Licensed under the MIT License.
7
7
  # -----------------------------------------------------------------------------
8
8
 
9
- from oagi import PyautoguiActionHandler, ScreenshotMaker, Task
9
+ from oagi import (
10
+ AsyncPyautoguiActionHandler,
11
+ AsyncScreenshotMaker,
12
+ AsyncTask,
13
+ PyautoguiActionHandler,
14
+ ScreenshotMaker,
15
+ Task,
16
+ )
10
17
 
11
18
 
12
19
  def execute_task_manual(task_desc, max_steps=5):
@@ -49,3 +56,43 @@ def execute_task_manual(task_desc, max_steps=5):
49
56
 
50
57
  print(f"manual execution completed: {is_completed=}, {task_desc=}\n")
51
58
  return is_completed, screenshot
59
+
60
+
61
+ async def async_execute_task_manual(task_desc, max_steps=5):
62
+ # set OAGI_API_KEY and OAGI_BASE_URL
63
+ # or ShortTask(api_key="your_api_key", base_url="your_base_url")
64
+ async with AsyncTask() as task:
65
+ await task.init_task(task_desc, max_steps=max_steps)
66
+ executor = AsyncPyautoguiActionHandler()
67
+
68
+ # by default, screenshot will be resized to 1260 * 700 and jpeg with quality 85
69
+ # for best image quality, use ScreenshotMaker(config=ImageConfig(format="PNG"))
70
+ image_provider = AsyncScreenshotMaker()
71
+
72
+ for i in range(max_steps):
73
+ # image can also be bytes
74
+ # with open("test_screenshot.png", "rb") as f:
75
+ # image = f.read()
76
+ image = await image_provider()
77
+
78
+ # For additional instructions
79
+ # step = task.step(image, instruction="some instruction")
80
+ step = await task.step(image)
81
+
82
+ # do something with step, maybe print to debug
83
+ print(f"Step {i}: {step.reason=}")
84
+
85
+ if step.stop:
86
+ print(f"Task completed after {i} steps.")
87
+ is_completed = True
88
+ screenshot = await image_provider.last_image()
89
+ break
90
+
91
+ await executor(step.actions)
92
+ else:
93
+ # If we didn't break out of the loop, we used up all our steps
94
+ is_completed = False
95
+ screenshot = await image_provider()
96
+
97
+ print(f"manual execution completed: {is_completed=}, {task_desc=}\n")
98
+ return is_completed, screenshot
@@ -6,14 +6,16 @@
6
6
  # Licensed under the MIT License.
7
7
  # -----------------------------------------------------------------------------
8
8
 
9
- from oagi import single_step
9
+ from oagi import ScreenshotMaker, single_step
10
10
 
11
+ image_provider = ScreenshotMaker()
12
+ image = image_provider()
11
13
  step = single_step(
12
14
  task_description="Search weather with Google",
13
- screenshot="some/path/to/local/image", # bytes or Path object or Image object
15
+ screenshot=image, # bytes or Path object or Image object
14
16
  instruction="The operating system is macos", # optional instruction
15
- # api_key="your-api-key", if not set with OAGI_API_KEY env var
16
- # base_url="https://api.example.com" if not set with OAGI_BASE_URL env var
17
+ api_key="sk-50DPDW87GnlNcH_0cAPRFZ4ntweCEdUrLEFIcQFaBhc",
18
+ base_url="http://127.0.0.1:8000",
17
19
  )
18
20
 
19
21
  print(step)
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "oagi"
7
- version = "0.3.0"
7
+ version = "0.4.1"
8
8
  description = "Official API of OpenAGI Foundation"
9
9
  readme = "README.md"
10
10
  license = { file = "LICENSE" }
@@ -29,6 +29,7 @@ dev = [
29
29
  "ruff>=0.12.9",
30
30
  "pytest>=8.0.0",
31
31
  "pytest-mock>=3.12.0",
32
+ "pytest-asyncio>=0.21.0",
32
33
  ]
33
34
 
34
35
  [tool.ruff.lint]
@@ -6,6 +6,12 @@
6
6
  # Licensed under the MIT License.
7
7
  # -----------------------------------------------------------------------------
8
8
 
9
+ from oagi.async_client import AsyncClient
10
+ from oagi.async_pyautogui_action_handler import AsyncPyautoguiActionHandler
11
+ from oagi.async_screenshot_maker import AsyncScreenshotMaker
12
+ from oagi.async_short_task import AsyncShortTask
13
+ from oagi.async_single_step import async_single_step
14
+ from oagi.async_task import AsyncTask
9
15
  from oagi.exceptions import (
10
16
  APIError,
11
17
  AuthenticationError,
@@ -19,26 +25,42 @@ from oagi.exceptions import (
19
25
  ValidationError,
20
26
  )
21
27
  from oagi.pil_image import PILImage
22
- from oagi.pyautogui_action_handler import PyautoguiActionHandler
28
+ from oagi.pyautogui_action_handler import PyautoguiActionHandler, PyautoguiConfig
23
29
  from oagi.screenshot_maker import ScreenshotMaker
24
30
  from oagi.short_task import ShortTask
25
31
  from oagi.single_step import single_step
26
32
  from oagi.sync_client import ErrorDetail, ErrorResponse, LLMResponse, SyncClient
27
33
  from oagi.task import Task
28
- from oagi.types import ImageConfig
34
+ from oagi.types import (
35
+ AsyncActionHandler,
36
+ AsyncImageProvider,
37
+ ImageConfig,
38
+ )
29
39
 
30
40
  __all__ = [
31
- # Core classes
41
+ # Core sync classes
32
42
  "Task",
33
43
  "ShortTask",
34
44
  "SyncClient",
45
+ # Core async classes
46
+ "AsyncTask",
47
+ "AsyncShortTask",
48
+ "AsyncClient",
35
49
  # Functions
36
50
  "single_step",
51
+ "async_single_step",
37
52
  # Image classes
38
53
  "PILImage",
39
54
  # Handler classes
40
55
  "PyautoguiActionHandler",
56
+ "PyautoguiConfig",
41
57
  "ScreenshotMaker",
58
+ # Async handler classes
59
+ "AsyncPyautoguiActionHandler",
60
+ "AsyncScreenshotMaker",
61
+ # Async protocols
62
+ "AsyncActionHandler",
63
+ "AsyncImageProvider",
42
64
  # Configuration
43
65
  "ImageConfig",
44
66
  # Response models
@@ -0,0 +1,239 @@
1
+ # -----------------------------------------------------------------------------
2
+ # Copyright (c) OpenAGI Foundation
3
+ # All rights reserved.
4
+ #
5
+ # This file is part of the official API project.
6
+ # Licensed under the MIT License.
7
+ # -----------------------------------------------------------------------------
8
+
9
+ import os
10
+ from functools import wraps
11
+
12
+ import httpx
13
+
14
+ from .exceptions import (
15
+ APIError,
16
+ AuthenticationError,
17
+ ConfigurationError,
18
+ NetworkError,
19
+ NotFoundError,
20
+ RateLimitError,
21
+ RequestTimeoutError,
22
+ ServerError,
23
+ ValidationError,
24
+ )
25
+ from .logging import get_logger
26
+ from .sync_client import ErrorResponse, LLMResponse
27
+
28
+ logger = get_logger("async_client")
29
+
30
+
31
+ def async_log_trace_on_failure(func):
32
+ """Async decorator that logs trace ID when a method fails."""
33
+
34
+ @wraps(func)
35
+ async def wrapper(*args, **kwargs):
36
+ try:
37
+ return await func(*args, **kwargs)
38
+ except Exception as e:
39
+ # Try to get response from the exception if it has one
40
+ if (response := getattr(e, "response", None)) is not None:
41
+ logger.error(f"Request Id: {response.headers.get('x-request-id', '')}")
42
+ logger.error(f"Trace Id: {response.headers.get('x-trace-id', '')}")
43
+ raise
44
+
45
+ return wrapper
46
+
47
+
48
+ class AsyncClient:
49
+ """Async HTTP client for the OAGI API."""
50
+
51
+ def __init__(self, base_url: str | None = None, api_key: str | None = None):
52
+ # Get from environment if not provided
53
+ self.base_url = base_url or os.getenv("OAGI_BASE_URL")
54
+ self.api_key = api_key or os.getenv("OAGI_API_KEY")
55
+
56
+ # Validate required configuration
57
+ if not self.base_url:
58
+ raise ConfigurationError(
59
+ "OAGI base URL must be provided either as 'base_url' parameter or "
60
+ "OAGI_BASE_URL environment variable"
61
+ )
62
+
63
+ if not self.api_key:
64
+ raise ConfigurationError(
65
+ "OAGI API key must be provided either as 'api_key' parameter or "
66
+ "OAGI_API_KEY environment variable"
67
+ )
68
+
69
+ self.base_url = self.base_url.rstrip("/")
70
+ self.client = httpx.AsyncClient(base_url=self.base_url)
71
+ self.timeout = 60
72
+
73
+ logger.info(f"AsyncClient initialized with base_url: {self.base_url}")
74
+
75
+ async def __aenter__(self):
76
+ return self
77
+
78
+ async def __aexit__(self, exc_type, exc_val, exc_tb):
79
+ await self.client.aclose()
80
+
81
+ async def close(self):
82
+ """Close the underlying httpx async client"""
83
+ await self.client.aclose()
84
+
85
+ @async_log_trace_on_failure
86
+ async def create_message(
87
+ self,
88
+ model: str,
89
+ screenshot: str, # base64 encoded
90
+ task_description: str | None = None,
91
+ task_id: str | None = None,
92
+ instruction: str | None = None,
93
+ max_actions: int | None = 5,
94
+ api_version: str | None = None,
95
+ ) -> LLMResponse:
96
+ """
97
+ Call the /v1/message endpoint to analyze task and screenshot
98
+
99
+ Args:
100
+ model: The model to use for task analysis
101
+ screenshot: Base64-encoded screenshot image
102
+ task_description: Description of the task (required for new sessions)
103
+ task_id: Task ID for continuing existing task
104
+ instruction: Additional instruction when continuing a session (only works with task_id)
105
+ max_actions: Maximum number of actions to return (1-20)
106
+ api_version: API version header
107
+
108
+ Returns:
109
+ LLMResponse: The response from the API
110
+
111
+ Raises:
112
+ httpx.HTTPStatusError: For HTTP error responses
113
+ """
114
+ headers = {}
115
+ if api_version:
116
+ headers["x-api-version"] = api_version
117
+ if self.api_key:
118
+ headers["x-api-key"] = self.api_key
119
+
120
+ payload = {"model": model, "screenshot": screenshot}
121
+
122
+ if task_description is not None:
123
+ payload["task_description"] = task_description
124
+ if task_id is not None:
125
+ payload["task_id"] = task_id
126
+ if instruction is not None:
127
+ payload["instruction"] = instruction
128
+ if max_actions is not None:
129
+ payload["max_actions"] = max_actions
130
+
131
+ logger.info(f"Making async API request to /v1/message with model: {model}")
132
+ logger.debug(
133
+ f"Request includes task_description: {task_description is not None}, task_id: {task_id is not None}"
134
+ )
135
+
136
+ try:
137
+ response = await self.client.post(
138
+ "/v1/message", json=payload, headers=headers, timeout=self.timeout
139
+ )
140
+ except httpx.TimeoutException as e:
141
+ logger.error(f"Request timed out after {self.timeout} seconds")
142
+ raise RequestTimeoutError(
143
+ f"Request timed out after {self.timeout} seconds", e
144
+ )
145
+ except httpx.NetworkError as e:
146
+ logger.error(f"Network error: {e}")
147
+ raise NetworkError(f"Network error: {e}", e)
148
+
149
+ try:
150
+ response_data = response.json()
151
+ except ValueError:
152
+ # If response is not JSON, raise API error
153
+ logger.error(f"Non-JSON API response: {response.status_code}")
154
+ raise APIError(
155
+ f"Invalid response format (status {response.status_code})",
156
+ status_code=response.status_code,
157
+ response=response,
158
+ )
159
+
160
+ # Check if it's an error response (non-200 status or has error field)
161
+ if response.status_code != 200:
162
+ error_resp = ErrorResponse(**response_data)
163
+ if error_resp.error:
164
+ error_code = error_resp.error.code
165
+ error_msg = error_resp.error.message
166
+ logger.error(f"API Error [{error_code}]: {error_msg}")
167
+
168
+ # Map to specific exception types based on status code
169
+ exception_class = self._get_exception_class(response.status_code)
170
+ raise exception_class(
171
+ error_msg,
172
+ code=error_code,
173
+ status_code=response.status_code,
174
+ response=response,
175
+ )
176
+ else:
177
+ # Error response without error details
178
+ logger.error(
179
+ f"API error response without details: {response.status_code}"
180
+ )
181
+ exception_class = self._get_exception_class(response.status_code)
182
+ raise exception_class(
183
+ f"API error (status {response.status_code})",
184
+ status_code=response.status_code,
185
+ response=response,
186
+ )
187
+
188
+ # Parse successful response
189
+ result = LLMResponse(**response_data)
190
+
191
+ # Check if the response contains an error (even with 200 status)
192
+ if result.error:
193
+ logger.error(
194
+ f"API Error in response: [{result.error.code}]: {result.error.message}"
195
+ )
196
+ raise APIError(
197
+ result.error.message,
198
+ code=result.error.code,
199
+ status_code=200,
200
+ response=response,
201
+ )
202
+
203
+ logger.info(
204
+ f"Async API request successful - task_id: {result.task_id}, step: {result.current_step}, complete: {result.is_complete}"
205
+ )
206
+ logger.debug(f"Response included {len(result.actions)} actions")
207
+ return result
208
+
209
+ def _get_exception_class(self, status_code: int) -> type[APIError]:
210
+ """Get the appropriate exception class based on status code."""
211
+ status_map = {
212
+ 401: AuthenticationError,
213
+ 404: NotFoundError,
214
+ 422: ValidationError,
215
+ 429: RateLimitError,
216
+ }
217
+
218
+ if status_code >= 500:
219
+ return ServerError
220
+
221
+ return status_map.get(status_code, APIError)
222
+
223
+ async def health_check(self) -> dict:
224
+ """
225
+ Call the /health endpoint for health check
226
+
227
+ Returns:
228
+ dict: Health check response
229
+ """
230
+ logger.debug("Making async health check request")
231
+ try:
232
+ response = await self.client.get("/health")
233
+ response.raise_for_status()
234
+ result = response.json()
235
+ logger.debug("Async health check successful")
236
+ return result
237
+ except httpx.HTTPStatusError as e:
238
+ logger.warning(f"Async health check failed: {e}")
239
+ raise
@@ -0,0 +1,44 @@
1
+ # -----------------------------------------------------------------------------
2
+ # Copyright (c) OpenAGI Foundation
3
+ # All rights reserved.
4
+ #
5
+ # This file is part of the official API project.
6
+ # Licensed under the MIT License.
7
+ # -----------------------------------------------------------------------------
8
+
9
+ import asyncio
10
+
11
+ from .pyautogui_action_handler import PyautoguiActionHandler, PyautoguiConfig
12
+ from .types import Action
13
+
14
+
15
+ class AsyncPyautoguiActionHandler:
16
+ """
17
+ Async wrapper for PyautoguiActionHandler that runs actions in a thread pool.
18
+
19
+ This allows PyAutoGUI operations to be non-blocking in async contexts,
20
+ enabling concurrent execution of other async tasks while GUI actions are performed.
21
+ """
22
+
23
+ def __init__(self, config: PyautoguiConfig | None = None):
24
+ """Initialize with optional configuration.
25
+
26
+ Args:
27
+ config: PyautoguiConfig instance for customizing behavior
28
+ """
29
+ self.sync_handler = PyautoguiActionHandler(config=config)
30
+ self.config = config or PyautoguiConfig()
31
+
32
+ async def __call__(self, actions: list[Action]) -> None:
33
+ """
34
+ Execute actions asynchronously using a thread pool executor.
35
+
36
+ This prevents PyAutoGUI operations from blocking the async event loop,
37
+ allowing other coroutines to run while GUI actions are being performed.
38
+
39
+ Args:
40
+ actions: List of actions to execute
41
+ """
42
+ loop = asyncio.get_event_loop()
43
+ # Run the synchronous handler in a thread pool to avoid blocking
44
+ await loop.run_in_executor(None, self.sync_handler, actions)
@@ -0,0 +1,47 @@
1
+ # -----------------------------------------------------------------------------
2
+ # Copyright (c) OpenAGI Foundation
3
+ # All rights reserved.
4
+ #
5
+ # This file is part of the official API project.
6
+ # Licensed under the MIT License.
7
+ # -----------------------------------------------------------------------------
8
+
9
+ import asyncio
10
+
11
+ from .screenshot_maker import ScreenshotMaker
12
+ from .types import Image, ImageConfig
13
+
14
+
15
+ class AsyncScreenshotMaker:
16
+ """
17
+ Async wrapper for ScreenshotMaker that captures screenshots in a thread pool.
18
+
19
+ This allows screenshot capture to be non-blocking in async contexts,
20
+ enabling concurrent execution of other async tasks while screenshots are taken.
21
+ """
22
+
23
+ def __init__(self, config: ImageConfig | None = None):
24
+ """Initialize with optional image configuration.
25
+
26
+ Args:
27
+ config: ImageConfig instance for customizing screenshot format and quality
28
+ """
29
+ self.sync_screenshot_maker = ScreenshotMaker(config=config)
30
+ self.config = config
31
+
32
+ async def __call__(self) -> Image:
33
+ """
34
+ Capture a screenshot asynchronously using a thread pool executor.
35
+
36
+ This prevents screenshot capture from blocking the async event loop,
37
+ allowing other coroutines to run while the screenshot is being taken.
38
+
39
+ Returns:
40
+ Image: The captured screenshot as a PILImage
41
+ """
42
+ loop = asyncio.get_event_loop()
43
+ # Run the synchronous screenshot capture in a thread pool to avoid blocking
44
+ return await loop.run_in_executor(None, self.sync_screenshot_maker)
45
+
46
+ async def last_image(self) -> Image:
47
+ return self.sync_screenshot_maker.last_image()