# LiveKit VoiceBot Deployment Guide

## Current Status ✅

### Services Running:
- **Frontend**: Next.js production build on port 3000
- **Backend**: Python agent with uv (LiveKit agents 1.4.6 + Cartesia support)

### Domain: 10.40.180.74

---

## Apache Configuration Setup

### Step 1: Copy Apache Configuration
```bash
sudo cp /tmp/livekit-voicebot.conf /etc/apache2/sites-available/
```

### Step 2: Enable the Site
```bash
sudo a2ensite livekit-voicebot.conf
```

### Step 3: Test Apache Configuration
```bash
sudo apache2ctl configtest
```

### Step 4: Reload Apache
```bash
sudo systemctl reload apache2
```

---

## Access URLs

After Apache configuration:
- **Public URL**: http://10.40.180.74
- **Frontend Direct**: http://localhost:3000
- **Backend Agent**: Running on dynamic port (connects to LiveKit Cloud)

---

## Service Management

### Check Service Status:
```bash
# Frontend
ps aux | grep "npm run start"

# Backend
ps aux | grep "uv run python"
```

### Restart Services:

**Frontend:**
```bash
cd /var/www/html/livekit_frontend/FrontEnd/agent-starter-react/agent-starter-react
npm run start
```

**Backend:**
```bash
cd /var/www/html/livekit_frontend/BackEnd/agent-starter-python
/home/aiteam/.local/bin/uv run python src/agent.py dev
```

### View Logs:

**Frontend Logs:**
- Check terminal output or
- `tail -f /var/www/html/livekit_frontend/logs/frontend-out.log`

**Backend Logs:**
- Check terminal output or
- `tail -f /var/www/html/livekit_frontend/BackEnd/agent-starter-python/logs/*.json`

---

## Architecture

```
Internet → Apache (Port 80) → Next.js Frontend (Port 3000)
                                      ↓
                              LiveKit Cloud ← Python Backend Agent
```

### Components:
1. **Apache**: Reverse proxy handling HTTP/WebSocket traffic
2. **Next.js Frontend**: React app with LiveKit client SDK
3. **Python Backend**: Voice AI agent with STT/TTS (ElevenLabs/Cartesia)
4. **LiveKit Cloud**: Real-time communication infrastructure

---

## Environment Variables

### Frontend (.env.local):
- `LIVEKIT_URL`: wss://test-voice-bot-fd6qy6cu.livekit.cloud
- `LIVEKIT_API_KEY`: APIVYTe7eb9UmEv
- `LIVEKIT_API_SECRET`: [configured]

### Backend (.env.local):
- `LIVEKIT_URL`: wss://test-voice-bot-fd6qy6cu.livekit.cloud
- `LIVEKIT_API_KEY`: APIVYTe7eb9UmEv
- `LIVEKIT_API_SECRET`: [configured]
- `ELEVENLABS_API_KEY`: [configured]
- `CARTESIA_API_KEY`: [configured]
- `TWILIO_*`: [configured for telephony]

---

## Troubleshooting

### Frontend not accessible:
```bash
# Check if Next.js is running
curl http://localhost:3000

# Check Apache proxy
sudo tail -f /var/log/apache2/livekit-voicebot-error.log
```

### Backend connection issues:
- Connection retries are normal until a session starts
- Backend connects to LiveKit Cloud when frontend initiates a call
- Check `.env.local` has correct LiveKit credentials

### Apache issues:
```bash
# Check Apache status
sudo systemctl status apache2

# Test configuration
sudo apache2ctl configtest

# View error logs
sudo tail -f /var/log/apache2/error.log
```

---

## Production Checklist

- [x] Git repository cloned (devmilan branch)
- [x] Dependencies installed (npm + uv)
- [x] Frontend production build created
- [x] Backend running with Cartesia support
- [x] Frontend running on port 3000
- [ ] Apache configuration applied (manual step required)
- [ ] Domain accessible from internet
- [ ] SSL certificate (optional, for HTTPS)

---

## Next Steps

1. Run the Apache configuration commands above
2. Test access at http://10.40.180.74
3. Optional: Set up SSL with Let's Encrypt for HTTPS
4. Optional: Set up systemd services for auto-restart on reboot
