How udarachamidu/medtrace-ai works
Purpose and Operating Modes
MedTrace AI is a patient-record review application: it consolidates visits into timelines, medication and lab views, safety findings, and citation-backed record Q&A. It explicitly avoids diagnosis or treatment recommendations. The default reliable path is a synthetic, cached demo; a production mode connects the UI to Supabase and n8n AI workflows.
• frontend/src/App.tsx
• frontend/src/lib/data-adapter.ts
• backend/fixtures/synthetic-demo.json
Frontend Entry and Patient Workspace
The Vite/React client starts at frontend/src/main.tsx, routes between public/auth/patient views, and renders the main patient dashboard. data-adapter is the key boundary: it selects demo data versus configured remote data, insulating UI views from the source mode.
• frontend/src/main.tsx
• frontend/src/lib/router.tsx
• frontend/src/pages/PatientDashboardPage.tsx
Clinical Review Presentation
Dashboard components present the longitudinal timeline, medication/safety results, lab trends, and record-question interaction. The evidence drawer exposes the provenance required for a claim—source document, page, snippet, and confidence—rather than treating AI output as standalone fact.
• frontend/src/components/DashboardViews.tsx
• frontend/src/components/EvidenceDrawer.tsx
• frontend/src/components/SafetyNotice.tsx
Ingestion and AI Orchestration
n8n is the connected pipeline runtime. A patient-record workflow coordinates per-document processing, classification/extraction prompts, normalization, reconciliation, trends, safety checks, evidence verification, and persistence/query stages. Workflow templates are inactive until secrets, identity checks, and storage policies are configured.
• backend/n8n/workflows/WF-01-process-patient-record.json
• backend/n8n/workflows/WF-02-process-one-document.json
• backend/n8n/workflows/WF-09-ask-record-question.json
Clinical Processing Stages
Extracted records pass through medication and laboratory normalization, then longitudinal medication reconciliation and lab trend generation. Deterministic TypeScript safety rules evaluate allergies, duplicate therapies, dosage conflicts, and interactions; this provides a non-LLM safety boundary alongside prompt-driven extraction and verification.
• backend/n8n/workflows/WF-03-normalize-medication.json
• backend/n8n/workflows/WF-07-safety-rules.json
• backend/src/rules/engine.ts
Evidence, Confidence, and Prompt Contracts
Versioned prompts define LLM roles for document classification, clinical extraction, reconciliation, trends, evidence verification, and Q&A. Backend domain utilities model citations, confidence, dates, identity, ownership, and normalization so claims can remain attributable and uncertainty-aware.
• backend/prompts/clinical-extractor/v1.md
• backend/prompts/evidence-verifier/v1.md
• backend/src/citations.ts
Persistence, Security, and External Services
Supabase is the system of record and private document store, with migrations establishing schema/extensions, RLS, storage/RPC, vector-search support, and cross-patient integrity protections. The frontend uses only public Supabase/n8n configuration; n8n holds server-side Supabase credentials, webhook secrets, and OpenAI access. OpenAI is the intended LLM provider.
• backend/supabase/migrations/202607300001_extensions_and_schema.sql
• backend/supabase/migrations/202607300002_rls_storage_and_rpc.sql
• backend/supabase/migrations/202607300003_cross_patient_integrity.sql
Main entry points: DashboardViews.tsx Clinical dashboard views · WF-09-ask-record-question.json Record Q&A workflow · 202607300001_extensions_and_schema.sql Supabase schema · PatientDashboardPage.tsx Patient dashboard
Components 19
React client
- Vite entry
frontend/src/main.tsx - App router · Routes public, authentication, and patient views.
frontend/src/lib/router.tsx - Patient dashboard
frontend/src/pages/PatientDashboardPage.tsx - Data adapter · Selects demo or configured remote data for UI views.
frontend/src/lib/data-adapter.ts - Clinical dashboard views · Shows timeline, medications, safety findings, labs, and record Q&A.
frontend/src/components/DashboardViews.tsx - Evidence drawer · Shows source, page, snippet, and confidence for claims.
frontend/src/components/EvidenceDrawer.tsx - Safety notice
frontend/src/components/SafetyNotice.tsx
n8n processing
- Patient record workflow · Coordinates per-document processing and persistence stages.
backend/n8n/workflows/WF-01-process-patient-record.json - Document processing · Classifies and extracts each patient document.
backend/n8n/workflows/WF-02-process-one-document.json - Medication normalization · Normalizes extracted medication records.
backend/n8n/workflows/WF-03-normalize-medication.json - Safety rules workflow · Runs deterministic medication safety evaluation.
backend/n8n/workflows/WF-07-safety-rules.json - Record Q&A workflow · Answers record questions with evidence verification.
backend/n8n/workflows/WF-09-ask-record-question.json
Backend contracts
- Deterministic rule engine · Checks allergies, duplicate therapies, dose conflicts, and interactions.
backend/src/rules/engine.ts - Versioned prompt contracts · Defines extraction, verification, reconciliation, trends, and Q&A roles.
backend/prompts/clinical-extractor/v1.md - Citation and confidence model · Keeps claims attributable and uncertainty-aware.
backend/src/citations.ts
Data and AI services
- Supabase schema · Establishes record schema, extensions, and vector support.
backend/supabase/migrations/202607300001_extensions_and_schema.sql - Supabase security policies · Applies RLS, storage/RPC controls, and cross-patient protections.
backend/supabase/migrations/202607300002_rls_storage_and_rpc.sql - OpenAI · Used by n8n with server-side credentials.
Other components
- Synthetic demo cache · Default reliable patient-record dataset.
backend/fixtures/synthetic-demo.json
Connections 20
- Vite entry → App router: starts
- App router → Patient dashboard: routes to
- Patient dashboard → Data adapter: loads records through
- Data adapter → Synthetic demo cache: demo mode reads
- Data adapter → Supabase schema: production mode reads
- Patient dashboard → Clinical dashboard views: renders
- Clinical dashboard views → Evidence drawer: opens claim evidence
- Clinical dashboard views → Safety notice: presents findings
- Clinical dashboard views → Record Q&A workflow: submits processing requests
- Patient record workflow → Document processing: processes documents
- Document processing → Medication normalization: sends extracted records
- Medication normalization → Safety rules workflow: provides normalized medications
- Safety rules workflow → Deterministic rule engine: executes
- Patient record workflow → Supabase schema: persists and queries
- Record Q&A workflow → Supabase schema: retrieves patient evidence
- Record Q&A workflow → Citation and confidence model: formats cited claims
- Document processing → Versioned prompt contracts: uses extraction prompts
- Record Q&A workflow → Versioned prompt contracts: uses Q&A and verification prompts
- Versioned prompt contracts → OpenAI: guides calls to
- Supabase security policies → Supabase schema: secures data access