Skip to content

Automate browser based workflows with AI

execute_task_webhook uses the latest non canceled step (#4007)

Shuchang Zheng25e375f78f6b
diff --git a/skyvern/forge/agent.py b/skyvern/forge/agent.py
index 3f963102d..9b84c31a8 100644
--- a/skyvern/forge/agent.py
+++ b/skyvern/forge/agent.py
@@ -3138,12 +3138,11 @@ class ForgeAgent:
await app.ARTIFACT_MANAGER.wait_for_upload_aiotasks([task.task_id])
if need_call_webhook:
- await self.execute_task_webhook(task=task, last_step=last_step, api_key=api_key)
+ await self.execute_task_webhook(task=task, api_key=api_key)
async def execute_task_webhook(
self,
task: Task,
- last_step: Step | None,
api_key: str | None,
) -> None:
if not api_key:
@@ -3159,6 +3158,7 @@ class ForgeAgent:
task_id=task.task_id,
)
return
+ last_step = await app.DATABASE.get_latest_step(task.task_id, organization_id=task.organization_id)
task_response = await self.build_task_response(task=task, last_step=last_step)
# try to build the new TaskRunResponse for backward compatibility
diff --git a/skyvern/forge/sdk/db/client.py b/skyvern/forge/sdk/db/client.py
index addf15482..ccd572c12 100644
--- a/skyvern/forge/sdk/db/client.py
+++ b/skyvern/forge/sdk/db/client.py
@@ -532,6 +532,7 @@ class AgentDB:
select(StepModel)
.filter_by(task_id=task_id)
.filter_by(organization_id=organization_id)
+ .filter(StepModel.status != StepStatus.canceled)
.order_by(StepModel.order.desc())
.order_by(StepModel.retry_index.desc())
)
diff --git a/skyvern/forge/sdk/routes/agent_protocol.py b/skyvern/forge/sdk/routes/agent_protocol.py
index 16b18f157..f42872995 100644
--- a/skyvern/forge/sdk/routes/agent_protocol.py
+++ b/skyvern/forge/sdk/routes/agent_protocol.py
@@ -1644,10 +1644,8 @@ async def cancel_task(
detail=f"Task not found {task_id}",
)
task = await app.agent.update_task(task_obj, status=TaskStatus.canceled)
- # get latest step
- latest_step = await app.DATABASE.get_latest_step(task_id, organization_id=current_org.organization_id)
# retry the webhook
- await app.agent.execute_task_webhook(task=task, last_step=latest_step, api_key=x_api_key)
+ await app.agent.execute_task_webhook(task=task, api_key=x_api_key)
async def _cancel_workflow_run(workflow_run_id: str, organization_id: str, x_api_key: str | None = None) -> None:
@@ -1778,7 +1776,7 @@ async def retry_webhook(
return await app.agent.build_task_response(task=task_obj)
# retry the webhook
- await app.agent.execute_task_webhook(task=task_obj, last_step=latest_step, api_key=x_api_key)
+ await app.agent.execute_task_webhook(task=task_obj, api_key=x_api_key)
return await app.agent.build_task_response(task=task_obj, last_step=latest_step)
diff --git a/skyvern/services/run_service.py b/skyvern/services/run_service.py
index a77f29777..ef143b432 100644
--- a/skyvern/services/run_service.py
+++ b/skyvern/services/run_service.py
@@ -86,8 +86,7 @@ async def cancel_task_v1(task_id: str, organization_id: str | None = None, api_k
if not task:
raise TaskNotFound(task_id=task_id)
task = await app.agent.update_task(task, status=TaskStatus.canceled)
- latest_step = await app.DATABASE.get_latest_step(task_id, organization_id=organization_id)
- await app.agent.execute_task_webhook(task=task, last_step=latest_step, api_key=api_key)
+ await app.agent.execute_task_webhook(task=task, api_key=api_key)
async def cancel_task_v2(task_id: str, organization_id: str | None = None) -> None:
@@ -164,7 +163,7 @@ async def retry_run_webhook(run_id: str, organization_id: str | None = None, api
raise TaskNotFound(task_id=run_id)
latest_step = await app.DATABASE.get_latest_step(run_id, organization_id=organization_id)
if latest_step:
- await app.agent.execute_task_webhook(task=task, last_step=latest_step, api_key=api_key)
+ await app.agent.execute_task_webhook(task=task, api_key=api_key)
elif run.task_run_type == RunType.task_v2:
task_v2 = await app.DATABASE.get_task_v2(run_id, organization_id=organization_id)
if not task_v2: