quantum-ai-v3/agents/models.py
Claude 33dcdc23ba 🏗️ Phase 1: Complete architecture simplification - pure file-based agent system
MAJOR ARCHITECTURAL CHANGES:
- Remove Agent/AgentCategory database models entirely
- Update AgentExecution/ChatSession to use agent_slug instead of foreign keys
- Eliminate hybrid complexity and AgentCompat workaround classes
- Enhanced AgentFileService with production-ready caching

CORE IMPROVEMENTS:
- Pure file-based architecture eliminates database sync complexity
- Enhanced caching: 5min in dev, 1hr in production, graceful fallback
- Fixed validation logic for 0.0 price fields
- Added database indexes for optimal query performance
- Updated admin interface to work with new slug-based fields

TECHNICAL BENEFITS:
- Simplified agent execution without database record creation
- Eliminated get_or_create_agent_db_record() complexity
- Streamlined imports across core and agents apps
- Better error handling and cache availability detection

FILE CHANGES:
- agents/models.py: Removed Agent/AgentCategory models, updated execution models
- agents/services.py: Enhanced caching, improved validation, removed DB sync
- agents/views.py: Updated execute_agent to use direct agent_data
- agents/admin.py: Updated for slug-based fields
- core/views.py: Updated to use AgentFileService instead of Agent model

All 8 agents remain fully operational with significantly reduced codebase complexity.
Migration applied successfully with proper defaults for existing data.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-14 09:55:45 +05:30

114 lines
4.8 KiB
Python

from django.db import models
import uuid
class AgentExecution(models.Model):
STATUS_CHOICES = [
('pending', 'Pending'),
('running', 'Running'),
('completed', 'Completed'),
('failed', 'Failed'),
]
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
agent_slug = models.SlugField(max_length=100, help_text="Agent identifier from JSON config", default="unknown")
agent_name = models.CharField(max_length=200, help_text="Agent name for display purposes", default="Unknown Agent")
user = models.ForeignKey('authentication.User', on_delete=models.CASCADE)
input_data = models.JSONField()
output_data = models.JSONField(null=True, blank=True)
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='pending')
fee_charged = models.DecimalField(max_digits=10, decimal_places=2)
webhook_response = models.JSONField(null=True, blank=True)
error_message = models.TextField(blank=True)
execution_time = models.DurationField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
completed_at = models.DateTimeField(null=True, blank=True)
class Meta:
ordering = ['-created_at']
indexes = [
models.Index(fields=['agent_slug', '-created_at']),
models.Index(fields=['user', '-created_at']),
models.Index(fields=['status', '-created_at']),
]
def __str__(self):
return f"{self.agent_name} - {self.user.email} - {self.status}"
class ChatSession(models.Model):
STATUS_CHOICES = [
('active', 'Active'),
('completed', 'Completed'),
('expired', 'Expired'),
('abandoned', 'Abandoned'),
('failed', 'Failed'),
]
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
session_id = models.CharField(max_length=100, unique=True, help_text="Unique session identifier")
agent_slug = models.SlugField(max_length=100, help_text="Agent identifier from JSON config", default="unknown")
agent_name = models.CharField(max_length=200, help_text="Agent name for display purposes", default="Unknown Agent")
user = models.ForeignKey('authentication.User', on_delete=models.CASCADE)
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='active')
context_data = models.JSONField(default=dict, help_text="Session context and progress tracking")
fee_charged = models.DecimalField(max_digits=10, decimal_places=2)
expires_at = models.DateTimeField(null=True, blank=True, help_text="Session expiration time (30 minutes from last activity)")
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
completed_at = models.DateTimeField(null=True, blank=True)
def save(self, *args, **kwargs):
# Set expires_at to 30 minutes from now if not set
if not self.expires_at:
from django.utils import timezone
from datetime import timedelta
self.expires_at = timezone.now() + timedelta(minutes=30)
super().save(*args, **kwargs)
class Meta:
ordering = ['-created_at']
indexes = [
models.Index(fields=['session_id']),
models.Index(fields=['agent_slug', '-created_at']),
models.Index(fields=['user', '-created_at']),
]
def __str__(self):
return f"{self.agent_name} - {self.user.email} - {self.session_id}"
def is_expired(self):
from django.utils import timezone
if not self.expires_at:
return False # Sessions without expiration date are considered active
return timezone.now() > self.expires_at
def extend_session(self):
"""Extend session by 30 minutes from now"""
from django.utils import timezone
from datetime import timedelta
self.expires_at = timezone.now() + timedelta(minutes=30)
self.updated_at = timezone.now()
self.save()
class ChatMessage(models.Model):
MESSAGE_TYPE_CHOICES = [
('user', 'User Message'),
('agent', 'Agent Response'),
('system', 'System Message'),
]
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
session = models.ForeignKey(ChatSession, on_delete=models.CASCADE, related_name='messages')
message_type = models.CharField(max_length=10, choices=MESSAGE_TYPE_CHOICES)
content = models.TextField()
metadata = models.JSONField(default=dict, help_text="Additional message data like webhook responses")
timestamp = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['timestamp']
indexes = [
models.Index(fields=['session', 'timestamp']),
]
def __str__(self):
return f"{self.session.session_id} - {self.message_type} - {self.timestamp}"