Script gen support - send email with file attachments (#3551)
This commit is contained in:
@@ -775,15 +775,14 @@ def _build_send_email_statement(block: dict[str, Any]) -> cst.SimpleStatementLin
|
|||||||
last_line=cst.SimpleWhitespace(INDENT),
|
last_line=cst.SimpleWhitespace(INDENT),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
# TODO: support file attachments?
|
cst.Arg(
|
||||||
# cst.Arg(
|
keyword=cst.Name("file_attachments"),
|
||||||
# keyword=cst.Name("file_attachments"),
|
value=_value(block.get("file_attachments", [])),
|
||||||
# value=_value(block.get("file_attachments", [])),
|
whitespace_after_arg=cst.ParenthesizedWhitespace(
|
||||||
# whitespace_after_arg=cst.ParenthesizedWhitespace(
|
indent=True,
|
||||||
# indent=True,
|
last_line=cst.SimpleWhitespace(INDENT),
|
||||||
# last_line=cst.SimpleWhitespace(INDENT),
|
),
|
||||||
# ),
|
),
|
||||||
# ),
|
|
||||||
cst.Arg(
|
cst.Arg(
|
||||||
keyword=cst.Name("label"),
|
keyword=cst.Name("label"),
|
||||||
value=_value(block.get("label", "")),
|
value=_value(block.get("label", "")),
|
||||||
|
|||||||
@@ -80,6 +80,35 @@ def _get_context_data(data: str | dict[str, Any] | None = None) -> dict[str, Any
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def _render_template_with_label(template: str, label: str | None = None) -> str:
|
||||||
|
template_data = {}
|
||||||
|
context = skyvern_context.current()
|
||||||
|
if context and context.workflow_run_id:
|
||||||
|
workflow_run_context = app.WORKFLOW_CONTEXT_MANAGER.get_workflow_run_context(context.workflow_run_id)
|
||||||
|
block_reference_data: dict[str, Any] = workflow_run_context.get_block_metadata(label)
|
||||||
|
template_data = workflow_run_context.values.copy()
|
||||||
|
if label in template_data:
|
||||||
|
current_value = template_data[label]
|
||||||
|
if isinstance(current_value, dict):
|
||||||
|
block_reference_data.update(current_value)
|
||||||
|
else:
|
||||||
|
LOG.warning(
|
||||||
|
f"Script service: Parameter {label} has a registered reference value, going to overwrite it by block metadata"
|
||||||
|
)
|
||||||
|
|
||||||
|
if label:
|
||||||
|
template_data[label] = block_reference_data
|
||||||
|
|
||||||
|
# inject the forloop metadata as global variables
|
||||||
|
if "current_index" in block_reference_data:
|
||||||
|
template_data["current_index"] = block_reference_data["current_index"]
|
||||||
|
if "current_item" in block_reference_data:
|
||||||
|
template_data["current_item"] = block_reference_data["current_item"]
|
||||||
|
if "current_value" in block_reference_data:
|
||||||
|
template_data["current_value"] = block_reference_data["current_value"]
|
||||||
|
return render_template(template, data=template_data)
|
||||||
|
|
||||||
|
|
||||||
def render_template(template: str, data: dict[str, Any] | None = None) -> str:
|
def render_template(template: str, data: dict[str, Any] | None = None) -> str:
|
||||||
"""
|
"""
|
||||||
Refer to Block.format_block_parameter_template_from_workflow_run_context
|
Refer to Block.format_block_parameter_template_from_workflow_run_context
|
||||||
@@ -118,6 +147,7 @@ class SkyvernPage:
|
|||||||
self.scraped_page = scraped_page
|
self.scraped_page = scraped_page
|
||||||
self.page = page
|
self.page = page
|
||||||
self._record = recorder or (lambda ac: None)
|
self._record = recorder or (lambda ac: None)
|
||||||
|
self.current_label: str | None = None
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def _get_or_create_browser_state(cls, browser_session_id: str | None = None) -> BrowserState:
|
async def _get_or_create_browser_state(cls, browser_session_id: str | None = None) -> BrowserState:
|
||||||
@@ -681,6 +711,7 @@ class SkyvernPage:
|
|||||||
tz_info = datetime.now(tz=timezone.utc).tzinfo
|
tz_info = datetime.now(tz=timezone.utc).tzinfo
|
||||||
if context and context.tz_info:
|
if context and context.tz_info:
|
||||||
tz_info = context.tz_info
|
tz_info = context.tz_info
|
||||||
|
prompt = _render_template_with_label(prompt, label=self.current_label)
|
||||||
extract_information_prompt = load_prompt_with_elements(
|
extract_information_prompt = load_prompt_with_elements(
|
||||||
element_tree_builder=scraped_page_refreshed,
|
element_tree_builder=scraped_page_refreshed,
|
||||||
prompt_engine=prompt_engine,
|
prompt_engine=prompt_engine,
|
||||||
|
|||||||
@@ -24,7 +24,11 @@ def cached(cache_key: str) -> Callable:
|
|||||||
|
|
||||||
async def wrapper(page: SkyvernPage, context: RunContext, *args: Any, **kwargs: Any) -> Any:
|
async def wrapper(page: SkyvernPage, context: RunContext, *args: Any, **kwargs: Any) -> Any:
|
||||||
# Store the function in context.cached_fns
|
# Store the function in context.cached_fns
|
||||||
return await func(page, context, *args, **kwargs)
|
page.current_label = cache_key
|
||||||
|
try:
|
||||||
|
return await func(page, context, *args, **kwargs)
|
||||||
|
finally:
|
||||||
|
page.current_label = None
|
||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|||||||
@@ -166,7 +166,9 @@ class WorkflowRunContext:
|
|||||||
return
|
return
|
||||||
self.blocks_metadata[label] = metadata
|
self.blocks_metadata[label] = metadata
|
||||||
|
|
||||||
def get_block_metadata(self, label: str) -> BlockMetadata:
|
def get_block_metadata(self, label: str | None) -> BlockMetadata:
|
||||||
|
if label is None:
|
||||||
|
label = ""
|
||||||
return self.blocks_metadata.get(label, BlockMetadata())
|
return self.blocks_metadata.get(label, BlockMetadata())
|
||||||
|
|
||||||
def get_original_secret_value_or_none(self, secret_id_or_value: Any) -> Any:
|
def get_original_secret_value_or_none(self, secret_id_or_value: Any) -> Any:
|
||||||
|
|||||||
@@ -1544,7 +1544,7 @@ async def run_script(
|
|||||||
def _render_template_with_label(template: str, label: str | None = None) -> str:
|
def _render_template_with_label(template: str, label: str | None = None) -> str:
|
||||||
template_data = {}
|
template_data = {}
|
||||||
context = skyvern_context.current()
|
context = skyvern_context.current()
|
||||||
if context and context.workflow_run_id and label:
|
if context and context.workflow_run_id:
|
||||||
workflow_run_context = app.WORKFLOW_CONTEXT_MANAGER.get_workflow_run_context(context.workflow_run_id)
|
workflow_run_context = app.WORKFLOW_CONTEXT_MANAGER.get_workflow_run_context(context.workflow_run_id)
|
||||||
block_reference_data: dict[str, Any] = workflow_run_context.get_block_metadata(label)
|
block_reference_data: dict[str, Any] = workflow_run_context.get_block_metadata(label)
|
||||||
template_data = workflow_run_context.values.copy()
|
template_data = workflow_run_context.values.copy()
|
||||||
@@ -1557,7 +1557,8 @@ def _render_template_with_label(template: str, label: str | None = None) -> str:
|
|||||||
f"Script service: Parameter {label} has a registered reference value, going to overwrite it by block metadata"
|
f"Script service: Parameter {label} has a registered reference value, going to overwrite it by block metadata"
|
||||||
)
|
)
|
||||||
|
|
||||||
template_data[label] = block_reference_data
|
if label:
|
||||||
|
template_data[label] = block_reference_data
|
||||||
|
|
||||||
# inject the forloop metadata as global variables
|
# inject the forloop metadata as global variables
|
||||||
if "current_index" in block_reference_data:
|
if "current_index" in block_reference_data:
|
||||||
|
|||||||
Reference in New Issue
Block a user