# RateRight Phone AI with VAPI

This directory contains the configuration and setup for RateRight's phone assistant using VAPI with OpenAI Realtime API.

## Overview

The phone assistant provides natural, conversational phone support for RateRight callers using:
- **VAPI** for phone infrastructure
- **OpenAI Realtime API** for ultra-low latency speech-to-speech conversations
- **Australian male voice** (OpenAI's `alloy` voice)
- **RateRight context** built into the system prompt

## Files

- `.env` - Environment variables (API keys, phone numbers)
- `vapi-config.json` - VAPI assistant configuration
- `setup-vapi.js` - Script to create/update the VAPI assistant
- `test-vapi.js` - Script to test VAPI connection and configuration
- `README.md` - This file

## Setup

### 1. Install dependencies
```bash
cd /home/ccuser/rateright-growth
npm install @vapi-ai/server-sdk dotenv
```

### 2. Configure environment
Edit `.env` file with your API keys:
- `VAPI_API_KEY` - From `/root/.clawdbot/secrets.json`
- `OPENAI_API_KEY` - From `/root/.clawdbot/.env`
- `WEBHOOK_URL` - Your Growth Engine webhook URL
- `PHONE_NUMBER_1`, `PHONE_NUMBER_2` - Your VAPI phone numbers

### 3. Test connection
```bash
cd /home/ccuser/rateright-growth/phone-ai
node test-vapi.js
```

### 4. Setup assistant
```bash
cd /home/ccuser/rateright-growth/phone-ai
node setup-vapi.js
```

## Assistant Configuration

### Model
- **Provider**: `openai`
- **Model**: `gpt-realtime-2025-08-28` (production-ready)
- **Temperature**: 0.7 (natural conversation)
- **Max tokens**: 250 (concise responses)

### Voice
- **Provider**: `openai`
- **Voice**: `alloy` (natural-sounding male voice)
- Alternative: `echo` (also male-sounding)

### System Prompt
The assistant knows:
- RateRight is a construction hiring marketplace in Australia
- $50 flat fee per hire for employers
- Free for workers to sign up and find jobs
- Focus on construction industry (builders, tradies, laborers)

### Personality
- Friendly, conversational Australian tone
- Natural, not robotic
- Helpful and patient
- Asks clarifying questions when needed

## Testing

1. **Test the connection**: `node test-vapi.js`
2. **Setup the assistant**: `node setup-vapi.js`
3. **Make a test call**: Call your VAPI phone number
4. **Check webhooks**: Verify calls hit your webhook endpoint

## Webhook Integration

The assistant is configured to send `end-of-call-report` webhooks to:
```
https://rateright-growth-production.up.railway.app/api/vapi/webhook
```

The webhook handler:
1. Receives full call transcripts
2. Parses tasks using AI
3. Saves to `vapi_tasks` database table
4. Executes tasks asynchronously

## Monitoring

- **VAPI Dashboard**: https://app.vapi.ai
- **Call logs**: Check VAPI dashboard for call history
- **Webhook logs**: Check your server logs
- **Database**: Check `vapi_tasks` table for parsed tasks

## Troubleshooting

### Assistant not responding
1. Check VAPI dashboard for assistant status
2. Verify phone number is assigned to assistant
3. Test with `node test-vapi.js`

### No webhooks received
1. Check `WEBHOOK_URL` in `.env`
2. Verify your server is accessible
3. Check server logs for incoming requests

### Poor voice quality
1. Try different OpenAI voice (`echo` instead of `alloy`)
2. Adjust `temperature` in `vapi-config.json`
3. Check network latency

## Updating Configuration

1. Edit `vapi-config.json`
2. Run `node setup-vapi.js` to update
3. Test with a call

## Notes

- OpenAI Realtime API handles speech-to-speech natively (no separate STT/TTS)
- Only certain OpenAI voices work with Realtime API
- Voice `alloy` is recommended for Australian context
- The assistant is configured for natural conversation, not just task parsing