# Tavus Integration Configuration

This directory contains all configuration files and guides for integrating Tavus conversational AI with the BuzzTrail slide show platform.

## Quick Start

Follow these steps to get Tavus working with your slideshow:

### 1. Deploy Webhook Handlers ⚡
```bash
# Apply database migration
# See DEPLOYMENT-GUIDE.md for detailed instructions

# Deploy to Railway
cd apps/slide-system
railway up
```

### 2. Configure Tavus Replica 🤖
```bash
# Follow step-by-step guide
open SETUP-GUIDE.md
```

### 3. Test Integration ✅
```bash
# Run automated tests
./test-webhooks.sh
```

## Files Overview

### Configuration Files

- **`system-prompt.md`** (1,500 words)
  - Complete Tavus replica system prompt
  - Defines conversation flow: Discovery → Generate → Present → Q&A
  - Copy this into Tavus dashboard

- **`function-schemas.json`**
  - JSON schemas for 2 Tavus functions:
    - `generate_custom_deck()` - Creates personalized slides
    - `control_slide()` - Navigates presentation
  - Copy these into Tavus function calling configuration

### Implementation Guides

- **`SETUP-GUIDE.md`** (detailed, ~500 lines)
  - Step-by-step Tavus dashboard configuration
  - Function calling setup
  - Testing scenarios and verification
  - Troubleshooting common issues

- **`DEPLOYMENT-GUIDE.md`** (detailed, ~400 lines)
  - Railway deployment instructions
  - Database migration steps
  - Environment variable configuration
  - End-to-end testing procedures
  - Rollback plan

### Testing Tools

- **`test-webhooks.sh`** (executable script)
  - Automated webhook testing
  - Tests both generate-deck and control-slide endpoints
  - Verifies Railway deployment
  - Usage: `./test-webhooks.sh`

## Architecture Overview

```
┌─────────────────┐
│  Tavus Replica  │  Discovery conversation
│   (AI Avatar)   │
└────────┬────────┘
         │
         │ Function Calls
         ▼
┌─────────────────────────────────────┐
│  Railway Webhook Handlers           │
│  --------------------------------   │
│  POST /api/tavus/generate-deck      │  Creates custom slide deck
│  POST /api/tavus/control-slide      │  Navigates slides
└────────┬────────────────────────────┘
         │
         ▼
┌─────────────────────────────────────┐
│  Existing Motia Steps               │
│  --------------------------------   │
│  POST /api/slidev/generate          │  Builds Slidev markdown
│  POST /api/slidev/control           │  Broadcasts to Supabase
└────────┬────────────────────────────┘
         │
         ▼
┌─────────────────────────────────────┐
│  Supabase Realtime                  │
│  --------------------------------   │
│  conversation_sessions table        │  Stores discovery data
│  slide_control_commands table       │  Real-time slide commands
└─────────────────────────────────────┘
```

## Implementation Status

### ✅ Complete
- [x] Tavus system prompt written
- [x] Function schemas defined
- [x] Webhook handlers implemented (`tavus-generate-deck.step.ts`, `tavus-control-slide.step.ts`)
- [x] Database migration created (`003_tavus_integration.sql`)
- [x] Deployment guide written
- [x] Setup guide written
- [x] Test script created

### ⏭️ Next Steps
1. **Deploy to Railway** (15 min)
   - Apply database migration
   - Deploy webhook handlers
   - Verify endpoints

2. **Configure Tavus** (30 min)
   - Create replica in Tavus dashboard
   - Add system prompt
   - Configure function calling
   - Set webhook URLs

3. **Test End-to-End** (20 min)
   - Run test script
   - Test in Tavus sandbox
   - Verify deck generation
   - Verify slide control

4. **Integrate Demo UI** (1-2 hours)
   - Connect Tavus CVI components
   - Add Daily.co room support
   - Test multi-participant layout

## Key Endpoints

After deployment, these endpoints will be live:

| Endpoint | Method | Purpose |
|----------|--------|---------|
| `/api/tavus/generate-deck` | POST | Tavus → Generate custom deck |
| `/api/tavus/control-slide` | POST | Tavus → Control slide navigation |
| `/api/slidev/generate` | POST | Internal: Build Slidev deck |
| `/api/slidev/control` | POST | Internal: Broadcast slide commands |
| `/demo-sessions` | POST | Create Daily.co room + session |

**Production Base URL**: `https://slide-system-production-5813.up.railway.app`

## Environment Variables Required

Ensure these are set in Railway:

```bash
SUPABASE_URL=https://YOUR_PROJECT.supabase.co
SUPABASE_SERVICE_KEY=your-service-key-here
DAILY_API_KEY=your-daily-api-key
```

## Testing Checklist

Before team demo:

- [ ] Database migration applied
- [ ] Railway deployment successful
- [ ] Test script passes all 5 tests
- [ ] Tavus replica configured
- [ ] Test conversation completes discovery phase
- [ ] Test conversation generates deck
- [ ] Test conversation controls slides
- [ ] Demo UI shows Tavus avatar
- [ ] Demo UI shows slides in sync
- [ ] Multi-participant layout working

## Troubleshooting Quick Reference

| Issue | Solution |
|-------|----------|
| 404 on webhook | Redeploy Railway: `railway up` |
| 500 error | Check Railway logs: `railway logs` |
| Tavus timeout | Increase webhook timeout to 90s |
| No deck generated | Check Supabase credentials in Railway |
| Slides don't advance | Verify conversation_id matches session |

## Resources

- **Tavus Dashboard**: https://platform.tavus.io
- **Tavus Docs**: https://docs.tavus.io
- **Railway Project**: https://railway.app
- **Supabase Dashboard**: https://app.supabase.com
- **Demo UI (local)**: http://localhost:4200

## Support

For issues:
1. Check Railway logs: `railway logs`
2. Review `DEPLOYMENT-GUIDE.md` troubleshooting section
3. Review `SETUP-GUIDE.md` troubleshooting section
4. Test webhooks manually: `./test-webhooks.sh`

## Timeline

**Total Implementation Time**: ~3-4 hours

1. **Deployment** (30 min): Database + Railway
2. **Tavus Setup** (30 min): Dashboard configuration
3. **Testing** (1 hour): Webhook + Tavus + Integration tests
4. **Demo UI Integration** (1-2 hours): Connect components
5. **Rehearsal** (30 min): Full team demo dry run

**Ready for team demo**: Same day or next morning! 🚀
