An open API service indexing awesome lists of open source software.

https://github.com/hgurung/ai-application-factory

AI-powered code generation pipeline with parallel validation, BullMQ job queue, and blue-green deployment
https://github.com/hgurung/ai-application-factory

ai blue-green-deployment bullmq docker monorepo nestjs nginx nx postgresql redis typescript

Last synced: 5 days ago
JSON representation

AI-powered code generation pipeline with parallel validation, BullMQ job queue, and blue-green deployment

Awesome Lists containing this project

README

          

# AI Application Factory

An autonomous code generation pipeline — describe a feature in plain English, get production-ready NestJS code generated by Claude AI, validated across 5 quality dimensions in parallel, and deployed with zero downtime.

---

## Architecture

```
Client


nginx (reverse proxy)

├── /api/jobs ──────────────────────► generator-agent
│ │
│ BullMQ Queue (Redis)
│ │
│ Job Worker
│ │
│ ┌────────────▼────────────┐
│ │ Claude AI / Mock │
│ │ Code Generator │
│ └────────────┬────────────┘
│ │
│ ┌────────────▼────────────┐
│ │ validation-agent │
│ │ │
│ │ SecurityValidator │
│ │ ArchitectureValidator │
│ │ PerformanceValidator │ (parallel)
│ │ TypeScriptValidator │
│ │ TestCoverageValidator │
│ └────────────┬────────────┘
│ │
│ PostgreSQL

└── /api/validate ──────────────────► validation-agent

Blue-Green Deployment:
nginx → blue (live) → ./deploy-green.sh → nginx → green (live), blue stopped
nginx → green (live) → ./rollback-blue.sh → nginx → blue (live), green stopped
```

---

## Tech Stack

| Layer | Technology |
|-------|-----------|
| Monorepo | Nx + NestJS |
| Language | TypeScript |
| Code Generation | Claude AI (Anthropic) with mock fallback |
| Job Queue | BullMQ + Redis |
| Database | PostgreSQL + TypeORM |
| Reverse Proxy | nginx |
| Deployment | Docker Compose with blue-green strategy |
| Testing | Jest (unit tests for validators and services) |

---

## How It Works

1. **POST /api/jobs** — client sends a `clientName` and `requirement` (e.g. "payment module"). The job is saved to PostgreSQL with status `pending` and enqueued into Redis. The API returns immediately.

2. **BullMQ Worker** picks up the job asynchronously. It calls the Claude AI API (or a mock during development) to generate a NestJS module from the requirement.

3. **Validation Pipeline** — the generated code is sent to `validation-agent`, which runs 5 validators simultaneously using `Promise.allSettled()`:
- **SecurityValidator** — checks for `eval()`, hardcoded secrets, missing guards
- **ArchitectureValidator** — checks for proper NestJS module structure
- **PerformanceValidator** — flags N+1 queries, missing pagination
- **TypeScriptValidator** — checks for `any` types, proper typing
- **TestCoverageValidator** — requires test files to be present

4. **GET /api/jobs/:id** — client polls for the result. Final status is `done` (all validators passed) or `failed` (with a detailed error message showing which validators failed and why).

---

## Project Structure

```
.
├── apps/
│ ├── generator-agent/ # NestJS app — receives jobs, generates code
│ │ └── src/job/
│ │ ├── job.controller.ts
│ │ ├── job.service.ts
│ │ ├── job.processor.ts # BullMQ worker
│ │ ├── job.entity.ts
│ │ └── generator.service.ts
│ ├── validation-agent/ # NestJS app — parallel validation pipeline
│ │ └── src/validation/
│ │ ├── validation.service.ts
│ │ └── validators/
│ │ ├── security.validator.ts
│ │ ├── architecture.validator.ts
│ │ ├── performance.validator.ts
│ │ ├── typescript.validator.ts
│ │ └── test-coverage.validator.ts
│ └── shared/ # Shared types and DB config
├── nginx/
│ └── nginx.conf # Swapped by deploy scripts
├── deploy-green.sh # Zero-downtime deploy to green slot
├── rollback-blue.sh # Instant rollback to blue slot
└── docker-compose.yml
```

---

## Quick Start

**Prerequisites:** Docker Desktop, Node.js 20+

```bash
# Clone the repo
git clone https://github.com/harrisgurung/ai-application-factory.git
cd ai-application-factory

# Install dependencies
npm install

# Start all services (nginx, postgres, redis, blue slot)
docker compose up -d --build

# Submit a job
curl -X POST http://localhost/api/jobs \
-H "Content-Type: application/json" \
-d '{"clientName": "Demo", "requirement": "user authentication module"}'

# Poll for result (use the id from above)
curl http://localhost/api/jobs/

# List all jobs
curl http://localhost/api/jobs
```

---

## Environment Variables

Create a `.env` file at the root (copy from `.env.example`):

```env
# Toggle real Claude API vs mock generator
USE_CLAUDE=false
CLAUDE_API_KEY=your-api-key-here

# Database
POSTGRES_HOST=localhost
POSTGRES_PORT=5433
POSTGRES_USER=agent
POSTGRES_PASSWORD=password
POSTGRES_DB=agentdb

# Redis
REDIS_HOST=localhost
REDIS_PORT=6379
```

Set `USE_CLAUDE=true` and add your Anthropic API key to use real AI generation. The mock generator works out of the box with no API key needed.

---

## Blue-Green Deployment

The system runs two identical slots — **blue** (live) and **green** (standby). Deployments switch traffic between them via nginx reload, giving zero downtime.

```bash
# Deploy new version to green, switch nginx, stop blue
./deploy-green.sh

# Instantly roll back to blue if anything goes wrong
./rollback-blue.sh
```

The deploy script:
1. Builds and starts green containers
2. Health checks green until it responds
3. Writes new nginx config pointing to green directly into the container
4. Reloads nginx gracefully (in-flight requests finish on blue)
5. Stops blue

---

## Running Tests

```bash
# Run all unit tests
npx nx run-many --target=test --projects=generator-agent,validation-agent

# Run tests for a single app
npx nx test validation-agent
npx nx test generator-agent
```

Tests cover:
- `SecurityValidator` — 8 cases including eval detection, hardcoded secrets, score floor
- `ValidationService` — parallel execution, crashed validator handling, score averaging
- `JobService` — repository calls, status transitions, data merging

---

## API Reference

### POST /api/jobs
Submit a new code generation job.

```json
// Request
{ "clientName": "string", "requirement": "string" }

// Response (immediate — job is queued)
{
"id": "uuid",
"clientName": "Demo",
"requirement": "payment module",
"status": "pending",
"generatedCode": null,
"errorMessage": null,
"createdAt": "2026-05-13T10:00:00.000Z",
"updatedAt": "2026-05-13T10:00:00.000Z"
}
```

### GET /api/jobs/:id
Poll for job result.

```json
// Response when complete
{
"id": "uuid",
"status": "done" | "failed",
"generatedCode": "import { Controller ... }",
"errorMessage": null | "Validation failed (score: 91). Issues: ..."
}
```

### GET /api/jobs
List all jobs ordered by creation date (newest first).

---

## Author

**Harris Gurung** — Senior Full Stack Engineer
12+ years building production systems across IoT, cloud, and enterprise platforms.