How agri-mitra/bff works
Project type and purpose
• This is a Backend‑For‑Frontend (BFF) built with NestJS. It exposes a unified API for frontend apps, orchestrating calls to language services (translation, ASR, TTS) and domain APIs (PM Kisan Aadhaar status, OTP).
• It uses Prisma for database access, XState for flow control of conversations, and provides telemetry/metrics and logging.
File structure
• Top‑level NestJS project layout under src/ with module directories: aiTools, conversation, health, monitoring, metrics, user, plus a global‐services folder for Prisma.
• A src/xstate/prompt subfolder defining state‑machine logic in prompt.machine.ts, actions, guards, utils.
• prisma/ folder holds migrations and schema.prisma, implying a relational DB.
• Dockerfile and docker‑compose.*.yaml for local, CI, production.
• Tests under test/ and *.spec.ts files.
• Config files for linting, CI workflows, TypeScript, GitHub actions.
README insights
• Describes key features: API orchestration, XState flows, telemetry for metrics (consumable by Grafana), comprehensive logging.
• Mentions Hasura and database migration with Prisma.
• Supports additional “scheme_name” parameter for chatbot UI.
a. Frontend Application
• Calls BFF via HTTP/REST or GraphQL (if Hasura is in play).
b. BFF (NestJS backend)
• API Gateway / Controllers (conversation.controller, metrics.controller, user.controller, health.controller, monitoring.controller)
• Business Logic / Services (conversation.service, ai-tools.service, metrics.service, monitoring.service, user.service, health.service)
• XState engine (prompt.machine, prompt.service) for conversation flows
• Rate‑limiter guard (rate-limiter.guard.ts)
• Prisma Service (global-services/prisma.service.ts) → RDBMS
• Logging module (NestJS built‑in logger or custom)
• Telemetry/metrics pipeline (metrics.module → expose Prometheus metrics endpoint)
c. Database
• PostgreSQL (or MySQL) managed by Prisma
• Tables for users, messages, metrics, telemetry, feedback
d. External APIs
• Bhashini translation, ASR, TTS endpoints
• PM Kisan Aadhaar/OTP endpoints
• Any other 3rd‑party AI tools or caching layer (cache.provider.ts)
e. Observability
• Prometheus scraping metrics endpoint
• Grafana dashboards
• Logs shipped to ELK or similar
Relationships and interactions
• Frontend → BFF Controllers (HTTP requests)
• Controller → Services → External APIs or DB (via Prisma)
• Conversation.Service uses XState engine to drive multi‑step flows, invoking aiTools.service and/or user.service as needed.
• Metrics.Service collects counts/timings; Monitoring.Service monitors health; Health.controller for readiness/liveness.
• Prisma.Service executes SQL on the RDBMS.
• Telemetry endpoint scraped by Prometheus → Grafana visualizes.
• Logs written to console/files and collected by centralized logging system.
Architectural patterns and design principles
• Modular Monolith: distinct NestJS modules encapsulate functionality.
• State Machine for flow control (XState).
• Controller‑Service‑Repository pattern (NestJS + Prisma).
• Separation of concerns: orchestration, persistence, external API clients, observability.
• Infrastructure as Code with Docker & docker-compose for dev/CI/prod.
Final advice
• Be very detailed: include all modules and data stores.