YapTrap
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
Viewing readonly version of main branch: v50View latest version
Version: 1.0
Date: December 2024
Status: Draft
This document describes the software design for YapTrap, detailing the system architecture, component design, and implementation approach that satisfies the requirements specified in the SRS.
This design covers:
- Overall system architecture and component relationships
- Database schema and data flow design
- Platform integration patterns and APIs
- AI/LLM integration architecture
- Administrative interface design
- Monitoring and logging infrastructure
- Modularity: Clear separation of concerns with well-defined interfaces
- Scalability: Horizontal scaling capabilities within Val Town constraints
- Reliability: Defensive programming with comprehensive error handling
- Maintainability: Clean code with extensive testing and documentation
- Security: Security-first design with data protection throughout
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Messaging │ │ YapTrap │ │ Admin │
│ Platforms │◄──►│ Core System │◄──►│ Dashboard │
│ │ │ │ │ │
│ • Slack │ │ • Message Proc │ │ • Monitoring │
│ • SMS/MMS │ │ • AI Engine │ │ • Configuration │
│ • Email │ │ • Database │ │ • Analytics │
│ • WhatsApp │ │ • Platform APIs │ │ • User Mgmt │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│
┌─────────────────┐
│ External │
│ Services │
│ │
│ • LLM APIs │
│ • Database │
│ • Monitoring │
└─────────────────┘
- Message Router: Central message processing and routing
- Platform Handlers: Platform-specific integration modules
- AI Engine: LLM integration and response generation
- Personality Engine: User profiling and adaptation logic
- Database Layer: Data persistence and retrieval
- Admin Interface: Web-based management dashboard
- Monitoring System: Logging, metrics, and health monitoring
backend/
├── index.ts # Main HTTP entry point (Hono app)
├── database/
│ ├── schema.ts # Database schema definitions
│ ├── queries.ts # Database query functions
│ └── migrations.ts # Schema migrations
├── platforms/
│ ├── base.ts # Base platform handler interface
│ ├── slack.ts # Slack integration
│ ├── sms.ts # SMS/MMS via Telnyx
│ ├── email.ts # Email via SendGrid
│ └── whatsapp.ts # WhatsApp Business API
├── ai/
│ ├── engine.ts # LLM integration layer
│ ├── personality.ts # Personality analysis and profiling
│ └── context.ts # Conversation context management
├── monitoring/
│ ├── logger.ts # Centralized logging
│ ├── metrics.ts # Performance and usage metrics
│ └── health.ts # System health monitoring
└── routes/
├── webhooks.ts # Platform webhook handlers
├── admin.ts # Admin API endpoints
└── static.ts # Static file serving
frontend/
├── index.html # Main admin dashboard
├── components/
│ ├── Dashboard.tsx # Main dashboard component
│ ├── ConversationView.tsx
│ ├── MetricsView.tsx
│ └── ConfigView.tsx
└── styles/
└── main.css
shared/
├── types.ts # Shared TypeScript interfaces
├── utils.ts # Common utility functions
└── constants.ts # System constants and configuration
-- Conversations: Track group conversations across platforms CREATE TABLE conversations ( id TEXT PRIMARY KEY, platform TEXT NOT NULL, platform_id TEXT NOT NULL, name TEXT, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, last_activity DATETIME, settings JSON, is_active BOOLEAN DEFAULT 1 ); -- Users: Individual participants across all conversations CREATE TABLE users ( id TEXT PRIMARY KEY, platform TEXT NOT NULL, platform_user_id TEXT NOT NULL, display_name TEXT, personality_profile JSON, preferences JSON, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ); -- Messages: All messages in conversations CREATE TABLE messages ( id TEXT PRIMARY KEY, conversation_id TEXT NOT NULL, user_id TEXT, content TEXT NOT NULL, message_type TEXT DEFAULT 'user', -- 'user', 'yaptrap', 'system' platform_message_id TEXT, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, metadata JSON, FOREIGN KEY (conversation_id) REFERENCES conversations(id), FOREIGN KEY (user_id) REFERENCES users(id) ); -- Conversation Participants: Many-to-many relationship CREATE TABLE conversation_participants ( conversation_id TEXT NOT NULL, user_id TEXT NOT NULL, joined_at DATETIME DEFAULT CURRENT_TIMESTAMP, role TEXT DEFAULT 'member', -- 'member', 'admin', 'bot' is_active BOOLEAN DEFAULT 1, PRIMARY KEY (conversation_id, user_id), FOREIGN KEY (conversation_id) REFERENCES conversations(id), FOREIGN KEY (user_id) REFERENCES users(id) ); -- System Logs: Comprehensive logging for monitoring CREATE TABLE system_logs ( id TEXT PRIMARY KEY, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, level TEXT NOT NULL, -- 'debug', 'info', 'warn', 'error' component TEXT NOT NULL, message TEXT NOT NULL, metadata JSON, conversation_id TEXT, user_id TEXT ); -- Configuration: System and conversation-specific settings CREATE TABLE configuration ( key TEXT PRIMARY KEY, value JSON NOT NULL, scope TEXT DEFAULT 'global', -- 'global', 'conversation', 'user' scope_id TEXT, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_by TEXT );
- Webhook Reception: Platform sends message to webhook endpoint
- Message Normalization: Convert platform-specific format to internal format
- User/Conversation Resolution: Identify or create user and conversation records
- Context Analysis: Analyze message content and conversation context
- Response Decision: Determine if and how YapTrap should respond
- Response Generation: Generate appropriate response using AI engine
- Platform Delivery: Send response back through platform API
- Logging: Record all activities for monitoring and learning
- Message Analysis: Extract communication patterns and preferences
- Profile Update: Update user personality profile with new insights
- Group Dynamics: Analyze group interaction patterns
- Adaptation: Adjust YapTrap's behavior based on learned preferences
interface MessageRouter {
// Route incoming webhook messages to appropriate platform handlers
routeMessage(platform: string, payload: any): Promise<void>;
// Process normalized messages through the AI pipeline
processMessage(message: NormalizedMessage): Promise<void>;
// Determine response strategy based on context and configuration
shouldRespond(context: ConversationContext): Promise<boolean>;
}
interface PlatformHandler {
// Normalize incoming platform messages to internal format
normalizeMessage(payload: any): Promise<NormalizedMessage>;
// Send response back to platform
sendMessage(conversationId: string, content: string): Promise<void>;
// Handle platform-specific authentication and setup
authenticate(): Promise<boolean>;
// Validate webhook signatures and requests
validateWebhook(payload: any, signature: string): boolean;
}
interface AIEngine {
// Generate contextual response based on conversation history
generateResponse(context: ConversationContext): Promise<string>;
// Analyze message sentiment and intent
analyzeMessage(message: NormalizedMessage): Promise<MessageAnalysis>;
// Update conversation context with new message
updateContext(context: ConversationContext, message: NormalizedMessage): Promise<ConversationContext>;
}
interface PersonalityEngine {
// Analyze user communication patterns
analyzeUserPersonality(userId: string, messages: NormalizedMessage[]): Promise<PersonalityProfile>;
// Update personality profile with new insights
updatePersonalityProfile(userId: string, insights: PersonalityInsights): Promise<void>;
// Get personality-adapted response style
getResponseStyle(userId: string, groupContext: GroupContext): Promise<ResponseStyle>;
}
// Platform webhook receivers
POST /webhooks/slack # Slack events and messages
POST /webhooks/sms # Telnyx SMS/MMS webhooks
POST /webhooks/email # SendGrid inbound email
POST /webhooks/whatsapp # WhatsApp Business API webhooks
// Administrative endpoints
GET /admin/conversations # List all conversations
GET /admin/users # List all users
GET /admin/metrics # System metrics and health
POST /admin/config # Update configuration
GET /admin/logs # Stream system logs
// Normalized message format across all platforms
interface NormalizedMessage {
id: string;
conversationId: string;
userId: string;
content: string;
timestamp: Date;
platform: string;
messageType: 'text' | 'image' | 'file' | 'system';
metadata: Record<string, any>;
}
// Conversation context for AI processing
interface ConversationContext {
conversationId: string;
participants: User[];
recentMessages: NormalizedMessage[];
groupDynamics: GroupDynamics;
yapTrapSettings: YapTrapSettings;
}
// User personality profile
interface PersonalityProfile {
userId: string;
communicationStyle: CommunicationStyle;
interests: string[];
responsePreferences: ResponsePreferences;
groupRole: GroupRole;
lastUpdated: Date;
}
- Webhook Validation: Verify all incoming webhooks using platform-specific signatures
- API Authentication: Secure admin endpoints with authentication tokens
- Rate Limiting: Implement rate limiting to prevent abuse
- Input Validation: Sanitize and validate all input data
- Encryption: Encrypt sensitive data at rest and in transit
- Access Control: Implement role-based access control for admin functions
- Audit Logging: Log all data access and modifications
- Data Retention: Implement configurable data retention policies
- Structured Logging: Use consistent JSON format for all log entries
- Log Levels: Implement debug, info, warn, error levels
- Contextual Logging: Include conversation and user context in logs
- Performance Logging: Track response times and system performance
- Message Metrics: Track message volume, response rates, engagement
- Performance Metrics: Monitor response times, error rates, throughput
- User Metrics: Track user satisfaction, personality learning accuracy
- System Metrics: Monitor resource usage, health status
- Main Entry Point: Single HTTP handler in
backend/index.ts
- Environment Configuration: Use Val Town environment variables
- Database: SQLite for development, external database for production
- Static Assets: Serve admin interface through Val Town
- Database: External PostgreSQL or SQLite for data persistence
- LLM Services: OpenAI, xAI, Claude APIs for AI functionality
- Platform APIs: Direct integration with messaging platform APIs
- Monitoring: External logging and metrics collection services
- Graceful Degradation: Continue operation with reduced functionality during failures
- Retry Logic: Implement exponential backoff for transient failures
- Circuit Breakers: Prevent cascade failures in external service calls
- Fallback Responses: Provide default responses when AI services fail
- Automatic Recovery: Self-healing for common failure scenarios
- Manual Intervention: Clear procedures for manual recovery
- Data Recovery: Backup and restore procedures for data loss
- Service Recovery: Procedures for restoring service after outages
- Stateless Design: All components designed for horizontal scaling
- Caching Strategy: Cache frequently accessed data and AI responses
- Database Optimization: Efficient queries and indexing strategy
- Rate Limiting: Protect against overload and abuse
- Response Time: Target sub-5-second response times
- Concurrent Processing: Handle multiple conversations simultaneously
- Resource Management: Efficient memory and CPU usage
- Network Optimization: Minimize external API calls and data transfer
Document Control:
- Author: Technical Architect
- Reviewers: Senior Developer, DevOps Engineer
- Approval: Technical Lead
- Implementation Reference: All code must trace back to this design
- Next Review: Upon major architectural changes