diff --git a/backend/igny8_core/ai/tasks.py b/backend/igny8_core/ai/tasks.py index 98faf489..40f1000b 100644 --- a/backend/igny8_core/ai/tasks.py +++ b/backend/igny8_core/ai/tasks.py @@ -10,7 +10,7 @@ logger = logging.getLogger(__name__) @shared_task(bind=True, max_retries=3) -def run_ai_task(self, function_name: str, payload: dict, account_id: int = None): +def run_ai_task(self, function_name: str, payload: dict, account_id: int): """ Single Celery entrypoint for all AI functions. Dynamically loads and executes the requested function. @@ -18,7 +18,10 @@ def run_ai_task(self, function_name: str, payload: dict, account_id: int = None) Args: function_name: Name of the AI function (e.g., 'auto_cluster') payload: Function-specific payload - account_id: Account ID for account isolation + account_id: Account ID for account isolation (required) + + Raises: + Returns error dict if account_id not provided or account not found """ logger.info("=" * 80) logger.info(f"run_ai_task STARTED: {function_name}") @@ -29,14 +32,28 @@ def run_ai_task(self, function_name: str, payload: dict, account_id: int = None) logger.info("=" * 80) try: - # Get account - account = None - if account_id: - from igny8_core.auth.models import Account - try: - account = Account.objects.get(id=account_id) - except Account.DoesNotExist: - logger.warning(f"Account {account_id} not found") + # Validate account_id is provided + if not account_id: + error_msg = "account_id is required for AI task execution" + logger.error(f"[run_ai_task] {error_msg}") + return { + 'success': False, + 'error': error_msg, + 'error_type': 'ConfigurationError' + } + + # Get account and validate it exists + from igny8_core.auth.models import Account + try: + account = Account.objects.get(id=account_id) + except Account.DoesNotExist: + error_msg = f"Account {account_id} not found" + logger.error(f"[run_ai_task] {error_msg}") + return { + 'success': False, + 'error': error_msg, + 'error_type': 'AccountNotFound' + } # Get function from registry fn = get_function_instance(function_name)