Skip to content

WeeeHung/riley

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

1 Commit
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Emergency Triage Phone App ๐Ÿš‘

A 24/7 emergency triaging phone call application that connects injured or endangered individuals with AI agents for situation assessment and guidance. The system persists caller memory across sessions and provides a real-time dashboard for monitoring active calls.

๐Ÿ—๏ธ Architecture Overview

Tech Stack

  • Frontend: React + TypeScript + Mantine UI
  • Backend: Node.js + Express + TypeScript
  • Database: PostgreSQL with emergency call schemas
  • Voice API: VAPI integration for phone calls
  • AI Agents: Letta (formerly MemGPT) for persistent memory
  • Real-time: WebSocket connections for dashboard updates
  • Hosting: Vercel (frontend) + Railway/Render (backend)

System Components

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Emergency     โ”‚    โ”‚   VAPI Voice     โ”‚    โ”‚   Dashboard     โ”‚
โ”‚   Caller        โ”‚โ—„โ”€โ”€โ–บโ”‚   Assistant      โ”‚โ—„โ”€โ”€โ–บโ”‚   Operators     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                              โ”‚                          โ”‚
                              โ–ผ                          โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Letta AI      โ”‚โ—„โ”€โ”€โ–บโ”‚   Backend API    โ”‚โ—„โ”€โ”€โ–บโ”‚   Real-time     โ”‚
โ”‚   Memory Agent  โ”‚    โ”‚   (Node.js)      โ”‚    โ”‚   WebSocket     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                              โ”‚
                              โ–ผ
                    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                    โ”‚   PostgreSQL     โ”‚
                    โ”‚   Database       โ”‚
                    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿš€ Quick Start

Prerequisites

  • Node.js 18+ and npm
  • PostgreSQL 14+
  • VAPI account and API key
  • Letta account and API key

1. Clone and Setup

# Clone the repository
git clone <repository-url>
cd emergency-triage

# Install backend dependencies
cd backend
npm install

# Install frontend dependencies
cd ../frontend
npm install

2. Environment Configuration

Create .env files for both backend and frontend:

Backend .env:

DATABASE_URL=postgresql://username:password@localhost:5432/emergency_triage
VAPI_API_KEY=your_vapi_api_key
VAPI_PHONE_NUMBER=your_emergency_number
LETTA_API_KEY=your_letta_api_key
JWT_SECRET=your_jwt_secret
PORT=3001
FRONTEND_URL=http://localhost:3000

Frontend .env:

REACT_APP_API_BASE_URL=http://localhost:3001
REACT_APP_WS_URL=http://localhost:3001

3. Database Setup

# Create PostgreSQL database
createdb emergency_triage

# Run setup and test script
npx ts-node setup-and-test.ts

4. Start the Application

# Terminal 1: Start backend
cd backend
npm run dev

# Terminal 2: Start frontend
cd frontend
npm start

5. Access the Dashboard

Open http://localhost:3000 to access the real-time emergency dashboard.

๐Ÿ“‹ Key Features

Emergency Call Handling

  • Instant Response: < 3 second call answering
  • AI Triage: Automated situation assessment
  • Multi-language: English, Spanish, French support, etc
  • Severity Classification: CRITICAL, HIGH, MEDIUM, LOW levels
  • Real-time Transcription: Live speech-to-text processing

Persistent Memory System

  • Caller Profiles: Automatic identification via phone number
  • Medical History: Persistent health condition tracking
  • Emergency History: Previous call outcomes and patterns
  • Location Data: GPS coordinates and address storage
  • Risk Assessment: Dynamic risk profile calculation

Real-time Dashboard

  • Live Call Monitoring: Active call status and transcriptions
  • Emergency Alerts: Critical situation notifications
  • Caller Context: Historical data and risk profiles
  • Escalation Controls: Human operator handoff
  • Analytics: Call volume, response times, outcomes

Emergency Escalation

  • Auto-escalation: Based on severity and caller history
  • Human Operators: Specialized emergency response staff
  • Emergency Services: Direct 911/emergency services dispatch
  • Supervisor Alerts: Complex situation management

๐Ÿ”ง API Documentation

Core Endpoints

# Calls Management
POST   /api/calls/initiate        # Start emergency call
GET    /api/calls/active          # Get active calls
GET    /api/calls/history         # Get call history
POST   /api/calls/escalate        # Escalate to human operator
POST   /api/calls/webhook/vapi    # VAPI webhook handler

# User & Memory Management
GET    /api/users/:id             # Get user profile
POST   /api/users                 # Create user profile
GET    /api/users/:id/memory      # Get caller memory
PUT    /api/users/:id/memory      # Update caller memory

# Dashboard & Analytics
GET    /api/dashboard/stats       # Dashboard statistics
GET    /api/dashboard/active-calls # Active calls for dashboard
GET    /api/dashboard/recent-calls # Recent call history

WebSocket Events

// Client-side event listeners
socket.on("newCall", (call) => {
  /* Handle new emergency call */
});
socket.on("callUpdate", (event) => {
  /* Handle call status update */
});
socket.on("callEscalated", (data) => {
  /* Handle escalation */
});
socket.on("emergencyAlert", (alert) => {
  /* Handle critical alert */
});

