implement an endpoint for running one or more labelled blocks in a workflow (#2878)
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import asyncio
|
||||
from enum import Enum
|
||||
from typing import Annotated, Any
|
||||
|
||||
@@ -76,6 +77,8 @@ from skyvern.forge.sdk.workflow.models.yaml import WorkflowCreateYAMLRequest
|
||||
from skyvern.schemas.artifacts import EntityType, entity_type_to_param
|
||||
from skyvern.schemas.runs import (
|
||||
CUA_ENGINES,
|
||||
BlockRunRequest,
|
||||
BlockRunResponse,
|
||||
RunEngine,
|
||||
RunResponse,
|
||||
RunType,
|
||||
@@ -85,7 +88,7 @@ from skyvern.schemas.runs import (
|
||||
WorkflowRunResponse,
|
||||
)
|
||||
from skyvern.schemas.workflows import WorkflowRequest
|
||||
from skyvern.services import run_service, task_v1_service, task_v2_service, workflow_service
|
||||
from skyvern.services import block_service, run_service, task_v1_service, task_v2_service, workflow_service
|
||||
from skyvern.webeye.actions.actions import Action
|
||||
|
||||
LOG = structlog.get_logger()
|
||||
@@ -850,6 +853,57 @@ async def retry_run_webhook(
|
||||
await run_service.retry_run_webhook(run_id, organization_id=current_org.organization_id, api_key=x_api_key)
|
||||
|
||||
|
||||
@base_router.post(
|
||||
"/run/workflows/blocks",
|
||||
include_in_schema=False,
|
||||
response_model=BlockRunResponse,
|
||||
)
|
||||
async def run_block(
|
||||
block_run_request: BlockRunRequest,
|
||||
organization: Organization = Depends(org_auth_service.get_current_org),
|
||||
template: bool = Query(False),
|
||||
x_api_key: Annotated[str | None, Header()] = None,
|
||||
) -> BlockRunResponse:
|
||||
"""
|
||||
Kick off the execution of one or more blocks in a workflow. Returns the
|
||||
workflow_run_id.
|
||||
"""
|
||||
|
||||
workflow_run = await block_service.ensure_workflow_run(
|
||||
organization=organization,
|
||||
template=template,
|
||||
workflow_permanent_id=block_run_request.workflow_id,
|
||||
workflow_run_request=block_run_request,
|
||||
)
|
||||
|
||||
browser_session_id = block_run_request.browser_session_id
|
||||
|
||||
asyncio.create_task(
|
||||
block_service.execute_blocks(
|
||||
api_key=x_api_key or "",
|
||||
block_labels=block_run_request.block_labels,
|
||||
workflow_run_id=workflow_run.workflow_run_id,
|
||||
organization=organization,
|
||||
browser_session_id=browser_session_id,
|
||||
)
|
||||
)
|
||||
|
||||
return BlockRunResponse(
|
||||
block_labels=block_run_request.block_labels,
|
||||
run_id=workflow_run.workflow_run_id,
|
||||
run_type=RunType.workflow_run,
|
||||
status=str(workflow_run.status),
|
||||
output=None,
|
||||
failure_reason=workflow_run.failure_reason,
|
||||
created_at=workflow_run.created_at,
|
||||
modified_at=workflow_run.modified_at,
|
||||
run_request=block_run_request,
|
||||
downloaded_files=None,
|
||||
recording_url=None,
|
||||
app_url=f"{settings.SKYVERN_APP_URL.rstrip('/')}/workflows/{workflow_run.workflow_permanent_id}/{workflow_run.workflow_run_id}",
|
||||
)
|
||||
|
||||
|
||||
################# Legacy Endpoints #################
|
||||
@legacy_base_router.post(
|
||||
"/webhook",
|
||||
|
||||
@@ -10,6 +10,7 @@ from skyvern import analytics
|
||||
from skyvern.config import settings
|
||||
from skyvern.constants import GET_DOWNLOADED_FILES_TIMEOUT, SAVE_DOWNLOADED_FILES_TIMEOUT
|
||||
from skyvern.exceptions import (
|
||||
BlockNotFound,
|
||||
BrowserSessionNotFound,
|
||||
FailedToSendWebhook,
|
||||
InvalidCredentialId,
|
||||
@@ -252,6 +253,7 @@ class WorkflowService:
|
||||
workflow_run_id: str,
|
||||
api_key: str,
|
||||
organization: Organization,
|
||||
block_labels: list[str] | None = None,
|
||||
browser_session_id: str | None = None,
|
||||
) -> WorkflowRun:
|
||||
"""Execute a workflow."""
|
||||
@@ -326,8 +328,32 @@ class WorkflowService:
|
||||
)
|
||||
return workflow_run
|
||||
|
||||
all_blocks = workflow.workflow_definition.blocks
|
||||
|
||||
if block_labels and len(block_labels):
|
||||
blocks: list[BlockTypeVar] = []
|
||||
all_labels = {block.label: block for block in all_blocks}
|
||||
|
||||
for label in block_labels:
|
||||
if label not in all_labels:
|
||||
raise BlockNotFound(block_label=label)
|
||||
|
||||
blocks.append(all_labels[label])
|
||||
|
||||
LOG.info(
|
||||
"Executing workflow blocks via whitelist",
|
||||
workflow_run_id=workflow_run.workflow_run_id,
|
||||
block_cnt=len(blocks),
|
||||
block_labels=block_labels,
|
||||
)
|
||||
|
||||
else:
|
||||
blocks = all_blocks
|
||||
|
||||
if not blocks:
|
||||
raise SkyvernException(f"No blocks found for the given block labels: {block_labels}")
|
||||
|
||||
# Execute workflow blocks
|
||||
blocks = workflow.workflow_definition.blocks
|
||||
blocks_cnt = len(blocks)
|
||||
block_result = None
|
||||
for block_idx, block in enumerate(blocks):
|
||||
|
||||
Reference in New Issue
Block a user