fix block screenshot bug (#3361)

This commit is contained in:
LawyZheng
2025-09-05 02:31:29 +08:00
committed by GitHub
parent 309176ef4b
commit 9413958f4b
2 changed files with 29 additions and 17 deletions

View File

@@ -581,22 +581,6 @@ class BaseTaskBlock(Block):
browser_state = await app.BROWSER_MANAGER.get_or_create_for_workflow_run(
workflow_run=workflow_run, url=self.url, browser_session_id=browser_session_id
)
# assert that the browser state is not None, otherwise we can't go through typing
assert browser_state is not None
# add screenshot artifact for the first task
screenshot = await browser_state.take_fullpage_screenshot(
use_playwright_fullpage=app.EXPERIMENTATION_PROVIDER.is_feature_enabled_cached(
"ENABLE_PLAYWRIGHT_FULLPAGE",
workflow_run_id,
properties={"organization_id": str(organization_id)},
)
)
if screenshot:
await app.ARTIFACT_MANAGER.create_workflow_run_block_artifact(
workflow_run_block=workflow_run_block,
artifact_type=ArtifactType.SCREENSHOT_LLM,
data=screenshot,
)
except Exception as e:
LOG.exception(
"Failed to get browser state for first task",
@@ -611,6 +595,28 @@ class BaseTaskBlock(Block):
failure_reason=str(e),
)
raise e
try:
# add screenshot artifact for the first task
screenshot = await browser_state.take_fullpage_screenshot(
use_playwright_fullpage=app.EXPERIMENTATION_PROVIDER.is_feature_enabled_cached(
"ENABLE_PLAYWRIGHT_FULLPAGE",
workflow_run_id,
properties={"organization_id": str(organization_id)},
)
)
if screenshot:
await app.ARTIFACT_MANAGER.create_workflow_run_block_artifact(
workflow_run_block=workflow_run_block,
artifact_type=ArtifactType.SCREENSHOT_LLM,
data=screenshot,
)
except Exception:
LOG.warning(
"Failed to take screenshot for first task",
task_id=task.task_id,
workflow_run_id=workflow_run_id,
exc_info=True,
)
else:
# if not the first task block, need to navigate manually
browser_state = app.BROWSER_MANAGER.get_for_workflow_run(workflow_run_id=workflow_run_id)

View File

@@ -310,7 +310,7 @@ class SkyvernFrame:
)
finally:
if x is not None and y is not None:
await skyvern_frame.scroll_to_x_y(x, y)
await skyvern_frame.safe_scroll_to_x_y(x, y)
@staticmethod
@TraceManager.traced_async(ignore_inputs=["page"])
@@ -366,6 +366,12 @@ class SkyvernFrame:
js_script = "([x, y]) => scrollToXY(x, y)"
return await self.evaluate(frame=self.frame, expression=js_script, arg=[x, y])
async def safe_scroll_to_x_y(self, x: int, y: int) -> None:
try:
await self.scroll_to_x_y(x, y)
except Exception:
LOG.warning("Failed to scroll to x, y, ignore it", x=x, y=y, exc_info=True)
async def scroll_to_element_bottom(self, element: ElementHandle, page_by_page: bool = False) -> None:
js_script = "([element, page_by_page]) => scrollToElementBottom(element, page_by_page)"
return await self.evaluate(frame=self.frame, expression=js_script, arg=[element, page_by_page])