Overview
Haggle is built on a modern, three-tier architecture combining FastAPI, Next.js, and Supabase. The platform uses AI-powered voice agents to negotiate with service providers on behalf of homeowners.The system is designed for high scalability and real-time voice communication using WebSocket connections.
Architecture Diagram
Core Components
Frontend Layer (Next.js)
Frontend Layer (Next.js)
Technology Stack:
- Next.js 16.0.7 (React 19.2.0)
- TypeScript
- Tailwind CSS 4.1.9
- Radix UI components
- Shadcn/ui component library
- Server-side rendering (SSR)
- Client-side state management
- Real-time provider status polling
- Responsive UI with gradient backgrounds
landing-page.tsx- Initial search interfacequestions-flow.tsx- AI-generated clarifying questionscall-console.tsx- Real-time call monitoring dashboard
/workspace/source/frontend/API Layer (FastAPI)
API Layer (FastAPI)
Technology Stack:
Port: 8000Location:
- FastAPI (Python)
- Uvicorn ASGI server
- Pydantic for data validation
- CORS middleware for frontend integration
| Endpoint | Method | Description |
|---|---|---|
/api/start-job | POST | Initiates job with AI task inference |
/api/complete-job | POST | Searches providers and saves to DB |
/api/providers/{job_id} | GET | Retrieves providers for a job |
/api/providers/{job_id}/status | GET | Polls call status and prices |
/api/start-calls/{job_id} | POST | Triggers voice agent calls |
/workspace/source/main.pyVoice Agent Service
Voice Agent Service
Technology Stack:Real-time Features:
- FastAPI with WebSocket support
- Twilio Voice API
- Grok Realtime API (WebSocket)
- Audio processing (audioop, base64)
- Bidirectional audio streaming
- Live transcript capture
- AI-powered negotiation
- Automatic price extraction
/workspace/source/backend/app.pyDatabase Layer (Supabase)
Database Layer (Supabase)
Technology Stack:
- PostgreSQL (via Supabase)
- Row Level Security (RLS)
- Real-time subscriptions (optional)
providersFeatures:- Automatic indexing on job_id, zip_code
- BIGSERIAL primary keys
- NUMERIC for precise price storage
- Timestamp tracking with
created_at
/workspace/source/db/models.pyRequest Flow
Job Creation Flow
User Submits Query
User enters free-text query (e.g., “fix my toilet”) with location and budget details via the Next.js frontend.
Provider Search Flow
Voice Call Flow
Stream Audio Bidirectionally
Two async tasks run in parallel:
receive_from_twilio(): Converts Twilio audio to Grok formatsend_to_twilio(): Converts Grok audio to Twilio format
Data Flow
Session Management
- Jobs are ephemeral session data
- Providers are the only persistent entities
- Reduces database writes
- Simplifies data model
Scalability Considerations
Horizontal Scaling
- FastAPI backend is stateless (except jobs_store)
- Voice agent service can run multiple instances
- Supabase handles connection pooling
Async Processing
- All I/O operations are async (httpx, websockets)
- Parallel background tasks for calling providers
- Non-blocking audio streaming
Database Optimization
- Indexed queries on job_id and zip_code
- BIGSERIAL for high-volume inserts
- Prepared statements via Supabase client
Error Handling
- Fallback providers when APIs fail
- Graceful degradation for missing API keys
- Comprehensive exception handling in voice flow
Environment Configuration
Required environment variables:.env
Deployment Architecture
- Development
- Production
Run all services locally:
Performance Metrics
| Metric | Target | Notes |
|---|---|---|
| API Response Time | < 2s | Task inference + question generation |
| Provider Search | < 5s | OpenAI web search latency |
| Call Initiation | < 1s | Twilio API call |
| Audio Latency | < 300ms | Round-trip audio processing |
| Transcript Accuracy | > 95% | Grok Realtime API transcription |
| Price Extraction | > 90% | LLM-based parsing accuracy |
Technology Choices
Why FastAPI?
Why FastAPI?
- Native async/await support for I/O-heavy operations
- Automatic OpenAPI documentation
- Pydantic validation out of the box
- Excellent performance (comparable to Node.js)
- WebSocket support for real-time features
Why Next.js?
Why Next.js?
- Server-side rendering for SEO
- File-based routing
- TypeScript support
- Vercel deployment optimization
- Rich component ecosystem (Radix UI)
Why Supabase?
Why Supabase?
- PostgreSQL with managed infrastructure
- Row Level Security for multi-tenant apps
- Real-time subscriptions (future feature)
- Python client library
- Free tier for development
Why Grok Realtime API?
Why Grok Realtime API?
- Low-latency voice conversations
- Built-in VAD (Voice Activity Detection)
- Streaming audio transcription
- Customizable voice and instructions
- WebSocket-based for bidirectional audio