camel-ai 0.2.68__py3-none-any.whl → 0.2.69a1__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 camel-ai might be problematic. Click here for more details.
- camel/__init__.py +1 -1
- camel/agents/chat_agent.py +170 -11
- camel/configs/vllm_config.py +2 -0
- camel/datagen/self_improving_cot.py +1 -1
- camel/memories/context_creators/score_based.py +129 -87
- camel/runtimes/configs.py +11 -11
- camel/runtimes/daytona_runtime.py +4 -4
- camel/runtimes/docker_runtime.py +6 -6
- camel/runtimes/remote_http_runtime.py +5 -5
- camel/societies/workforce/prompts.py +13 -12
- camel/societies/workforce/single_agent_worker.py +252 -22
- camel/societies/workforce/utils.py +10 -2
- camel/societies/workforce/worker.py +21 -45
- camel/societies/workforce/workforce.py +36 -15
- camel/tasks/task.py +18 -12
- camel/toolkits/__init__.py +2 -0
- camel/toolkits/aci_toolkit.py +19 -19
- camel/toolkits/arxiv_toolkit.py +6 -6
- camel/toolkits/dappier_toolkit.py +5 -5
- camel/toolkits/file_write_toolkit.py +10 -10
- camel/toolkits/github_toolkit.py +3 -3
- camel/toolkits/non_visual_browser_toolkit/__init__.py +18 -0
- camel/toolkits/non_visual_browser_toolkit/actions.py +196 -0
- camel/toolkits/non_visual_browser_toolkit/agent.py +278 -0
- camel/toolkits/non_visual_browser_toolkit/browser_non_visual_toolkit.py +363 -0
- camel/toolkits/non_visual_browser_toolkit/nv_browser_session.py +175 -0
- camel/toolkits/non_visual_browser_toolkit/snapshot.js +188 -0
- camel/toolkits/non_visual_browser_toolkit/snapshot.py +164 -0
- camel/toolkits/pptx_toolkit.py +4 -4
- camel/toolkits/sympy_toolkit.py +1 -1
- camel/toolkits/task_planning_toolkit.py +3 -3
- camel/toolkits/thinking_toolkit.py +1 -1
- {camel_ai-0.2.68.dist-info → camel_ai-0.2.69a1.dist-info}/METADATA +1 -1
- {camel_ai-0.2.68.dist-info → camel_ai-0.2.69a1.dist-info}/RECORD +36 -29
- {camel_ai-0.2.68.dist-info → camel_ai-0.2.69a1.dist-info}/WHEEL +0 -0
- {camel_ai-0.2.68.dist-info → camel_ai-0.2.69a1.dist-info}/licenses/LICENSE +0 -0
camel/tasks/task.py
CHANGED
|
@@ -25,10 +25,12 @@ from typing import (
|
|
|
25
25
|
Union,
|
|
26
26
|
)
|
|
27
27
|
|
|
28
|
-
from pydantic import BaseModel
|
|
28
|
+
from pydantic import BaseModel, Field
|
|
29
29
|
|
|
30
30
|
if TYPE_CHECKING:
|
|
31
31
|
from camel.agents import ChatAgent
|
|
32
|
+
import uuid
|
|
33
|
+
|
|
32
34
|
from camel.logger import get_logger
|
|
33
35
|
from camel.messages import BaseMessage
|
|
34
36
|
from camel.prompts import TextPrompt
|
|
@@ -142,27 +144,29 @@ class Task(BaseModel):
|
|
|
142
144
|
content (str): string content for task.
|
|
143
145
|
id (str): An unique string identifier for the task. This should
|
|
144
146
|
ideally be provided by the provider/model which created the task.
|
|
145
|
-
(default: :obj
|
|
147
|
+
(default: :obj:`uuid.uuid4()`)
|
|
146
148
|
state (TaskState): The state which should be OPEN, RUNNING, DONE or
|
|
147
|
-
DELETED. (default: :obj
|
|
148
|
-
type (Optional[str]): task type. (default: :obj
|
|
149
|
+
DELETED. (default: :obj:`TaskState.FAILED`)
|
|
150
|
+
type (Optional[str]): task type. (default: :obj:`None`)
|
|
149
151
|
parent (Optional[Task]): The parent task, None for root task.
|
|
150
|
-
(default: :obj
|
|
152
|
+
(default: :obj:`None`)
|
|
151
153
|
subtasks (List[Task]): The childrent sub-tasks for the task.
|
|
152
|
-
(default: :obj
|
|
154
|
+
(default: :obj:`[]`)
|
|
153
155
|
result (Optional[str]): The answer for the task.
|
|
154
|
-
(default: :obj
|
|
156
|
+
(default: :obj:`""`)
|
|
155
157
|
failure_count (int): The failure count for the task.
|
|
156
|
-
(default: :obj
|
|
158
|
+
(default: :obj:`0`)
|
|
157
159
|
additional_info (Optional[Dict[str, Any]]): Additional information for
|
|
158
|
-
the task. (default: :obj
|
|
160
|
+
the task. (default: :obj:`None`)
|
|
159
161
|
"""
|
|
160
162
|
|
|
161
163
|
content: str
|
|
162
164
|
|
|
163
|
-
id: str =
|
|
165
|
+
id: str = Field(default_factory=lambda: str(uuid.uuid4()))
|
|
164
166
|
|
|
165
|
-
state: TaskState =
|
|
167
|
+
state: TaskState = (
|
|
168
|
+
TaskState.FAILED
|
|
169
|
+
) # TODO: Add logic for OPEN in workforce.py
|
|
166
170
|
|
|
167
171
|
type: Optional[str] = None
|
|
168
172
|
|
|
@@ -204,7 +208,9 @@ class Task(BaseModel):
|
|
|
204
208
|
|
|
205
209
|
def reset(self):
|
|
206
210
|
r"""Reset Task to initial state."""
|
|
207
|
-
self.state =
|
|
211
|
+
self.state = (
|
|
212
|
+
TaskState.FAILED
|
|
213
|
+
) # TODO: Add logic for OPEN in workforce.py
|
|
208
214
|
self.result = ""
|
|
209
215
|
|
|
210
216
|
def update_result(self, result: str):
|
camel/toolkits/__init__.py
CHANGED
|
@@ -77,6 +77,7 @@ from .aci_toolkit import ACIToolkit
|
|
|
77
77
|
from .playwright_mcp_toolkit import PlaywrightMCPToolkit
|
|
78
78
|
from .wolfram_alpha_toolkit import WolframAlphaToolkit
|
|
79
79
|
from .task_planning_toolkit import TaskPlanningToolkit
|
|
80
|
+
from .non_visual_browser_toolkit import BrowserNonVisualToolkit
|
|
80
81
|
|
|
81
82
|
|
|
82
83
|
__all__ = [
|
|
@@ -142,4 +143,5 @@ __all__ = [
|
|
|
142
143
|
'WolframAlphaToolkit',
|
|
143
144
|
'BohriumToolkit',
|
|
144
145
|
'TaskPlanningToolkit',
|
|
146
|
+
'BrowserNonVisualToolkit',
|
|
145
147
|
]
|
camel/toolkits/aci_toolkit.py
CHANGED
|
@@ -48,14 +48,14 @@ class ACIToolkit(BaseToolkit):
|
|
|
48
48
|
|
|
49
49
|
Args:
|
|
50
50
|
api_key (Optional[str]): The API key for authentication.
|
|
51
|
-
(default: :obj
|
|
51
|
+
(default: :obj:`None`)
|
|
52
52
|
base_url (Optional[str]): The base URL for the ACI API.
|
|
53
|
-
(default: :obj
|
|
53
|
+
(default: :obj:`None`)
|
|
54
54
|
linked_account_owner_id (Optional[str]): ID of the owner of the
|
|
55
55
|
linked account, e.g., "johndoe"
|
|
56
|
-
(default: :obj
|
|
56
|
+
(default: :obj:`None`)
|
|
57
57
|
timeout (Optional[float]): Request timeout.
|
|
58
|
-
(default: :obj
|
|
58
|
+
(default: :obj:`None`)
|
|
59
59
|
"""
|
|
60
60
|
from aci import ACI
|
|
61
61
|
|
|
@@ -80,20 +80,20 @@ class ACIToolkit(BaseToolkit):
|
|
|
80
80
|
Args:
|
|
81
81
|
intent (Optional[str]): Search results will be sorted by relevance
|
|
82
82
|
to this intent.
|
|
83
|
-
(default: :obj
|
|
83
|
+
(default: :obj:`None`)
|
|
84
84
|
allowed_app_only (bool): If true, only return apps that
|
|
85
85
|
are allowed by the agent/accessor, identified by the api key.
|
|
86
|
-
(default: :obj
|
|
86
|
+
(default: :obj:`True`)
|
|
87
87
|
include_functions (bool): If true, include functions
|
|
88
88
|
(name and description) in the search results.
|
|
89
|
-
(default: :obj
|
|
89
|
+
(default: :obj:`False`)
|
|
90
90
|
categories (Optional[List[str]]): List of categories to filter the
|
|
91
91
|
search results. Defaults to an empty list.
|
|
92
|
-
(default: :obj
|
|
92
|
+
(default: :obj:`None`)
|
|
93
93
|
limit (Optional[int]): Maximum number of results to return.
|
|
94
|
-
(default: :obj
|
|
94
|
+
(default: :obj:`10`)
|
|
95
95
|
offset (Optional[int]): Offset for pagination.
|
|
96
|
-
(default: :obj
|
|
96
|
+
(default: :obj:`0`)
|
|
97
97
|
|
|
98
98
|
Returns:
|
|
99
99
|
Optional[List[AppBasic]]: List of matching apps if successful,
|
|
@@ -123,10 +123,10 @@ class ACIToolkit(BaseToolkit):
|
|
|
123
123
|
|
|
124
124
|
Args:
|
|
125
125
|
app_names (Optional[List[str]]): List of app names to filter the
|
|
126
|
-
results. (default: :obj
|
|
126
|
+
results. (default: :obj:`None`)
|
|
127
127
|
limit (Optional[int]): Maximum number of results to return.
|
|
128
|
-
(default: :obj
|
|
129
|
-
offset (Optional[int]): Offset for pagination. (default: :obj
|
|
128
|
+
(default: :obj:`10`)
|
|
129
|
+
offset (Optional[int]): Offset for pagination. (default: :obj:`0`)
|
|
130
130
|
|
|
131
131
|
Returns:
|
|
132
132
|
Union[List[AppConfiguration], str]: List of configured apps if
|
|
@@ -356,15 +356,15 @@ class ACIToolkit(BaseToolkit):
|
|
|
356
356
|
|
|
357
357
|
Args:
|
|
358
358
|
app_names (Optional[List[str]]): List of app names to filter the
|
|
359
|
-
search results. (default: :obj
|
|
359
|
+
search results. (default: :obj:`None`)
|
|
360
360
|
intent (Optional[str]): The search query/intent.
|
|
361
|
-
(default: :obj
|
|
361
|
+
(default: :obj:`None`)
|
|
362
362
|
allowed_apps_only (bool): If true, only return
|
|
363
|
-
functions from allowed apps. (default: :obj
|
|
363
|
+
functions from allowed apps. (default: :obj:`True`)
|
|
364
364
|
limit (Optional[int]): Maximum number of results to return.
|
|
365
|
-
(default: :obj
|
|
365
|
+
(default: :obj:`10`)
|
|
366
366
|
offset (Optional[int]): Offset for pagination.
|
|
367
|
-
(default: :obj
|
|
367
|
+
(default: :obj:`0`)
|
|
368
368
|
|
|
369
369
|
Returns:
|
|
370
370
|
List[Dict]: List of matching functions
|
|
@@ -395,7 +395,7 @@ class ACIToolkit(BaseToolkit):
|
|
|
395
395
|
owner id in the ACI dashboard (https://platform.aci.dev).
|
|
396
396
|
allowed_apps_only (bool): If true, only returns functions/apps
|
|
397
397
|
that are allowed to be used by the agent/accessor, identified
|
|
398
|
-
by the api key. (default: :obj
|
|
398
|
+
by the api key. (default: :obj:`False`)
|
|
399
399
|
|
|
400
400
|
Returns:
|
|
401
401
|
Dict: Result of the function execution
|
camel/toolkits/arxiv_toolkit.py
CHANGED
|
@@ -49,9 +49,9 @@ class ArxivToolkit(BaseToolkit):
|
|
|
49
49
|
query (str): The search query string used to search for papers on
|
|
50
50
|
arXiv.
|
|
51
51
|
paper_ids (List[str], optional): A list of specific arXiv paper
|
|
52
|
-
IDs to search for. (default: :obj
|
|
52
|
+
IDs to search for. (default: :obj:`None`)
|
|
53
53
|
max_results (int, optional): The maximum number of search results
|
|
54
|
-
to retrieve. (default: :obj
|
|
54
|
+
to retrieve. (default: :obj:`5`)
|
|
55
55
|
|
|
56
56
|
Returns:
|
|
57
57
|
Generator: A generator that yields results from the arXiv search
|
|
@@ -80,9 +80,9 @@ class ArxivToolkit(BaseToolkit):
|
|
|
80
80
|
Args:
|
|
81
81
|
query (str): The search query string.
|
|
82
82
|
paper_ids (List[str], optional): A list of specific arXiv paper
|
|
83
|
-
IDs to search for. (default: :obj
|
|
83
|
+
IDs to search for. (default: :obj:`None`)
|
|
84
84
|
max_results (int, optional): The maximum number of search results
|
|
85
|
-
to return. (default: :obj
|
|
85
|
+
to return. (default: :obj:`5`)
|
|
86
86
|
|
|
87
87
|
Returns:
|
|
88
88
|
List[Dict[str, str]]: A list of dictionaries, each containing
|
|
@@ -138,9 +138,9 @@ class ArxivToolkit(BaseToolkit):
|
|
|
138
138
|
Args:
|
|
139
139
|
query (str): The search query string.
|
|
140
140
|
paper_ids (List[str], optional): A list of specific arXiv paper
|
|
141
|
-
IDs to download. (default: :obj
|
|
141
|
+
IDs to download. (default: :obj:`None`)
|
|
142
142
|
max_results (int, optional): The maximum number of search results
|
|
143
|
-
to download. (default: :obj
|
|
143
|
+
to download. (default: :obj:`5`)
|
|
144
144
|
output_dir (str, optional): The directory to save the downloaded
|
|
145
145
|
PDFs. Defaults to the current directory.
|
|
146
146
|
|
|
@@ -126,22 +126,22 @@ class DappierToolkit(BaseToolkit):
|
|
|
126
126
|
query (str): The user query for retrieving recommendations.
|
|
127
127
|
data_model_id (str, optional): The data model ID to use for
|
|
128
128
|
recommendations. Data model IDs always start with the prefix
|
|
129
|
-
"dm_". (default: :obj
|
|
129
|
+
"dm_". (default: :obj:`dm_01j0pb465keqmatq9k83dthx34`)
|
|
130
130
|
similarity_top_k (int, optional): The number of top documents to
|
|
131
|
-
retrieve based on similarity. (default: :obj
|
|
131
|
+
retrieve based on similarity. (default: :obj:`9`)
|
|
132
132
|
ref (Optional[str], optional): The site domain where AI
|
|
133
|
-
recommendations should be displayed. (default: :obj
|
|
133
|
+
recommendations should be displayed. (default: :obj:`None`)
|
|
134
134
|
num_articles_ref (int, optional): The minimum number of articles
|
|
135
135
|
to return from the specified reference domain (`ref`). The
|
|
136
136
|
remaining articles will come from other sites in the RAG
|
|
137
|
-
model. (default: :obj
|
|
137
|
+
model. (default: :obj:`0`)
|
|
138
138
|
search_algorithm (Literal[
|
|
139
139
|
"most_recent",
|
|
140
140
|
"semantic",
|
|
141
141
|
"most_recent_semantic",
|
|
142
142
|
"trending",
|
|
143
143
|
], optional): The search algorithm to use for retrieving
|
|
144
|
-
articles. (default: :obj
|
|
144
|
+
articles. (default: :obj:`most_recent`)
|
|
145
145
|
|
|
146
146
|
Returns:
|
|
147
147
|
List[Dict[str, str]]: A list of recommended articles or content
|
|
@@ -50,11 +50,11 @@ class FileWriteToolkit(BaseToolkit):
|
|
|
50
50
|
output_dir (str): The default directory for output files.
|
|
51
51
|
Defaults to the current working directory.
|
|
52
52
|
timeout (Optional[float]): The timeout for the toolkit.
|
|
53
|
-
(default: :obj
|
|
53
|
+
(default: :obj:`None`)
|
|
54
54
|
default_encoding (str): Default character encoding for text
|
|
55
|
-
operations. (default: :obj
|
|
55
|
+
operations. (default: :obj:`utf-8`)
|
|
56
56
|
backup_enabled (bool): Whether to create backups of existing files
|
|
57
|
-
before overwriting. (default: :obj
|
|
57
|
+
before overwriting. (default: :obj:`True`)
|
|
58
58
|
"""
|
|
59
59
|
super().__init__(timeout=timeout)
|
|
60
60
|
self.output_dir = Path(output_dir).resolve()
|
|
@@ -96,7 +96,7 @@ class FileWriteToolkit(BaseToolkit):
|
|
|
96
96
|
Args:
|
|
97
97
|
file_path (Path): The target file path.
|
|
98
98
|
content (str): The text content to write.
|
|
99
|
-
encoding (str): Character encoding to use. (default: :obj
|
|
99
|
+
encoding (str): Character encoding to use. (default: :obj:`utf-8`)
|
|
100
100
|
"""
|
|
101
101
|
with file_path.open("w", encoding=encoding) as f:
|
|
102
102
|
f.write(content)
|
|
@@ -157,7 +157,7 @@ class FileWriteToolkit(BaseToolkit):
|
|
|
157
157
|
content (str): The text content to write.
|
|
158
158
|
use_latex (bool): Whether to use LaTeX for rendering. (requires
|
|
159
159
|
LaTeX toolchain). If False, uses FPDF for simpler PDF
|
|
160
|
-
generation. (default: :obj
|
|
160
|
+
generation. (default: :obj:`False`)
|
|
161
161
|
|
|
162
162
|
Raises:
|
|
163
163
|
RuntimeError: If the 'pylatex' or 'fpdf' library is not installed
|
|
@@ -236,7 +236,7 @@ class FileWriteToolkit(BaseToolkit):
|
|
|
236
236
|
file_path (Path): The target file path.
|
|
237
237
|
content (Union[str, List[List]]): The CSV content as a string or
|
|
238
238
|
list of lists.
|
|
239
|
-
encoding (str): Character encoding to use. (default: :obj
|
|
239
|
+
encoding (str): Character encoding to use. (default: :obj:`utf-8`)
|
|
240
240
|
"""
|
|
241
241
|
import csv
|
|
242
242
|
|
|
@@ -259,7 +259,7 @@ class FileWriteToolkit(BaseToolkit):
|
|
|
259
259
|
Args:
|
|
260
260
|
file_path (Path): The target file path.
|
|
261
261
|
content (str): The JSON content as a string.
|
|
262
|
-
encoding (str): Character encoding to use. (default: :obj
|
|
262
|
+
encoding (str): Character encoding to use. (default: :obj:`utf-8`)
|
|
263
263
|
"""
|
|
264
264
|
import json
|
|
265
265
|
|
|
@@ -288,7 +288,7 @@ class FileWriteToolkit(BaseToolkit):
|
|
|
288
288
|
Args:
|
|
289
289
|
file_path (Path): The target file path.
|
|
290
290
|
content (str): The YAML content as a string.
|
|
291
|
-
encoding (str): Character encoding to use. (default: :obj
|
|
291
|
+
encoding (str): Character encoding to use. (default: :obj:`utf-8`)
|
|
292
292
|
"""
|
|
293
293
|
with file_path.open("w", encoding=encoding) as f:
|
|
294
294
|
f.write(content)
|
|
@@ -302,7 +302,7 @@ class FileWriteToolkit(BaseToolkit):
|
|
|
302
302
|
Args:
|
|
303
303
|
file_path (Path): The target file path.
|
|
304
304
|
content (str): The HTML content to write.
|
|
305
|
-
encoding (str): Character encoding to use. (default: :obj
|
|
305
|
+
encoding (str): Character encoding to use. (default: :obj:`utf-8`)
|
|
306
306
|
"""
|
|
307
307
|
with file_path.open("w", encoding=encoding) as f:
|
|
308
308
|
f.write(content)
|
|
@@ -316,7 +316,7 @@ class FileWriteToolkit(BaseToolkit):
|
|
|
316
316
|
Args:
|
|
317
317
|
file_path (Path): The target file path.
|
|
318
318
|
content (str): The Markdown content to write.
|
|
319
|
-
encoding (str): Character encoding to use. (default: :obj
|
|
319
|
+
encoding (str): Character encoding to use. (default: :obj:`utf-8`)
|
|
320
320
|
"""
|
|
321
321
|
with file_path.open("w", encoding=encoding) as f:
|
|
322
322
|
f.write(content)
|
camel/toolkits/github_toolkit.py
CHANGED
|
@@ -158,7 +158,7 @@ class GithubToolkit(BaseToolkit):
|
|
|
158
158
|
Args:
|
|
159
159
|
repo_name (str): The name of the GitHub repository.
|
|
160
160
|
state (Literal["open", "closed", "all"]): The state of pull
|
|
161
|
-
requests to retrieve. (default: :obj
|
|
161
|
+
requests to retrieve. (default: :obj:`all`)
|
|
162
162
|
Options are:
|
|
163
163
|
- "open": Retrieve only open pull requests.
|
|
164
164
|
- "closed": Retrieve only closed pull requests.
|
|
@@ -202,7 +202,7 @@ class GithubToolkit(BaseToolkit):
|
|
|
202
202
|
Args:
|
|
203
203
|
repo_name (str): The name of the GitHub repository.
|
|
204
204
|
state (Literal["open", "closed", "all"]): The state of pull
|
|
205
|
-
requests to retrieve. (default: :obj
|
|
205
|
+
requests to retrieve. (default: :obj:`all`)
|
|
206
206
|
Options are:
|
|
207
207
|
- "open": Retrieve only open pull requests.
|
|
208
208
|
- "closed": Retrieve only closed pull requests.
|
|
@@ -285,7 +285,7 @@ class GithubToolkit(BaseToolkit):
|
|
|
285
285
|
repo_name (str): The name of the GitHub repository.
|
|
286
286
|
path (str): The repository path to start the traversal from.
|
|
287
287
|
empty string means starts from the root directory.
|
|
288
|
-
(default: :obj
|
|
288
|
+
(default: :obj:`""`)
|
|
289
289
|
|
|
290
290
|
Returns:
|
|
291
291
|
List[str]: A list of file paths within the specified directory
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
|
2
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
+
# you may not use this file except in compliance with the License.
|
|
4
|
+
# You may obtain a copy of the License at
|
|
5
|
+
#
|
|
6
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
#
|
|
8
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
# See the License for the specific language governing permissions and
|
|
12
|
+
# limitations under the License.
|
|
13
|
+
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
|
14
|
+
from .browser_non_visual_toolkit import BrowserNonVisualToolkit
|
|
15
|
+
|
|
16
|
+
__all__ = [
|
|
17
|
+
"BrowserNonVisualToolkit",
|
|
18
|
+
]
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
|
2
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
+
# you may not use this file except in compliance with the License.
|
|
4
|
+
# You may obtain a copy of the License at
|
|
5
|
+
#
|
|
6
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
#
|
|
8
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
# See the License for the specific language governing permissions and
|
|
12
|
+
# limitations under the License.
|
|
13
|
+
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
|
14
|
+
import asyncio
|
|
15
|
+
from typing import TYPE_CHECKING, Any, Dict
|
|
16
|
+
|
|
17
|
+
if TYPE_CHECKING:
|
|
18
|
+
from playwright.async_api import Page
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class ActionExecutor:
|
|
22
|
+
r"""Executes high-level actions (click, type …) on a Playwright Page."""
|
|
23
|
+
|
|
24
|
+
# Configuration constants
|
|
25
|
+
DEFAULT_TIMEOUT = 5000 # 5 seconds
|
|
26
|
+
SHORT_TIMEOUT = 2000 # 2 seconds
|
|
27
|
+
|
|
28
|
+
def __init__(self, page: "Page"):
|
|
29
|
+
self.page = page
|
|
30
|
+
|
|
31
|
+
# ------------------------------------------------------------------
|
|
32
|
+
# Public helpers
|
|
33
|
+
# ------------------------------------------------------------------
|
|
34
|
+
async def execute(self, action: Dict[str, Any]) -> str:
|
|
35
|
+
if not action:
|
|
36
|
+
return "No action to execute"
|
|
37
|
+
|
|
38
|
+
action_type = action.get("type")
|
|
39
|
+
if not action_type:
|
|
40
|
+
return "Error: action has no type"
|
|
41
|
+
|
|
42
|
+
try:
|
|
43
|
+
# small helper to ensure basic stability
|
|
44
|
+
await self._wait_dom_stable()
|
|
45
|
+
|
|
46
|
+
handler = {
|
|
47
|
+
"click": self._click,
|
|
48
|
+
"type": self._type,
|
|
49
|
+
"select": self._select,
|
|
50
|
+
"wait": self._wait,
|
|
51
|
+
"extract": self._extract,
|
|
52
|
+
"scroll": self._scroll,
|
|
53
|
+
"enter": self._enter,
|
|
54
|
+
}.get(action_type)
|
|
55
|
+
|
|
56
|
+
if handler is None:
|
|
57
|
+
return f"Error: Unknown action type '{action_type}'"
|
|
58
|
+
|
|
59
|
+
return await handler(action)
|
|
60
|
+
except Exception as exc:
|
|
61
|
+
return f"Error executing {action_type}: {exc}"
|
|
62
|
+
|
|
63
|
+
# ------------------------------------------------------------------
|
|
64
|
+
# Internal handlers
|
|
65
|
+
# ------------------------------------------------------------------
|
|
66
|
+
async def _click(self, action: Dict[str, Any]) -> str:
|
|
67
|
+
ref = action.get("ref")
|
|
68
|
+
text = action.get("text")
|
|
69
|
+
selector = action.get("selector")
|
|
70
|
+
if not (ref or text or selector):
|
|
71
|
+
return "Error: click requires ref/text/selector"
|
|
72
|
+
|
|
73
|
+
strategies = []
|
|
74
|
+
if selector:
|
|
75
|
+
strategies.append(selector)
|
|
76
|
+
if text:
|
|
77
|
+
strategies.append(f'text="{text}"')
|
|
78
|
+
if ref:
|
|
79
|
+
strategies.append(f"[aria-ref='{ref}']")
|
|
80
|
+
|
|
81
|
+
for sel in strategies:
|
|
82
|
+
try:
|
|
83
|
+
if await self.page.locator(sel).count() > 0:
|
|
84
|
+
await self.page.click(
|
|
85
|
+
sel, timeout=self.SHORT_TIMEOUT, force=True
|
|
86
|
+
)
|
|
87
|
+
return f"Clicked element via {sel}"
|
|
88
|
+
except Exception:
|
|
89
|
+
pass
|
|
90
|
+
return "Error: Could not click element"
|
|
91
|
+
|
|
92
|
+
async def _type(self, action: Dict[str, Any]) -> str:
|
|
93
|
+
ref = action.get("ref")
|
|
94
|
+
selector = action.get("selector")
|
|
95
|
+
text = action.get("text", "")
|
|
96
|
+
if not (ref or selector):
|
|
97
|
+
return "Error: type requires ref/selector"
|
|
98
|
+
target = selector or f"[aria-ref='{ref}']"
|
|
99
|
+
try:
|
|
100
|
+
await self.page.fill(target, text, timeout=self.SHORT_TIMEOUT)
|
|
101
|
+
return f"Typed '{text}' into {target}"
|
|
102
|
+
except Exception as exc:
|
|
103
|
+
return f"Type failed: {exc}"
|
|
104
|
+
|
|
105
|
+
async def _select(self, action: Dict[str, Any]) -> str:
|
|
106
|
+
ref = action.get("ref")
|
|
107
|
+
selector = action.get("selector")
|
|
108
|
+
value = action.get("value", "")
|
|
109
|
+
if not (ref or selector):
|
|
110
|
+
return "Error: select requires ref/selector"
|
|
111
|
+
target = selector or f"[aria-ref='{ref}']"
|
|
112
|
+
try:
|
|
113
|
+
await self.page.select_option(
|
|
114
|
+
target, value, timeout=self.DEFAULT_TIMEOUT
|
|
115
|
+
)
|
|
116
|
+
return f"Selected '{value}' in {target}"
|
|
117
|
+
except Exception as exc:
|
|
118
|
+
return f"Select failed: {exc}"
|
|
119
|
+
|
|
120
|
+
async def _wait(self, action: Dict[str, Any]) -> str:
|
|
121
|
+
if "timeout" in action:
|
|
122
|
+
ms = action["timeout"]
|
|
123
|
+
await asyncio.sleep(ms / 1000)
|
|
124
|
+
return f"Waited {ms}ms"
|
|
125
|
+
if "selector" in action:
|
|
126
|
+
sel = action["selector"]
|
|
127
|
+
await self.page.wait_for_selector(
|
|
128
|
+
sel, timeout=self.DEFAULT_TIMEOUT
|
|
129
|
+
)
|
|
130
|
+
return f"Waited for {sel}"
|
|
131
|
+
return "Error: wait requires timeout/selector"
|
|
132
|
+
|
|
133
|
+
async def _extract(self, action: Dict[str, Any]) -> str:
|
|
134
|
+
ref = action.get("ref")
|
|
135
|
+
if not ref:
|
|
136
|
+
return "Error: extract requires ref"
|
|
137
|
+
target = f"[aria-ref='{ref}']"
|
|
138
|
+
await self.page.wait_for_selector(target, timeout=self.DEFAULT_TIMEOUT)
|
|
139
|
+
txt = await self.page.text_content(target)
|
|
140
|
+
return f"Extracted: {txt[:100] if txt else 'None'}"
|
|
141
|
+
|
|
142
|
+
async def _scroll(self, action: Dict[str, Any]) -> str:
|
|
143
|
+
direction = action.get("direction", "down")
|
|
144
|
+
amount = action.get("amount", 300)
|
|
145
|
+
|
|
146
|
+
# Validate inputs to prevent injection
|
|
147
|
+
if direction not in ("up", "down"):
|
|
148
|
+
return "Error: direction must be 'up' or 'down'"
|
|
149
|
+
|
|
150
|
+
try:
|
|
151
|
+
# Safely convert amount to integer and clamp to reasonable range
|
|
152
|
+
amount_int = int(amount)
|
|
153
|
+
amount_int = max(
|
|
154
|
+
-5000, min(5000, amount_int)
|
|
155
|
+
) # Clamp between -5000 and 5000
|
|
156
|
+
except (ValueError, TypeError):
|
|
157
|
+
return "Error: amount must be a valid number"
|
|
158
|
+
|
|
159
|
+
# Use safe evaluation with bound parameters
|
|
160
|
+
scroll_offset = amount_int if direction == "down" else -amount_int
|
|
161
|
+
await self.page.evaluate(f"window.scrollBy(0, {scroll_offset})")
|
|
162
|
+
await asyncio.sleep(0.5)
|
|
163
|
+
return f"Scrolled {direction} by {abs(amount_int)}px"
|
|
164
|
+
|
|
165
|
+
async def _enter(self, action: Dict[str, Any]) -> str:
|
|
166
|
+
ref = action.get("ref")
|
|
167
|
+
selector = action.get("selector")
|
|
168
|
+
if ref:
|
|
169
|
+
await self.page.focus(f"[aria-ref='{ref}']")
|
|
170
|
+
elif selector:
|
|
171
|
+
await self.page.focus(selector)
|
|
172
|
+
await self.page.keyboard.press("Enter")
|
|
173
|
+
await asyncio.sleep(0.3)
|
|
174
|
+
return "Pressed Enter"
|
|
175
|
+
|
|
176
|
+
# utilities
|
|
177
|
+
async def _wait_dom_stable(self) -> None:
|
|
178
|
+
try:
|
|
179
|
+
await self.page.wait_for_load_state(
|
|
180
|
+
'domcontentloaded', timeout=self.SHORT_TIMEOUT
|
|
181
|
+
)
|
|
182
|
+
except Exception:
|
|
183
|
+
pass
|
|
184
|
+
|
|
185
|
+
# static helpers
|
|
186
|
+
@staticmethod
|
|
187
|
+
def should_update_snapshot(action: Dict[str, Any]) -> bool:
|
|
188
|
+
change_types = {
|
|
189
|
+
"click",
|
|
190
|
+
"type",
|
|
191
|
+
"select",
|
|
192
|
+
"scroll",
|
|
193
|
+
"navigate",
|
|
194
|
+
"enter",
|
|
195
|
+
}
|
|
196
|
+
return action.get("type") in change_types
|