botserver/docs/src/chapter-07-gbapp/architecture.md

454 lines
8.6 KiB
Markdown
Raw Normal View History

2025-11-21 10:44:29 -03:00
# Architecture Overview
2025-10-28 19:47:29 -03:00
2025-11-22 16:12:32 -03:00
BotServer follows a modular architecture designed for scalability, maintainability, and extensibility. Each module handles specific responsibilities and communicates through well-defined interfaces.
2025-10-28 19:47:29 -03:00
2025-11-22 16:12:32 -03:00
## Core Architecture
2025-10-28 19:47:29 -03:00
2025-11-24 08:42:58 -03:00
### Data Flow Architecture
2025-11-23 17:02:22 -03:00
2025-11-24 14:15:01 -03:00
<img src="./assets/data-flow.svg" alt="BotServer Data Flow Architecture" style="max-height: 400px; width: 100%; object-fit: contain;">
2025-11-24 08:42:58 -03:00
### System Architecture
2025-11-24 14:15:01 -03:00
<img src="./assets/system-architecture.svg" alt="BotServer System Architecture" style="max-height: 400px; width: 100%; object-fit: contain;">
2025-11-23 17:02:22 -03:00
2025-11-24 08:42:58 -03:00
## Module Dependency Graph
2025-11-23 17:02:22 -03:00
2025-11-24 14:15:01 -03:00
<img src="./assets/module-dependency.svg" alt="Module Dependency Graph" style="max-height: 400px; width: 100%; object-fit: contain;">
2025-10-28 19:47:29 -03:00
2025-11-22 16:12:32 -03:00
## Module Organization
2025-10-28 19:47:29 -03:00
2025-11-23 17:02:22 -03:00
### Data Flow Through Modules
2025-11-24 14:15:01 -03:00
<img src="./assets/module-data-flow.svg" alt="Data Flow Through Modules" style="max-height: 400px; width: 100%; object-fit: contain;">
2025-11-23 17:02:22 -03:00
2025-11-22 16:12:32 -03:00
### Core Modules
2025-10-28 19:47:29 -03:00
2025-11-22 16:12:32 -03:00
#### `auth/`
Authentication and authorization system handling:
- User management
- Group/organization management
- Role-based access control (RBAC)
- JWT token management
- OAuth integration
- Two-factor authentication
2025-10-28 19:47:29 -03:00
2025-11-22 16:12:32 -03:00
#### `automation/`
Workflow automation engine providing:
- Process automation
- Scheduled tasks
- Event-driven automation
- Workflow orchestration
- Integration with external systems
2025-10-28 19:47:29 -03:00
2025-11-22 16:12:32 -03:00
#### `basic/`
BASIC dialect interpreter and runtime:
- Keyword implementation
- Script compilation
- Variable management
- Flow control
- Tool integration
- Error handling
#### `bootstrap/`
System initialization and startup:
- Component verification
- Service startup sequencing
- Database migrations
- Template deployment
- Health checks
- Configuration loading
#### `bot/`
Bot instance management:
- Bot lifecycle (create, mount, unmount)
- Conversation handling
- User input processing
- Response generation
- Multi-bot coordination
- Session isolation
### Communication Modules
#### `channels/`
Multi-channel messaging adapters:
- Web interface
- WhatsApp Business API
- Microsoft Teams
- Slack
- Instagram
- SMS
- Voice
#### `meet/`
Real-time communication features:
- Video conferencing
- Voice calls
- Screen sharing
- Recording
- Transcription
- Meeting scheduling
#### `web_server/`
HTTP server and web interface:
- Static file serving
- WebSocket handling
- REST API routing
- CORS management
- Request/response processing
### AI & Knowledge Modules
#### `llm/`
Large Language Model integration:
- Model selection
- Prompt formatting
- Token management
- Response streaming
- Cost tracking
- Model fallbacks
#### `llm_models/`
Specific model implementations:
- OpenAI (GPT-3.5, GPT-4)
- Anthropic (Claude)
- Google (Gemini)
- Meta (Llama)
- Local models
- Custom models
#### `prompt_manager/`
Centralized prompt management:
- Prompt templates
- Variable substitution
- Model-specific optimization
- Version control
- A/B testing
- Performance tracking
#### `context/`
Conversation context management:
- Context window optimization
- History management
- Context compression
- Relevance filtering
- Multi-turn tracking
### Storage & Data Modules
#### `drive/`
File and document management:
- File upload/download
- Document processing
- Version control
- Sharing permissions
- Quota management
- Search indexing
#### `drive_monitor/`
Storage monitoring and sync:
- Change detection
- Auto-sync
- Conflict resolution
- Backup management
- Storage analytics
#### `package_manager/`
Bot package management:
- Package loading
- Dependency resolution
- Version management
- Hot reload
- Package validation
### Processing Modules
#### `engines/`
Various processing engines:
- Rule engine
- Workflow engine
- Event processor
- Message queue
- Job scheduler
#### `calendar_engine/`
Calendar and scheduling:
- Event management
- Availability checking
- Meeting coordination
- Reminder system
- Timezone handling
#### `task_engine/`
Task management system:
- Task creation
- Assignment
- Status tracking
- Dependencies
- Notifications
#### `email/`
Email integration:
- SMTP sending
- IMAP receiving
- Template management
- Tracking
- Bounce handling
### Utility Modules
#### `session/`
User session management:
- Session creation
- State persistence
- Session timeout
- Concurrent sessions
- Session recovery
#### `config/`
Configuration management:
- Config loading
2025-10-28 19:47:29 -03:00
- Environment variables
2025-11-22 16:12:32 -03:00
- Hot reload
- Validation
- Defaults
#### `shared/`
Shared utilities and models:
- Database models
- Common types
- Helper functions
- Constants
- Error types
#### `compliance/`
Regulatory compliance:
- GDPR compliance
- Data retention
- Audit logging
- Privacy controls
- Consent management
#### `nvidia/`
GPU acceleration support:
- CUDA integration
- Model inference
- Batch processing
- Performance optimization
#### `ui_tree/`
UI component tree management:
- Virtual DOM
- Component lifecycle
- State management
- Event handling
- Rendering optimization
#### `web_automation/`
Web scraping and automation:
- Browser automation
- Content extraction
- Form filling
- Screenshot capture
- Change monitoring
## Data Flow
### Request Processing Pipeline
1. **Channel Adapter** receives user input
2. **Session Manager** identifies/creates session
3. **Context Manager** loads conversation history
4. **BASIC Interpreter** executes dialog script
5. **LLM Integration** processes natural language
6. **Knowledge Base** provides relevant information
7. **Response Generator** formats output
8. **Channel Adapter** delivers response
### Storage Architecture
#### Primary Database (PostgreSQL)
- User accounts
- Bot configurations
- Session data
- Conversation history
- System metadata
2025-11-23 13:46:55 -03:00
#### Object Storage (Drive)
2025-11-22 16:12:32 -03:00
- File uploads
- Document storage
- Media files
- Backups
- Logs
2025-11-23 13:46:55 -03:00
#### Cache Layer
2025-11-22 16:12:32 -03:00
- Session cache
- Frequently accessed data
- Rate limiting
- Temporary storage
- Pub/sub messaging
#### Vector Database (Qdrant)
- Document embeddings
- Semantic search index
- Knowledge base vectors
- Similarity matching
## Security Architecture
### Authentication Flow
1. User provides credentials
2. Auth module validates
3. JWT token issued
4. Token verified on each request
5. Session established
6. Permissions checked
### Data Protection
- Encryption at rest (database, files)
- Encryption in transit (TLS/SSL)
- Sensitive data masking
- PII detection
- Secure key management
### Access Control
- Role-based permissions
- Resource-level authorization
- API rate limiting
- IP allowlisting
- Audit logging
## Deployment Architecture
### Container Structure
- Main application container
- PostgreSQL database
2025-11-23 13:46:55 -03:00
- Drive storage (S3-compatible)
- Cache (Valkey)
2025-11-22 16:12:32 -03:00
- Qdrant vector DB
- Nginx reverse proxy
### Scaling Strategy
- Horizontal scaling for web servers
- Read replicas for database
- Distributed cache
- Load balancing
- Auto-scaling policies
### High Availability
- Multi-zone deployment
- Database replication
- Storage redundancy
- Health monitoring
- Automatic failover
## Performance Optimization
### Caching Strategy
- Response caching
- Query result caching
- Static asset caching
- API response caching
- Knowledge base caching
### Async Processing
- Background jobs
- Message queues
- Event-driven architecture
- Non-blocking I/O
- Worker pools
### Resource Management
- Connection pooling
- Memory management
- Token optimization
- Query optimization
- Lazy loading
## Monitoring & Observability
### Metrics Collection
- System metrics
- Application metrics
- Business metrics
- User analytics
- Performance tracking
### Logging
- Structured logging
- Log aggregation
- Error tracking
- Audit trails
- Debug logging
### Health Checks
- Liveness probes
- Readiness probes
- Dependency checks
- Performance monitoring
- Alert system
2025-10-28 19:47:29 -03:00
2025-11-22 16:12:32 -03:00
## Extension Points
2025-10-28 19:47:29 -03:00
2025-11-22 16:12:32 -03:00
### Plugin System
- Custom keywords
- External tools
- API integrations
- Custom channels
- Model providers
2025-10-28 19:47:29 -03:00
2025-11-22 16:12:32 -03:00
### Webhook Support
- Incoming webhooks
- Outgoing webhooks
- Event subscriptions
- Callback handling
- Retry mechanisms
2025-10-28 19:47:29 -03:00
2025-11-22 16:12:32 -03:00
### API Integration
- REST API
- GraphQL (planned)
- WebSocket
- gRPC (planned)
- OpenAPI spec
2025-10-28 19:47:29 -03:00
2025-11-22 16:12:32 -03:00
## Development Workflow
2025-10-28 19:47:29 -03:00
2025-11-22 16:12:32 -03:00
### Local Development
1. Clone repository
2. Install dependencies
3. Configure environment
4. Run migrations
5. Start services
6. Load templates
2025-10-28 19:47:29 -03:00
2025-11-22 16:12:32 -03:00
### Testing Strategy
- Unit tests
- Integration tests
- End-to-end tests
- Load testing
- Security testing
2025-10-28 19:47:29 -03:00
2025-11-22 16:12:32 -03:00
### CI/CD Pipeline
- Automated testing
- Code quality checks
- Security scanning
- Build process
- Deployment automation
2025-10-28 19:47:29 -03:00
2025-11-22 16:12:32 -03:00
## Future Architecture Plans
2025-10-28 19:47:29 -03:00
2025-11-22 16:12:32 -03:00
### Planned Enhancements
- Microservices migration
- Kubernetes native
- Multi-region support
- Edge deployment
- Serverless functions
2025-10-28 19:47:29 -03:00
2025-11-22 16:12:32 -03:00
### Performance Goals
- Sub-100ms response time
- 10,000+ concurrent users
- 99.99% uptime
- Elastic scaling
- Global CDN
2025-10-28 19:47:29 -03:00