2 Commits

Author SHA1 Message Date
IGNY8 VPS (Salman)
25b1aa39b0 Step 5: Validate account_id is required and exists in tasks.py
- Make account_id parameter required (remove default None)
- Validate account_id is provided before proceeding
- Validate account exists in database
- Return proper error responses with error_type
- Update function docstring to reflect required parameter
2025-11-16 09:22:33 +00:00
IGNY8 VPS (Salman)
91d31ece31 Step 4: Validate account exists before calling get_model_config() in engine.py
- Add account validation before calling get_model_config()
- Wrap get_model_config() in try/except to handle ValueError (IntegrationSettings not configured)
- Handle other unexpected errors from get_model_config()
- Return proper error messages through _handle_error()
- Remove redundant model_from_integration code (get_model_config already handles this)
2025-11-16 09:21:24 +00:00
2 changed files with 48 additions and 33 deletions

View File

@@ -193,6 +193,12 @@ class AIEngine:
self.tracker.update("PREP", 25, prep_message, meta=self.step_tracker.get_meta())
# Phase 3: AI_CALL - Provider API Call (25-70%)
# Validate account exists before proceeding
if not self.account:
error_msg = "Account is required for AI function execution"
logger.error(f"[AIEngine] {error_msg}")
return self._handle_error(error_msg, fn)
ai_core = AICore(account=self.account)
function_name = fn.get_name()
@@ -201,29 +207,21 @@ class AIEngine:
function_id_base = function_name.replace('_', '-')
function_id = f"ai-{function_id_base}-01-desktop"
# Get model config from settings (Stage 4 requirement)
# Pass account to read model from IntegrationSettings
model_config = get_model_config(function_name, account=self.account)
model = model_config.get('model')
# Read model straight from IntegrationSettings for visibility
model_from_integration = None
if self.account:
try:
from igny8_core.modules.system.models import IntegrationSettings
openai_settings = IntegrationSettings.objects.filter(
integration_type='openai',
account=self.account,
is_active=True
).first()
if openai_settings and openai_settings.config:
model_from_integration = openai_settings.config.get('model')
except Exception as integration_error:
logger.warning(
"[AIEngine] Unable to read model from IntegrationSettings: %s",
integration_error,
exc_info=True,
)
# Get model config from settings (requires account)
# This will raise ValueError if IntegrationSettings not configured
try:
model_config = get_model_config(function_name, account=self.account)
model = model_config.get('model')
except ValueError as e:
# IntegrationSettings not configured or model missing
error_msg = str(e)
logger.error(f"[AIEngine] {error_msg}")
return self._handle_error(error_msg, fn)
except Exception as e:
# Other unexpected errors
error_msg = f"Failed to get model configuration: {str(e)}"
logger.error(f"[AIEngine] {error_msg}", exc_info=True)
return self._handle_error(error_msg, fn)
# Debug logging: Show model configuration (console only, not in step tracker)
logger.info(f"[AIEngine] Model Configuration for {function_name}:")

View File

@@ -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)