Skip to main content

Architecture

Components​

┌───────────────────────────── clients ─────────────────────────────┐
│ Dispatcher console ER board Crew web /caller /debug/voice │ (React, frontend/src/live)
│ iOS Vaazhvu (citizen) iOS VaazhvuCrew │ (SwiftUI, ios/Vaazhvu)
└───────┬───────────────HTTP / SSE / WebSocket──────────────┬───────┘
│ │
┌───────────────────────────▼───────────── FastAPI (backend/app) ───────────────▼──────────────────────────┐
│ api_ops.py (/api/ops) voice/agent.py (Pipecat: VAD → Whisper → call-taker → TTS) │
│ │ │ │
│ ▼ ▼ │
│ ops/controller.py OpsController ◄──────────► ops/calltaker.py (LLM agent, guarded, streamed) │
│ ├─ triage/pipeline.py ── nlp/langid · nlp/transliterate · nlp/translate · triage/extractor · llm/client │
│ ├─ geo/geocode.py ─── geo/gis.py (OSM gazetteer, hospitals, stations, zones) │
│ ├─ safety/policy.py (R1–R9) │
│ ├─ dispatch/risk.py (J = E[H] + ρ·Std[H] + F) ── routing/provider.py · routing/traffic.py │
│ ├─ ops/world.py · ops/fleet.py (MEXCLP) · ops/sim.py (clock, movement, arrivals) │
│ ├─ vitals/physiology.py (NeuroKit2) · vitals/news2.py │
│ ├─ fhir/resources.py · cad/resgrid_adapter.py │
│ └─ ops/bus.py LiveBus ──► ops/persistence.py (SQLite) ──► SSE /api/ops/stream ──► events/nats_bus.py │
│ mcp/server.py (/api/mcp, stdio bridge) ── mcp/tools.py: read views + labelled simulation, no decisions │
│ copilot.py (/api/ops/copilot): LLM tool calls ──► in-process MCP session ──► the same tools │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────┘
│ │ │ │ │ │ │
DeepSeek API Ollama IndicXlit OSRM :5001 VROOM :3000 HAPI FHIR NATS :4222
(cloud LLM) gemma3:4b :8095 (roads) (baseline) :8090 (mirror)
(fallback)
Local models in-process: faster-whisper (MLX Whisper on a macOS host), IndicTrans2, fastText lid.176,
Silero VAD, NeuroKit2 · TTS: espeak-ng in Docker (macOS `say` on a host) · Go dispatch scorer :8081
Deployment: every box above runs in Docker Compose except Ollama (native app, host.docker.internal)

Incident lifecycle​

┌──────────── caller turns (voice / text) re-triage at every step ─────────────┐
▼ │
received ─► triaging ─┬─► awaiting_location ──(location confirmed)──┐ │
│ ▼ │
├─► pending_approval ──(approve / override)──► enroute ─► on_scene ─► transporting ─► at_hospital ─► resolved
│ ▲ ▲
└─(auto: confident, or provisional critical)──┘ └ re-route (closure), backup unit,
change hospital (deterioration / diversion)
any open state ──(caller cancels)──► cancelled
  • pending_approval: the safety policy requires a human (low confidence, contradiction, etc.). A likely-critical call is still dispatched provisionally.
  • awaiting_location: the location could not be resolved. The AI call-taker asks for it, and the dispatcher can Pick on map.
  • cancelled: the caller withdrew the emergency. Units stand down and return to base.

Unit statuses: idle → assigned → enroute → on_scene → transporting → at_hospital → returning → idle, plus out_of_service.

Event types​

Every state change is one event: {seq, type, ts, source, incident_id, payload, idempotency_key?}. seq is global and monotonic. Events are written to SQLite before they are fanned out to SSE subscribers and mirrored to NATS (ems.<type>).

AreaTypes
Intakecall.received, transcription.ready, caller.update, message.received, message.sent
Triage / policytriage.completed, triage.updated, triage.escalated, policy.decision, location.resolved
Incidentincident.created, incident.status, incident.resolved, incident.cancelled, incident.linked, incident.unlinked (duplicate-call merge / split)
Dispatchdispatch.requested, dispatch.decision, dispatch.pending_approval, dispatch.approved, dispatch.overridden, dispatch.reassigned, ambulance.assigned, backup.requested, cad.dispatch_mirrored
Movementroute.started, unit.rerouted, unit.on_scene, unit.transporting, unit.at_hospital, unit.available, unit.status, unit.out_of_service, fleet.positions
Patient / hospitalvitals.updated, vitals.voided, patient.deteriorated, hospital.alerted, hospital.status, hospital.recommended, hospital.released
ER commander.acknowledged, er.prep, er.accepted, er.declined, er.ift_requested (ops/er_handoff.py), outreach.no_answer, outreach.calling, outreach.unreachable (ops/outreach.py)
Clinical recordstrip.updated, consult.opened, consult.message, consult.advice, consult.closed, incident.referred, referral.requested, referral.accepted, referral.declined, referral.queued, referral.dispatched, referral.completed, referral.cancelled, referral.redirected
Networkcoverage.alert, ambulance.redeployed, traffic.disruption, traffic.cleared
Opsdemo.started, sim.clock, mcp.action (a simulation run through an MCP tool: tool, actor, args; AI access and the Copilot), ops.error, ops.degraded, ops.recovered (a component such as the triage LLM went down / came back; the terminal shows a banner until it recovers, state.degraded)

Persistence and state​

WhatWhere
Event log, incidents, audit trailPostgres in the compose stack (VAAZHVU_DB; SQLite for tests and offline runs). This is the system of record. One live api holds it at a time (an advisory lock). Database
Platform state (comments, attachments, shares, webhooks, deliveries, API keys, drafts, telemetry)The same database (platform/store.py).
Live world (unit positions, tracks, traffic)Memory (ops/world.py), rebuilt from the fleet plan on start.
NATS JetStreamA durable mirror for other consumers, not the source of truth.
FHIRPatient / Encounter / Observations in HAPI FHIR (separate store).
Generated databackend/app/data/*.json (GIS, hospital profiles, fleet plan), regenerated by scripts. Not in the DB.

Deleting var/ resets live state; the next start begins clean. Schema, row counts, retention and reset are in Database.

Concurrency model​

  • FastAPI event loop: API handlers, SSE, and the Pipecat voice pipelines (one per call).
  • OpsController.pool (thread pool): triage, dispatch, FHIR posts and announcements, so intake returns immediately.
  • Simulator thread: advances the clock, moves units and fires arrivals. The world lock (World.lock) guards shared state.
  • Voice line: STT and TTS run in threads (asyncio.to_thread). The call-taker LLM stream runs in a thread and feeds sentences to the pipeline through an asyncio queue.

Live vs legacy​

Live (ops/, /api/ops, frontend/src/live, ios/Vaazhvu)Legacy (sim/runner.py, /api/*, /legacy*)
IncidentsContinuous, many at once, from real intakeOne scripted scenario per run
Dispatchdispatch/risk.py risk objectivedispatch/engine.py weighted utility
RoutingOSRM road geometry, traffic, moving unitsStraight lines / single ETA
StateSQLite + live worldIn-memory STORE
MountedAlwaysOnly with VAAZHVU_LEGACY=1; tests in tests/legacy/

The legacy weighted utility, mirrored by services/dispatch-scorer/main.go:

J(a) = w_eta·exp(-eta/τ)·(sev+βσ)/10 + w_cap·capability + w_hosp·readiness + w_conf·confidence − w_cov·coverage_loss − w_fut·future_risk

The live risk objective is described in Safety and dispatch, and mirrored by services/dispatch-scorer/risk.go. If you change the terms or weights on one side, change the other too.