quantum-ai-v2/agents/management/commands/create_five_whys_agent.py
Claude ab2360a91d 💬 Implement complete chat-based agent system with 5 Whys Analysis
🏗️ **Database Extensions:**
- Add agent_type field to Agent model (form/chat distinction)
- Create ChatSession model for conversation management
- Create ChatMessage model for message storage with metadata
- Add database indexes for performance optimization

🎨 **Frontend Implementation:**
- Create agent_chat.html template with modern chat interface
- Implement real-time messaging UI with message bubbles
- Add typing indicators and loading states
- Responsive design for mobile and desktop
- Empty states and error handling

🔧 **Backend Logic:**
- Extend agent_detail_view to route chat vs form agents
- Implement chat_agent_view for chat interface rendering
- Add start_chat_session API endpoint with session management
- Add send_chat_message API endpoint with webhook integration
- Add get_chat_history and end_chat_session endpoints
- Session-based fee charging system

🤖 **5 Whys Agent:**
- Create "Analysis & Problem Solving" category
- Implement 5 Whys Analysis chat-based agent (15 AED)
- Interactive problem-solving methodology guidance
- N8N webhook integration for AI responses

 **Interactive Features:**
- JavaScript ChatInterface class for real-time communication
- Auto-session creation and management
- Message validation and error handling
- Automatic scrolling and UI state management
- CSRF protection and security measures

🔗 **API Integration:**
- Chat-specific API endpoints with RESTful design
- Webhook payload formatting for N8N integration
- Session ID tracking and conversation continuity
- Metadata storage for webhook responses

🛡️ **Security & UX:**
- Login required for chat agents
- Wallet balance validation before session creation
- Session isolation per user and agent
- Comprehensive error handling and user feedback

**New URLs:**
- /agents/api/chat/start/ - Start new chat session
- /agents/api/chat/send/ - Send chat message
- /agents/api/chat/history/<session_id>/ - Get chat history
- /agents/api/chat/end/ - End chat session
- /agents/five-whys-analysis/ - 5 Whys chat interface

**Backward Compatibility:**
- Existing form-based agents (Social Ads, Job Posting, PDF) unchanged
- Admin interface updated to show agent types
- Marketplace displays both agent types seamlessly

🚀 Ready for interactive 5 Whys problem-solving conversations\!

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-01 09:39:43 +05:30

76 lines
3.4 KiB
Python

from django.core.management.base import BaseCommand
from agents.models import AgentCategory, Agent
class Command(BaseCommand):
help = 'Create the 5 Whys chat-based analysis agent'
def handle(self, *args, **options):
# Create or get the Analysis category
category, created = AgentCategory.objects.get_or_create(
slug='analysis',
defaults={
'name': 'Analysis & Problem Solving',
'description': 'Advanced analytical tools for problem-solving and decision making',
'icon': '🧠',
'is_active': True
}
)
if created:
self.stdout.write(f'✅ Created category: {category.name}')
else:
self.stdout.write(f'📂 Using existing category: {category.name}')
# Create the 5 Whys agent
agent, created = Agent.objects.get_or_create(
slug='five-whys-analysis',
defaults={
'name': '5 Whys Analysis',
'short_description': 'Interactive problem-solving using the proven 5 Whys methodology',
'description': '''Discover the root cause of any problem through guided conversation using the 5 Whys technique.
This interactive agent helps you systematically drill down to the core issue by asking "why" five times. Perfect for:
• Troubleshooting operational problems
• Understanding process failures
• Identifying systemic issues
• Improving quality and efficiency
The conversation-based approach ensures you think deeply about each layer of the problem, leading to more effective solutions.''',
'category': category,
'price': 15.00,
'agent_type': 'chat', # This is a chat-based agent
'form_schema': None, # Chat agents don't use form schemas
'webhook_url': 'http://localhost:5678/webhook/5-whys-web', # N8N webhook URL
'is_active': True
}
)
if created:
self.stdout.write(
self.style.SUCCESS(f'🎉 Successfully created 5 Whys Analysis agent!')
)
self.stdout.write(f' 💬 Agent Type: {agent.agent_type}')
self.stdout.write(f' 💰 Price: {agent.price} AED')
self.stdout.write(f' 🔗 Webhook: {agent.webhook_url}')
self.stdout.write(f' 📂 Category: {agent.category.name}')
else:
self.stdout.write(
self.style.WARNING(f'⚠️ 5 Whys Analysis agent already exists')
)
# Update existing agent to ensure it's chat-based
if agent.agent_type != 'chat':
agent.agent_type = 'chat'
agent.form_schema = None
agent.save()
self.stdout.write(
self.style.SUCCESS(f'✅ Updated existing agent to chat-based')
)
self.stdout.write('')
self.stdout.write('🚀 Next steps:')
self.stdout.write(' 1. Ensure N8N webhook is running on localhost:5678')
self.stdout.write(' 2. Visit /agents/five-whys-analysis/ to test the chat interface')
self.stdout.write(' 3. Start a conversation to test the 5 Whys methodology')
self.stdout.write('')
self.stdout.write('💡 The agent is now ready for interactive problem-solving!')