AI Agent System

7 specialized AI agents powered by Cloudflare Workers AI. Agent swarm orchestrator dispatches queries to the right specialist. RAG pipeline via Vectorize for legal/bylaw search.

Architecture

POST /v1/ai/copilot/swarm
    │
    ├─→ aiOrchestrator.ts (src/services/aiOrchestrator.ts)
    │   │
    │   ├─→ Route to specialist agent based on query intent
    │   ├─→ Execute tools (D1 queries, R2 presigned URLs, etc.)
    │   ├─→ Optional: request human approval before actions
    │   └─→ Log execution to ai_agent_logs table
    │
    ├─→ POST /v1/ai/rag/query
    │   └─→ Vectorize.similarity_search() → Workers AI generate()
    │
    └─→ POST /v1/ai/queue/event
        └─→ Cloudflare Queue → async processing

7 AI Agents

AgentRoleCapabilitiesTools
AccountantFinancial operationsInvoice generation, payment tracking, GST/TDS compliance, journal entries, bank reconciliationD1 queries, accounting engine, GST calculator
SecretaryAdministrative opsNotice drafting, meeting scheduling, agenda preparation, minutes generationD1 queries, WhatsApp sender, email
ComplianceRegulatory complianceGST return prep, TDS filing, audit trail review, DPDP complianceD1 queries, GST/TDS engines, audit log reader
CommunicationMessaging & alertsBroadcast notices, payment reminders, emergency alerts, visitor notificationsWhatsApp API, push notifications, email
AnalyticsData analysisFinancial reports, defaulter analysis, trend detection, anomaly flaggingD1 queries, Workers AI, CSV export
GatekeeperSecurity operationsANPR matching, visitor auto-checkout, RFID verification, boom barrier controlDO WebSocket, ANPR webhook, RFID webhook
EmergencyEmergency responseSOS broadcasting, lockdown management, escalation to authorities, first-responder alertsDO WebSocket, SMS/WhatsApp blast, incident logging

AI Agent DB Tables

TablePurpose
ai_agent_logsExecution log: agent_name, action, input, output, tokens_used, cost_estimate, status
ai_agent_memoriesPersistent memory: agent_name, memory_key, memory_value_json, embedding_vector, ttl_expiry
ai_agent_toolsTool registry: tool_name, handler_endpoint, requires_approval, is_enabled
society_tasksTask queue: assigned_to_type (AI_AGENT/EMPLOYEE/VENDOR), ai_agent_type, status workflow

RAG Pipeline

// POST /v1/ai/rag/query
{
  "query": "What is the society's pet policy according to bylaws?",
  "model": "@cf/meta/llama-3.1-8b-instruct"
}

// Pipeline:
// 1. Embed query via Workers AI embedding model
// 2. Vectorize.similarity_search(query_embedding, topK=5)
// 3. Retrieve matching document chunks from ai_agent_memories
// 4. Generate answer with context via Workers AI
// 5. Return { answer, sources[] }

Task Workflow

society_tasks status flow:
  TODO → IN_PROGRESS → AI_PROCESSING → PENDING_REVIEW → COMPLETED
                                                  ↓
                                             CANCELLED

assigned_to_type:
  AI_AGENT    — processed by specialist agent
  COMMITTEE   — manual review by committee member
  EMPLOYEE    — assigned to society staff
  VENDOR      — assigned to external vendor
  SELF        — self-service by resident