From 76a256ffaaf99e3933ccbd93c367b68643de0885 Mon Sep 17 00:00:00 2001 From: Shuchang Zheng Date: Fri, 20 Sep 2024 19:21:00 -0700 Subject: [PATCH] skip large size svg (#869) Co-authored-by: LawyZheng --- skyvern/config.py | 2 ++ skyvern/forge/agent_functions.py | 15 +++++++++++++++ skyvern/webeye/scraper/scraper.py | 6 ++++++ 3 files changed, 23 insertions(+) diff --git a/skyvern/config.py b/skyvern/config.py index ce2eb7da..869a3fdd 100644 --- a/skyvern/config.py +++ b/skyvern/config.py @@ -122,6 +122,8 @@ class Settings(BaseSettings): VERIFICATION_CODE_INITIAL_WAIT_TIME_SECS: int = 40 VERIFICATION_CODE_POLLING_TIMEOUT_MINS: int = 5 + SVG_MAX_LENGTH: int = 100000 + def is_cloud_environment(self) -> bool: """ :return: True if env is not local, else False diff --git a/skyvern/forge/agent_functions.py b/skyvern/forge/agent_functions.py index f5d29c91..a75362e5 100644 --- a/skyvern/forge/agent_functions.py +++ b/skyvern/forge/agent_functions.py @@ -6,6 +6,7 @@ from typing import Dict, List import structlog from playwright.async_api import Page +from skyvern.config import settings from skyvern.constants import SKYVERN_ID_ATTR from skyvern.exceptions import StepUnableToExecuteError, SVGConversionFailed from skyvern.forge import app @@ -62,6 +63,9 @@ async def _convert_svg_to_string(task: Task, step: Step, organization: Organizat if element.get("tagName") != "svg": return + if element.get("isDropped", False): + return + element_id = element.get("id", "") svg_element = _remove_skyvern_attributes(element) svg_html = json_to_html(svg_element) @@ -83,6 +87,17 @@ async def _convert_svg_to_string(task: Task, step: Step, organization: Organizat if svg_shape: LOG.debug("SVG loaded from cache", element_id=element_id, shape=svg_shape) else: + if len(svg_html) > settings.SVG_MAX_LENGTH: + # TODO: implement a fallback solution for "too large" case, maybe convert by screenshot + LOG.warning( + "SVG element is too large to convert, going to drop the svg element.", + element_id=element_id, + length=len(svg_html), + ) + del element["children"] + element["isDropped"] = True + return + LOG.debug("call LLM to convert SVG to string shape", element_id=element_id) svg_convert_prompt = prompt_engine.load_prompt("svg-convert", svg_element=svg_html) diff --git a/skyvern/webeye/scraper/scraper.py b/skyvern/webeye/scraper/scraper.py index 790ba816..96d3ec63 100644 --- a/skyvern/webeye/scraper/scraper.py +++ b/skyvern/webeye/scraper/scraper.py @@ -86,6 +86,12 @@ def build_attribute(key: str, value: Any) -> str: def json_to_html(element: dict, need_skyvern_attrs: bool = True) -> str: + """ + if element is flagged as dropped, the html format is empty + """ + if element.get("isDropped", False): + return "" + attributes: dict[str, Any] = copy.deepcopy(element.get("attributes", {})) if need_skyvern_attrs: