implement an endpoint for running one or more labelled blocks in a workflow (#2878)
This commit is contained in:
72
skyvern/services/block_service.py
Normal file
72
skyvern/services/block_service.py
Normal file
@@ -0,0 +1,72 @@
|
||||
import structlog
|
||||
|
||||
from skyvern.forge import app
|
||||
from skyvern.forge.sdk.core import skyvern_context
|
||||
from skyvern.forge.sdk.schemas.organizations import Organization
|
||||
from skyvern.forge.sdk.workflow.models.workflow import WorkflowRequestBody, WorkflowRun
|
||||
from skyvern.schemas.runs import WorkflowRunRequest
|
||||
from skyvern.services import workflow_service
|
||||
|
||||
LOG = structlog.get_logger()
|
||||
|
||||
|
||||
async def ensure_workflow_run(
|
||||
organization: Organization,
|
||||
template: bool,
|
||||
workflow_permanent_id: str,
|
||||
workflow_run_request: WorkflowRunRequest,
|
||||
x_max_steps_override: int | None = None,
|
||||
) -> WorkflowRun:
|
||||
context = skyvern_context.ensure_context()
|
||||
|
||||
legacy_workflow_request = WorkflowRequestBody(
|
||||
data=workflow_run_request.parameters,
|
||||
proxy_location=workflow_run_request.proxy_location,
|
||||
webhook_callback_url=workflow_run_request.webhook_url,
|
||||
totp_identifier=workflow_run_request.totp_identifier,
|
||||
totp_verification_url=workflow_run_request.totp_url,
|
||||
browser_session_id=workflow_run_request.browser_session_id,
|
||||
max_screenshot_scrolls=workflow_run_request.max_screenshot_scrolls,
|
||||
extra_http_headers=workflow_run_request.extra_http_headers,
|
||||
)
|
||||
|
||||
workflow_run = await workflow_service.prepare_workflow(
|
||||
workflow_id=workflow_permanent_id,
|
||||
organization=organization,
|
||||
workflow_request=legacy_workflow_request,
|
||||
template=template,
|
||||
version=None,
|
||||
max_steps=x_max_steps_override,
|
||||
request_id=context.request_id,
|
||||
)
|
||||
|
||||
return workflow_run
|
||||
|
||||
|
||||
async def execute_blocks(
|
||||
api_key: str,
|
||||
block_labels: list[str],
|
||||
workflow_run_id: str,
|
||||
organization: Organization,
|
||||
browser_session_id: str | None = None,
|
||||
) -> WorkflowRun:
|
||||
"""
|
||||
Runs one or more blocks of a workflow.
|
||||
"""
|
||||
|
||||
LOG.info(
|
||||
"Executing block(s)",
|
||||
organization_id=organization.organization_id,
|
||||
workflow_run_id=workflow_run_id,
|
||||
block_labels=block_labels,
|
||||
)
|
||||
|
||||
workflow_run = await app.WORKFLOW_SERVICE.execute_workflow(
|
||||
workflow_run_id=workflow_run_id,
|
||||
api_key=api_key,
|
||||
organization=organization,
|
||||
block_labels=block_labels,
|
||||
browser_session_id=browser_session_id,
|
||||
)
|
||||
|
||||
return workflow_run
|
||||
@@ -12,18 +12,18 @@ from skyvern.schemas.runs import RunStatus, RunType, WorkflowRunRequest, Workflo
|
||||
LOG = structlog.get_logger(__name__)
|
||||
|
||||
|
||||
async def run_workflow(
|
||||
async def prepare_workflow(
|
||||
workflow_id: str,
|
||||
organization: Organization,
|
||||
workflow_request: WorkflowRequestBody, # this is the deprecated workflow request body
|
||||
template: bool = False,
|
||||
version: int | None = None,
|
||||
max_steps: int | None = None,
|
||||
api_key: str | None = None,
|
||||
request_id: str | None = None,
|
||||
request: Request | None = None,
|
||||
background_tasks: BackgroundTasks | None = None,
|
||||
) -> WorkflowRun:
|
||||
"""
|
||||
Prepare a workflow to be run.
|
||||
"""
|
||||
if template:
|
||||
if workflow_id not in await app.STORAGE.retrieve_global_workflows():
|
||||
raise InvalidTemplateWorkflowPermanentId(workflow_permanent_id=workflow_id)
|
||||
@@ -37,19 +37,48 @@ async def run_workflow(
|
||||
max_steps_override=max_steps,
|
||||
is_template_workflow=template,
|
||||
)
|
||||
|
||||
workflow = await app.WORKFLOW_SERVICE.get_workflow_by_permanent_id(
|
||||
workflow_permanent_id=workflow_id,
|
||||
organization_id=None if template else organization.organization_id,
|
||||
version=version,
|
||||
)
|
||||
|
||||
await app.DATABASE.create_task_run(
|
||||
task_run_type=RunType.workflow_run,
|
||||
organization_id=organization.organization_id,
|
||||
run_id=workflow_run.workflow_run_id,
|
||||
title=workflow.title,
|
||||
)
|
||||
|
||||
if max_steps:
|
||||
LOG.info("Overriding max steps per run", max_steps_override=max_steps)
|
||||
|
||||
return workflow_run
|
||||
|
||||
|
||||
async def run_workflow(
|
||||
workflow_id: str,
|
||||
organization: Organization,
|
||||
workflow_request: WorkflowRequestBody, # this is the deprecated workflow request body
|
||||
template: bool = False,
|
||||
version: int | None = None,
|
||||
max_steps: int | None = None,
|
||||
api_key: str | None = None,
|
||||
request_id: str | None = None,
|
||||
request: Request | None = None,
|
||||
background_tasks: BackgroundTasks | None = None,
|
||||
) -> WorkflowRun:
|
||||
workflow_run = await prepare_workflow(
|
||||
workflow_id=workflow_id,
|
||||
organization=organization,
|
||||
workflow_request=workflow_request,
|
||||
template=template,
|
||||
version=version,
|
||||
max_steps=max_steps,
|
||||
request_id=request_id,
|
||||
)
|
||||
|
||||
await AsyncExecutorFactory.get_executor().execute_workflow(
|
||||
request=request,
|
||||
background_tasks=background_tasks,
|
||||
@@ -60,6 +89,7 @@ async def run_workflow(
|
||||
browser_session_id=workflow_request.browser_session_id,
|
||||
api_key=api_key,
|
||||
)
|
||||
|
||||
return workflow_run
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user