Skip to content

Automate browser based workflows with AI

feat(SKY-9416): surface print-to-PDF in block downloaded_files (#5784)

Shuchang Zheng396a99099f9d
diff --git a/skyvern/forge/sdk/workflow/models/block.py b/skyvern/forge/sdk/workflow/models/block.py
index 3c197b698..4c4efc276 100644
--- a/skyvern/forge/sdk/workflow/models/block.py
+++ b/skyvern/forge/sdk/workflow/models/block.py
@@ -41,6 +41,7 @@ from skyvern.constants import (
GET_DOWNLOADED_FILES_TIMEOUT,
MAX_FILE_PARSE_INPUT_TOKENS,
MAX_UPLOAD_FILE_COUNT,
+ SAVE_DOWNLOADED_FILES_TIMEOUT,
)
from skyvern.exceptions import (
AzureConfigurationError,
@@ -6396,6 +6397,53 @@ class PrintPageBlock(Block):
return artifact_uri, artifact_url
+ async def _register_pdf_as_downloaded_file(
+ self,
+ *,
+ organization_id: str | None,
+ workflow_run_id: str,
+ workflow_run_block_id: str,
+ ) -> list[FileInfo]:
+ # Workflow finalization eventually runs save_downloaded_files, but the block
+ # output snapshot is recorded now and the UI keys off downloaded_file_urls
+ # on the block — so we register up front and let finalization re-run safely.
+ if not organization_id:
+ return []
+ try:
+ async with asyncio.timeout(SAVE_DOWNLOADED_FILES_TIMEOUT):
+ await app.STORAGE.save_downloaded_files(
+ organization_id=organization_id,
+ run_id=workflow_run_id,
+ )
+ except asyncio.TimeoutError:
+ LOG.warning(
+ "Timeout to save downloaded files",
+ workflow_run_id=workflow_run_id,
+ workflow_run_block_id=workflow_run_block_id,
+ )
+ return []
+ except Exception:
+ LOG.warning(
+ "PrintPageBlock failed to register PDF as downloaded file; will retry at workflow finalization",
+ workflow_run_id=workflow_run_id,
+ workflow_run_block_id=workflow_run_block_id,
+ exc_info=True,
+ )
+ return []
+ try:
+ async with asyncio.timeout(GET_DOWNLOADED_FILES_TIMEOUT):
+ return await app.STORAGE.get_downloaded_files(
+ organization_id=organization_id,
+ run_id=workflow_run_id,
+ )
+ except asyncio.TimeoutError:
+ LOG.warning(
+ "Timeout getting downloaded files",
+ workflow_run_id=workflow_run_id,
+ workflow_run_block_id=workflow_run_block_id,
+ )
+ return []
+
async def execute(
self,
workflow_run_id: str,
@@ -6406,6 +6454,11 @@ class PrintPageBlock(Block):
) -> BlockResult:
workflow_run_context = self.get_workflow_run_context(workflow_run_id)
+ # Scope downloaded files to this block only.
+ block_context = skyvern_context.current()
+ if block_context:
+ await capture_block_download_baseline(block_context, organization_id or "", workflow_run_id, self.label)
+
browser_state = await self.get_or_create_browser_state(
workflow_run_id=workflow_run_id,
organization_id=organization_id,
@@ -6473,12 +6526,27 @@ class PrintPageBlock(Block):
organization_id=organization_id,
)
+ artifact_org_id = organization_id or workflow_run_context.organization_id
+ downloaded_files = await self._register_pdf_as_downloaded_file(
+ organization_id=artifact_org_id,
+ workflow_run_id=workflow_run_id,
+ workflow_run_block_id=workflow_run_block_id,
+ )
+
+ current_context = skyvern_context.current()
+ downloaded_files = filter_downloaded_files_for_current_iteration(
+ downloaded_files,
+ current_context.loop_internal_state if current_context else None,
+ )
output = {
"filename": filename,
"file_path": file_path,
"size_bytes": len(pdf_bytes),
"artifact_uri": artifact_uri,
"artifact_url": artifact_url,
+ "downloaded_files": [fi.model_dump() for fi in downloaded_files],
+ "downloaded_file_urls": [fi.url for fi in downloaded_files],
+ "downloaded_file_artifact_ids": [fi.artifact_id for fi in downloaded_files if fi.artifact_id],
}
await self.record_output_parameter_value(workflow_run_context, workflow_run_id, output)
diff --git a/tests/unit/test_print_page_block_downloaded_files.py b/tests/unit/test_print_page_block_downloaded_files.py
new file mode 100644
index 000000000..002a35b2e
--- /dev/null
+++ b/tests/unit/test_print_page_block_downloaded_files.py
@@ -0,0 +1,341 @@
+"""Tests for PrintPageBlock surfacing the generated PDF in ``downloaded_files`` (SKY-9416)."""
+
+from datetime import UTC, datetime
+from types import SimpleNamespace
+from unittest.mock import AsyncMock
+
+import pytest
+
+from skyvern.forge.sdk.core import skyvern_context
+from skyvern.forge.sdk.core.skyvern_context import SkyvernContext
+from skyvern.forge.sdk.schemas.files import FileInfo
+from skyvern.forge.sdk.workflow.models import block as block_module
+from skyvern.forge.sdk.workflow.models.block import PrintPageBlock
+from skyvern.forge.sdk.workflow.models.parameter import OutputParameter, ParameterType
+
+
+def _output_parameter(key: str) -> OutputParameter:
+ now = datetime.now(UTC)
+ return OutputParameter(
+ parameter_type=ParameterType.OUTPUT,
+ key=key,
+ output_parameter_id=f"op_{key}",
+ workflow_id="wf_test",
+ created_at=now,
+ modified_at=now,
+ )
+
+
[email protected](autouse=True)
+def _reset_context() -> None:
+ skyvern_context.reset()
+ yield
+ skyvern_context.reset()
+
+
+def _isolated_download_path(tmp_path, monkeypatch: pytest.MonkeyPatch) -> str:
+ download_root = tmp_path / "downloads"
+ download_root.mkdir()
+ monkeypatch.setattr(
+ "skyvern.forge.sdk.api.files.settings.DOWNLOAD_PATH",
+ str(download_root),
+ )
+ return str(download_root)
+
+
+def _make_page_pdf_mock() -> AsyncMock:
+ page = SimpleNamespace()
+ page.pdf = AsyncMock(return_value=b"%PDF-1.4 test bytes")
+ return page
+
+
+async def _identity_record(self, workflow_run_context, workflow_run_id, value):
+ self._captured_output = value
+
+
+async def _identity_build_block_result(self, **kwargs):
+ return SimpleNamespace(**kwargs)
+
+
+async def test_print_page_block_includes_downloaded_files_in_output(
+ monkeypatch: pytest.MonkeyPatch, _isolated_download_path: str
+) -> None:
+ """The PDF the block generates should appear in the block output's
+ ``downloaded_files`` / ``downloaded_file_urls`` / ``downloaded_file_artifact_ids``
+ so the UI can render the printed page like a regular download."""
+ skyvern_context.set(
+ SkyvernContext(
+ organization_id="o_1",
+ workflow_run_id="wr_1",
+ run_id="wr_1",
+ )
+ )
+
+ # Set modified_at so the assertion actually exercises FileInfo.model_dump() with a
+ # non-None datetime — production reads this from the artifact row's created_at.
+ file_info = FileInfo(
+ url="https://api.example.com/v1/artifacts/a_dl_1/content?artifact_name=page.pdf",
+ filename="page.pdf",
+ checksum="deadbeef",
+ artifact_id="a_dl_1",
+ modified_at=datetime(2026, 4, 30, 12, 0, tzinfo=UTC),
+ )
+
+ save_mock = AsyncMock()
+ # capture_block_download_baseline calls get_downloaded_files first (empty here),
+ # then the helper calls it again post-PDF.
+ get_mock = AsyncMock(side_effect=[[], [file_info]])
+
+ fake_app = SimpleNamespace(
+ STORAGE=SimpleNamespace(
+ save_downloaded_files=save_mock,
+ get_downloaded_files=get_mock,
+ ),
+ )
+ monkeypatch.setattr(block_module, "app", fake_app)
+
+ block = PrintPageBlock(
+ label="print",
+ output_parameter=_output_parameter("print_out"),
+ )
+
+ monkeypatch.setattr(
+ PrintPageBlock,
+ "get_workflow_run_context",
+ lambda self, workflow_run_id: SimpleNamespace(organization_id="o_1"),
+ )
+
+ page = _make_page_pdf_mock()
+ browser_state = SimpleNamespace(get_working_page=AsyncMock(return_value=page))
+ monkeypatch.setattr(
+ PrintPageBlock,
+ "get_or_create_browser_state",
+ AsyncMock(return_value=browser_state),
+ )
+
+ upload_mock = AsyncMock(return_value=("s3://artifacts/wr_1/receipt.pdf", "https://example.com/receipt.pdf"))
+ monkeypatch.setattr(PrintPageBlock, "_upload_pdf_artifact", upload_mock)
+
+ monkeypatch.setattr(PrintPageBlock, "record_output_parameter_value", _identity_record)
+ monkeypatch.setattr(PrintPageBlock, "build_block_result", _identity_build_block_result)
+
+ result = await block.execute(
+ workflow_run_id="wr_1",
+ workflow_run_block_id="wrb_1",
+ organization_id="o_1",
+ )
+
+ assert result.success is True
+ save_mock.assert_awaited_once_with(organization_id="o_1", run_id="wr_1")
+ assert get_mock.await_count == 2
+
+ output = block._captured_output
+ assert output["filename"].endswith(".pdf")
+ assert output["downloaded_files"] == [file_info.model_dump()]
+ assert output["downloaded_file_urls"] == [file_info.url]
+ assert output["downloaded_file_artifact_ids"] == ["a_dl_1"]
+
+
+async def test_print_page_block_filters_downloads_to_current_loop_iteration(
+ monkeypatch: pytest.MonkeyPatch, _isolated_download_path: str
+) -> None:
+ """When inside a ForLoopBlock iteration, only the PDF generated this iteration
+ should land in the block output — earlier iterations' files must be filtered out."""
+ skyvern_context.set(
+ SkyvernContext(
+ organization_id="o_1",
+ workflow_run_id="wr_1",
+ run_id="wr_1",
+ loop_internal_state={
+ "downloaded_file_signatures_before_iteration": [
+ ["prev.pdf", "abc", "https://api.example.com/v1/artifacts/a_prev/content"],
+ ],
+ },
+ )
+ )
+
+ prev_file = FileInfo(
+ url="https://api.example.com/v1/artifacts/a_prev/content?artifact_name=prev.pdf",
+ filename="prev.pdf",
+ checksum="abc",
+ artifact_id="a_prev",
+ )
+ new_file = FileInfo(
+ url="https://api.example.com/v1/artifacts/a_new/content?artifact_name=page.pdf",
+ filename="page.pdf",
+ checksum="def",
+ artifact_id="a_new",
+ )
+
+ fake_app = SimpleNamespace(
+ STORAGE=SimpleNamespace(
+ save_downloaded_files=AsyncMock(),
+ get_downloaded_files=AsyncMock(return_value=[prev_file, new_file]),
+ ),
+ )
+ monkeypatch.setattr(block_module, "app", fake_app)
+
+ block = PrintPageBlock(
+ label="print",
+ output_parameter=_output_parameter("print_out"),
+ )
+ monkeypatch.setattr(
+ PrintPageBlock,
+ "get_workflow_run_context",
+ lambda self, workflow_run_id: SimpleNamespace(organization_id="o_1"),
+ )
+ page = _make_page_pdf_mock()
+ monkeypatch.setattr(
+ PrintPageBlock,
+ "get_or_create_browser_state",
+ AsyncMock(return_value=SimpleNamespace(get_working_page=AsyncMock(return_value=page))),
+ )
+ monkeypatch.setattr(
+ PrintPageBlock,
+ "_upload_pdf_artifact",
+ AsyncMock(return_value=("s3://artifacts/wr_1/page.pdf", "https://example.com/page.pdf")),
+ )
+ monkeypatch.setattr(PrintPageBlock, "record_output_parameter_value", _identity_record)
+ monkeypatch.setattr(PrintPageBlock, "build_block_result", _identity_build_block_result)
+
+ await block.execute(
+ workflow_run_id="wr_1",
+ workflow_run_block_id="wrb_1",
+ organization_id="o_1",
+ )
+
+ output = block._captured_output
+ assert [fi["filename"] for fi in output["downloaded_files"]] == ["page.pdf"]
+ assert output["downloaded_file_artifact_ids"] == ["a_new"]
+
+
+async def test_print_page_block_tolerates_save_failure(
+ monkeypatch: pytest.MonkeyPatch, _isolated_download_path: str
+) -> None:
+ """If ``save_downloaded_files`` raises, the block must still succeed —
+ workflow finalization will retry the upload, and the artifact-only output
+ fields (``filename`` / ``artifact_uri`` / ``artifact_url``) are still useful."""
+ skyvern_context.set(SkyvernContext(organization_id="o_1", workflow_run_id="wr_1", run_id="wr_1"))
+
+ save_mock = AsyncMock(side_effect=RuntimeError("S3 down"))
+ # Baseline-capture awaits get_downloaded_files once before save runs.
+ get_mock = AsyncMock(return_value=[])
+
+ fake_app = SimpleNamespace(
+ STORAGE=SimpleNamespace(
+ save_downloaded_files=save_mock,
+ get_downloaded_files=get_mock,
+ ),
+ )
+ monkeypatch.setattr(block_module, "app", fake_app)
+
+ block = PrintPageBlock(label="print", output_parameter=_output_parameter("print_out"))
+ monkeypatch.setattr(
+ PrintPageBlock,
+ "get_workflow_run_context",
+ lambda self, workflow_run_id: SimpleNamespace(organization_id="o_1"),
+ )
+ page = _make_page_pdf_mock()
+ monkeypatch.setattr(
+ PrintPageBlock,
+ "get_or_create_browser_state",
+ AsyncMock(return_value=SimpleNamespace(get_working_page=AsyncMock(return_value=page))),
+ )
+ monkeypatch.setattr(
+ PrintPageBlock,
+ "_upload_pdf_artifact",
+ AsyncMock(return_value=("s3://artifacts/wr_1/page.pdf", "https://example.com/page.pdf")),
+ )
+ monkeypatch.setattr(PrintPageBlock, "record_output_parameter_value", _identity_record)
+ monkeypatch.setattr(PrintPageBlock, "build_block_result", _identity_build_block_result)
+
+ result = await block.execute(
+ workflow_run_id="wr_1",
+ workflow_run_block_id="wrb_1",
+ organization_id="o_1",
+ )
+
+ assert result.success is True
+ # Baseline call happened, but the post-save fetch did not (save failed first).
+ assert get_mock.await_count == 1
+ output = block._captured_output
+ assert output["downloaded_files"] == []
+ assert output["downloaded_file_urls"] == []
+ assert output["downloaded_file_artifact_ids"] == []
+ assert output["artifact_uri"] == "s3://artifacts/wr_1/page.pdf"
+
+
+async def test_print_page_block_excludes_files_downloaded_by_prior_block(
+ monkeypatch: pytest.MonkeyPatch, _isolated_download_path: str
+) -> None:
+ """A PrintPageBlock placed after another block (e.g. TaskBlock) whose downloads
+ already landed in the run's download directory must filter those out — its own
+ output should only carry the PDF it just generated. Captured by mirroring the
+ `capture_block_download_baseline` call TaskBlock/TaskV2Block use."""
+ skyvern_context.set(
+ SkyvernContext(
+ organization_id="o_1",
+ workflow_run_id="wr_1",
+ run_id="wr_1",
+ )
+ )
+
+ prior_file = FileInfo(
+ url="https://api.example.com/v1/artifacts/a_prior/content?artifact_name=prior.pdf",
+ filename="prior.pdf",
+ checksum="abc",
+ artifact_id="a_prior",
+ )
+ new_file = FileInfo(
+ url="https://api.example.com/v1/artifacts/a_new/content?artifact_name=page.pdf",
+ filename="page.pdf",
+ checksum="def",
+ artifact_id="a_new",
+ )
+
+ # First call: baseline capture sees the prior block's file.
+ # Second call: post-PDF read sees both prior + new.
+ get_mock = AsyncMock(side_effect=[[prior_file], [prior_file, new_file]])
+ fake_app = SimpleNamespace(
+ STORAGE=SimpleNamespace(
+ save_downloaded_files=AsyncMock(),
+ get_downloaded_files=get_mock,
+ ),
+ )
+ monkeypatch.setattr(block_module, "app", fake_app)
+
+ block = PrintPageBlock(label="print", output_parameter=_output_parameter("print_out"))
+ monkeypatch.setattr(
+ PrintPageBlock,
+ "get_workflow_run_context",
+ lambda self, workflow_run_id: SimpleNamespace(organization_id="o_1"),
+ )
+ page = _make_page_pdf_mock()
+ monkeypatch.setattr(
+ PrintPageBlock,
+ "get_or_create_browser_state",
+ AsyncMock(return_value=SimpleNamespace(get_working_page=AsyncMock(return_value=page))),
+ )
+ monkeypatch.setattr(
+ PrintPageBlock,
+ "_upload_pdf_artifact",
+ AsyncMock(return_value=("s3://artifacts/wr_1/page.pdf", "https://example.com/page.pdf")),
+ )
+ monkeypatch.setattr(PrintPageBlock, "record_output_parameter_value", _identity_record)
+ monkeypatch.setattr(PrintPageBlock, "build_block_result", _identity_build_block_result)
+
+ await block.execute(
+ workflow_run_id="wr_1",
+ workflow_run_block_id="wrb_1",
+ organization_id="o_1",
+ )
+
+ output = block._captured_output
+ assert [fi["filename"] for fi in output["downloaded_files"]] == ["page.pdf"]
+ assert output["downloaded_file_urls"] == [new_file.url]
+ assert output["downloaded_file_artifact_ids"] == ["a_new"]