# garg-tejas/blood-report-parser architecture

> Architecture diagram of the GitHub repository garg-tejas/blood-report-parser, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/garg-tejas/blood-report-parser
- Repository: https://github.com/garg-tejas/blood-report-parser

## Overview

1. **Overall shape**
   - BloodParser is a **Next.js App Router** application with a thin server-side API layer and a mostly client-driven dashboard UI.
   - The main architecture is: **file upload → AI extraction → normalized biomarker results → interactive insights/chat/export**.

2. **Top-level application boundary**
   - `app/layout.tsx`, `app/page.tsx`, and `app/globals.css` define the single-page dashboard shell.
   - `app/page.tsx` is the likely orchestration point for the upload workflow, results display, insights panel, and chat drawer.

3. **User input subsystem**
   - `components/upload-zone.tsx` is the entry point for **PDF/image uploads**.
   - The repo supports report images and PDFs up to 10MB, so the UI boundary is designed around asynchronous file ingestion rather than form-style input.

4. **Analysis backend**
   - `app/api/analyze/route.ts` is the core processing endpoint.
   - Per the README, it implements a **dual pipeline**:
     - **OCR-first path:** GLM-OCR document parsing, then Gemini structured extraction.
     - **Fallback path:** direct Gemini Vision multimodal extraction for faster/free-tier usage.
   - This route is the primary integration boundary to external AI services.

5. **OCR access control**
   - `app/api/verify-ocr/route.ts` exists specifically to gate the OCR path.
   - The README indicates security controls here: **timing-safe passphrase comparison**, **per-IP cooldown tracking**, and a **temporary lockout** after failed attempts.
   - Architecturally, this separates “can use secure OCR mode” from the actual extraction pipeline.

6. **Report data model and derived scoring**
   - `components/results-panel.tsx` and `components/results-display.tsx` render the extracted biomarkers and summary state.
   - The dashboard computes a **health score** from categorized biomarkers, then presents counts for normal/borderline/abnormal results.
   - That means the UI is not a passive renderer; it also performs a meaningful client-side derivation from normalized analysis output.

7. **Clinical interpretation layer**
   - `components/insights-panel.tsx` and `components/mobile-insights-sheet.tsx` provide contextual explanations for biomarkers.
   - The design suggests a two-pane information model: the left side shows biomarker cards, while the right side shows explanations that update on hover/click.
   - This is the main “understanding” layer between raw extraction and user-facing interpretation.

8. **AI assistant subsystem**
   - `components/chat-drawer.tsx` and `components/chat-interface.tsx` back the contextual chat experience.
   - `app/api/chat/route.ts` is the server endpoint that answers questions using **full report context plus selected biomarker context**.
   - This is a separate conversation boundary from report extraction, which keeps Q&A isolated from the ingestion pipeline.

9. **Export and review utilities**
   - `components/export-modal.tsx` and `lib/export-utils.ts` indicate a reporting/export boundary for packaging results.
   - Even without user accounts or persistence, the app is designed to let users review and likely export analysis artifacts from the dashboard state.

10. **Shared UI and responsiveness**
   - `components/ui/*` is a large Radix-based design system used across dialogs, drawers, sheets, tables, tabs, toasts, and other primitives.
   - `components/theme-provider.tsx`, `hooks/use-mobile.ts`, and `hooks/use-toast.ts` support theme, responsive behavior, and feedback.
   - The presence of drawer/sheet/mobile-specific components shows the UI is intentionally split between **desktop split-pane** and **mobile stacked** layouts.

11. **External dependencies that shape architecture**
   - The important runtime stack is **Node.js + Next.js 16 + React 19 + Tailwind CSS 4**.
   - AI integration depends on **Google Generative AI** and **GLM-OCR (Z.ai)**.
   - `react-markdown`/`remark-gfm` implies chat or insight content can include rendered markdown, which affects how responses are displayed.

12. **Configuration and deployment boundaries**
   - `.env.example` shows the app is environment-driven, with keys for at least **Gemini**, **ZAI/OCR**, and the **OCR passphrase**.
   - The repo is positioned for **Vercel deployment**, which fits the serverless API route model used by `app/api/*`.

13. **What an architecture diagram should show**
   - One clear path from **upload UI → analyze API → AI providers → normalized report state → results/insights/chat UI**.
   - A second, separate path for **OCR verification → gated OCR analyze flow**.
   - A shared UI layer of reusable components and a shared utility layer (`lib/*`) underneath the dashboard experience.

Main entry points: [`app/page.tsx`](https://github.com/garg-tejas/blood-report-parser/blob/main/app/page.tsx) (Dashboard), [`components/results-panel.tsx`](https://github.com/garg-tejas/blood-report-parser/blob/main/components/results-panel.tsx) (Results panel), [`app/api/analyze/route.ts`](https://github.com/garg-tejas/blood-report-parser/blob/main/app/api/analyze/route.ts) (Analyze API), [`app/layout.tsx`](https://github.com/garg-tejas/blood-report-parser/blob/main/app/layout.tsx) (Layout)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["App shell"]
  node_layout["Layout<br/>[layout.tsx]"]
  node_page["Dashboard<br/>next page<br/>[page.tsx]"]
  node_globals["Global styles<br/>css<br/>[globals.css]"]
  node_analyze_api["Analyze API<br/>server route<br/>[route.ts]"]
  node_chat_api["Chat API<br/>server route<br/>[route.ts]"]
  node_verify_api["OCR verify<br/>server route<br/>[route.ts]"]
end

subgraph group_components["Dashboard UI"]
  node_upload_zone["Upload zone<br/>file intake<br/>[upload-zone.tsx]"]
  node_results_panel["Results panel<br/>analysis UI<br/>[results-panel.tsx]"]
  node_results_display["Results display<br/>analysis UI"]
  node_insights_panel["Insights panel<br/>interpretation UI<br/>[insights-panel.tsx]"]
  node_mobile_sheet["Mobile sheet<br/>responsive UI"]
  node_chat_drawer["Chat drawer<br/>assistant UI<br/>[chat-drawer.tsx]"]
  node_chat_interface["Chat interface<br/>assistant UI<br/>[chat-interface.tsx]"]
  node_export_modal["Export modal<br/>export UI<br/>[export-modal.tsx]"]
  node_ocr_dialog["OCR unlock<br/>security UI"]
end

subgraph group_shared["Shared primitives"]
  node_theme_provider["Theme provider<br/>ui provider<br/>[theme-provider.tsx]"]
  node_ui_kit["UI kit<br/>design system"]
  node_use_mobile["use-mobile<br/>hook<br/>[use-mobile.ts]"]
  node_use_toast["use-toast<br/>hook<br/>[use-toast.ts]"]
  node_utils["Utils<br/>[utils.ts]"]
end

subgraph group_lib["Core logic"]
  node_export_utils["Export utils<br/>[export-utils.ts]"]
  node_glm_ocr["GLM-OCR<br/>ocr client<br/>[glm-ocr.ts]"]
end

node_layout -->|"renders"| node_page
node_layout -->|"wraps"| node_theme_provider
node_layout -->|"loads"| node_globals
node_page -->|"starts"| node_upload_zone
node_page -->|"shows"| node_results_panel
node_page -->|"shows"| node_insights_panel
node_page -->|"opens"| node_chat_drawer
node_page -->|"exports"| node_export_modal
node_page -->|"unlocks"| node_ocr_dialog
node_upload_zone -->|"uploads"| node_analyze_api
node_ocr_dialog -->|"verifies"| node_verify_api
node_verify_api -->|"gates"| node_analyze_api
node_analyze_api -->|"ocr-first"| node_glm_ocr
node_analyze_api -.->|"context feed"| node_chat_api
node_results_panel -->|"drills into"| node_results_display
node_results_panel -->|"selects"| node_insights_panel
node_results_panel -->|"adapts"| node_mobile_sheet
node_chat_drawer -->|"contains"| node_chat_interface
node_chat_interface -->|"queries"| node_chat_api
node_export_modal -->|"formats"| node_export_utils
node_results_panel -.->|"feeds"| node_export_utils
node_ui_kit -.->|"builds"| node_results_panel
node_ui_kit -.->|"builds"| node_chat_drawer
node_ui_kit -.->|"builds"| node_ocr_dialog
node_use_mobile -.->|"drives"| node_mobile_sheet
node_use_toast -.->|"notifies"| node_page
node_utils -.->|"supports"| node_page

click node_layout "https://github.com/garg-tejas/blood-report-parser/blob/main/app/layout.tsx"
click node_page "https://github.com/garg-tejas/blood-report-parser/blob/main/app/page.tsx"
click node_globals "https://github.com/garg-tejas/blood-report-parser/blob/main/app/globals.css"
click node_analyze_api "https://github.com/garg-tejas/blood-report-parser/blob/main/app/api/analyze/route.ts"
click node_chat_api "https://github.com/garg-tejas/blood-report-parser/blob/main/app/api/chat/route.ts"
click node_verify_api "https://github.com/garg-tejas/blood-report-parser/blob/main/app/api/verify-ocr/route.ts"
click node_upload_zone "https://github.com/garg-tejas/blood-report-parser/blob/main/components/upload-zone.tsx"
click node_results_panel "https://github.com/garg-tejas/blood-report-parser/blob/main/components/results-panel.tsx"
click node_results_display "https://github.com/garg-tejas/blood-report-parser/blob/main/components/results-display.tsx"
click node_insights_panel "https://github.com/garg-tejas/blood-report-parser/blob/main/components/insights-panel.tsx"
click node_mobile_sheet "https://github.com/garg-tejas/blood-report-parser/blob/main/components/mobile-insights-sheet.tsx"
click node_chat_drawer "https://github.com/garg-tejas/blood-report-parser/blob/main/components/chat-drawer.tsx"
click node_chat_interface "https://github.com/garg-tejas/blood-report-parser/blob/main/components/chat-interface.tsx"
click node_export_modal "https://github.com/garg-tejas/blood-report-parser/blob/main/components/export-modal.tsx"
click node_ocr_dialog "https://github.com/garg-tejas/blood-report-parser/blob/main/components/ocr-unlock-dialog.tsx"
click node_theme_provider "https://github.com/garg-tejas/blood-report-parser/blob/main/components/theme-provider.tsx"
click node_ui_kit "https://github.com/garg-tejas/blood-report-parser/tree/main/components/ui"
click node_use_mobile "https://github.com/garg-tejas/blood-report-parser/blob/main/hooks/use-mobile.ts"
click node_use_toast "https://github.com/garg-tejas/blood-report-parser/blob/main/hooks/use-toast.ts"
click node_export_utils "https://github.com/garg-tejas/blood-report-parser/blob/main/lib/export-utils.ts"
click node_glm_ocr "https://github.com/garg-tejas/blood-report-parser/blob/main/lib/glm-ocr.ts"
click node_utils "https://github.com/garg-tejas/blood-report-parser/blob/main/lib/utils.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_layout,node_page,node_globals,node_analyze_api,node_chat_api,node_verify_api toneBlue
class node_upload_zone,node_results_panel,node_results_display,node_insights_panel,node_mobile_sheet,node_chat_drawer,node_chat_interface,node_export_modal,node_ocr_dialog toneAmber
class node_theme_provider,node_ui_kit,node_use_mobile,node_use_toast,node_utils toneMint
class node_export_utils,node_glm_ocr toneRose
```

## Components

### App shell

Next.js App Router surface and API routes

- [Layout](https://github.com/garg-tejas/blood-report-parser/blob/main/app/layout.tsx): next layout · Root document shell and providers · `app/layout.tsx`
- [Dashboard](https://github.com/garg-tejas/blood-report-parser/blob/main/app/page.tsx): next page · Main orchestration for upload, results, insights, and chat · `app/page.tsx`
- [Global styles](https://github.com/garg-tejas/blood-report-parser/blob/main/app/globals.css): css · App-wide styling layer · `app/globals.css`
- [Analyze API](https://github.com/garg-tejas/blood-report-parser/blob/main/app/api/analyze/route.ts): server route · Dual pipeline extraction endpoint with OCR-first and vision fallback · `app/api/analyze/route.ts`
- [Chat API](https://github.com/garg-tejas/blood-report-parser/blob/main/app/api/chat/route.ts): server route · Contextual assistant endpoint over report data · `app/api/chat/route.ts`
- [OCR verify](https://github.com/garg-tejas/blood-report-parser/blob/main/app/api/verify-ocr/route.ts): server route · Gates access to secure OCR mode with cooldown and lockout · `app/api/verify-ocr/route.ts`

### Dashboard UI

Primary interactive dashboard components

- [Upload zone](https://github.com/garg-tejas/blood-report-parser/blob/main/components/upload-zone.tsx): file intake · PDF and image upload entry point · `components/upload-zone.tsx`
- [Results panel](https://github.com/garg-tejas/blood-report-parser/blob/main/components/results-panel.tsx): analysis UI · Primary biomarker summary and status view · `components/results-panel.tsx`
- [Results display](https://github.com/garg-tejas/blood-report-parser/blob/main/components/results-display.tsx): analysis UI · Detailed normalized biomarker rendering · `components/results-display.tsx`
- [Insights panel](https://github.com/garg-tejas/blood-report-parser/blob/main/components/insights-panel.tsx): interpretation UI · Clinical explanations for selected biomarkers · `components/insights-panel.tsx`
- [Mobile sheet](https://github.com/garg-tejas/blood-report-parser/blob/main/components/mobile-insights-sheet.tsx): responsive UI · Mobile-friendly insights presentation · `components/mobile-insights-sheet.tsx`
- [Chat drawer](https://github.com/garg-tejas/blood-report-parser/blob/main/components/chat-drawer.tsx): assistant UI · Contextual chat container for report questions · `components/chat-drawer.tsx`
- [Chat interface](https://github.com/garg-tejas/blood-report-parser/blob/main/components/chat-interface.tsx): assistant UI · Conversation view and message composition · `components/chat-interface.tsx`
- [Export modal](https://github.com/garg-tejas/blood-report-parser/blob/main/components/export-modal.tsx): export UI · Dashboard export and review dialog · `components/export-modal.tsx`
- [OCR unlock](https://github.com/garg-tejas/blood-report-parser/blob/main/components/ocr-unlock-dialog.tsx): security UI · Passphrase dialog for unlocking OCR path · `components/ocr-unlock-dialog.tsx`

### Shared primitives

Design system, hooks, and utilities

- [Theme provider](https://github.com/garg-tejas/blood-report-parser/blob/main/components/theme-provider.tsx): ui provider · Theme and color-mode wiring · `components/theme-provider.tsx`
- [UI kit](https://github.com/garg-tejas/blood-report-parser/tree/main/components/ui): design system · Reusable Radix-based primitives across the dashboard · `components/ui`
- [use-mobile](https://github.com/garg-tejas/blood-report-parser/blob/main/hooks/use-mobile.ts): hook · Responsive layout detection · `hooks/use-mobile.ts`
- [use-toast](https://github.com/garg-tejas/blood-report-parser/blob/main/hooks/use-toast.ts): hook · Toast notification state · `hooks/use-toast.ts`
- [Utils](https://github.com/garg-tejas/blood-report-parser/blob/main/lib/utils.ts): utility · Shared helper functions · `lib/utils.ts`

### Core logic

Reusable parsing and export helpers

- [Export utils](https://github.com/garg-tejas/blood-report-parser/blob/main/lib/export-utils.ts): utility · Formatting and packaging support for exports · `lib/export-utils.ts`
- [GLM-OCR](https://github.com/garg-tejas/blood-report-parser/blob/main/lib/glm-ocr.ts): ocr client · OCR integration helper for document parsing · `lib/glm-ocr.ts`

## Connections

- Layout → Dashboard: renders · Root shell hosts the dashboard page
- Layout → Theme provider: wraps · Provides app-wide theme context
- Layout → Global styles: loads · Global styling is applied at the document level
- Dashboard → Upload zone: starts · Upload workflow begins here
- Dashboard → Results panel: shows · Primary analysis summary is displayed in the page shell
- Dashboard → Insights panel: shows · Insight pane is part of the main dashboard
- Dashboard → Chat drawer: opens · Chat experience is launched from the dashboard
- Dashboard → Export modal: exports · Review/export actions are surfaced from the page
- Dashboard → OCR unlock: unlocks · OCR access flow is initiated from the dashboard
- Upload zone → Analyze API: uploads · Files are sent to the analysis endpoint
- OCR unlock → OCR verify: verifies · Passphrase checks are handled by the verify route
- OCR verify → Analyze API: gates · Successful verification enables the OCR-first path
- Analyze API → GLM-OCR: ocr-first · Document parsing begins with OCR when enabled
- Analyze API → Chat API: context feed · Normalized report state becomes chat context
- Results panel → Results display: drills into · Summary state expands into detailed biomarker view
- Results panel → Insights panel: selects · Chosen biomarker drives contextual explanation
- Results panel → Mobile sheet: adapts · Mobile presentation mirrors the insight model
- Chat drawer → Chat interface: contains · Drawer hosts the conversation UI
- Chat interface → Chat API: queries · Questions are answered by the server endpoint
- Export modal → Export utils: formats · Export packaging uses shared helpers
- Results panel → Export utils: feeds · Current report state is prepared for export
- UI kit → Results panel: builds · Dashboard surfaces use shared primitives
- UI kit → Chat drawer: builds · Drawer and dialog behavior rely on the design system
- UI kit → OCR unlock: builds · Security prompt uses shared primitives
- use-mobile → Mobile sheet: drives · Responsive branching selects the mobile sheet
- use-toast → Dashboard: notifies · Feedback/toast state supports dashboard actions
- Utils → Dashboard: supports · General helpers are used by the dashboard layer
