inspect-ai 0.3.75__py3-none-any.whl → 0.3.77__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.
- inspect_ai/_cli/eval.py +16 -0
- inspect_ai/_display/core/results.py +6 -1
- inspect_ai/_eval/eval.py +8 -1
- inspect_ai/_eval/evalset.py +6 -2
- inspect_ai/_eval/registry.py +3 -5
- inspect_ai/_eval/run.py +7 -2
- inspect_ai/_eval/task/run.py +4 -0
- inspect_ai/_util/content.py +3 -0
- inspect_ai/_util/logger.py +3 -0
- inspect_ai/_view/www/dist/assets/index.css +28 -16
- inspect_ai/_view/www/dist/assets/index.js +4811 -4609
- inspect_ai/_view/www/log-schema.json +79 -9
- inspect_ai/_view/www/src/samples/chat/tools/ToolCallView.tsx +22 -4
- inspect_ai/_view/www/src/samples/chat/tools/ToolInput.tsx +1 -1
- inspect_ai/_view/www/src/samples/descriptor/score/CategoricalScoreDescriptor.tsx +1 -1
- inspect_ai/_view/www/src/samples/descriptor/score/NumericScoreDescriptor.tsx +2 -2
- inspect_ai/_view/www/src/samples/sample-tools/SortFilter.tsx +1 -1
- inspect_ai/_view/www/src/samples/transcript/ModelEventView.module.css +2 -2
- inspect_ai/_view/www/src/types/log.d.ts +11 -5
- inspect_ai/log/_recorders/json.py +8 -0
- inspect_ai/log/_transcript.py +13 -4
- inspect_ai/model/_call_tools.py +13 -4
- inspect_ai/model/_chat_message.py +3 -0
- inspect_ai/model/_model.py +5 -1
- inspect_ai/model/_model_output.py +6 -1
- inspect_ai/model/_openai.py +78 -10
- inspect_ai/model/_openai_responses.py +277 -0
- inspect_ai/model/_providers/anthropic.py +134 -75
- inspect_ai/model/_providers/azureai.py +2 -2
- inspect_ai/model/_providers/mistral.py +29 -13
- inspect_ai/model/_providers/openai.py +64 -57
- inspect_ai/model/_providers/openai_responses.py +177 -0
- inspect_ai/model/_providers/openrouter.py +52 -2
- inspect_ai/model/_providers/providers.py +1 -1
- inspect_ai/model/_providers/vertex.py +5 -2
- inspect_ai/tool/__init__.py +6 -0
- inspect_ai/tool/_tool.py +23 -3
- inspect_ai/tool/_tool_call.py +5 -2
- inspect_ai/tool/_tool_support_helpers.py +200 -0
- inspect_ai/tool/_tools/_bash_session.py +119 -0
- inspect_ai/tool/_tools/_computer/_computer.py +1 -1
- inspect_ai/tool/_tools/_text_editor.py +121 -0
- inspect_ai/tool/_tools/_think.py +48 -0
- inspect_ai/tool/_tools/_web_browser/_back_compat.py +150 -0
- inspect_ai/tool/_tools/_web_browser/_web_browser.py +75 -130
- inspect_ai/tool/_tools/_web_search.py +1 -1
- inspect_ai/util/_json.py +28 -0
- inspect_ai/util/_sandbox/context.py +16 -7
- inspect_ai/util/_sandbox/docker/config.py +1 -1
- inspect_ai/util/_sandbox/docker/internal.py +3 -3
- {inspect_ai-0.3.75.dist-info → inspect_ai-0.3.77.dist-info}/METADATA +5 -2
- {inspect_ai-0.3.75.dist-info → inspect_ai-0.3.77.dist-info}/RECORD +56 -80
- {inspect_ai-0.3.75.dist-info → inspect_ai-0.3.77.dist-info}/WHEEL +1 -1
- inspect_ai/model/_image.py +0 -15
- inspect_ai/tool/_tools/_web_browser/_resources/.pylintrc +0 -8
- inspect_ai/tool/_tools/_web_browser/_resources/.vscode/launch.json +0 -24
- inspect_ai/tool/_tools/_web_browser/_resources/.vscode/settings.json +0 -25
- inspect_ai/tool/_tools/_web_browser/_resources/Dockerfile +0 -22
- inspect_ai/tool/_tools/_web_browser/_resources/README.md +0 -63
- inspect_ai/tool/_tools/_web_browser/_resources/accessibility_tree.py +0 -71
- inspect_ai/tool/_tools/_web_browser/_resources/accessibility_tree_node.py +0 -323
- inspect_ai/tool/_tools/_web_browser/_resources/cdp/__init__.py +0 -5
- inspect_ai/tool/_tools/_web_browser/_resources/cdp/a11y.py +0 -279
- inspect_ai/tool/_tools/_web_browser/_resources/cdp/dom.py +0 -9
- inspect_ai/tool/_tools/_web_browser/_resources/cdp/dom_snapshot.py +0 -293
- inspect_ai/tool/_tools/_web_browser/_resources/cdp/page.py +0 -94
- inspect_ai/tool/_tools/_web_browser/_resources/constants.py +0 -2
- inspect_ai/tool/_tools/_web_browser/_resources/images/usage_diagram.svg +0 -2
- inspect_ai/tool/_tools/_web_browser/_resources/mock_environment.py +0 -45
- inspect_ai/tool/_tools/_web_browser/_resources/playwright_browser.py +0 -50
- inspect_ai/tool/_tools/_web_browser/_resources/playwright_crawler.py +0 -48
- inspect_ai/tool/_tools/_web_browser/_resources/playwright_page_crawler.py +0 -280
- inspect_ai/tool/_tools/_web_browser/_resources/pyproject.toml +0 -65
- inspect_ai/tool/_tools/_web_browser/_resources/rectangle.py +0 -64
- inspect_ai/tool/_tools/_web_browser/_resources/rpc_client_helpers.py +0 -146
- inspect_ai/tool/_tools/_web_browser/_resources/scale_factor.py +0 -64
- inspect_ai/tool/_tools/_web_browser/_resources/test_accessibility_tree_node.py +0 -180
- inspect_ai/tool/_tools/_web_browser/_resources/test_playwright_crawler.py +0 -99
- inspect_ai/tool/_tools/_web_browser/_resources/test_rectangle.py +0 -15
- inspect_ai/tool/_tools/_web_browser/_resources/test_web_client.py +0 -44
- inspect_ai/tool/_tools/_web_browser/_resources/web_browser_rpc_types.py +0 -39
- inspect_ai/tool/_tools/_web_browser/_resources/web_client.py +0 -214
- inspect_ai/tool/_tools/_web_browser/_resources/web_client_new_session.py +0 -35
- inspect_ai/tool/_tools/_web_browser/_resources/web_server.py +0 -192
- {inspect_ai-0.3.75.dist-info → inspect_ai-0.3.77.dist-info}/entry_points.txt +0 -0
- {inspect_ai-0.3.75.dist-info → inspect_ai-0.3.77.dist-info/licenses}/LICENSE +0 -0
- {inspect_ai-0.3.75.dist-info → inspect_ai-0.3.77.dist-info}/top_level.txt +0 -0
@@ -1,280 +0,0 @@
|
|
1
|
-
"""A crawler implementation using Playwright.
|
2
|
-
|
3
|
-
Portions based on https://github.com/web-arena-x/webarena
|
4
|
-
"""
|
5
|
-
|
6
|
-
from __future__ import annotations
|
7
|
-
|
8
|
-
import asyncio
|
9
|
-
import re
|
10
|
-
from typing import Literal
|
11
|
-
|
12
|
-
from playwright.async_api import CDPSession, Page
|
13
|
-
|
14
|
-
from accessibility_tree import AccessibilityTree, create_accessibility_tree
|
15
|
-
from accessibility_tree_node import AccessibilityTreeNode
|
16
|
-
from cdp.a11y import AXNodeId, AXTree
|
17
|
-
from cdp.dom_snapshot import DOMSnapshot
|
18
|
-
from rectangle import Rectangle
|
19
|
-
|
20
|
-
# Number of seconds to wait for possible click induced navigation before proceeding
|
21
|
-
_WAIT_FOR_NAVIGATION_TIME = 2.0
|
22
|
-
|
23
|
-
# The waiting strategy to use between browser commands.
|
24
|
-
# see https://playwright.dev/docs/api/class-page.
|
25
|
-
_WAIT_STRATEGY: Literal["domcontentloaded"] = "domcontentloaded"
|
26
|
-
|
27
|
-
|
28
|
-
class PageCrawler:
|
29
|
-
@classmethod
|
30
|
-
async def create(
|
31
|
-
cls, page: Page, device_scale_factor: float | None = None
|
32
|
-
) -> PageCrawler:
|
33
|
-
# Enable chrome development tools, and accessibility tree output.
|
34
|
-
cdp_session = await page.context.new_cdp_session(page)
|
35
|
-
await cdp_session.send("Accessibility.enable")
|
36
|
-
return PageCrawler(
|
37
|
-
page,
|
38
|
-
cdp_session,
|
39
|
-
device_scale_factor or await page.evaluate("window.devicePixelRatio"),
|
40
|
-
)
|
41
|
-
|
42
|
-
def __init__(
|
43
|
-
self, page: Page, cdp_session: CDPSession, device_scale_factor: float
|
44
|
-
) -> None:
|
45
|
-
self._page = page
|
46
|
-
self._cdp_session = cdp_session
|
47
|
-
|
48
|
-
# Start with an empty accessibility tree
|
49
|
-
self._rendered_main_content: str | None = None
|
50
|
-
self._rendered_accessibility_tree: str = ""
|
51
|
-
self._accessibility_tree: AccessibilityTree | None = None
|
52
|
-
self._device_scale_factor = device_scale_factor
|
53
|
-
|
54
|
-
@property
|
55
|
-
def page(self) -> Page:
|
56
|
-
return self._page
|
57
|
-
|
58
|
-
@property
|
59
|
-
def url(self) -> str:
|
60
|
-
return self._page.url
|
61
|
-
|
62
|
-
def lookup_node(self, node_id_or_tag: int | str) -> AccessibilityTreeNode:
|
63
|
-
"""Looks up the node by id or tag.
|
64
|
-
|
65
|
-
Args:
|
66
|
-
node_id_or_tag: Either the id number (as int or str), or <tag_name>
|
67
|
-
|
68
|
-
Returns:
|
69
|
-
AccessibilityNode.
|
70
|
-
|
71
|
-
Raise:
|
72
|
-
LookupError if node is not matched.
|
73
|
-
"""
|
74
|
-
node: AccessibilityTreeNode | None = None
|
75
|
-
node_id_or_tag = str(node_id_or_tag)
|
76
|
-
nodes = self._accessibility_tree["nodes"] if self._accessibility_tree else {}
|
77
|
-
if re.match("^<.*>", node_id_or_tag):
|
78
|
-
tag = node_id_or_tag[1:-1].lower()
|
79
|
-
# This is a smart tag, try to resolve it.
|
80
|
-
if node := next(
|
81
|
-
# We match on anything that starts with the code, this is potentially
|
82
|
-
# a little brittle, can be replaced with an RE if there are issues.
|
83
|
-
(
|
84
|
-
n
|
85
|
-
for n in nodes.values()
|
86
|
-
if n.name.lower().startswith(tag) and not n.is_ignored
|
87
|
-
),
|
88
|
-
None,
|
89
|
-
):
|
90
|
-
return node
|
91
|
-
else:
|
92
|
-
raise LookupError(
|
93
|
-
f"Could not find tag {node_id_or_tag} from {[node.name for node in nodes.values() if node.name]}"
|
94
|
-
)
|
95
|
-
else:
|
96
|
-
if (
|
97
|
-
node := nodes.get(AXNodeId(node_id_or_tag), None)
|
98
|
-
) and not node.is_ignored:
|
99
|
-
return node
|
100
|
-
else:
|
101
|
-
raise LookupError(f"Could not find element with id {node_id_or_tag}")
|
102
|
-
|
103
|
-
async def update(self) -> None:
|
104
|
-
"""Updates the accessibility tree and DOM from current page."""
|
105
|
-
await self._page.wait_for_load_state(_WAIT_STRATEGY)
|
106
|
-
|
107
|
-
available_retries = 2
|
108
|
-
retry_delay = 0.25
|
109
|
-
while available_retries:
|
110
|
-
self._accessibility_tree = create_accessibility_tree(
|
111
|
-
ax_nodes=AXTree(
|
112
|
-
**await self._cdp_session.send("Accessibility.getFullAXTree", {})
|
113
|
-
).nodes,
|
114
|
-
dom_snapshot=DOMSnapshot(
|
115
|
-
**await self._cdp_session.send(
|
116
|
-
"DOMSnapshot.captureSnapshot",
|
117
|
-
{
|
118
|
-
"computedStyles": [],
|
119
|
-
"includeDOMRects": True,
|
120
|
-
},
|
121
|
-
)
|
122
|
-
),
|
123
|
-
device_scale_factor=self._device_scale_factor,
|
124
|
-
window_bounds=Rectangle(
|
125
|
-
await self._page.evaluate("window.pageXOffset"),
|
126
|
-
await self._page.evaluate("window.pageYOffset"),
|
127
|
-
await self._page.evaluate("window.screen.width"),
|
128
|
-
await self._page.evaluate("window.screen.height"),
|
129
|
-
),
|
130
|
-
)
|
131
|
-
|
132
|
-
self._rendered_main_content, self._rendered_accessibility_tree = (
|
133
|
-
(
|
134
|
-
self._accessibility_tree["root"].render_main_content(),
|
135
|
-
self._accessibility_tree["root"].render_accessibility_tree(),
|
136
|
-
)
|
137
|
-
if self._accessibility_tree
|
138
|
-
else (None, "")
|
139
|
-
)
|
140
|
-
|
141
|
-
if self._rendered_accessibility_tree:
|
142
|
-
return
|
143
|
-
# sometimes, the entire tree is initially ignored. in such cases, it's typically
|
144
|
-
# because we're sampling too soon. Waiting a small amount of time and trying again
|
145
|
-
# resolves the issue.
|
146
|
-
available_retries = available_retries - 1
|
147
|
-
await asyncio.sleep(retry_delay)
|
148
|
-
|
149
|
-
def render_at(self) -> str:
|
150
|
-
"""Returns the current webpage accessibility tree.
|
151
|
-
|
152
|
-
Only elements visible on the screen will be rendered.
|
153
|
-
"""
|
154
|
-
return self._rendered_accessibility_tree
|
155
|
-
|
156
|
-
def render_main_content(self) -> str | None:
|
157
|
-
return self._rendered_main_content
|
158
|
-
|
159
|
-
async def go_to_url(self, url: str) -> None:
|
160
|
-
"""Goes to the given url.
|
161
|
-
|
162
|
-
Args:
|
163
|
-
url: The url to redirect crawler to.
|
164
|
-
"""
|
165
|
-
if "://" not in url:
|
166
|
-
url = f"https://{url}"
|
167
|
-
try:
|
168
|
-
await self._page.goto(url, wait_until=_WAIT_STRATEGY)
|
169
|
-
except Exception as e:
|
170
|
-
print(f"caught {e}")
|
171
|
-
raise
|
172
|
-
|
173
|
-
async def click(self, element_id: int | str) -> None:
|
174
|
-
"""Clicks the element with the given id.
|
175
|
-
|
176
|
-
Args:
|
177
|
-
element_id: The id for the element we want to click on.
|
178
|
-
"""
|
179
|
-
element = self.lookup_node(element_id)
|
180
|
-
if element.bounds is None:
|
181
|
-
raise LookupError(f"Element with id {element_id} has no layout info.")
|
182
|
-
|
183
|
-
# Mouse.click() requires coordinates relative to the viewport:
|
184
|
-
# https://playwright.dev/python/docs/api/class-mouse#mouse-click,
|
185
|
-
# thus adjusting the Y coordinate since we only scroll up/down.
|
186
|
-
scroll_y = await self._page.evaluate("window.scrollY")
|
187
|
-
await self._click_and_await_navigation(
|
188
|
-
element.bounds.center_x, element.bounds.center_y - scroll_y
|
189
|
-
)
|
190
|
-
|
191
|
-
async def clear(self, element_id: int | str) -> None:
|
192
|
-
"""Clears text within a field."""
|
193
|
-
await self.click(element_id)
|
194
|
-
await self._page.keyboard.press("Control+A")
|
195
|
-
await self._page.keyboard.press("Backspace")
|
196
|
-
|
197
|
-
async def type(self, element_id: int | str, text: str) -> None:
|
198
|
-
"""Types into the element with the given id."""
|
199
|
-
await self.click(element_id)
|
200
|
-
await self._page.keyboard.type(text)
|
201
|
-
|
202
|
-
async def scroll(self, direction: Literal["up", "down"]) -> None:
|
203
|
-
"""Scrolls the page to the given direction.
|
204
|
-
|
205
|
-
Args:
|
206
|
-
direction: The direction to scroll in ('up' or 'down')
|
207
|
-
"""
|
208
|
-
match direction.lower():
|
209
|
-
case "up":
|
210
|
-
await self._page.evaluate(
|
211
|
-
"(document.scrollingElement || document.body).scrollTop ="
|
212
|
-
" (document.scrollingElement || document.body).scrollTop -"
|
213
|
-
" window.innerHeight;"
|
214
|
-
)
|
215
|
-
case "down":
|
216
|
-
await self._page.evaluate(
|
217
|
-
"(document.scrollingElement || document.body).scrollTop ="
|
218
|
-
" (document.scrollingElement || document.body).scrollTop +"
|
219
|
-
" window.innerHeight;"
|
220
|
-
)
|
221
|
-
|
222
|
-
case _:
|
223
|
-
raise ValueError(f"Invalid scroll direction {direction}")
|
224
|
-
|
225
|
-
async def forward(self) -> None:
|
226
|
-
"""Move browser forward one history step."""
|
227
|
-
await self._page.go_forward(wait_until=_WAIT_STRATEGY)
|
228
|
-
|
229
|
-
async def back(self) -> None:
|
230
|
-
"""Move browser backward one history step."""
|
231
|
-
await self._page.go_back(wait_until=_WAIT_STRATEGY)
|
232
|
-
|
233
|
-
async def refresh(self) -> None:
|
234
|
-
"""Refresh (reload) the page."""
|
235
|
-
await self._page.reload(wait_until=_WAIT_STRATEGY)
|
236
|
-
|
237
|
-
async def _click_and_await_navigation(self, x: float, y: float) -> None:
|
238
|
-
"""
|
239
|
-
Clicks on the specified coordinates and waits for navigation (if any) to occur.
|
240
|
-
|
241
|
-
This function sets up event listeners to detect in-page navigation or new page
|
242
|
-
navigation, performs a mouse click at the given coordinates, and waits for the
|
243
|
-
navigation to complete within the specified timeout period.
|
244
|
-
|
245
|
-
The point of this is to allow enough time to switch our page in the event of a new
|
246
|
-
page being opened. The problem is that it takes some amount of time, and the challenge
|
247
|
-
is determining how long to wait.
|
248
|
-
|
249
|
-
A naïve approach would simply sleep for some amount of time. However, this time may
|
250
|
-
not be long enough AND it would delay the common case by that delay waiting for a new
|
251
|
-
page navigation that never comes.
|
252
|
-
|
253
|
-
This approach accomplishes waiting the minimal amount of time in the common cases of
|
254
|
-
a click inducing an in page or new page navigation. The downside is that clicks that
|
255
|
-
do not induce navigation are delayed by the timeout. Since navigating clicks are much
|
256
|
-
more common, this is a reasonable approach.
|
257
|
-
"""
|
258
|
-
future = asyncio.Future[None]()
|
259
|
-
|
260
|
-
async def on_in_page_navigation(_frame):
|
261
|
-
if not future.done():
|
262
|
-
await self._page.wait_for_load_state(_WAIT_STRATEGY)
|
263
|
-
future.set_result()
|
264
|
-
|
265
|
-
async def on_new_page(new_page):
|
266
|
-
if not future.done():
|
267
|
-
await new_page.wait_for_load_state(_WAIT_STRATEGY)
|
268
|
-
future.set_result(None)
|
269
|
-
|
270
|
-
self._page.once("framenavigated", on_in_page_navigation)
|
271
|
-
self._page.context.once("page", on_new_page)
|
272
|
-
|
273
|
-
await self._page.mouse.click(x, y)
|
274
|
-
|
275
|
-
try:
|
276
|
-
await asyncio.wait_for(future, timeout=_WAIT_FOR_NAVIGATION_TIME)
|
277
|
-
# a navigation of some sort has occurred and gotten to domcontentloaded
|
278
|
-
except (asyncio.TimeoutError, TimeoutError):
|
279
|
-
# No navigation occurred within the timeout period
|
280
|
-
pass
|
@@ -1,65 +0,0 @@
|
|
1
|
-
[build-system]
|
2
|
-
requires = ["setuptools>=64", "setuptools_scm[toml]>=8"]
|
3
|
-
build-backend = "setuptools.build_meta"
|
4
|
-
|
5
|
-
[tool.setuptools_scm]
|
6
|
-
|
7
|
-
[tool.setuptools.packages.find]
|
8
|
-
where = ["."]
|
9
|
-
include = ["inspect_ai*"]
|
10
|
-
|
11
|
-
[tool.ruff]
|
12
|
-
src = ["."]
|
13
|
-
|
14
|
-
[tool.ruff.lint]
|
15
|
-
select = [
|
16
|
-
"E", # pycodestyle errors
|
17
|
-
"W", # pycodestyle warnings
|
18
|
-
"F", # flake8
|
19
|
-
"D", # pydocstyle
|
20
|
-
"I", # isort
|
21
|
-
"SIM101", # duplicate isinstance
|
22
|
-
"UP038", # non-pep604-isinstance
|
23
|
-
# "RET", # flake8-return
|
24
|
-
# "RUF", # ruff rules
|
25
|
-
]
|
26
|
-
ignore = ["E203", "E501", "D10", "D212", "D415"]
|
27
|
-
|
28
|
-
[tool.ruff.lint.pydocstyle]
|
29
|
-
convention = "google"
|
30
|
-
|
31
|
-
[tool.pytest.ini_options]
|
32
|
-
minversion = "7.0"
|
33
|
-
addopts = "-rA --doctest-modules --color=yes"
|
34
|
-
doctest_optionflags = ["NORMALIZE_WHITESPACE", "IGNORE_EXCEPTION_DETAIL"]
|
35
|
-
asyncio_mode = "auto"
|
36
|
-
asyncio_default_fixture_loop_scope = "function"
|
37
|
-
log_level = "warning"
|
38
|
-
|
39
|
-
[tool.mypy]
|
40
|
-
warn_unused_ignores = true
|
41
|
-
no_implicit_reexport = true
|
42
|
-
strict_equality = true
|
43
|
-
warn_redundant_casts = true
|
44
|
-
warn_unused_configs = true
|
45
|
-
disallow_any_explicit = true
|
46
|
-
disallow_any_generics = true
|
47
|
-
disallow_subclassing_any = true
|
48
|
-
plugins=["pydantic.mypy"]
|
49
|
-
|
50
|
-
|
51
|
-
[tool.pydantic-mypy]
|
52
|
-
init_forbid_extra = true
|
53
|
-
init_typed = true
|
54
|
-
|
55
|
-
[tool.check-wheel-contents]
|
56
|
-
ignore = ["W002", "W009"]
|
57
|
-
|
58
|
-
[project]
|
59
|
-
name = "web_browser_tool_container"
|
60
|
-
requires-python = ">=3.10"
|
61
|
-
dynamic = ["version", "dependencies"]
|
62
|
-
|
63
|
-
|
64
|
-
[project.optional-dependencies]
|
65
|
-
dev = ["pytest"]
|
@@ -1,64 +0,0 @@
|
|
1
|
-
from __future__ import annotations
|
2
|
-
|
3
|
-
|
4
|
-
class Rectangle:
|
5
|
-
def __init__(self, left: float, top: float, width: float, height: float):
|
6
|
-
self._left = int(left)
|
7
|
-
self._width = int(width)
|
8
|
-
self._top = int(top)
|
9
|
-
self._height = int(height)
|
10
|
-
|
11
|
-
@classmethod
|
12
|
-
def _from_left_right_top_bottom(
|
13
|
-
cls, left: float, right: float, top: float, bottom: float
|
14
|
-
) -> Rectangle:
|
15
|
-
return cls(left, top, right - left, bottom - top)
|
16
|
-
|
17
|
-
@property
|
18
|
-
def _right(self) -> int:
|
19
|
-
return self._left + self._width
|
20
|
-
|
21
|
-
@property
|
22
|
-
def _bottom(self) -> int:
|
23
|
-
return self._top + self._height
|
24
|
-
|
25
|
-
@property
|
26
|
-
def center_x(self) -> int:
|
27
|
-
return self._left + self._width // 2
|
28
|
-
|
29
|
-
@property
|
30
|
-
def center_y(self) -> int:
|
31
|
-
return self._top + self._height // 2
|
32
|
-
|
33
|
-
@property
|
34
|
-
def has_area(self) -> bool:
|
35
|
-
return self._width > 0 and self._height > 0
|
36
|
-
|
37
|
-
def __str__(self) -> str:
|
38
|
-
return f"({self._left}, {self._top}, {self._width}, {self._height})"
|
39
|
-
|
40
|
-
def scale(self, scale: float) -> Rectangle:
|
41
|
-
return self._from_left_right_top_bottom(
|
42
|
-
self._left * scale,
|
43
|
-
self._right * scale,
|
44
|
-
self._top * scale,
|
45
|
-
self._bottom * scale,
|
46
|
-
)
|
47
|
-
|
48
|
-
def overlaps(self, other: Rectangle) -> bool:
|
49
|
-
"""Returns if the two rectangles intersect."""
|
50
|
-
return (
|
51
|
-
other._left < self._right # pylint: disable=protected-access
|
52
|
-
and other._right > self._left # pylint: disable=protected-access
|
53
|
-
and other._top < self._bottom # pylint: disable=protected-access
|
54
|
-
and other._bottom > self._top # pylint: disable=protected-access
|
55
|
-
)
|
56
|
-
|
57
|
-
def within(self, other: Rectangle) -> bool:
|
58
|
-
"""Returns if this rectangle is within the other rectangle."""
|
59
|
-
return (
|
60
|
-
other._left <= self._left # pylint: disable=protected-access
|
61
|
-
and other._right >= self._left # pylint: disable=protected-access
|
62
|
-
and other._top <= self._bottom # pylint: disable=protected-access
|
63
|
-
and other._bottom >= self._top # pylint: disable=protected-access
|
64
|
-
)
|
@@ -1,146 +0,0 @@
|
|
1
|
-
"""
|
2
|
-
This module provides helper functions and classes for making strongly typed RPC calls.
|
3
|
-
|
4
|
-
The module is designed to be generic and should not contain any use case specific logic.
|
5
|
-
It uses the `httpx` library for making HTTP requests, `jsonrpcclient` for handling JSON-RPC responses,
|
6
|
-
and `pydantic` for validating and parsing response data into Python objects.
|
7
|
-
|
8
|
-
Classes:
|
9
|
-
RPCError: Custom exception for handling RPC errors.
|
10
|
-
|
11
|
-
Functions:
|
12
|
-
typed_rpc_call: Makes a typed RPC call and returns the response as a Pydantic model.
|
13
|
-
"""
|
14
|
-
|
15
|
-
from typing import Generic, Mapping, Type, TypedDict, TypeVar
|
16
|
-
|
17
|
-
from httpx import (
|
18
|
-
URL,
|
19
|
-
Client,
|
20
|
-
ConnectError,
|
21
|
-
ConnectTimeout,
|
22
|
-
HTTPStatusError,
|
23
|
-
ReadTimeout,
|
24
|
-
Response,
|
25
|
-
)
|
26
|
-
from jsonrpcclient import Error, Ok, parse, request
|
27
|
-
from pydantic import BaseModel
|
28
|
-
from tenacity import (
|
29
|
-
retry,
|
30
|
-
retry_if_exception,
|
31
|
-
stop_after_attempt,
|
32
|
-
stop_after_delay,
|
33
|
-
wait_exponential_jitter,
|
34
|
-
)
|
35
|
-
|
36
|
-
|
37
|
-
class RPCError(RuntimeError):
|
38
|
-
def __init__(self, *, code: int, message: str, data: object):
|
39
|
-
self.code = code
|
40
|
-
self.message = message
|
41
|
-
self.data = data
|
42
|
-
super().__init__(f"RPCError {code}: {message}")
|
43
|
-
|
44
|
-
def __str__(self):
|
45
|
-
return f"RPCError {self.code}: {self.message} (data: {self.data})"
|
46
|
-
|
47
|
-
|
48
|
-
TBaseModel = TypeVar("TBaseModel", bound=BaseModel)
|
49
|
-
|
50
|
-
|
51
|
-
def rpc_call(
|
52
|
-
url: URL | str,
|
53
|
-
method: str,
|
54
|
-
params: dict[str, object] | None,
|
55
|
-
response_class: Type[TBaseModel],
|
56
|
-
) -> TBaseModel:
|
57
|
-
"""
|
58
|
-
Makes an RPC call to the specified URL with the given method and parameters, and returns the response as a parsed and validated instance of the specified response class.
|
59
|
-
|
60
|
-
Args:
|
61
|
-
url (URL | str): The URL to which the RPC call is made.
|
62
|
-
method (str): The RPC method to be called.
|
63
|
-
response_class (Type[TBaseModel]): The class to which the response should be deserialized.
|
64
|
-
params (dict[str, object] | None, optional): The parameters to be sent with the RPC call. Defaults to None.
|
65
|
-
|
66
|
-
Returns:
|
67
|
-
TBaseModel: An instance of the response class containing the result of the RPC call.
|
68
|
-
|
69
|
-
Raises:
|
70
|
-
RPCError: If the RPC call returns an error response.
|
71
|
-
RuntimeError: If an unexpected response is received.
|
72
|
-
"""
|
73
|
-
match parse(_retrying_post(url, request(method, params)).json()):
|
74
|
-
case Ok(ok_result):
|
75
|
-
return response_class(**ok_result)
|
76
|
-
case Error(code, message, data):
|
77
|
-
raise RPCError(code=code, message=message, data=data)
|
78
|
-
case _:
|
79
|
-
raise RuntimeError("how did we get here")
|
80
|
-
|
81
|
-
|
82
|
-
def _retrying_post(url: URL | str, json: object | None = None) -> Response:
|
83
|
-
max_retries = 3
|
84
|
-
total_timeout = 180
|
85
|
-
|
86
|
-
@retry(
|
87
|
-
wait=wait_exponential_jitter(),
|
88
|
-
stop=(stop_after_attempt(max_retries) | stop_after_delay(total_timeout)),
|
89
|
-
retry=retry_if_exception(httpx_should_retry),
|
90
|
-
)
|
91
|
-
def do_post() -> Response:
|
92
|
-
with Client() as client:
|
93
|
-
return client.post(url, json=json, timeout=30)
|
94
|
-
|
95
|
-
return do_post()
|
96
|
-
|
97
|
-
|
98
|
-
RPCArgsType = Type[Mapping[str, object]]
|
99
|
-
|
100
|
-
|
101
|
-
class RPCCallTypes(TypedDict, Generic[TBaseModel]):
|
102
|
-
args_type: RPCArgsType
|
103
|
-
response_class: Type[TBaseModel]
|
104
|
-
|
105
|
-
|
106
|
-
# TODO: cloned from inspect_ai repo code that is unavailable in the container
|
107
|
-
# fix this by copying that source file into the container
|
108
|
-
def httpx_should_retry(ex: BaseException) -> bool:
|
109
|
-
"""Check whether an exception raised from httpx should be retried.
|
110
|
-
|
111
|
-
Implements the strategy described here: https://cloud.google.com/storage/docs/retry-strategy
|
112
|
-
|
113
|
-
Args:
|
114
|
-
ex (BaseException): Exception to examine for retry behavior
|
115
|
-
|
116
|
-
Returns:
|
117
|
-
True if a retry should occur
|
118
|
-
"""
|
119
|
-
# httpx status exception
|
120
|
-
if isinstance(ex, HTTPStatusError):
|
121
|
-
# request timeout
|
122
|
-
if ex.response.status_code == 408:
|
123
|
-
return True
|
124
|
-
# lock timeout
|
125
|
-
elif ex.response.status_code == 409:
|
126
|
-
return True
|
127
|
-
# rate limit
|
128
|
-
elif ex.response.status_code == 429:
|
129
|
-
return True
|
130
|
-
# internal errors
|
131
|
-
elif ex.response.status_code >= 500:
|
132
|
-
return True
|
133
|
-
else:
|
134
|
-
return False
|
135
|
-
|
136
|
-
# connection error
|
137
|
-
elif is_httpx_connection_error(ex):
|
138
|
-
return True
|
139
|
-
|
140
|
-
# don't retry
|
141
|
-
else:
|
142
|
-
return False
|
143
|
-
|
144
|
-
|
145
|
-
def is_httpx_connection_error(ex: BaseException) -> bool:
|
146
|
-
return isinstance(ex, ConnectTimeout | ConnectError | ConnectionError | ReadTimeout)
|
@@ -1,64 +0,0 @@
|
|
1
|
-
import subprocess
|
2
|
-
import sys
|
3
|
-
|
4
|
-
# Playwright launches Chromium with --force-device-scale-factor=1 by default, which
|
5
|
-
# ensures consistent rendering and measurement behavior using CSS pixels instead of
|
6
|
-
# native device pixels, regardless of the actual display's DPI. On HiDPI displays,
|
7
|
-
# like Apple Retina, the ratio of native pixels to CSS pixels is typically 2x or even 3x.
|
8
|
-
#
|
9
|
-
# However, the Chrome DevTools Protocol (CDP) has an inconsistency when running on
|
10
|
-
# HiDPI devices with --force-device-scale-factor=1. Although window.devicePixelRatio
|
11
|
-
# correctly reports as 1 (honoring the forced scale factor), CDP's LayoutTreeSnapshot.bounds
|
12
|
-
# still returns coordinates in native device pixels without applying the forced scale factor,
|
13
|
-
# resulting in a mismatch between CSS and native pixels.
|
14
|
-
#
|
15
|
-
# To work around this inconsistency, we determine the native scale factor independently
|
16
|
-
# of browser-reported values, using system-level data via the get_screen_scale_factor helper
|
17
|
-
# function. We then apply this actual scale factor to adjust the window bounds, ensuring
|
18
|
-
# they are in the same coordinate space as LayoutTreeSnapshot.bounds. This alignment
|
19
|
-
# allows the code to accurately calculate node visibility.
|
20
|
-
|
21
|
-
|
22
|
-
def get_screen_scale_factor() -> float:
|
23
|
-
if sys.platform == "darwin":
|
24
|
-
from AppKit import NSScreen
|
25
|
-
|
26
|
-
return NSScreen.mainScreen().backingScaleFactor()
|
27
|
-
elif sys.platform == "win32":
|
28
|
-
try:
|
29
|
-
# Using GetDpiForSystem from Windows API
|
30
|
-
import ctypes
|
31
|
-
|
32
|
-
user32 = ctypes.windll.user32
|
33
|
-
user32.SetProcessDPIAware()
|
34
|
-
return user32.GetDpiForSystem() / 96.0
|
35
|
-
except Exception:
|
36
|
-
return 1.0
|
37
|
-
elif sys.platform.startswith("linux"):
|
38
|
-
try:
|
39
|
-
# Try to get scaling from gsettings (GNOME)
|
40
|
-
result = subprocess.run(
|
41
|
-
["gsettings", "get", "org.gnome.desktop.interface", "scaling-factor"],
|
42
|
-
capture_output=True,
|
43
|
-
text=True,
|
44
|
-
)
|
45
|
-
if result.returncode == 0:
|
46
|
-
return float(result.stdout.strip())
|
47
|
-
|
48
|
-
# Try to get scaling from xrandr (X11)
|
49
|
-
result = subprocess.run(
|
50
|
-
["xrandr", "--current"], capture_output=True, text=True
|
51
|
-
)
|
52
|
-
if result.returncode == 0:
|
53
|
-
# Parse xrandr output to find scaling
|
54
|
-
# This is a simplified check - might need adjustment
|
55
|
-
for line in result.stdout.splitlines():
|
56
|
-
if "connected primary" in line and "x" in line:
|
57
|
-
# Look for something like "3840x2160+0+0"
|
58
|
-
resolution = line.split()[3].split("+")[0]
|
59
|
-
if "3840x2160" in resolution: # 4K
|
60
|
-
return 2.0
|
61
|
-
return 1.0
|
62
|
-
except Exception:
|
63
|
-
return 1.0
|
64
|
-
return 1.0 # Default fallback
|