๐Ÿ—„๏ธ Database Schema

Core Tables

-- User profiles and caller information
users (id, phone_number, risk_profile, preferred_language, created_at)

-- Emergency call records
calls (id, user_id, emergency_type, severity_level, status, transcription, outcome, created_at)

-- Persistent caller memory
memory_records (id, user_id, memory_type, content, created_at, updated_at)

-- Escalation tracking
escalation_log (id, call_id, escalation_type, reason, created_at)

-- Emergency services dispatch
emergency_dispatch_log (id, call_id, service_type, dispatch_time, status)

๐Ÿค– AI Agent Configuration

Letta Memory Agent

The system uses Letta AI agents for persistent caller memory:

// Caller memory structure
interface CallerMemory {
  userId: string;
  phoneNumber: string;
  medicalHistory: string[];
  previousEmergencies: EmergencyRecord[];
  location: LocationData;
  emergencyContacts: Contact[];
  riskProfile: "LOW" | "MEDIUM" | "HIGH" | "CRITICAL";
  lastCallSummary: string;
}

Emergency Assessment Protocol

  1. Immediate Assessment: Severity classification within 30 seconds
  2. Contextual Analysis: Historical pattern recognition
  3. Escalation Logic: Automated decision tree for human handoff
  4. Memory Updates: Real-time learning from each interaction

๐Ÿ“ž VAPI Integration

Voice Assistant Configuration

// Emergency triage assistant settings
{
  "name": "Emergency Triage Assistant",
  "model": "gpt-4",
  "voice": "professional-calm",
  "systemMessage": "Emergency triage AI with medical protocols...",
  "functions": ["escalate_to_human", "dispatch_emergency_services"],
  "recordingEnabled": true,
  "maxDurationSeconds": 1800
}

Call Flow Process

  1. Incoming Call: VAPI answers with emergency greeting
  2. Situation Assessment: Structured triage questions
  3. Memory Retrieval: Access caller's historical context
  4. AI Analysis: Severity determination and guidance
  5. Escalation Decision: Human/emergency services if needed
  6. Memory Update: Store call outcomes and learnings

๐Ÿšจ Emergency Response Protocols

Severity Levels

  • CRITICAL: Life-threatening (heart attack, stroke, severe bleeding)
  • HIGH: Serious but stable (broken bones, difficulty breathing)
  • MEDIUM: Concerning but manageable (moderate injury, infection)
  • LOW: Minor issues (small cuts, mild symptoms)

Auto-Escalation Triggers

  • Critical severity assessment
  • High-risk caller with concerning symptoms
  • Extended call duration (>10 minutes)
  • Caller becomes unresponsive
  • Specific keywords ("can't breathe", "unconscious")

๐Ÿ“Š Monitoring & Analytics

Dashboard Metrics

  • Active Calls: Real-time emergency call count
  • Response Times: Average time to answer and assess
  • Escalation Rate: Percentage requiring human intervention
  • Outcome Tracking: Call resolution and follow-up
  • Risk Distribution: Caller risk profile analytics

System Alerts

  • High call volume detection
  • Extended response times
  • Service availability issues
  • Critical emergency patterns

๐Ÿ” Security & Compliance

Data Protection

  • HIPAA-compliant medical information handling
  • Encrypted database storage
  • Secure API authentication
  • Audit logging for all actions

Emergency Access

  • Override capabilities for supervisors
  • Emergency service direct dispatch
  • Call recording and transcription storage
  • Regulatory compliance reporting

๐Ÿš€ Deployment

Production Setup

# Frontend (Vercel)
vercel --prod

# Backend (Railway)
railway login
railway deploy

# Database (Railway PostgreSQL)
railway add postgresql

Environment Variables

Ensure all production environment variables are configured:

  • Database connection strings
  • API keys for VAPI and Letta
  • JWT secrets and security tokens
  • Emergency service integration credentials

๐Ÿงช Testing

Run the comprehensive test suite:

# Setup database and run all tests
npx ts-node setup-and-test.ts

# Individual test components
npm run test:emergency-flow
npm run test:dashboard
npm run test:escalation

๐Ÿ“ˆ Performance Optimization

Response Time Targets

  • Call Answer: < 3 seconds
  • Initial Assessment: < 30 seconds
  • Escalation: < 60 seconds for critical cases
  • Memory Retrieval: < 1 second

Scalability Features

  • Database connection pooling
  • WebSocket connection management
  • Caching for frequent queries
  • Load balancing for high call volumes

๐Ÿ”ฎ Future Enhancements

  • Video call capability for visual assessment
  • Integration with local emergency services
  • Advanced AI diagnosis and treatment recommendations
  • Mobile app for emergency contacts
  • Geographic routing to local resources
  • Multi-language support expansion
  • Wearable device integration
  • Predictive emergency analysis

๐Ÿ“ž Support & Contact

For technical support or questions about the Emergency Triage System:

  • Documentation: See inline code comments and API docs
  • Issues: Report bugs via the issue tracker
  • Emergency Protocol Questions: Consult medical professionals
  • System Status: Monitor dashboard health indicators

โš ๏ธ IMPORTANT: This system is designed to assist with emergency triage but should never replace professional medical advice or emergency services. Always call 911 or local emergency services for life-threatening situations.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors