ai-voicebot/shared/README.md
James Ketrenos 2908fe1ee7 Refactor: Create shared Pydantic models for API communication
- Created shared/models.py with all common Pydantic models
- Updated voicebot/main.py to use shared models and remove dd.get() calls
- Updated server/main.py to use shared models
- Fixed lobby_state message handling with proper validation
- Updated Dockerfiles to include shared/ directory
- Added comprehensive documentation and migration guide

Benefits:
- Type safety across both components
- Consistent data validation
- Eliminated unsafe dictionary access patterns
- Centralized model definitions for easier maintenance
2025-09-01 13:36:24 -07:00

69 lines
2.1 KiB
Markdown

# Shared Models
This directory contains shared Pydantic models used for API communication between the voicebot and server components.
## Structure
- **`models.py`**: Contains all shared Pydantic models organized into logical sections:
- Core Data Models (persistence and API communication)
- HTTP API Request/Response Models
- WebSocket Message Models
- WebRTC Signaling Models (specific to voicebot)
- Data Persistence Models
## Usage
Both the `server/` and `voicebot/` components import these shared models to ensure consistent data validation and type safety across the entire application.
### In Server (`server/main.py`):
```python
from shared.models import (
SessionModel,
LobbyCreateResponse,
AdminActionResponse,
# ... other models
)
```
### In Voicebot (`voicebot/main.py`):
```python
from shared.models import (
WebSocketMessageModel,
JoinStatusModel,
LobbyStateModel,
AddPeerModel,
# ... other models
)
```
## Benefits
1. **Consistency**: Both components use the same data structures
2. **Type Safety**: Pydantic validation prevents runtime errors
3. **Documentation**: Models serve as living documentation of the API
4. **Maintainability**: Changes to data structures only need to be made in one place
5. **Validation**: Automatic validation of incoming/outgoing data
## Model Categories
### Core Data Models
- `LobbyModel`: Core lobby structure
- `SessionModel`: Core session structure
- `ParticipantModel`: Participant information
### HTTP API Models
- `AdminNamesResponse`, `AdminActionResponse`: Admin endpoints
- `LobbiesResponse`, `SessionResponse`: Lobby/session endpoints
- `LobbyCreateRequest`, `LobbyCreateResponse`: Lobby creation
### WebSocket Models
- `WebSocketMessageModel`: Base WebSocket message structure
- `JoinStatusModel`, `UserJoinedModel`: User events
- `LobbyStateModel`: Lobby state updates
- `UpdateModel`: Generic updates
### WebRTC Signaling Models
- `AddPeerModel`, `RemovePeerModel`: Peer management
- `SessionDescriptionModel`: WebRTC session descriptions
- `IceCandidateModel`: ICE candidate exchange