How ajaymehta097/kishanai works
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: app.js Express app · App.jsx Shell · index.jsx Pages · chatbot.service.js Chat service
Components 34
Frontend
React/Vite UI, shared state, and API client boundary
- Entry · Bootstraps the React app
src/main.jsx - Shell · Top-level UI composition and routing
src/App.jsx - Pages · Central page exports and journey wiring
src/pages/index.jsx - Layout · Shared layout and navigation structure
src/components/layout/index.jsx - Home · Main home experience and feature entry point
src/components/home/HomePage.jsx - App state · Application-wide state and shared inputs
src/context/AppContext.jsx - Auth state · Login/session state for the frontend
src/context/AuthContext.jsx - API client · Primary client for backend requests
src/api/agriBackend.js - Feature switch · Chooses backend usage and fallback behavior
src/api/config.js - Claude fallback · Fallback AI path for disabled backend features
src/api/claude.js - Location hook · Captures and normalizes user location on the client
src/hooks/useLocation.js - Geo utils · Maps coordinates to a usable state/location name
src/utils/geoUtils.js - Voice input · Browser speech input for voice-driven interaction
src/hooks/useVoice.js
Backend
Express API, controllers, services, middleware, and storage
- Server entry · Starts the Express runtime
server/server.js - Express app · Middleware chain and route mounting
server/app.js - Auth guard · Protects authenticated routes
server/middleware/auth.middleware.js - Location mw · Normalizes location headers and params
server/middleware/location.middleware.js - Error handler · Central API error handling
server/middleware/error.middleware.js - Crop API · Crop recommendation endpoints
server/routes/crop.routes.js - Crop controller · Crop recommendation request handling
server/controllers/crop.controller.js - Weather API · Weather and forecast endpoints
server/routes/weather.routes.js - Weather controller · Weather request handling
server/controllers/weather.controller.js - Weather service · External weather aggregation and enrichment
server/services/weather.service.js - Chat API · Chatbot and multilingual assistant endpoints
server/routes/chatbot.routes.js - Chat controller · Chatbot request handling
server/controllers/chatbot.controller.js - Chat service · Conversation orchestration and AI integration
server/services/chatbot.service.js - Market controller · State-aware market price handling
server/controllers/market.controller.js - Market service · Market data lookup and filtering
server/services/market.service.js - Location service · Server-side location normalization and lookup
server/services/location.service.js - Mongo config · MongoDB connection setup
server/config/db.js - Users · User account persistence
server/models/user.js - Markets · Market price persistence
server/models/Market.js - Response util · Standardized API response formatting
server/utils/responseHandler.js
Data & externals
Persistent data, local ML assets, and third-party integrations
- Disease model · Bundled disease inference model
server/models/model.h5
Connections 33
- 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