ai-voicebot/docs/REFACTORING_SUMMARY.md

83 lines
3.0 KiB
Markdown

# Voicebot Module Refactoring
The voicebot/main.py functionality has been broken down into individual Python files for better organization and maintainability:
## New File Structure
### Core Modules
1. **`models.py`** - Data models and configuration
- `VoicebotArgs` - Pydantic model for CLI arguments and configuration
- `VoicebotMode` - Enum for client/provider modes
- `Peer` - WebRTC peer representation
- `JoinRequest` - Request model for joining lobbies
- `MessageData` - Type alias for message payloads
2. **`webrtc_signaling.py`** - WebRTC signaling client functionality
- `WebRTCSignalingClient` - Main WebRTC signaling client class
- Handles peer connection management, ICE candidates, session descriptions
- Registration status tracking and reconnection logic
- Message processing and event handling
3. **`session_manager.py`** - Session and lobby management
- `create_or_get_session()` - Session creation/retrieval
- `create_or_get_lobby()` - Lobby creation/retrieval
- HTTP API communication utilities
4. **`bot_orchestrator.py`** - FastAPI bot orchestration service
- Bot discovery and management
- FastAPI endpoints for bot operations
- Provider registration with main server
- Bot instance lifecycle management
5. **`client_main.py`** - Main client logic
- `main_with_args()` - Core client functionality
- `start_client_with_reload()` - Development mode with reload
- Event handlers for peer and track management
6. **`client_app.py`** - Client FastAPI application
- `create_client_app()` - Creates FastAPI app for client mode
- Health check and status endpoints
- Process isolation and locking
7. **`utils.py`** - Utility functions
- URL conversion utilities (`http_base_url`, `ws_url`)
- SSL context creation
- Network information logging
8. **`main.py`** - Main orchestration and entry point
- Command-line argument parsing
- Mode selection (client vs provider)
- Entry points for both modes
### Key Improvements
- **Separation of Concerns**: Each file handles specific functionality
- **Better Maintainability**: Smaller, focused modules are easier to understand and modify
- **Reduced Coupling**: Dependencies between components are more explicit
- **Type Safety**: Proper type hints and Pydantic models throughout
- **Error Handling**: Centralized error handling and logging
### Usage
The refactored code maintains the same CLI interface:
```bash
# Client mode
python voicebot/main.py --mode client --server-url http://localhost:8000/ai-voicebot
# Provider mode
python voicebot/main.py --mode provider --host 0.0.0.0 --port 8788
```
### Import Structure
```python
from voicebot import VoicebotArgs, VoicebotMode, WebRTCSignalingClient
from voicebot.models import Peer, JoinRequest
from voicebot.session_manager import create_or_get_session, create_or_get_lobby
from voicebot.client_main import main_with_args
```
The original `main_old.py` contains the monolithic implementation for reference.