Skip to content

gastonche/lunelo

Repository files navigation

Lunelo

A production-ready multi-agent AI customer support system for e-commerce, built with NestJS microservices architecture. Features WhatsApp integration, intelligent conversation management, and multi-tenant support.

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    HTTP/REST
β”‚   Client    β”‚ ──────────► β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ (WhatsApp)  β”‚             β”‚   Gateway    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜             β”‚  (Port 3000) β”‚
                            β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
                                   β”‚ RPC via Consul
                                   β–Ό
                            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                            β”‚ Orchestrator β”‚
                            β”‚ (Port 3002)  β”‚
                            β”‚  WhatsApp    β”‚
                            β”‚  AI Agents   β”‚
                            β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
                                   β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚              β”‚              β”‚
                    β–Ό              β–Ό              β–Ό
            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
            β”‚   Tenants   β”‚ β”‚Conversationsβ”‚ β”‚   Agent     β”‚
            β”‚ (Port 3001) β”‚ β”‚ (Port 3004) β”‚ β”‚  Package    β”‚
            β”‚ Multi-tenantβ”‚ β”‚  Messaging  β”‚ β”‚  (VoltAI)   β”‚
            β”‚  Management β”‚ β”‚  pgvector   β”‚ β”‚             β”‚
            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Core Components

  • API Gateway: HTTP entry point with Swagger documentation and health monitoring
  • Orchestrator Service: Processes WhatsApp webhooks, manages conversations, and orchestrates multi-agent AI interactions
  • Tenants Service: Multi-tenant management with store configuration, channels, and commerce backend integration
  • Conversations Service: Chat and messaging management with semantic search capabilities using pgvector
  • Agent Package: Multi-agent AI system powered by VoltAI with specialized agents (Shopping, Logistics, Customer Support, Linguist)

Infrastructure Services

  • Consul: Service discovery and health monitoring
  • PostgreSQL: Two databases (Tenants and Conversations)
  • Redis: Caching and queue management
  • Bull Board: Queue monitoring dashboard

πŸš€ Quick Start

Prerequisites

  • Node.js >= 18
  • pnpm package manager
  • Docker and Docker Compose (for infrastructure services)

Automated Setup

# Clone the repository
git clone <repository>
cd lunelo

# Run setup script
./setup.sh

Manual Setup

Option 1: Full Docker Environment

# Install dependencies
pnpm install

# Build all packages
pnpm run build

# Start complete environment (all services + infrastructure)
docker-compose --profile full up --build

Option 2: Local Development

# Install dependencies
pnpm install

# Build all packages
pnpm run build

# Terminal 1: Start infrastructure services
docker-compose --profile consul --profile databases up

# Terminal 2: Start all microservices
pnpm run dev

# Or start individual services:
pnpm run dev --filter=@lunelo/gateway
pnpm run dev --filter=@lunelo/orchestrator
pnpm run dev --filter=@lunelo/tenants
pnpm run dev --filter=@lunelo/conversations

πŸ“Š Services Overview

API Gateway (Port 3000)

Role: HTTP entry point and routing hub

Features:

  • Swagger API documentation (/api)
  • Aggregated health checks (/health)
  • Bull Board queue monitoring
  • CORS configuration
  • Request validation

Endpoints:

  • GET /health - Service health check
  • GET /api - Swagger documentation
  • GET /webhook/health - Gateway health

Orchestrator Service (Port 3002)

Role: Business logic coordinator and AI agent orchestrator

Features:

  • WhatsApp webhook processing (Twilio integration)
  • Multi-agent AI system orchestration
  • Queue-based message processing
  • Conversation management
  • Event-driven architecture

Key Components:

  • WhatsApp webhook handler
  • Multi-agent AI supervisor (VoltAI)
  • Queue listeners for async processing
  • RPC client for service communication

Tenants Service (Port 3001)

Role: Multi-tenant management

Features:

  • Tenant/store configuration
  • Channel management (WhatsApp, Instagram, Telegram, SMS, Webchat)
  • Commerce backend integration (WooCommerce, Shopify, Medusa, Spree, Custom)
  • User and role management
  • Store metadata and persona configuration

Database: PostgreSQL with Prisma ORM

Conversations Service (Port 3004)

Role: Chat and messaging management

Features:

  • Conversation lifecycle management
  • Message storage and retrieval
  • Semantic search with pgvector
  • Customer management
  • Agent action tracking
  • Semantic memory storage

Database: PostgreSQL with pgvector extension

πŸ€– Multi-Agent AI System

The system uses a sophisticated multi-agent architecture powered by VoltAI:

Agent Hierarchy

  1. Supervisor Agent: Orchestrates all interactions and delegates to specialized agents
  2. Linguist Agent: Humanizes all user-facing responses (mandatory for all outputs)
  3. Shopping Agent: Handles product search, cart management, and order creation
  4. Logistics Agent: Manages order tracking, shipping, and delivery status
  5. Customer Support Agent: Handles returns, refunds, and post-purchase support

