""" AI Framework Models """ from django.db import models from igny8_core.auth.models import AccountBaseModel class IntegrationState(models.Model): """ Tracks whether AI integrations are enabled/disabled for each account. Single record per account with separate fields for each integration type. """ account = models.OneToOneField( 'igny8_core_auth.Account', on_delete=models.CASCADE, related_name='integration_state', help_text='Account that owns this integration state', primary_key=True ) # Enable/disable flags for each integration is_openai_enabled = models.BooleanField( default=True, help_text='Whether OpenAI integration is enabled for this account' ) is_runware_enabled = models.BooleanField( default=True, help_text='Whether Runware integration is enabled for this account' ) is_image_generation_enabled = models.BooleanField( default=True, help_text='Whether Image Generation Service is enabled for this account' ) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: db_table = 'ai_integration_state' verbose_name = 'Integration State' verbose_name_plural = 'Integration States' indexes = [ models.Index(fields=['is_openai_enabled']), models.Index(fields=['is_runware_enabled']), models.Index(fields=['is_image_generation_enabled']), ] def __str__(self): states = [] if self.is_openai_enabled: states.append('OpenAI') if self.is_runware_enabled: states.append('Runware') if self.is_image_generation_enabled: states.append('Image Gen') enabled_str = ', '.join(states) if states else 'None' return f"{self.account.name} - Enabled: {enabled_str}" class AITaskLog(AccountBaseModel): """ Unified logging table for all AI tasks. Stores request/response steps, costs, tokens, and results. """ task_id = models.CharField(max_length=255, db_index=True, null=True, blank=True) function_name = models.CharField(max_length=100, db_index=True) phase = models.CharField(max_length=50, default='INIT') message = models.TextField(blank=True) status = models.CharField(max_length=20, choices=[ ('success', 'Success'), ('error', 'Error'), ('pending', 'Pending'), ], default='pending') # Timing duration = models.IntegerField(null=True, blank=True, help_text="Duration in milliseconds") # Cost tracking cost = models.DecimalField(max_digits=10, decimal_places=6, default=0.0) tokens = models.IntegerField(default=0) # Step tracking request_steps = models.JSONField(default=list, blank=True) response_steps = models.JSONField(default=list, blank=True) # Error tracking error = models.TextField(null=True, blank=True) # Data payload = models.JSONField(null=True, blank=True) result = models.JSONField(null=True, blank=True) class Meta: db_table = 'igny8_ai_task_logs' ordering = ['-created_at'] indexes = [ models.Index(fields=['task_id']), models.Index(fields=['function_name', 'account']), models.Index(fields=['status', 'created_at']), ] def __str__(self): return f"{self.function_name} - {self.status} - {self.created_at}"