aegisai / docs / INTEGRATION.md
INTEGRATION.md
Raw

πŸ”— Frontend-Backend Integration Guide

Complete guide to connecting AegisAI frontend and backend.


🎯 Two Deployment Modes

Mode 1: Client-Side (Current Default)

Browser β†’ Gemini API (Direct)

Pros:

  • βœ… Simpler setup (no backend needed)
  • βœ… Works immediately
  • βœ… Perfect for demos
  • βœ… No server costs

Cons:

  • ❌ API key exposed in browser
  • ❌ No incident storage
  • ❌ No multi-user support
  • ❌ Limited to browser capabilities

Use When:

  • Quick demos
  • Development
  • Single-user scenarios

Mode 2: Full Stack (Production)

Browser β†’ Backend API β†’ Gemini API
              ↓
         Database (SQLite)

Pros:

  • βœ… API key secured on server
  • βœ… Incidents stored in database
  • βœ… Multi-user support
  • βœ… Advanced features (actions, analytics)
  • βœ… Production-ready

Cons:

  • ❌ Requires backend setup
  • ❌ More complex deployment

Use When:

  • Production deployments
  • Multiple users
  • Need incident history
  • Enterprise use

πŸš€ Quick Start: Enable Full Stack

Step 1: Start Backend

cd backend
source venv/bin/activate  # Windows: venv\Scripts\activate
python main.py

Verify: http://localhost:8000/health should return:

{"status":"healthy","components":{...}}

Step 2: Enable Backend in Frontend

Edit frontend/src/constants.ts:

// Change this line:
ENABLE_BACKEND_API: false

// To this:
ENABLE_BACKEND_API: true

Step 3: Configure Backend URL

Edit frontend/.env.local:

VITE_API_URL=http://localhost:8000

Step 4: Start Frontend

cd frontend
npm run dev

Step 5: Verify Connection

Open http://localhost:3000

Look for two indicators in header:

  • SYSTEM ONLINE (green) - Frontend working
  • BACKEND (blue pulsing) - Connected to backend

If shows CLIENT-SIDE (orange) - Backend not connected


πŸ”§ Testing the Connection

Test 1: Health Check

curl http://localhost:8000/health

Expected:

{
  "status": "healthy",
  "components": {
    "database": "ok",
    "vision_agent": "ok",
    "planner_agent": "ok"
  }
}

Test 2: Analyze Endpoint

curl -X POST http://localhost:8000/api/analyze \
  -H "Content-Type: application/json" \
  -d '{"image":"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=="}'

Expected:

{
  "incident": false,
  "type": "normal",
  "severity": "low",
  "confidence": 80,
  "reasoning": "...",
  "subjects": [],
  "recommended_actions": []
}

Test 3: Frontend β†’ Backend Flow

  1. Open http://localhost:3000
  2. Check header shows BACKEND (blue)
  3. Activate monitoring
  4. Trigger threat
  5. Check backend logs:

Backend Terminal Should Show:

INFO: POST /api/analyze
INFO: Analysis complete: {"incident":true,...}

πŸ“Š Architecture Comparison

Client-Side Mode

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Browser (Port 3000)     β”‚
β”‚                          β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚   React Frontend   β”‚  β”‚
β”‚  β”‚                    β”‚  β”‚
β”‚  β”‚   ↓ (direct call)  β”‚  β”‚
β”‚  β”‚                    β”‚  β”‚
β”‚  β”‚   Gemini API       β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Data Flow:
1. Capture frame
2. Call Gemini API directly
3. Display result
4. Store in browser memory (lost on refresh)

Full Stack Mode

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Browser (Port 3000)  β”‚      β”‚  Backend (Port 8000) β”‚
β”‚                      β”‚      β”‚                      β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚      β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚ React Frontend β”‚  β”‚ POST β”‚  β”‚  FastAPI       β”‚  β”‚
β”‚  β”‚                β”œβ”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”Όβ”€β–Άβ”‚                β”‚  β”‚
β”‚  β”‚  (UI only)     β”‚  β”‚      β”‚  β”‚  Vision Agent  β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚      β”‚  β”‚      ↓         β”‚  β”‚
β”‚                      β”‚      β”‚  β”‚  Planner Agent β”‚  β”‚
β”‚                      β”‚      β”‚  β”‚      ↓         β”‚  β”‚
β”‚                      β”‚      β”‚  β”‚  Actions       β”‚  β”‚
β”‚                      β”‚      β”‚  β”‚      ↓         β”‚  β”‚
β”‚                      β”‚      β”‚  β”‚  Database      β”‚  β”‚
β”‚                      β”‚      β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Data Flow:
1. Capture frame
2. Send to backend API
3. Backend analyzes with Gemini
4. Backend saves to database
5. Backend executes actions
6. Return result to frontend
7. Display result

πŸ”„ Switching Between Modes

Switch to Client-Side

// frontend/src/constants.ts
ENABLE_BACKEND_API: false

Restart frontend:

npm run dev

Indicator: Shows CLIENT-SIDE (orange)


Switch to Full Stack

// frontend/src/constants.ts
ENABLE_BACKEND_API: true

Start backend first:

cd backend && python main.py

Then frontend:

cd frontend && npm run dev

Indicator: Shows BACKEND (blue)


πŸ› Troubleshooting

Issue 1: Shows CLIENT-SIDE but Backend is Running

Check:

  1. Backend URL in .env.local:
    VITE_API_URL=http://localhost:8000
    
  2. CORS is enabled (should be by default)
  3. No firewall blocking port 8000

Test:

curl http://localhost:8000/health

Issue 2: CORS Errors

Error: Access-Control-Allow-Origin

Fix: Ensure backend .env has:

CORS_ORIGINS=["http://localhost:3000","http://localhost:5173"]

Restart backend.


Issue 3: 404 on /api/analyze

Fix: Update backend to latest version with analyze endpoint.

Check:

curl http://localhost:8000/docs

Should list /api/analyze endpoint.


Issue 4: Backend Not Responding

Check:

# Is it running?
curl http://localhost:8000/

# Check logs
# (Look at terminal where you ran python main.py)

Restart:

cd backend
python main.py

πŸ“ˆ Feature Comparison

Feature Client-Side Full Stack
Threat Detection βœ… βœ…
Real-time Analysis βœ… βœ…
Incident Storage ❌ βœ…
Action Execution ❌ βœ…
Multi-user ❌ βœ…
API Security ❌ βœ…
Database ❌ βœ…
Email Alerts ❌ βœ…
Historical Data ❌ βœ…
Load Balancing ❌ βœ…

βœ… Verification Checklist

Client-Side Mode

  • Frontend runs on port 3000
  • Shows CLIENT-SIDE in header
  • Threat detection works
  • Dashboard updates
  • No backend required

Full Stack Mode

  • Backend runs on port 8000
  • Frontend runs on port 3000
  • Shows BACKEND in header (blue)
  • /health returns healthy
  • Threat detection works
  • Incidents saved to database
  • Can query incidents via API

πŸš€ Production Deployment

Client-Side

# Build frontend only
cd frontend
npm run build

# Deploy to Vercel
vercel --prod

Environment Variables (Vercel):

  • VITE_GEMINI_API_KEY = your_key

Full Stack

Backend β†’ Render:

git push render main

Frontend β†’ Vercel:

cd frontend
vercel --prod

Environment Variables:

Render (Backend):

  • GEMINI_API_KEY = your_key

Vercel (Frontend):


πŸ“ Summary

Aspect Current State After Integration
Default Mode Client-Side Client-Side
Can Use Backend ❌ No βœ… Yes
Toggle N/A ENABLE_BACKEND_API flag
Indicator None Blue "BACKEND" or Orange "CLIENT-SIDE"
Flexibility One mode only Switch anytime

Key Point: You can now use BOTH modes! Switch by changing one flag in constants.ts.


Integration Complete! πŸŽ‰