skip large size svg (#869)

Co-authored-by: LawyZheng <lawyzheng1106@gmail.com>
This commit is contained in:
Shuchang Zheng
2024-09-20 19:21:00 -07:00
committed by GitHub
parent aedaeb4433
commit 76a256ffaa
3 changed files with 23 additions and 0 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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: