# aryan-a1/hal architecture

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

- Interactive diagram: https://gitdiagram.com/aryan-a1/hal
- Repository: https://github.com/aryan-a1/hal

## Overview

1. **Overall shape**
   HAL is a two-tier application: a React/Vite single-page frontend and a FastAPI backend. The frontend is the user-facing dashboard, while the backend owns authentication, crop records, AI-assisted insights, and persistence.

2. **Frontend boundary**
   The frontend is organized as feature pages (`Dashboard`, `CatchUp`, `CropDisease`, `CropIrrigation`, `Schemes`, `Login`, `Signup`) composed from smaller UI sections. Shared UI primitives live under `components/ui`, and the app shell is wired in `main.tsx`/`App.tsx`.

3. **Frontend state and client-side services**
   Zustand stores (`useAuthStore`, `useChatStore`, `useLanguageStore`, `useNotificationStore`, `useProfileStore`) hold cross-cutting client state such as session, language, and chat UI. API access is isolated in `frontend/src/services`, which acts as the only intended bridge from the UI to backend endpoints.

4. **Internationalization and UX**
   The frontend includes bilingual English/Hindi support via locale JSON files and a translation hook. This is not just cosmetic; it affects how the dashboard, assistant, and support flows are rendered for farmers in the field.

5. **Backend entry point and API surface**
   `backend/main.py` is the FastAPI composition root that mounts feature routers and static assets. The backend is split into routers by domain: `auth`, `crop`, `disease`, `irrigation`, `weather`, `chatbot`, `scheme`, `catchup`, and `stores`.

6. **Authentication and user model**
   Authentication is JWT-based, with helper utilities in `backend/utils/auth.py`. User data is modeled in `backend/models/user.py` and validated through Pydantic schemas in `backend/schemas/user.py`, creating a clean boundary between request payloads, domain models, and token/session logic.

7. **Crop and farm data domain**
   Crop lifecycle data is centered on `backend/models/crop.py` and related schemas. The crop/router pair exposes farm tracking features that feed the dashboard, history views, and catch-up summaries shown in the frontend.

8. **Disease intelligence path**
   Crop disease analysis is a dedicated subsystem: `routers/disease.py` handles requests, `services/disease_service.py` and `services/ai_insights.py` provide the analysis logic, and `backend/data/disease_solutions.json` supplies curated remediation guidance. The frontend disease flow includes image upload, analysis, report, and nearby-store lookup screens.

9. **Irrigation intelligence path**
   Irrigation is split between API routing and a service layer under `backend/services/irrigation`. `forecast_engine.py` and `ml_service.py` consume model artifacts and agronomic configuration data in `data/irrigation_prediction/` and `models/irrigation/` to generate water-use recommendations from crop, soil, and climate inputs.

10. **Weather, chatbot, and support content**
   Weather insights are encapsulated in `backend/services/weather_service.py` and exposed through the weather router. The chatbot router likely fronts an external LLM integration described in the README, while the schemes and stores routers serve reference/utility content for farmers.

11. **Persistence and file storage**
   The runtime database is SQLite (`hal.db`, with `test.db` for testing), managed through SQLAlchemy in `backend/database.py`. The backend also writes/serves local files under `backend/static/audio` and `backend/uploads`, which supports voice responses and uploaded media without needing object storage.

12. **Offline data and ML lifecycle**
   There is a clear separation between runtime inference and model development. Training/evaluation scripts live under `ml/irrigation/`, while serialized production artifacts live under `models/irrigation/`. This suggests the irrigation recommender is deployed as a local ML asset rather than calling a remote inference service.

13. **Testing and maintenance**
   The repo includes backend tests (`test_auth.py`, `test_crop.py`, `test_api.py`) and frontend tests under `frontend/src/test`. `backend/reset_db.py` indicates a developer-oriented reset path for rebuilding local state during iteration.

14. **Diagramming guidance**
   For an architecture diagram, the key boundaries are: user/browser ↔ frontend SPA ↔ FastAPI routers ↔ service layer ↔ SQLite/local files/external AI APIs. The strongest domain partitions are authentication, crop monitoring, disease analysis, irrigation forecasting, and advisory content delivery.

Main entry points: [`backend/main.py`](https://github.com/aryan-a1/hal/blob/main/backend/main.py) (Backend app), [`backend/services`](https://github.com/aryan-a1/hal/tree/main/backend/services) (Service layer), [`frontend/src/main.tsx`](https://github.com/aryan-a1/hal/blob/main/frontend/src/main.tsx) (App shell), [`backend/routers/auth.py`](https://github.com/aryan-a1/hal/blob/main/backend/routers/auth.py) (Auth domain)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend SPA"]
  node_frontend_app["App shell<br/>React/Vite entry<br/>[main.tsx]"]
  node_frontend_pages["Feature pages<br/>SPA pages"]
  node_frontend_components["Shared UI<br/>UI components"]
  node_frontend_state["Client state<br/>Zustand stores"]
  node_frontend_i18n["i18n<br/>Locale hooks"]
  node_frontend_api["API client<br/>Service layer"]
end

subgraph group_backend["FastAPI Backend"]
  node_backend_main["Backend app<br/>FastAPI root<br/>[main.py]"]
  node_backend_auth["Auth domain<br/>JWT auth<br/>[auth.py]"]
  node_backend_crop["Crop domain<br/>Farm records<br/>[crop.py]"]
  node_backend_disease["Disease domain<br/>AI diagnosis<br/>[disease.py]"]
  node_backend_irrigation["Irrigation domain<br/>Forecasting<br/>[irrigation.py]"]
  node_backend_advice["Advisory APIs<br/>Weather/chat/schemes"]
  node_backend_services["Service layer<br/>Domain services"]
  node_backend_models["Data models<br/>ORM/schemas"]
end

subgraph group_ml["ML Assets"]
  node_ml_assets["Irrigation ML<br/>Model artifacts"]
end

subgraph group_storage["Local Data"]
  node_backend_db[("SQLite store<br/>Persistence<br/>[hal.db]")]
  node_backend_assets["Local assets<br/>Files"]
end

node_frontend_app -->|"renders"| node_frontend_pages
node_frontend_app -->|"uses"| node_frontend_components
node_frontend_app -->|"reads/writes"| node_frontend_state
node_frontend_app -->|"translates"| node_frontend_i18n
node_frontend_pages -->|"calls"| node_frontend_api
node_frontend_state -->|"auth/chat"| node_frontend_api
node_frontend_api -->|"HTTP API"| node_backend_main
node_backend_main -->|"mounts"| node_backend_auth
node_backend_main -->|"mounts"| node_backend_crop
node_backend_main -->|"mounts"| node_backend_disease
node_backend_main -->|"mounts"| node_backend_irrigation
node_backend_main -->|"mounts"| node_backend_advice
node_backend_auth -->|"uses"| node_backend_services
node_backend_auth -->|"validates"| node_backend_models
node_backend_crop -->|"persists"| node_backend_models
node_backend_disease -->|"analyzes"| node_backend_services
node_backend_disease -->|"reads guidance"| node_backend_assets
node_backend_irrigation -->|"forecasts"| node_backend_services
node_backend_irrigation -->|"loads models"| node_ml_assets
node_backend_services -->|"consumes data"| node_backend_assets
node_backend_services -->|"stores/loads"| node_backend_db

click node_frontend_app "https://github.com/aryan-a1/hal/blob/main/frontend/src/main.tsx"
click node_frontend_pages "https://github.com/aryan-a1/hal/tree/main/frontend/src/pages"
click node_frontend_components "https://github.com/aryan-a1/hal/tree/main/frontend/src/components"
click node_frontend_state "https://github.com/aryan-a1/hal/tree/main/frontend/src/store"
click node_frontend_i18n "https://github.com/aryan-a1/hal/tree/main/frontend/src/locales"
click node_frontend_api "https://github.com/aryan-a1/hal/tree/main/frontend/src/services"
click node_backend_main "https://github.com/aryan-a1/hal/blob/main/backend/main.py"
click node_backend_auth "https://github.com/aryan-a1/hal/blob/main/backend/routers/auth.py"
click node_backend_crop "https://github.com/aryan-a1/hal/blob/main/backend/routers/crop.py"
click node_backend_disease "https://github.com/aryan-a1/hal/blob/main/backend/routers/disease.py"
click node_backend_irrigation "https://github.com/aryan-a1/hal/blob/main/backend/routers/irrigation.py"
click node_backend_advice "https://github.com/aryan-a1/hal/tree/main/backend/routers"
click node_backend_services "https://github.com/aryan-a1/hal/tree/main/backend/services"
click node_backend_models "https://github.com/aryan-a1/hal/tree/main/backend/models"
click node_backend_db "https://github.com/aryan-a1/hal/blob/main/hal.db"
click node_backend_assets "https://github.com/aryan-a1/hal/tree/main/backend/data"
click node_ml_assets "https://github.com/aryan-a1/hal/tree/main/models/irrigation"

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_frontend_app,node_frontend_pages,node_frontend_components,node_frontend_state,node_frontend_i18n,node_frontend_api toneBlue
class node_backend_main,node_backend_auth,node_backend_crop,node_backend_disease,node_backend_irrigation,node_backend_advice,node_backend_services,node_backend_models toneAmber
class node_ml_assets toneMint
class node_backend_db,node_backend_assets toneRose
```

## Components

### Frontend SPA

React/Vite dashboard and farmer workflows

- [App shell](https://github.com/aryan-a1/hal/blob/main/frontend/src/main.tsx): React/Vite entry · Wires routing, layout, and global providers · `frontend/src/main.tsx`
- [Feature pages](https://github.com/aryan-a1/hal/tree/main/frontend/src/pages): SPA pages · Dashboard, catch-up, disease, irrigation, schemes, auth · `frontend/src/pages`
- [Shared UI](https://github.com/aryan-a1/hal/tree/main/frontend/src/components): UI components · Reusable sections and primitive widgets · `frontend/src/components`
- [Client state](https://github.com/aryan-a1/hal/tree/main/frontend/src/store): Zustand stores · Session, chat, language, profile, notifications · `frontend/src/store`
- [i18n](https://github.com/aryan-a1/hal/tree/main/frontend/src/locales): Locale hooks · English/Hindi translations and language selection · `frontend/src/locales`
- [API client](https://github.com/aryan-a1/hal/tree/main/frontend/src/services): Service layer · Typed bridge from UI to backend endpoints · `frontend/src/services`

### FastAPI Backend

API composition, domain routers, and service layer

- [Backend app](https://github.com/aryan-a1/hal/blob/main/backend/main.py): FastAPI root · Composition root mounting routers and static assets · `backend/main.py`
- [Auth domain](https://github.com/aryan-a1/hal/blob/main/backend/routers/auth.py): JWT auth · Login, signup, token/session handling · `backend/routers/auth.py`
- [Crop domain](https://github.com/aryan-a1/hal/blob/main/backend/routers/crop.py): Farm records · Crop lifecycle and dashboard/catch-up data · `backend/routers/crop.py`
- [Disease domain](https://github.com/aryan-a1/hal/blob/main/backend/routers/disease.py): AI diagnosis · Image analysis and remediation guidance · `backend/routers/disease.py`
- [Irrigation domain](https://github.com/aryan-a1/hal/blob/main/backend/routers/irrigation.py): Forecasting · Water-use recommendations from crop, soil, and climate inputs · `backend/routers/irrigation.py`
- [Advisory APIs](https://github.com/aryan-a1/hal/tree/main/backend/routers): Weather/chat/schemes · Weather, chatbot, schemes, stores, and catch-up support · `backend/routers`
- [Service layer](https://github.com/aryan-a1/hal/tree/main/backend/services): Domain services · Analysis logic, orchestration, and external integrations · `backend/services`
- [Data models](https://github.com/aryan-a1/hal/tree/main/backend/models): ORM/schemas · User and crop models plus Pydantic schemas · `backend/models`

### ML Assets

Irrigation training and production artifacts

- [Irrigation ML](https://github.com/aryan-a1/hal/tree/main/models/irrigation): Model artifacts · Trained classifier/regressor and feature metadata · `models/irrigation`

### Local Data

SQLite, uploads, static audio, and curated JSON data

- [SQLite store](https://github.com/aryan-a1/hal/blob/main/hal.db): Persistence · Runtime database and local file-backed assets · `hal.db`
- [Local assets](https://github.com/aryan-a1/hal/tree/main/backend/data): Files · Uploads, static audio, and curated JSON knowledge · `backend/data`

## Connections

- App shell → Feature pages: renders · Application shell composes the feature pages
- App shell → Shared UI: uses · Pages are built from shared UI sections
- App shell → Client state: reads/writes · Global client state is consumed across the app
- App shell → i18n: translates · Language selection and locale strings shape the UX
- Feature pages → API client: calls · Feature pages use the service layer for backend access
- Client state → API client: auth/chat · Stores coordinate session, chat, and profile-backed requests
- API client → Backend app: HTTP API · Frontend service layer is the intended bridge to FastAPI
- Backend app → Auth domain: mounts · Composition root registers auth routes
- Backend app → Crop domain: mounts · Composition root registers crop routes
- Backend app → Disease domain: mounts · Composition root registers disease routes
- Backend app → Irrigation domain: mounts · Composition root registers irrigation routes
- Backend app → Advisory APIs: mounts · Composition root registers weather/chat/schemes/stores/catch-up routes
- Auth domain → Service layer: uses · Auth helpers and token logic live in the service layer
- Auth domain → Data models: validates · User payloads and persistence schemas define the auth boundary
- Crop domain → Data models: persists · Crop records are modeled and validated here
- Disease domain → Service layer: analyzes · Disease router delegates to analysis and AI insight services
- Disease domain → Local assets: reads guidance · Curated remediation data supplements diagnosis
- Irrigation domain → Service layer: forecasts · Irrigation router calls forecasting and ML services
- Irrigation domain → Irrigation ML: loads models · Production inference uses serialized irrigation artifacts
- Service layer → Local assets: consumes data · Services read local JSON and audio/file assets as needed
- Service layer → SQLite store: stores/loads · Services and routers use SQLite-backed persistence