Agent Capabilities

  • Natural Language Understanding: Context-aware conversation handling
  • Tool Integration: Access to store backends, product catalogs, and order systems
  • Memory Management: Persistent conversation context
  • Parallel Execution: Simultaneous agent calls for faster responses
  • Threaded Replies: Understanding of message threading and context

πŸ”§ Service Discovery with Consul

Key Features

  • Dynamic Service Registration: Services auto-register on startup
  • Health Monitoring: TTL-based health checks (30s intervals)
  • Load Balancing: Round-robin across healthy instances
  • Auto Scaling: Start multiple instances without code changes
  • Service Constants: Type-safe service names prevent errors

Consul UI

Access the Consul dashboard at: http://localhost:8500

View all registered services, health status, and service instances.

🐳 Docker Commands

# Full stack (all services + infrastructure)
docker-compose --profile full up --build

# Infrastructure only (Consul + databases + Redis)
docker-compose --profile consul --profile databases up

# Only Consul
docker-compose --profile consul up

# Only databases
docker-compose --profile databases up

# View logs
docker-compose logs -f [service-name]

# Stop all services
docker-compose down

# Clean rebuild
docker-compose build --no-cache

πŸ—„οΈ Database Setup

The project uses two PostgreSQL databases:

Tenants Database (Port 5432)

  • Purpose: Multi-tenant management
  • Schema: Stores tenants, channels, commerce backends, and users

Conversations Database (Port 5433)

  • Purpose: Chat and messaging with semantic search
  • Schema: Stores conversations, messages, customers, and semantic memory
  • Extension: pgvector for vector similarity search

Database Commands

# Generate Prisma clients
cd apps/tenants && pnpm db:generate
cd apps/conversations && pnpm db:generate

# Push schemas to databases
cd apps/tenants && pnpm db:push
cd apps/conversations && pnpm db:push

# Run migrations
cd apps/tenants && pnpm db:migrate
cd apps/conversations && pnpm db:migrate

# Open Prisma Studio
cd apps/tenants && pnpm db:studio      # Tenants DB GUI
cd apps/conversations && pnpm db:studio # Conversations DB GUI

βš™οΈ Configuration

Environment Files

Each service has environment files for different deployment scenarios:

apps/[service]/
β”œβ”€β”€ .env          # Local development (CONSUL_HOST=localhost)
└── .env.docker   # Docker environment (CONSUL_HOST=consul)

Environment Variables

Common Configuration (All Services)

NODE_ENV=development
HOST=localhost          # Local: localhost, Docker: 0.0.0.0
PORT=3001              # Unique per service
LOG_LEVEL=info
CONSUL_HOST=localhost  # Local: localhost, Docker: consul
CONSUL_PORT=8500

Gateway Specific

CORS_ENABLED=true
CORS_ORIGINS=*
SWAGGER_ENABLED=true
SWAGGER_PATH=api

Database URLs

# Tenants Service
DATABASE_URL=postgresql://tenants_user:tenants_password@localhost:5432/tenants_db

# Conversations Service
DATABASE_URL=postgresql://conversations_user:conversations_password@localhost:5433/conversations_db

# Orchestrator/Agent Service (requires pgvector)
AGENT_DATABASE_URL=postgresql://agent_user:agent_password@localhost:5436/agent_db

Redis

REDIS_HOST=localhost
REDIS_PORT=6379

Auth Service (Supabase)

# Supabase Configuration
SUPABASE_URL=https://your-project-id.supabase.co
SUPABASE_ANON_KEY=your-anon-key
SUPABASE_SERVICE_ROLE_KEY=your-service-role-key

# Password Reset Redirect URL
AUTH_REDIRECT_URL=http://localhost:3005

# Optional JWT Configuration
JWT_SECRET=your-jwt-secret
JWT_EXPIRES_IN=24h

πŸ“ Project Structure

lunelo/                          # Monorepo root
β”œβ”€β”€ apps/                        # Applications
β”‚   β”œβ”€β”€ gateway/                # @lunelo/gateway - HTTP API Gateway
β”‚   β”œβ”€β”€ orchestrator/           # @lunelo/orchestrator - WhatsApp & AI orchestration
β”‚   β”œβ”€β”€ tenants/                # @lunelo/tenants - Multi-tenant management
β”‚   └── conversations/         # @lunelo/conversations - Chat and messaging
β”œβ”€β”€ packages/                   # Shared packages
β”‚   β”œβ”€β”€ agent/                  # @lunelo/agent - Multi-agent AI system (VoltAI)
β”‚   β”œβ”€β”€ adapters/              # @lunelo/adapters - External integrations (WooCommerce, etc.)
β”‚   β”œβ”€β”€ cache/                 # @lunelo/cache - Caching utilities
β”‚   β”œβ”€β”€ config/                # @lunelo/config - Configuration schemas
β”‚   β”œβ”€β”€ events/                # @lunelo/events - Event system and queues
β”‚   β”œβ”€β”€ shared/                # @lunelo/shared - Common utilities & Consul integration
β”‚   └── typescript-config/     # Shared TypeScript configurations
β”œβ”€β”€ docker-compose.yml          # Container orchestration
β”œβ”€β”€ turbo.json                 # Turbo monorepo configuration
β”œβ”€β”€ pnpm-workspace.yaml        # pnpm workspace configuration
└── README.md                  # This file

