Skip to Content
Luna AIEmergency Handling

Emergency Handling

Luna includes multi-stage emergency detection to identify crisis situations and provide immediate safety guidance.

Detection Flow

User Message Received ┌───────────────────────┐ │ Stage 1: Keyword │ Check for emergency keywords │ Detection │ (chest pain, overdose, suicide, etc.) └────────┬──────────────┘ │ (if detected) ┌───────────────────────┐ │ Stage 2: Severity │ Classify as 'immediate' or 'concerning' │ Classification │ └────────┬──────────────┘ ┌────┴────┐ │ │ ▼ ▼ Immediate Concerning │ │ ▼ ▼ ┌──────────┐ ┌──────────────────┐ │ Emergency│ │ Patient Context │ │ Response │ │ Check │ │ + 911 │ │ (emergency flags)│ └──────────┘ └──────────────────┘

Stage 1: Emergency Detection

The detectEmergency() function scans the user’s message for crisis keywords:

Immediate Emergencies

Keywords that trigger an immediate emergency response:

  • “chest pain”, “heart attack”
  • “can’t breathe”, “difficulty breathing”
  • “overdose”, “took too much”
  • “suicidal”, “want to die”, “end my life”
  • “allergic reaction”, “anaphylaxis”
  • “seizure”
  • “severe bleeding”

Concerning Situations

Keywords that warrant context checking but may not be immediate emergencies:

  • “side effects getting worse”
  • “extreme pain”
  • “dizzy”, “fainting”
  • “irregular heartbeat”
  • “severe headache”

Stage 2: Emergency Response

For Immediate Emergencies

Luna immediately returns a safety-first response:

🚨 This sounds like a medical emergency. **Call 911 (or your local emergency number) immediately.** If you are experiencing [specific emergency type]: 1. [First aid instructions specific to the emergency] 2. Do not attempt to [relevant safety guidance] 3. Stay on the line with emergency services I am an AI health advisor and cannot provide emergency medical care. Your safety is the priority — please seek immediate professional help.

The response is generated without waiting for AI model inference to minimize latency.

For Concerning Situations

Luna checks the patient context for emergency flags:

const context = await getPatientContext(userId); if (requiresEmergencyEscalation(context)) { // Return error and log escalation }

If the patient has emergency flags (e.g., known cardiac condition + chest pain mention), the situation is escalated.

Stage 3: Dismissal Detection

If a user dismisses an emergency concern (“I’m fine, it was just a joke”), Luna uses detectEmergencyDismissal() to determine if the dismissal is genuine or if the user may still be at risk.

Escalation Logging

All emergency detections are logged:

logEscalation({ userId: user.id, type: 'emergency_detected', message: userMessage, severity: 'critical', emergencyType: detectedType, timestamp: new Date().toISOString(), });

Escalations trigger:

  1. Entry in compliance logs
  2. Staff notification (if configured)
  3. Conversation flagged for review

Configuration

Emergency detection patterns are defined in the Luna chat route (apps/luna/src/app/api/chat/route.ts). The detection runs before the AI model is invoked to ensure the fastest possible response for genuine emergencies.