# ajaymehta097/kishanai architecture

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

- Interactive diagram: https://gitdiagram.com/ajaymehta097/kishanai
- Repository: https://github.com/ajaymehta097/kishanai

## Overview

1. **Repository shape**
   - This is a split-stack agricultural advisor app: a **React/Vite frontend** in `src/` and an **Express backend** in `server/`.
   - The frontend is the user-facing product; the backend owns most domain logic, data access, ML inference, and calls to external APIs.

2. **Frontend shell and composition**
   - `src/App.jsx`, `src/main.jsx`, and the layout/components folders define the UI shell, page composition, and shared design system.
   - `src/pages/` maps the main user journeys: crop recommendation, disease detection, fertilizer guidance, satellite view, chat, weather/irrigation/profit/market, and auth.

3. **Frontend state and cross-cutting services**
   - `src/context/AppContext.jsx` and `src/context/AuthContext.jsx` centralize app-wide state and login state.
   - `src/hooks/useLocation.js` and `src/utils/geoUtils.js` determine the user’s location/state, which is then reused across features.
   - `src/hooks/useVoice.js` adds browser speech input, supporting voice-driven interaction.

4. **Frontend API boundary**
   - `src/api/agriBackend.js` is the main client gateway to the backend.
   - `src/api/config.js` is the feature switchboard: it chooses the backend base URL and toggles whether a feature uses the real backend or Claude AI fallback.
   - `vite.config.js` proxies `/api/*` to the backend during local development.

5. **Location as a shared input**
   - The app captures GPS once, maps it to a state name, and stores it in session storage.
   - That location is sent to backend calls through query params, request bodies, and `x-latitude` / `x-longitude` / `x-state` headers.
   - `server/middleware/location.middleware.js` normalizes this input so controllers can consume location consistently.

6. **Backend HTTP layer**
   - `server/server.js` and `server/app.js` define the Express app, middleware chain, and route mounting.
   - `server/routes/*.routes.js` split the API by domain: auth, chatbot, crop, disease, fertilizer, irrigation, location, market, profit, satellite, and weather.
   - Controllers in `server/controllers/` implement the request/response logic for each domain.

7. **Backend service layer**
   - `server/services/` contains the external-integration and orchestration layer.
   - Notable services include `weather.service.js`, `market.service.js`, `location.service.js`, `chatbot.service.js`, and `bhashini.service.js`.
   - This is where API aggregation and AI/service calls are isolated from controllers.

8. **Core domain flows**
   - **Crop recommendation** uses soil inputs plus live weather to rank candidate crops.
   - **Disease detection** accepts an uploaded image and returns disease/treatment guidance.
   - **Fertilizer**, **irrigation**, and **profit** endpoints provide agronomy calculators and advice.
   - **Weather** and **market** are location-aware: weather enriches farming recommendations, and market prices are filtered by the user’s state.

9. **Data storage and models**
   - MongoDB is the primary persistent store, configured through `server/config/db.js`.
   - `server/models/user.js` and `server/models/Market.js` are the main database models.
   - `server/uploads/` supports file handling for image-based workflows such as disease detection.

10. **ML and AI boundaries**
   - The backend ships a local disease model in `server/models/model.h5` with labels in `server/models/labels.txt`, so disease inference is not purely external.
   - The frontend also supports a Claude-based fallback in `src/api/claude.js`; the README notes this is used especially for the vegetable planner and any feature disabled in `USE_BACKEND`.
   - The chatbot path combines backend orchestration with Hugging Face and Bhashini for multilingual/STT/TTS behavior.

11. **Security and request shaping**
   - `server/middleware/auth.middleware.js` protects authenticated routes.
   - `server/middleware/error.middleware.js` centralizes API error handling.
   - `server/utils/responseHandler.js` suggests a standardized response envelope across endpoints.

12. **Deployment/runtime boundaries**
   - Local development is a two-process setup: React/Vite on the frontend and Express on port 5000 for the backend.
   - `capacitor.config.json` indicates the frontend is also intended for mobile packaging.
   - `vercel.json` suggests a web deployment path for the frontend, while the backend depends on environment variables for MongoDB and third-party API keys.

13. **Architecture summary for a diagram**
   - Draw the frontend as a presentation/orchestration layer that gathers user input, location, and voice.
   - Draw the backend as a domain API layer with route → controller → service → data/external API paths.
   - Show MongoDB, the local ML model, and external services (OpenWeatherMap, Hugging Face, Bhashini, Claude) as separate dependencies behind the backend.

