import uuid from django.conf import settings from django.db import models from encrypted_model_fields.fields import EncryptedCharField class AIInteraction(models.Model): class MessageRole(models.TextChoices): USER = "user", "User" ASSISTANT = "assistant", "Assistant" id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="ai_interactions") session_id = models.CharField(max_length=255, blank=True) message_role = models.CharField(max_length=10, choices=MessageRole.choices) message_content = models.TextField() tokens_used = models.IntegerField(default=0) created_at = models.DateTimeField(auto_now_add=True) class Meta: db_table = "ai_interactions" ordering = ["-created_at"] def __str__(self): return f"AI {self.message_role} - {self.user.email}" class AgentConfig(models.Model): class Provider(models.TextChoices): OPENAI = "openai", "OpenAI" GOOGLE = "google", "Google Gemini" DEEPSEEK = "deepseek", "DeepSeek" OLLAMA = "ollama", "Ollama" CUSTOM = "custom", "Custom" id = models.AutoField(primary_key=True) user = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, blank=True, related_name="agent_configs" ) name = models.CharField(max_length=100) description = models.TextField(blank=True) provider = models.CharField(max_length=20, choices=Provider.choices, default=Provider.OPENAI) api_key = EncryptedCharField(max_length=500, blank=True, default="") api_secret = EncryptedCharField(max_length=500, blank=True, default="") base_url = models.URLField(max_length=500, blank=True, default="") model_name = models.CharField(max_length=100, default="gpt-4o") system_prompt = models.TextField(blank=True, default="") temperature = models.FloatField(default=0.7) max_tokens = models.IntegerField(default=2000) capabilities = models.JSONField(default=list, blank=True) is_active = models.BooleanField(default=True) is_default = models.BooleanField(default=False) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: db_table = "agent_configs" verbose_name = "Agent Configuration" verbose_name_plural = "Agent Configurations" constraints = [ models.UniqueConstraint( fields=["user", "name"], name="unique_agent_per_user" ), ] def __str__(self): return self.name