48 lines
1.6 KiB
Python
48 lines
1.6 KiB
Python
# Copyright (c) 2025 Patrick Motsch
|
|
# All rights reserved.
|
|
"""
|
|
State Tools
|
|
Shared utilities for workflow state management and validation.
|
|
"""
|
|
|
|
import logging
|
|
from typing import Any
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class WorkflowStoppedException(Exception):
|
|
"""Exception raised when a workflow is stopped by the user."""
|
|
pass
|
|
|
|
|
|
def checkWorkflowStopped(services: Any) -> None:
|
|
"""
|
|
Check if workflow has been stopped by user and raise exception if so.
|
|
|
|
Args:
|
|
services: Services object with workflow and interfaceDbChat for fresh status check
|
|
|
|
Raises:
|
|
WorkflowStoppedException: If workflow status is "stopped"
|
|
"""
|
|
workflow = services.workflow
|
|
if not workflow:
|
|
return
|
|
|
|
try:
|
|
# Get the current workflow status from the database to avoid stale data
|
|
currentWorkflow = services.interfaceDbChat.getWorkflow(workflow.id)
|
|
if currentWorkflow and currentWorkflow.status == "stopped":
|
|
logger.info("Workflow stopped by user, aborting operation")
|
|
raise WorkflowStoppedException("Workflow was stopped by user")
|
|
except WorkflowStoppedException:
|
|
# Re-raise the stop signal immediately
|
|
raise
|
|
except Exception as e:
|
|
# If we can't get the current status due to other database issues, fall back to the in-memory object
|
|
logger.warning(f"Could not check current workflow status from database: {str(e)}")
|
|
if workflow and workflow.status == "stopped":
|
|
logger.info("Workflow stopped by user (from in-memory object), aborting operation")
|
|
raise WorkflowStoppedException("Workflow was stopped by user")
|
|
|