How jmesplana/openmrs_ai_insights works
Project type and purpose
• This is a full-stack, single-page React application (built with Vite) that provides an AI-powered conversational interface for querying patient data from an OpenMRS server. It includes both a frontend and a small backend (either an Express.js proxy server locally or serverless functions on Vercel) to handle CORS and streaming.
Main components
a. Frontend (Browser)
• Static host / CDN (Vercel or local dev server via Vite)
• React SPA – index.html + bundle served by Vite
• React components (Layout, Sidebar, Patient panels, AiChatPanel, SettingsModal)
• Context provider (OpenMRSContext) for auth & configuration
• Services module (aiService, openMrsService, mockData)
• React Query / Axios for data fetching
b. Backend Proxy
• Local Express proxy (server.cjs) for development on port 3001
• Vercel serverless functions (api/proxy.js, api/stream.js) for production
c. External systems
• OpenMRS REST API (patient data source)
• AI providers:
– OpenAI API (cloud)
– Ollama local LLM server
• (Mock data as a fallback)
Relationships and data flows
1. Browser loads React SPA via Vite server or CDN.
2. User authenticates / configures via SettingsModal → stored in React Context.
3. For patient data:
• Components call openMrsService → HTTP to /api/proxy/* → proxy forwards to OpenMRS REST API → returns JSON → React Query caches & feeds components.
• If mock mode enabled, openMrsService returns data from mockData.js directly, skipping proxy.
4. For AI queries:
• AiChatPanel calls aiService → HTTP to /api/stream (with patient data & prompt) → serverless/local proxy streams events → proxy calls selected AI provider (OpenAI or Ollama) → streams tokens back → aiService pipes tokens to UI.
5. All sensitive credentials and API keys remain on client or in proxy; none stored on external servers.
Architectural patterns and design principles
• SPA on the client, clearly separated from backend proxy
• React Context for global state (OpenMRS credentials & settings)
• Service layer (aiService & openMrsService) isolates API logic
• Proxy as BFF (Backend-for-Frontend) to handle CORS, authentication, streaming
• Mock provider for fast development & testing