3.0 KiB
3.0 KiB
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
-
models.py
- Data models and configurationVoicebotArgs
- Pydantic model for CLI arguments and configurationVoicebotMode
- Enum for client/provider modesPeer
- WebRTC peer representationJoinRequest
- Request model for joining lobbiesMessageData
- Type alias for message payloads
-
webrtc_signaling.py
- WebRTC signaling client functionalityWebRTCSignalingClient
- Main WebRTC signaling client class- Handles peer connection management, ICE candidates, session descriptions
- Registration status tracking and reconnection logic
- Message processing and event handling
-
session_manager.py
- Session and lobby managementcreate_or_get_session()
- Session creation/retrievalcreate_or_get_lobby()
- Lobby creation/retrieval- HTTP API communication utilities
-
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
-
client_main.py
- Main client logicmain_with_args()
- Core client functionalitystart_client_with_reload()
- Development mode with reload- Event handlers for peer and track management
-
client_app.py
- Client FastAPI applicationcreate_client_app()
- Creates FastAPI app for client mode- Health check and status endpoints
- Process isolation and locking
-
utils.py
- Utility functions- URL conversion utilities (
http_base_url
,ws_url
) - SSL context creation
- Network information logging
- URL conversion utilities (
-
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:
# 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
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.