diff --git a/skyvern/forge/sdk/routes/agent_protocol.py b/skyvern/forge/sdk/routes/agent_protocol.py index 75fdc79a..a6b6b652 100644 --- a/skyvern/forge/sdk/routes/agent_protocol.py +++ b/skyvern/forge/sdk/routes/agent_protocol.py @@ -1084,11 +1084,17 @@ async def observer_cruise( if x_max_iterations_override: LOG.info("Overriding max iterations for observer", max_iterations_override=x_max_iterations_override) - observer_cruise = await observer_service.initialize_observer_cruise( - organization=organization, - user_prompt=data.user_prompt, - user_url=str(data.url) if data.url else None, - ) + try: + observer_cruise = await observer_service.initialize_observer_cruise( + organization=organization, + user_prompt=data.user_prompt, + user_url=str(data.url) if data.url else None, + ) + except LLMProviderError: + LOG.error("LLM failure to initialize observer cruise", exc_info=True) + raise HTTPException( + status_code=500, detail="Skyvern LLM failure to initialize observer cruise. Please try again later." + ) analytics.capture("skyvern-oss-agent-observer-cruise", data={"url": observer_cruise.url}) await AsyncExecutorFactory.get_executor().execute_cruise( request=request, diff --git a/skyvern/forge/sdk/services/observer_service.py b/skyvern/forge/sdk/services/observer_service.py index df2a124a..d74e630c 100644 --- a/skyvern/forge/sdk/services/observer_service.py +++ b/skyvern/forge/sdk/services/observer_service.py @@ -109,34 +109,57 @@ async def initialize_observer_cruise( # create workflow and workflow run max_steps_override = 10 - new_workflow = await app.WORKFLOW_SERVICE.create_empty_workflow(organization, metadata.workflow_title) - workflow_run = await app.WORKFLOW_SERVICE.setup_workflow_run( - request_id=None, - workflow_request=WorkflowRequestBody(), - workflow_permanent_id=new_workflow.workflow_permanent_id, - organization_id=organization.organization_id, - version=None, - max_steps_override=max_steps_override, - ) - await app.DATABASE.update_observer_thought( - observer_thought_id=observer_thought.observer_thought_id, - organization_id=organization.organization_id, - workflow_run_id=workflow_run.workflow_run_id, - workflow_id=new_workflow.workflow_id, - workflow_permanent_id=new_workflow.workflow_permanent_id, - thought=metadata_response.get("thoughts", ""), - output=metadata.model_dump(), - ) + try: + new_workflow = await app.WORKFLOW_SERVICE.create_empty_workflow(organization, metadata.workflow_title) + workflow_run = await app.WORKFLOW_SERVICE.setup_workflow_run( + request_id=None, + workflow_request=WorkflowRequestBody(), + workflow_permanent_id=new_workflow.workflow_permanent_id, + organization_id=organization.organization_id, + version=None, + max_steps_override=max_steps_override, + ) + except Exception: + LOG.error("Failed to setup cruise workflow run", exc_info=True) + # fail the workflow run + await app.WORKFLOW_SERVICE.mark_workflow_run_as_failed( + workflow_run_id=workflow_run.workflow_run_id, + failure_reason="Skyvern failed to setup the workflow run", + ) + raise + + try: + await app.DATABASE.update_observer_thought( + observer_thought_id=observer_thought.observer_thought_id, + organization_id=organization.organization_id, + workflow_run_id=workflow_run.workflow_run_id, + workflow_id=new_workflow.workflow_id, + workflow_permanent_id=new_workflow.workflow_permanent_id, + thought=metadata_response.get("thoughts", ""), + output=metadata.model_dump(), + ) + except Exception: + LOG.warning("Failed to update observer thought", exc_info=True) # update oserver cruise - observer_cruise = await app.DATABASE.update_observer_cruise( - observer_cruise_id=observer_cruise.observer_cruise_id, - workflow_run_id=workflow_run.workflow_run_id, - workflow_id=new_workflow.workflow_id, - workflow_permanent_id=new_workflow.workflow_permanent_id, - url=url, - organization_id=organization.organization_id, - ) + try: + observer_cruise = await app.DATABASE.update_observer_cruise( + observer_cruise_id=observer_cruise.observer_cruise_id, + workflow_run_id=workflow_run.workflow_run_id, + workflow_id=new_workflow.workflow_id, + workflow_permanent_id=new_workflow.workflow_permanent_id, + url=url, + organization_id=organization.organization_id, + ) + except Exception: + LOG.warning("Failed to update observer cruise", exc_info=True) + # fail the workflow run + await app.WORKFLOW_SERVICE.mark_workflow_run_as_failed( + workflow_run_id=workflow_run.workflow_run_id, + failure_reason="Skyvern failed to update the observer cruise after initializing the workflow run", + ) + raise + return observer_cruise