Main entry points: [`server/app.js`](https://github.com/ajaymehta097/kishanai/blob/main/server/app.js) (Express app), [`src/App.jsx`](https://github.com/ajaymehta097/kishanai/blob/main/src/App.jsx) (Shell), [`src/pages/index.jsx`](https://github.com/ajaymehta097/kishanai/blob/main/src/pages/index.jsx) (Pages), [`server/services/chatbot.service.js`](https://github.com/ajaymehta097/kishanai/blob/main/server/services/chatbot.service.js) (Chat service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_src_main["Entry<br/>[main.jsx]"]
  node_src_app["Shell<br/>[App.jsx]"]
  node_src_pages_index["Pages<br/>page registry<br/>[index.jsx]"]
  node_src_layout["Layout<br/>[index.jsx]"]
  node_src_home["Home<br/>landing view<br/>[HomePage.jsx]"]
  node_src_app_context(("App state<br/>global state<br/>[AppContext.jsx]"))
  node_src_auth_context(("Auth state<br/>[AuthContext.jsx]"))
  node_src_api_backend["API client<br/>backend gateway<br/>[agriBackend.js]"]
  node_src_api_config["Feature switch<br/>api config<br/>[config.js]"]
  node_src_api_claude["Claude fallback<br/>ai fallback<br/>[claude.js]"]
  node_src_use_location(("Location hook<br/>location input<br/>[useLocation.js]"))
  node_src_geo_utils["Geo utils<br/>geo helper<br/>[geoUtils.js]"]
  node_src_use_voice(("Voice input<br/>speech hook<br/>[useVoice.js]"))
end

subgraph group_backend["Backend"]
  node_server_entry["Server entry<br/>express entry<br/>[server.js]"]
  node_server_app["Express app<br/>api app<br/>[app.js]"]
  node_server_auth_mw{{"Auth guard<br/>middleware<br/>[auth.middleware.js]"}}
  node_server_location_mw{{"Location mw<br/>middleware"}}
  node_server_error_mw{{"Error handler<br/>middleware"}}
  node_server_crop_route["Crop API<br/>route<br/>[crop.routes.js]"]
  node_server_crop_ctrl["Crop controller<br/>[crop.controller.js]"]
  node_server_weather_route["Weather API<br/>route<br/>[weather.routes.js]"]
  node_server_weather_ctrl["Weather controller"]
  node_server_weather_service["Weather service<br/>[weather.service.js]"]
  node_server_chat_route["Chat API<br/>route<br/>[chatbot.routes.js]"]
  node_server_chat_ctrl["Chat controller"]
  node_server_chat_service["Chat service<br/>[chatbot.service.js]"]
  node_server_market_ctrl["Market controller"]
  node_server_market_service["Market service<br/>[market.service.js]"]
  node_server_location_service["Location service"]
  node_server_db[("Mongo config<br/>database config<br/>[db.js]")]
  node_server_user_model["Users<br/>mongo model<br/>[user.js]"]
  node_server_market_model["Markets<br/>mongo model<br/>[Market.js]"]
  node_server_response["Response util<br/>response envelope<br/>[responseHandler.js]"]
end

subgraph group_data["Data & externals"]
  node_server_disease_model{{"Disease model<br/>local ml asset<br/>[model.h5]"}}
end

node_src_main -->|"bootstraps"| node_src_app
node_src_app -->|"routes to"| node_src_pages_index
node_src_app -->|"uses"| node_src_layout
node_src_app -->|"starts at"| node_src_home
node_src_app -->|"shares state"| node_src_app_context
node_src_app -->|"shares auth"| node_src_auth_context
node_src_pages_index -->|"calls"| node_src_api_backend
node_src_pages_index -->|"switches"| node_src_api_config
node_src_api_config -->|"fallback"| node_src_api_claude
node_src_use_location -->|"normalizes"| node_src_geo_utils
node_src_pages_index -->|"reuses"| node_src_use_location
node_src_pages_index -->|"supports"| node_src_use_voice
node_src_api_backend -->|"requests"| node_server_app
node_server_entry -->|"starts"| node_server_app
node_server_app -->|"protects"| node_server_auth_mw
node_server_app -->|"normalizes"| node_server_location_mw
node_server_app -->|"handles errors"| node_server_error_mw
node_server_app -->|"mounts"| node_server_crop_route
node_server_app -->|"mounts"| node_server_weather_route
node_server_app -->|"mounts"| node_server_chat_route
node_server_crop_route -->|"delegates"| node_server_crop_ctrl
node_server_weather_route -->|"delegates"| node_server_weather_ctrl
node_server_chat_route -->|"delegates"| node_server_chat_ctrl
node_server_crop_ctrl -->|"enriches with"| node_server_weather_service
node_server_weather_ctrl -->|"uses"| node_server_weather_service
node_server_chat_ctrl -->|"uses"| node_server_chat_service
node_server_weather_service -->|"resolves"| node_server_location_service
node_server_chat_service -->|"formats"| node_server_response
node_server_chat_service -->|"local inference"| node_server_disease_model
node_server_chat_service -->|"aggregates"| node_server_market_service
node_server_market_service -->|"reads"| node_server_market_model
node_server_db -->|"persists"| node_server_user_model
node_server_db -->|"persists"| node_server_market_model

click node_src_main "https://github.com/ajaymehta097/kishanai/blob/main/src/main.jsx"
click node_src_app "https://github.com/ajaymehta097/kishanai/blob/main/src/App.jsx"
click node_src_pages_index "https://github.com/ajaymehta097/kishanai/blob/main/src/pages/index.jsx"
click node_src_layout "https://github.com/ajaymehta097/kishanai/blob/main/src/components/layout/index.jsx"
click node_src_home "https://github.com/ajaymehta097/kishanai/blob/main/src/components/home/HomePage.jsx"
click node_src_app_context "https://github.com/ajaymehta097/kishanai/blob/main/src/context/AppContext.jsx"
click node_src_auth_context "https://github.com/ajaymehta097/kishanai/blob/main/src/context/AuthContext.jsx"
click node_src_api_backend "https://github.com/ajaymehta097/kishanai/blob/main/src/api/agriBackend.js"
click node_src_api_config "https://github.com/ajaymehta097/kishanai/blob/main/src/api/config.js"
click node_src_api_claude "https://github.com/ajaymehta097/kishanai/blob/main/src/api/claude.js"
click node_src_use_location "https://github.com/ajaymehta097/kishanai/blob/main/src/hooks/useLocation.js"
click node_src_geo_utils "https://github.com/ajaymehta097/kishanai/blob/main/src/utils/geoUtils.js"
click node_src_use_voice "https://github.com/ajaymehta097/kishanai/blob/main/src/hooks/useVoice.js"
click node_server_entry "https://github.com/ajaymehta097/kishanai/blob/main/server/server.js"
click node_server_app "https://github.com/ajaymehta097/kishanai/blob/main/server/app.js"
click node_server_auth_mw "https://github.com/ajaymehta097/kishanai/blob/main/server/middleware/auth.middleware.js"
click node_server_location_mw "https://github.com/ajaymehta097/kishanai/blob/main/server/middleware/location.middleware.js"
click node_server_error_mw "https://github.com/ajaymehta097/kishanai/blob/main/server/middleware/error.middleware.js"
click node_server_crop_route "https://github.com/ajaymehta097/kishanai/blob/main/server/routes/crop.routes.js"
click node_server_crop_ctrl "https://github.com/ajaymehta097/kishanai/blob/main/server/controllers/crop.controller.js"
click node_server_weather_route "https://github.com/ajaymehta097/kishanai/blob/main/server/routes/weather.routes.js"
click node_server_weather_ctrl "https://github.com/ajaymehta097/kishanai/blob/main/server/controllers/weather.controller.js"
click node_server_weather_service "https://github.com/ajaymehta097/kishanai/blob/main/server/services/weather.service.js"
click node_server_chat_route "https://github.com/ajaymehta097/kishanai/blob/main/server/routes/chatbot.routes.js"
click node_server_chat_ctrl "https://github.com/ajaymehta097/kishanai/blob/main/server/controllers/chatbot.controller.js"
click node_server_chat_service "https://github.com/ajaymehta097/kishanai/blob/main/server/services/chatbot.service.js"
click node_server_market_ctrl "https://github.com/ajaymehta097/kishanai/blob/main/server/controllers/market.controller.js"
click node_server_market_service "https://github.com/ajaymehta097/kishanai/blob/main/server/services/market.service.js"
click node_server_location_service "https://github.com/ajaymehta097/kishanai/blob/main/server/services/location.service.js"
click node_server_db "https://github.com/ajaymehta097/kishanai/blob/main/server/config/db.js"
click node_server_user_model "https://github.com/ajaymehta097/kishanai/blob/main/server/models/user.js"
click node_server_market_model "https://github.com/ajaymehta097/kishanai/blob/main/server/models/Market.js"
click node_server_response "https://github.com/ajaymehta097/kishanai/blob/main/server/utils/responseHandler.js"
click node_server_disease_model "https://github.com/ajaymehta097/kishanai/blob/main/server/models/model.h5"

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_src_main,node_src_app,node_src_pages_index,node_src_layout,node_src_home,node_src_app_context,node_src_auth_context,node_src_api_backend,node_src_api_config,node_src_api_claude,node_src_use_location,node_src_geo_utils,node_src_use_voice toneBlue
class node_server_entry,node_server_app,node_server_auth_mw,node_server_location_mw,node_server_error_mw,node_server_crop_route,node_server_crop_ctrl,node_server_weather_route,node_server_weather_ctrl,node_server_weather_service,node_server_chat_route,node_server_chat_ctrl,node_server_chat_service,node_server_market_ctrl,node_server_market_service,node_server_location_service,node_server_db,node_server_user_model,node_server_market_model,node_server_response toneAmber
class node_server_disease_model toneMint
```

## Components

### Frontend

React/Vite UI, shared state, and API client boundary

- [Entry](https://github.com/ajaymehta097/kishanai/blob/main/src/main.jsx): frontend entry · Bootstraps the React app · `src/main.jsx`
- [Shell](https://github.com/ajaymehta097/kishanai/blob/main/src/App.jsx): frontend shell · Top-level UI composition and routing · `src/App.jsx`
- [Pages](https://github.com/ajaymehta097/kishanai/blob/main/src/pages/index.jsx): page registry · Central page exports and journey wiring · `src/pages/index.jsx`
- [Layout](https://github.com/ajaymehta097/kishanai/blob/main/src/components/layout/index.jsx): ui layout · Shared layout and navigation structure · `src/components/layout/index.jsx`
- [Home](https://github.com/ajaymehta097/kishanai/blob/main/src/components/home/HomePage.jsx): landing view · Main home experience and feature entry point · `src/components/home/HomePage.jsx`
- [App state](https://github.com/ajaymehta097/kishanai/blob/main/src/context/AppContext.jsx): global state · Application-wide state and shared inputs · `src/context/AppContext.jsx`
- [Auth state](https://github.com/ajaymehta097/kishanai/blob/main/src/context/AuthContext.jsx): auth state · Login/session state for the frontend · `src/context/AuthContext.jsx`
- [API client](https://github.com/ajaymehta097/kishanai/blob/main/src/api/agriBackend.js): backend gateway · Primary client for backend requests · `src/api/agriBackend.js`
- [Feature switch](https://github.com/ajaymehta097/kishanai/blob/main/src/api/config.js): api config · Chooses backend usage and fallback behavior · `src/api/config.js`
- [Claude fallback](https://github.com/ajaymehta097/kishanai/blob/main/src/api/claude.js): ai fallback · Fallback AI path for disabled backend features · `src/api/claude.js`
- [Location hook](https://github.com/ajaymehta097/kishanai/blob/main/src/hooks/useLocation.js): location input · Captures and normalizes user location on the client · `src/hooks/useLocation.js`
- [Geo utils](https://github.com/ajaymehta097/kishanai/blob/main/src/utils/geoUtils.js): geo helper · Maps coordinates to a usable state/location name · `src/utils/geoUtils.js`
- [Voice input](https://github.com/ajaymehta097/kishanai/blob/main/src/hooks/useVoice.js): speech hook · Browser speech input for voice-driven interaction · `src/hooks/useVoice.js`

### Backend

Express API, controllers, services, middleware, and storage

- [Server entry](https://github.com/ajaymehta097/kishanai/blob/main/server/server.js): express entry · Starts the Express runtime · `server/server.js`
- [Express app](https://github.com/ajaymehta097/kishanai/blob/main/server/app.js): api app · Middleware chain and route mounting · `server/app.js`
- [Auth guard](https://github.com/ajaymehta097/kishanai/blob/main/server/middleware/auth.middleware.js): middleware · Protects authenticated routes · `server/middleware/auth.middleware.js`
- [Location mw](https://github.com/ajaymehta097/kishanai/blob/main/server/middleware/location.middleware.js): middleware · Normalizes location headers and params · `server/middleware/location.middleware.js`
- [Error handler](https://github.com/ajaymehta097/kishanai/blob/main/server/middleware/error.middleware.js): middleware · Central API error handling · `server/middleware/error.middleware.js`
- [Crop API](https://github.com/ajaymehta097/kishanai/blob/main/server/routes/crop.routes.js): route · Crop recommendation endpoints · `server/routes/crop.routes.js`
- [Crop controller](https://github.com/ajaymehta097/kishanai/blob/main/server/controllers/crop.controller.js): controller · Crop recommendation request handling · `server/controllers/crop.controller.js`
- [Weather API](https://github.com/ajaymehta097/kishanai/blob/main/server/routes/weather.routes.js): route · Weather and forecast endpoints · `server/routes/weather.routes.js`
- [Weather controller](https://github.com/ajaymehta097/kishanai/blob/main/server/controllers/weather.controller.js): controller · Weather request handling · `server/controllers/weather.controller.js`
- [Weather service](https://github.com/ajaymehta097/kishanai/blob/main/server/services/weather.service.js): service · External weather aggregation and enrichment · `server/services/weather.service.js`
- [Chat API](https://github.com/ajaymehta097/kishanai/blob/main/server/routes/chatbot.routes.js): route · Chatbot and multilingual assistant endpoints · `server/routes/chatbot.routes.js`
- [Chat controller](https://github.com/ajaymehta097/kishanai/blob/main/server/controllers/chatbot.controller.js): controller · Chatbot request handling · `server/controllers/chatbot.controller.js`
- [Chat service](https://github.com/ajaymehta097/kishanai/blob/main/server/services/chatbot.service.js): service · Conversation orchestration and AI integration · `server/services/chatbot.service.js`
- [Market controller](https://github.com/ajaymehta097/kishanai/blob/main/server/controllers/market.controller.js): controller · State-aware market price handling · `server/controllers/market.controller.js`
- [Market service](https://github.com/ajaymehta097/kishanai/blob/main/server/services/market.service.js): service · Market data lookup and filtering · `server/services/market.service.js`
- [Location service](https://github.com/ajaymehta097/kishanai/blob/main/server/services/location.service.js): service · Server-side location normalization and lookup · `server/services/location.service.js`
- [Mongo config](https://github.com/ajaymehta097/kishanai/blob/main/server/config/db.js): database config · MongoDB connection setup · `server/config/db.js`
- [Users](https://github.com/ajaymehta097/kishanai/blob/main/server/models/user.js): mongo model · User account persistence · `server/models/user.js`
- [Markets](https://github.com/ajaymehta097/kishanai/blob/main/server/models/Market.js): mongo model · Market price persistence · `server/models/Market.js`
- [Response util](https://github.com/ajaymehta097/kishanai/blob/main/server/utils/responseHandler.js): response envelope · Standardized API response formatting · `server/utils/responseHandler.js`

### Data & externals

Persistent data, local ML assets, and third-party integrations

- [Disease model](https://github.com/ajaymehta097/kishanai/blob/main/server/models/model.h5): local ml asset · Bundled disease inference model · `server/models/model.h5`

## Connections

- Entry → Shell: bootstraps · Application entry mounts the top-level shell
- Shell → Pages: routes to · Shell delegates to page-level journeys
- Shell → Layout: uses · Shared layout frames the UI
- Shell → Home: starts at · Home experience is a major landing surface
- Shell → App state: shares state · App-wide state feeds multiple journeys
- Shell → Auth state: shares auth · Auth state is available across the app
- Pages → API client: calls · Pages invoke the backend gateway for domain actions
- Pages → Feature switch: switches · Feature flags decide backend vs fallback paths
- Feature switch → Claude fallback: fallback · Disabled backend features can use Claude-based flows
- Location hook → Geo utils: normalizes · GPS is mapped to a usable state/location
- Pages → Location hook: reuses · Many features consume the shared location input
- Pages → Voice input: supports · Voice input is available across conversational flows
- API client → Express app: requests · Frontend API client targets the Express API
- Server entry → Express app: starts · Runtime entry creates and runs the app
- Express app → Auth guard: protects · Authenticated endpoints pass through auth checks
- Express app → Location mw: normalizes · Location metadata is shaped before controllers run
- Express app → Error handler: handles errors · Central error middleware standardizes failures
- Express app → Crop API: mounts · Crop domain route is mounted on the API
- Express app → Weather API: mounts · Weather domain route is mounted on the API
- Express app → Chat API: mounts · Chatbot route is mounted on the API
- Crop API → Crop controller: delegates · Route handlers forward crop requests to controllers
- Weather API → Weather controller: delegates · Route handlers forward weather requests to controllers
- Chat API → Chat controller: delegates · Route handlers forward chat requests to controllers
- Crop controller → Weather service: enriches with · Crop advice uses live weather data
- Weather controller → Weather service: uses · Weather controller delegates to the weather service
- Chat controller → Chat service: uses · Chat controller delegates orchestration to the service layer
- Weather service → Location service: resolves · Weather queries depend on location normalization
- Chat service → Response util: formats · Service output is wrapped in a standard response shape
- Chat service → Disease model: local inference · Local ML asset supports disease-related workflows
- Chat service → Market service: aggregates · Chat flows can coordinate domain lookups
- Market service → Markets: reads · Market service relies on stored market data
- Mongo config → Users: persists · MongoDB stores user accounts
- Mongo config → Markets: persists · MongoDB stores market records