Monorepo Benefits

  • Shared Dependencies: Common packages used across all services
  • Parallel Execution: Turbo runs tasks in parallel with dependency awareness
  • Workspace Filtering: Run commands on specific services with --filter
  • Dependency Management: pnpm efficiently manages dependencies across workspaces
  • Type Safety: Shared TypeScript configuration ensures consistency

πŸ”„ Data Flow

WhatsApp Message Processing

  1. WhatsApp β†’ Orchestrator (webhook via Twilio)
  2. Orchestrator β†’ Tenants Service (get channel/tenant info)
  3. Orchestrator β†’ Conversations Service (get/create conversation)
  4. Orchestrator β†’ Agent Package (process with multi-agent AI)
  5. Agent Package β†’ Tenants Service (get store details)
  6. Agent Package β†’ Conversations Service (store agent response)
  7. Orchestrator β†’ WhatsApp (send response via Twilio)

Service Communication

All inter-service communication uses RPC via Consul service discovery:

  • Services register with Consul on startup
  • RPC calls use service names (type-safe constants)
  • Automatic load balancing across healthy instances
  • Health checks ensure service availability

πŸ› οΈ Development Workflow

Monorepo Commands

# Install dependencies for all packages
pnpm install

# Build all packages (respects dependency order)
pnpm run build

# Start all services in development mode
pnpm run dev

# Run specific service
pnpm run dev --filter=@lunelo/gateway

# Run multiple services
pnpm run dev --filter=@lunelo/gateway --filter=@lunelo/orchestrator

# Lint all packages
pnpm run lint

# Format all code
pnpm run format

# Type check all packages
pnpm run check-types

# Build specific service
pnpm run build --filter=@lunelo/orchestrator

# Add dependency to specific workspace
pnpm --filter=@lunelo/gateway add express

πŸ” Monitoring & Health

Service URLs

Health Monitoring

# Check gateway health
curl http://localhost:3000/health

# Check Consul services
curl http://localhost:8500/v1/catalog/services

# Check specific service health
curl "http://localhost:8500/v1/health/service/orchestrator?passing=true"

πŸš€ Scaling Services

Horizontal Scaling

# Start multiple instances locally (different ports)
PORT=3002 pnpm run dev --filter=@lunelo/orchestrator  # Instance 1
PORT=3003 pnpm run dev --filter=@lunelo/orchestrator  # Instance 2

# Or with Docker
docker-compose --profile full up --scale orchestrator=3

Benefits

  • No code changes required for scaling
  • Automatic load balancing across instances
  • Health-based routing (unhealthy instances excluded)
  • Zero-downtime scaling

πŸ”§ Troubleshooting

Common Issues

Services Not Registering

  • Check Consul is running: curl http://localhost:8500/v1/status/leader
  • Verify CONSUL_HOST and CONSUL_PORT environment variables
  • Check service logs for registration errors

Service Discovery Failing

  • Verify services are registered in Consul UI
  • Check service health status
  • Ensure network connectivity between services

Database Connection Issues

# Check database is running
docker-compose ps postgres-tenants
docker-compose ps postgres-conversations

# Check database logs
docker-compose logs postgres-tenants
docker-compose logs postgres-conversations

# Verify connection strings in .env files

Docker Issues

# Clean up containers and volumes
docker-compose down -v

# Rebuild from scratch
docker-compose build --no-cache

# Check container logs
docker-compose logs [service-name]

# If Consul fails to start
docker-compose down -v  # Clears volumes
docker-compose --profile consul up  # Restart Consul

πŸ“‹ Package Names

  • @lunelo/gateway - HTTP API Gateway
  • @lunelo/orchestrator - WhatsApp & AI orchestration
  • @lunelo/tenants - Multi-tenant management
  • @lunelo/conversations - Chat and messaging management
  • @lunelo/agent - Multi-agent AI system
  • @lunelo/adapters - External integrations (WooCommerce, etc.)
  • @lunelo/cache - Caching utilities
  • @lunelo/config - Configuration schemas
  • @lunelo/events - Event system and queues
  • @lunelo/shared - Common utilities & Consul integration

🎯 Production Considerations

Infrastructure

  • Deploy 3+ Consul nodes for high availability
  • Enable Consul ACLs for security
  • Use TLS for Consul communication
  • Set up database replication and backups
  • Configure Redis clustering for high availability

Service Security

  • Implement service-to-service authentication
  • Use environment-specific configurations
  • Enable request/response encryption
  • Secure WhatsApp webhook endpoints

Monitoring

  • Integrate with Prometheus/Grafana
  • Set up alerting for service health
  • Monitor Consul cluster health
  • Track queue metrics (Bull Board)
  • Monitor AI agent performance

πŸ“ž Getting Help


The architecture is designed to be developer-friendly with automatic service discovery, health monitoring, and easy scaling. All services use consistent patterns for communication and configuration.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors