camel-ai 0.2.22__py3-none-any.whl → 0.2.23__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.

Files changed (110) hide show
  1. camel/__init__.py +1 -1
  2. camel/agents/_types.py +41 -0
  3. camel/agents/_utils.py +188 -0
  4. camel/agents/chat_agent.py +570 -965
  5. camel/agents/knowledge_graph_agent.py +7 -1
  6. camel/agents/multi_hop_generator_agent.py +1 -1
  7. camel/configs/base_config.py +10 -13
  8. camel/configs/deepseek_config.py +4 -30
  9. camel/configs/gemini_config.py +5 -31
  10. camel/configs/openai_config.py +14 -32
  11. camel/configs/qwen_config.py +36 -36
  12. camel/datagen/self_improving_cot.py +81 -3
  13. camel/datagen/self_instruct/filter/instruction_filter.py +19 -3
  14. camel/datagen/self_instruct/self_instruct.py +52 -3
  15. camel/datasets/__init__.py +28 -0
  16. camel/datasets/base.py +969 -0
  17. camel/environments/__init__.py +16 -0
  18. camel/environments/base.py +503 -0
  19. camel/extractors/__init__.py +16 -0
  20. camel/extractors/base.py +263 -0
  21. camel/memories/agent_memories.py +16 -1
  22. camel/memories/blocks/chat_history_block.py +10 -2
  23. camel/memories/blocks/vectordb_block.py +1 -0
  24. camel/memories/context_creators/score_based.py +20 -3
  25. camel/memories/records.py +10 -0
  26. camel/messages/base.py +8 -8
  27. camel/models/__init__.py +2 -0
  28. camel/models/_utils.py +57 -0
  29. camel/models/aiml_model.py +48 -17
  30. camel/models/anthropic_model.py +41 -3
  31. camel/models/azure_openai_model.py +39 -3
  32. camel/models/base_audio_model.py +92 -0
  33. camel/models/base_model.py +88 -13
  34. camel/models/cohere_model.py +88 -11
  35. camel/models/deepseek_model.py +107 -45
  36. camel/models/fish_audio_model.py +18 -8
  37. camel/models/gemini_model.py +133 -15
  38. camel/models/groq_model.py +72 -10
  39. camel/models/internlm_model.py +14 -3
  40. camel/models/litellm_model.py +9 -2
  41. camel/models/mistral_model.py +42 -5
  42. camel/models/model_manager.py +57 -3
  43. camel/models/moonshot_model.py +33 -4
  44. camel/models/nemotron_model.py +32 -3
  45. camel/models/nvidia_model.py +43 -3
  46. camel/models/ollama_model.py +139 -17
  47. camel/models/openai_audio_models.py +87 -2
  48. camel/models/openai_compatible_model.py +37 -3
  49. camel/models/openai_model.py +158 -46
  50. camel/models/qwen_model.py +61 -4
  51. camel/models/reka_model.py +53 -3
  52. camel/models/samba_model.py +209 -4
  53. camel/models/sglang_model.py +153 -14
  54. camel/models/siliconflow_model.py +16 -3
  55. camel/models/stub_model.py +46 -4
  56. camel/models/togetherai_model.py +38 -3
  57. camel/models/vllm_model.py +37 -3
  58. camel/models/yi_model.py +36 -3
  59. camel/models/zhipuai_model.py +38 -3
  60. camel/retrievers/__init__.py +3 -0
  61. camel/retrievers/hybrid_retrival.py +237 -0
  62. camel/toolkits/__init__.py +15 -1
  63. camel/toolkits/arxiv_toolkit.py +2 -1
  64. camel/toolkits/ask_news_toolkit.py +4 -2
  65. camel/toolkits/audio_analysis_toolkit.py +238 -0
  66. camel/toolkits/base.py +22 -3
  67. camel/toolkits/code_execution.py +2 -0
  68. camel/toolkits/dappier_toolkit.py +2 -1
  69. camel/toolkits/data_commons_toolkit.py +38 -12
  70. camel/toolkits/excel_toolkit.py +172 -0
  71. camel/toolkits/function_tool.py +13 -0
  72. camel/toolkits/github_toolkit.py +5 -1
  73. camel/toolkits/google_maps_toolkit.py +2 -1
  74. camel/toolkits/google_scholar_toolkit.py +2 -0
  75. camel/toolkits/human_toolkit.py +0 -3
  76. camel/toolkits/image_analysis_toolkit.py +202 -0
  77. camel/toolkits/linkedin_toolkit.py +3 -2
  78. camel/toolkits/meshy_toolkit.py +3 -2
  79. camel/toolkits/mineru_toolkit.py +2 -2
  80. camel/toolkits/networkx_toolkit.py +240 -0
  81. camel/toolkits/notion_toolkit.py +2 -0
  82. camel/toolkits/openbb_toolkit.py +3 -2
  83. camel/toolkits/page_script.js +376 -0
  84. camel/toolkits/reddit_toolkit.py +11 -3
  85. camel/toolkits/retrieval_toolkit.py +6 -1
  86. camel/toolkits/semantic_scholar_toolkit.py +2 -1
  87. camel/toolkits/stripe_toolkit.py +8 -2
  88. camel/toolkits/sympy_toolkit.py +6 -1
  89. camel/toolkits/video_analysis_toolkit.py +407 -0
  90. camel/toolkits/{video_toolkit.py → video_download_toolkit.py} +21 -25
  91. camel/toolkits/web_toolkit.py +1307 -0
  92. camel/toolkits/whatsapp_toolkit.py +3 -2
  93. camel/toolkits/zapier_toolkit.py +191 -0
  94. camel/types/__init__.py +2 -2
  95. camel/types/agents/__init__.py +16 -0
  96. camel/types/agents/tool_calling_record.py +52 -0
  97. camel/types/enums.py +3 -0
  98. camel/types/openai_types.py +16 -14
  99. camel/utils/__init__.py +2 -1
  100. camel/utils/async_func.py +2 -2
  101. camel/utils/commons.py +114 -1
  102. camel/verifiers/__init__.py +23 -0
  103. camel/verifiers/base.py +340 -0
  104. camel/verifiers/models.py +82 -0
  105. camel/verifiers/python_verifier.py +202 -0
  106. camel_ai-0.2.23.dist-info/METADATA +671 -0
  107. {camel_ai-0.2.22.dist-info → camel_ai-0.2.23.dist-info}/RECORD +122 -97
  108. {camel_ai-0.2.22.dist-info → camel_ai-0.2.23.dist-info}/WHEEL +1 -1
  109. camel_ai-0.2.22.dist-info/METADATA +0 -527
  110. {camel_ai-0.2.22.dist-info → camel_ai-0.2.23.dist-info/licenses}/LICENSE +0 -0
@@ -111,7 +111,7 @@ class MinerUToolkit(BaseToolkit):
111
111
  if self.wait:
112
112
  return self.client.wait_for_completion(
113
113
  response['task_id'],
114
- timeout=self.timeout,
114
+ timeout=self.timeout, # type: ignore[arg-type]
115
115
  )
116
116
  return response
117
117
  else:
@@ -125,7 +125,7 @@ class MinerUToolkit(BaseToolkit):
125
125
  return self.client.wait_for_completion(
126
126
  batch_id,
127
127
  is_batch=True,
128
- timeout=self.timeout if self.timeout > 300 else 600,
128
+ timeout=self.timeout if self.timeout > 300 else 600, # type: ignore[arg-type,operator]
129
129
  )
130
130
  return {"batch_id": batch_id}
131
131
 
@@ -0,0 +1,240 @@
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
+
15
+ import json
16
+ from typing import Any, Callable, Dict, List, Literal, Optional, Tuple, Union
17
+
18
+ from camel.logger import get_logger
19
+ from camel.toolkits import FunctionTool
20
+ from camel.toolkits.base import BaseToolkit
21
+
22
+ logger = get_logger(__name__)
23
+
24
+
25
+ class NetworkXToolkit(BaseToolkit):
26
+ _nx = None # Class variable to store the networkx module
27
+
28
+ @classmethod
29
+ def _get_nx(cls):
30
+ r"""Lazily import networkx module when needed."""
31
+ if cls._nx is None:
32
+ import networkx
33
+
34
+ cls._nx = networkx
35
+ return cls._nx
36
+
37
+ def __init__(
38
+ self,
39
+ graph_type: Literal[
40
+ 'graph', 'digraph', 'multigraph', 'multidigraph'
41
+ ] = 'graph',
42
+ ):
43
+ r"""Initializes the NetworkX graph client.
44
+
45
+ Args:
46
+ graph_type (Literal['graph', 'digraph', 'multigraph',
47
+ 'multidigraph']):
48
+ Type of graph to create. Options are:
49
+ - 'graph': Undirected graph
50
+ - 'digraph': Directed graph
51
+ - 'multigraph': Undirected graph with parallel edges
52
+ - 'multidigraph': Directed graph with parallel edges
53
+ (default: :obj:`'graph'`)
54
+ """
55
+ nx = self._get_nx()
56
+ graph_types = {
57
+ 'graph': nx.Graph,
58
+ 'digraph': nx.DiGraph,
59
+ 'multigraph': nx.MultiGraph,
60
+ 'multidigraph': nx.MultiDiGraph,
61
+ }
62
+ graph_class = graph_types.get(graph_type.lower())
63
+ if graph_class is None:
64
+ raise ValueError(
65
+ f"Invalid graph type: {graph_type}. Must be one "
66
+ f"of: {list(graph_types.keys())}"
67
+ )
68
+
69
+ self.graph = graph_class()
70
+ logger.info(f"Initialized NetworkX {graph_type} instance.")
71
+
72
+ def add_node(self, node_id: str, **attributes: Any) -> None:
73
+ r"""Adds a node to the graph.
74
+
75
+ Args:
76
+ node_id (str): The ID of the node.
77
+ attributes (dict): Additional node attributes.
78
+ """
79
+ logger.info(f"Adding node: {node_id}, attributes: {attributes}")
80
+ self.graph.add_node(node_id, **attributes)
81
+
82
+ def add_edge(self, source: str, target: str, **attributes: Any) -> None:
83
+ r"""Adds an edge to the graph.
84
+
85
+ Args:
86
+ source (str): Source node ID.
87
+ target (str): Target node ID.
88
+ attributes (dict): Additional edge attributes.
89
+ """
90
+ logger.info(
91
+ f"Adding edge: {source} -> {target}, attributes: {attributes}"
92
+ )
93
+ self.graph.add_edge(source, target, **attributes)
94
+
95
+ def get_nodes(self) -> List[str]:
96
+ r"""Returns all nodes in the graph.
97
+
98
+ Returns:
99
+ List[str]: A list of node IDs.
100
+ """
101
+ logger.info("Fetching all nodes.")
102
+ return list(self.graph.nodes)
103
+
104
+ def get_edges(self) -> List[Tuple[str, str]]:
105
+ r"""Returns all edges in the graph.
106
+
107
+ Returns:
108
+ List[Tuple[str, str]]: A list of edges as (source, target).
109
+ """
110
+ logger.info("Fetching all edges.")
111
+ return list(self.graph.edges)
112
+
113
+ def get_shortest_path(
114
+ self,
115
+ source: str,
116
+ target: str,
117
+ weight: Optional[Union[str, Callable]] = None,
118
+ method: Literal['dijkstra', 'bellman-ford'] = 'dijkstra',
119
+ ) -> List[str]:
120
+ r"""Finds the shortest path between two nodes.
121
+
122
+ Args:
123
+ source (str): The source node ID.
124
+ target (str): The target node ID.
125
+ weight (None, str or function, optional): Edge weights/distances.
126
+ If None, every edge has weight/distance/cost 1.
127
+ If string, use this edge attribute as the edge weight.
128
+ If function, the weight of an edge is the value returned by
129
+ the function. The function must accept three positional
130
+ arguments: the two endpoints and the edge attribute
131
+ dictionary. (default: :obj:`None`)
132
+ method (Literal['dijkstra', 'bellman-ford'], optional): Algorithm
133
+ to compute the path. Ignored if weight is None. (default:
134
+ :obj:`'dijkstra'`)
135
+
136
+ Returns:
137
+ List[str]: A list of nodes in the shortest path.
138
+ """
139
+ logger.info(
140
+ f"Finding shortest path from '{source}' to '{target}' "
141
+ f"using {method} algorithm"
142
+ )
143
+ try:
144
+ nx = self._get_nx()
145
+ path = nx.shortest_path(
146
+ self.graph,
147
+ source=source,
148
+ target=target,
149
+ weight=weight,
150
+ method=method,
151
+ )
152
+ logger.debug(f"Found path: {' -> '.join(path)}")
153
+ return path
154
+ except nx.NetworkXNoPath:
155
+ error_msg = f"No path exists between '{source}' and '{target}'"
156
+ logger.error(error_msg)
157
+ return [error_msg]
158
+ except nx.NodeNotFound as e:
159
+ error_msg = f"Node not found in graph: {e!s}"
160
+ logger.error(error_msg)
161
+ return [error_msg]
162
+
163
+ def compute_centrality(self) -> Dict[str, float]:
164
+ r"""Computes centrality measures for the graph.
165
+
166
+ Returns:
167
+ Dict[str, float]: Centrality values for each node.
168
+ """
169
+ logger.info("Computing centrality measures.")
170
+ nx = self._get_nx()
171
+ return nx.degree_centrality(self.graph)
172
+
173
+ def serialize_graph(self) -> str:
174
+ r"""Serializes the graph to a JSON string.
175
+
176
+ Returns:
177
+ str: The serialized graph in JSON format.
178
+ """
179
+ logger.info("Serializing the graph.")
180
+ nx = self._get_nx()
181
+ return json.dumps(nx.node_link_data(self.graph))
182
+
183
+ def deserialize_graph(self, data: str) -> None:
184
+ r"""Loads a graph from a serialized JSON string.
185
+
186
+ Args:
187
+ data (str): The JSON string representing the graph.
188
+ """
189
+ logger.info("Deserializing graph from JSON data.")
190
+ nx = self._get_nx()
191
+ self.graph = nx.node_link_graph(json.loads(data))
192
+
193
+ def export_to_file(self, file_path: str) -> None:
194
+ r"""Exports the graph to a file in JSON format.
195
+
196
+ Args:
197
+ file_path (str): The file path to save the graph.
198
+ """
199
+ logger.info(f"Exporting graph to file: {file_path}")
200
+ nx = self._get_nx()
201
+ with open(file_path, "w") as file:
202
+ json.dump(nx.node_link_data(self.graph), file)
203
+
204
+ def import_from_file(self, file_path: str) -> None:
205
+ r"""Imports a graph from a JSON file.
206
+
207
+ Args:
208
+ file_path (str): The file path to load the graph from.
209
+ """
210
+ logger.info(f"Importing graph from file: {file_path}")
211
+ nx = self._get_nx()
212
+ with open(file_path, "r") as file:
213
+ self.graph = nx.node_link_graph(json.load(file))
214
+
215
+ def clear_graph(self) -> None:
216
+ r"""Clears the current graph."""
217
+ logger.info("Clearing the graph.")
218
+ self.graph.clear()
219
+
220
+ def get_tools(self) -> List[FunctionTool]:
221
+ r"""Returns a list of FunctionTool objects representing the
222
+ functions in the toolkit.
223
+
224
+ Returns:
225
+ List[FunctionTool]: A list of FunctionTool objects for the
226
+ toolkit methods.
227
+ """
228
+ return [
229
+ FunctionTool(self.add_edge),
230
+ FunctionTool(self.add_node),
231
+ FunctionTool(self.clear_graph),
232
+ FunctionTool(self.compute_centrality),
233
+ FunctionTool(self.deserialize_graph),
234
+ FunctionTool(self.export_to_file),
235
+ FunctionTool(self.get_edges),
236
+ FunctionTool(self.get_nodes),
237
+ FunctionTool(self.import_from_file),
238
+ FunctionTool(self.serialize_graph),
239
+ FunctionTool(self.get_shortest_path),
240
+ ]
@@ -79,6 +79,7 @@ class NotionToolkit(BaseToolkit):
79
79
  def __init__(
80
80
  self,
81
81
  notion_token: Optional[str] = None,
82
+ timeout: Optional[float] = None,
82
83
  ) -> None:
83
84
  r"""Initializes the NotionToolkit.
84
85
 
@@ -86,6 +87,7 @@ class NotionToolkit(BaseToolkit):
86
87
  notion_token (Optional[str], optional): The optional notion_token
87
88
  used to interact with notion APIs.(default: :obj:`None`)
88
89
  """
90
+ super().__init__(timeout=timeout)
89
91
  from notion_client import Client
90
92
 
91
93
  self.notion_token = notion_token or os.environ.get("NOTION_TOKEN")
@@ -37,15 +37,16 @@ class OpenBBToolkit(BaseToolkit):
37
37
  (None, "OPENBB_TOKEN"),
38
38
  ]
39
39
  )
40
- def __init__(self) -> None:
40
+ def __init__(self, timeout: Optional[float] = None) -> None:
41
41
  r"""Initialize the OpenBBToolkit.
42
42
 
43
43
  This method sets up the OpenBB client and initializes the OpenBB
44
44
  Hub account system.
45
45
  """
46
+ super().__init__(timeout=timeout)
46
47
  import os
47
48
 
48
- from openbb import obb
49
+ from openbb import obb # type: ignore[import-not-found]
49
50
 
50
51
  self.client = obb
51
52
  # Initialize OpenBB Hub account with access token
@@ -0,0 +1,376 @@
1
+ var MultimodalWebSurfer = MultimodalWebSurfer || (function() {
2
+ let nextLabel = 10;
3
+
4
+ let roleMapping = {
5
+ "a": "link",
6
+ "area": "link",
7
+ "button": "button",
8
+ "input, type=button": "button",
9
+ "input, type=checkbox": "checkbox",
10
+ "input, type=email": "textbox",
11
+ "input, type=number": "spinbutton",
12
+ "input, type=radio": "radio",
13
+ "input, type=range": "slider",
14
+ "input, type=reset": "button",
15
+ "input, type=search": "searchbox",
16
+ "input, type=submit": "button",
17
+ "input, type=tel": "textbox",
18
+ "input, type=text": "textbox",
19
+ "input, type=url": "textbox",
20
+ "search": "search",
21
+ "select": "combobox",
22
+ "option": "option",
23
+ "textarea": "textbox"
24
+ };
25
+
26
+ let getCursor = function(elm) {
27
+ return window.getComputedStyle(elm)["cursor"];
28
+ };
29
+
30
+ let getInteractiveElements = function() {
31
+
32
+ let results = []
33
+ let roles = ["scrollbar", "searchbox", "slider", "spinbutton", "switch", "tab", "treeitem", "button", "checkbox", "gridcell", "link", "menuitem", "menuitemcheckbox", "menuitemradio", "option", "progressbar", "radio", "textbox", "combobox", "menu", "tree", "treegrid", "grid", "listbox", "radiogroup", "widget"];
34
+ let inertCursors = ["auto", "default", "none", "text", "vertical-text", "not-allowed", "no-drop"];
35
+
36
+ // Get the main interactive elements
37
+ let nodeList = document.querySelectorAll("input, select, textarea, button, [href], [onclick], [contenteditable], [tabindex]:not([tabindex='-1'])");
38
+ for (let i=0; i<nodeList.length; i++) { // Copy to something mutable
39
+ results.push(nodeList[i]);
40
+ }
41
+
42
+ // Anything not already included that has a suitable role
43
+ nodeList = document.querySelectorAll("[role]");
44
+ for (let i=0; i<nodeList.length; i++) { // Copy to something mutable
45
+ if (results.indexOf(nodeList[i]) == -1) {
46
+ let role = nodeList[i].getAttribute("role");
47
+ if (roles.indexOf(role) > -1) {
48
+ results.push(nodeList[i]);
49
+ }
50
+ }
51
+ }
52
+
53
+ // Any element that changes the cursor to something implying interactivity
54
+ nodeList = document.querySelectorAll("*");
55
+ for (let i=0; i<nodeList.length; i++) {
56
+ let node = nodeList[i];
57
+
58
+ // Cursor is default, or does not suggest interactivity
59
+ let cursor = getCursor(node);
60
+ if (inertCursors.indexOf(cursor) >= 0) {
61
+ continue;
62
+ }
63
+
64
+ // Move up to the first instance of this cursor change
65
+ parent = node.parentNode;
66
+ while (parent && getCursor(parent) == cursor) {
67
+ node = parent;
68
+ parent = node.parentNode;
69
+ }
70
+
71
+ // Add the node if it is new
72
+ if (results.indexOf(node) == -1) {
73
+ results.push(node);
74
+ }
75
+ }
76
+
77
+ return results;
78
+ };
79
+
80
+ let labelElements = function(elements) {
81
+ for (let i=0; i<elements.length; i++) {
82
+ if (!elements[i].hasAttribute("__elementId")) {
83
+ elements[i].setAttribute("__elementId", "" + (nextLabel++));
84
+ }
85
+ }
86
+ };
87
+
88
+ let isTopmost = function(element, x, y) {
89
+ let hit = document.elementFromPoint(x, y);
90
+
91
+ // Hack to handle elements outside the viewport
92
+ if (hit === null) {
93
+ return true;
94
+ }
95
+
96
+ while (hit) {
97
+ if (hit == element) return true;
98
+ hit = hit.parentNode;
99
+ }
100
+ return false;
101
+ };
102
+
103
+ let getFocusedElementId = function() {
104
+ let elm = document.activeElement;
105
+ while (elm) {
106
+ if (elm.hasAttribute && elm.hasAttribute("__elementId")) {
107
+ return elm.getAttribute("__elementId");
108
+ }
109
+ elm = elm.parentNode;
110
+ }
111
+ return null;
112
+ };
113
+
114
+ let trimmedInnerText = function(element) {
115
+ if (!element) {
116
+ return "";
117
+ }
118
+ let text = element.innerText;
119
+ if (!text) {
120
+ return "";
121
+ }
122
+ return text.trim();
123
+ };
124
+
125
+ let getApproximateAriaName = function(element) {
126
+ // Check for aria labels
127
+ if (element.hasAttribute("aria-labelledby")) {
128
+ let buffer = "";
129
+ let ids = element.getAttribute("aria-labelledby").split(" ");
130
+ for (let i=0; i<ids.length; i++) {
131
+ let label = document.getElementById(ids[i]);
132
+ if (label) {
133
+ buffer = buffer + " " + trimmedInnerText(label);
134
+ }
135
+ }
136
+ return buffer.trim();
137
+ }
138
+
139
+ if (element.hasAttribute("aria-label")) {
140
+ return element.getAttribute("aria-label");
141
+ }
142
+
143
+ // Check for labels
144
+ if (element.hasAttribute("id")) {
145
+ let label_id = element.getAttribute("id");
146
+ let label = "";
147
+ let labels = document.querySelectorAll("label[for='" + label_id + "']");
148
+ for (let j=0; j<labels.length; j++) {
149
+ label += labels[j].innerText + " ";
150
+ }
151
+ label = label.trim();
152
+ if (label != "") {
153
+ return label;
154
+ }
155
+ }
156
+
157
+ if (element.parentElement && element.parentElement.tagName == "LABEL") {
158
+ return element.parentElement.innerText;
159
+ }
160
+
161
+ // Check for alt text or titles
162
+ if (element.hasAttribute("alt")) {
163
+ return element.getAttribute("alt")
164
+ }
165
+
166
+ if (element.hasAttribute("title")) {
167
+ return element.getAttribute("title")
168
+ }
169
+
170
+ return trimmedInnerText(element);
171
+ };
172
+
173
+ let getApproximateAriaRole = function(element) {
174
+ let tag = element.tagName.toLowerCase();
175
+ if (tag == "input" && element.hasAttribute("type")) {
176
+ tag = tag + ", type=" + element.getAttribute("type");
177
+ }
178
+
179
+ if (element.hasAttribute("role")) {
180
+ return [element.getAttribute("role"), tag];
181
+ }
182
+ else if (tag in roleMapping) {
183
+ return [roleMapping[tag], tag];
184
+ }
185
+ else {
186
+ return ["", tag];
187
+ }
188
+ };
189
+
190
+ let getInteractiveRects = function() {
191
+ labelElements(getInteractiveElements());
192
+ let elements = document.querySelectorAll("[__elementId]");
193
+ let results = {};
194
+ for (let i=0; i<elements.length; i++) {
195
+ let key = elements[i].getAttribute("__elementId");
196
+ let rects = elements[i].getClientRects();
197
+ let ariaRole = getApproximateAriaRole(elements[i]);
198
+ let ariaName = getApproximateAriaName(elements[i]);
199
+ let vScrollable = elements[i].scrollHeight - elements[i].clientHeight >= 1;
200
+
201
+ let record = {
202
+ "tag_name": ariaRole[1],
203
+ "role": ariaRole[0],
204
+ "aria-name": ariaName,
205
+ "v-scrollable": vScrollable,
206
+ "rects": []
207
+ };
208
+
209
+ for (const rect of rects) {
210
+ let x = rect.left + rect.width/2;
211
+ let y = rect.top + rect.height/2;
212
+ if (isTopmost(elements[i], x, y)) {
213
+ record["rects"].push(JSON.parse(JSON.stringify(rect)));
214
+ }
215
+ }
216
+
217
+ if (record["rects"].length > 0) {
218
+ results[key] = record;
219
+ }
220
+ }
221
+ return results;
222
+ };
223
+
224
+ let getVisualViewport = function() {
225
+ let vv = window.visualViewport;
226
+ let de = document.documentElement;
227
+ return {
228
+ "height": vv ? vv.height : 0,
229
+ "width": vv ? vv.width : 0,
230
+ "offsetLeft": vv ? vv.offsetLeft : 0,
231
+ "offsetTop": vv ? vv.offsetTop : 0,
232
+ "pageLeft": vv ? vv.pageLeft : 0,
233
+ "pageTop": vv ? vv.pageTop : 0,
234
+ "scale": vv ? vv.scale : 0,
235
+ "clientWidth": de ? de.clientWidth : 0,
236
+ "clientHeight": de ? de.clientHeight : 0,
237
+ "scrollWidth": de ? de.scrollWidth : 0,
238
+ "scrollHeight": de ? de.scrollHeight : 0
239
+ };
240
+ };
241
+
242
+ let _getMetaTags = function() {
243
+ let meta = document.querySelectorAll("meta");
244
+ let results = {};
245
+ for (let i = 0; i<meta.length; i++) {
246
+ let key = null;
247
+ if (meta[i].hasAttribute("name")) {
248
+ key = meta[i].getAttribute("name");
249
+ }
250
+ else if (meta[i].hasAttribute("property")) {
251
+ key = meta[i].getAttribute("property");
252
+ }
253
+ else {
254
+ continue;
255
+ }
256
+ if (meta[i].hasAttribute("content")) {
257
+ results[key] = meta[i].getAttribute("content");
258
+ }
259
+ }
260
+ return results;
261
+ };
262
+
263
+ let _getJsonLd = function() {
264
+ let jsonld = [];
265
+ let scripts = document.querySelectorAll('script[type="application/ld+json"]');
266
+ for (let i=0; i<scripts.length; i++) {
267
+ jsonld.push(scripts[i].innerHTML.trim());
268
+ }
269
+ return jsonld;
270
+ };
271
+
272
+ // From: https://www.stevefenton.co.uk/blog/2022/12/parse-microdata-with-javascript/
273
+ let _getMicrodata = function() {
274
+ function sanitize(input) {
275
+ return input.replace(/\s/gi, ' ').trim();
276
+ }
277
+
278
+ function addValue(information, name, value) {
279
+ if (information[name]) {
280
+ if (typeof information[name] === 'array') {
281
+ information[name].push(value);
282
+ } else {
283
+ const arr = [];
284
+ arr.push(information[name]);
285
+ arr.push(value);
286
+ information[name] = arr;
287
+ }
288
+ } else {
289
+ information[name] = value;
290
+ }
291
+ }
292
+
293
+ function traverseItem(item, information) {
294
+ const children = item.children;
295
+
296
+ for (let i = 0; i < children.length; i++) {
297
+ const child = children[i];
298
+
299
+ if (child.hasAttribute('itemscope')) {
300
+ if (child.hasAttribute('itemprop')) {
301
+ const itemProp = child.getAttribute('itemprop');
302
+ const itemType = child.getAttribute('itemtype');
303
+
304
+ const childInfo = {
305
+ itemType: itemType
306
+ };
307
+
308
+ traverseItem(child, childInfo);
309
+
310
+ itemProp.split(' ').forEach(propName => {
311
+ addValue(information, propName, childInfo);
312
+ });
313
+ }
314
+
315
+ } else if (child.hasAttribute('itemprop')) {
316
+ const itemProp = child.getAttribute('itemprop');
317
+ itemProp.split(' ').forEach(propName => {
318
+ if (propName === 'url') {
319
+ addValue(information, propName, child.href);
320
+ } else {
321
+ addValue(information, propName, sanitize(child.getAttribute("content") || child.content || child.textContent || child.src || ""));
322
+ }
323
+ });
324
+ traverseItem(child, information);
325
+ } else {
326
+ traverseItem(child, information);
327
+ }
328
+ }
329
+ }
330
+
331
+ const microdata = [];
332
+
333
+ document.querySelectorAll("[itemscope]").forEach(function(elem, i) {
334
+ const itemType = elem.getAttribute('itemtype');
335
+ const information = {
336
+ itemType: itemType
337
+ };
338
+ traverseItem(elem, information);
339
+ microdata.push(information);
340
+ });
341
+
342
+ return microdata;
343
+ };
344
+
345
+ let getPageMetadata = function() {
346
+ let jsonld = _getJsonLd();
347
+ let metaTags = _getMetaTags();
348
+ let microdata = _getMicrodata();
349
+ let results = {}
350
+ if (jsonld.length > 0) {
351
+ try {
352
+ results["jsonld"] = JSON.parse(jsonld);
353
+ }
354
+ catch (e) {
355
+ results["jsonld"] = jsonld;
356
+ }
357
+ }
358
+ if (microdata.length > 0) {
359
+ results["microdata"] = microdata;
360
+ }
361
+ for (let key in metaTags) {
362
+ if (metaTags.hasOwnProperty(key)) {
363
+ results["meta_tags"] = metaTags;
364
+ break;
365
+ }
366
+ }
367
+ return results;
368
+ };
369
+
370
+ return {
371
+ getInteractiveRects: getInteractiveRects,
372
+ getVisualViewport: getVisualViewport,
373
+ getFocusedElementId: getFocusedElementId,
374
+ getPageMetadata: getPageMetadata,
375
+ };
376
+ })();