A production-ready NestJS backend demonstrating AI-driven agent architecture with MongoDB persistence, pluggable AI services, and flexible premium data modes.
This project showcases clean separation-of-concerns principles:
- Orchestration:
AgentServicecoordinates data fetching and AI calls - Persistence: Mongoose + MongoDB for customer/policy data
- AI Integration: Pluggable service (OpenAI by default, extensible to LangChain)
- Premium Data: External APIs, mock servers, or local computation
- Monitoring: Built-in health checks and graceful shutdown
Core Principles: KISS, DRY, and clear module boundaries
- ✅ Mongoose + MongoDB integration with schema validation
- ✅ Health check endpoint (
GET /api/health) for readiness - ✅ Agent endpoint (
GET /api/agent/customer/:id/insight) for AI insights - ✅ Customer CRUD (
POST /api/customer,GET /api/customer) - ✅ Pluggable AI service (OpenAI by default)
- ✅ Premium data modes: external API, local computation, mock server
- ✅ Global validation pipe with class-validator
- ✅ Graceful shutdown and error handling
- Node.js ≥ 18.x
- Yarn or npm
- MongoDB (local, Docker, or Atlas)
yarn installCreate a .env file:
MONGODB_URI=mongodb://localhost:27017/ai-agent
OPENAI_API_KEY=sk_test_...
OPENAI_MODEL=gpt-3.5-turbo
PREMIUM_API_MODE=local
PORT=3000yarn start:devExpected output: Listening on http://localhost:3000/api
GET /api/healthResponse: { "status": "up", "mongo": { "readyState": 1 }, "timestamp": "..." }
POST /api/customer
Content-Type: application/jsonBody:
{
"name": "John Doe",
"email": "john@example.com",
"phone": "+1-555-0100",
"policies": [{ "policyNumber": "POL-001", "type": "auto", "coverageAmount": 50000 }]
}GET /api/customerGET /api/agent/customer/:customerId/insightResponse: { "customer": {...}, "premium": {...}, "answer": "..." }
Computes premium from customer policies — no external calls.
export PREMIUM_API_MODE=local
yarn start:devCalls local mock premium API server.
# Terminal 1
node scripts/mock-premium.js
# Terminal 2
export PREMIUM_API_URL=http://localhost:4000
export PREMIUM_API_MODE=mock
yarn start:devCalls a real external premium API.
export PREMIUM_API_URL=https://api.your-service.com
export PREMIUM_API_MODE=external
yarn start:devsrc/
├── agent/ # Agent orchestration
├── customer/ # Customer CRUD
├── ai/ # AI service wrapper
├── health/ # Health checks
├── pipes/ # Validation
└── main.ts # Bootstrap
yarn start:dev # Development watch mode
yarn build # TypeScript build
yarn start:prod # Production
yarn test # Unit tests
yarn test:e2e # E2E tests
yarn test:cov # Coverage
node scripts/mock-premium.js # Mock premium API| Issue | Solution |
|---|---|
| MongoDB connection fails | Ensure MongoDB is running; check MONGODB_URI |
| AI responses are fallback | Set valid OPENAI_API_KEY |
| Premium API errors | Use local mode or start mock server |
- Add unit tests for
AgentService,CustomerService,AiService - Integrate Swagger/OpenAPI documentation
- Add LangChain for advanced prompt management
- Implement JWT authentication
- Add pagination for customer endpoints
MIT