Skip to content

ovishkh/anygpt

Repository files navigation

AnyGPT - Multi-API Intelligent Chat Platform

πŸ—οΈ System Architecture Overview

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                             CLIENT LAYER                                β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  Next.js 14+ (App Router) β”‚  React 18+ β”‚  TypeScript β”‚ TailwindCSS     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  Components:                                                             β”‚
β”‚  β”œβ”€β”€ Chat Interface                                                      β”‚
β”‚  β”œβ”€β”€ Provider Dashboard                                                  β”‚
β”‚  β”œβ”€β”€ Real-time Analytics                                                 β”‚
β”‚  β”œβ”€β”€ Admin Panel                                                         β”‚
β”‚  └── Theme System                                                        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                   β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                            EDGE LAYER                                   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  Vercel Edge Functions β”‚  Middleware β”‚  Rate Limiting β”‚  Caching        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  Routes:                                                                β”‚
β”‚  β”œβ”€β”€ /api/chat/* (Streaming)                                            β”‚
β”‚  β”œβ”€β”€ /api/admin/* (Protected)                                           β”‚
β”‚  β”œβ”€β”€ /api/analytics/*                                                   β”‚
β”‚  └── /api/webhooks/*                                                    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                   β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         API GATEWAY LAYER                               β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  OpenRouter Integration β”‚  Load Balancer β”‚  Fallback System             β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  Features:                                                               β”‚
β”‚  β”œβ”€β”€ Intelligent API routing                                             β”‚
β”‚  β”œβ”€β”€ Cost optimization                                                   β”‚
β”‚  β”œβ”€β”€ Automatic failover                                                  β”‚
β”‚  └── Model compatibility layer                                           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                   β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                       PROVIDER ABSTRACTION LAYER                        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  OpenAI β”‚  Anthropic β”‚  Google Gemini β”‚  OpenRouter β”‚  Custom LLMs      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  Unified Interface:                                                      β”‚
β”‚  β”œβ”€β”€ Standardized message format                                         β”‚
β”‚  β”œβ”€β”€ Token counting                                                      β”‚
β”‚  β”œβ”€β”€ Cost calculation                                                    β”‚
β”‚  └── Error handling                                                      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                   β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         DATA LAYER                                      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  PostgreSQL/Neon β”‚  Redis β”‚  Vector DB β”‚  Blob Storage                  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  Models:                                                                 β”‚
β”‚  β”œβ”€β”€ User & Authentication                                              β”‚
β”‚  β”œβ”€β”€ Conversations & Messages                                           β”‚
β”‚  β”œβ”€β”€ API Keys (Encrypted)                                               β”‚
β”‚  β”œβ”€β”€ Usage Analytics                                                    β”‚
β”‚  └── Vector Embeddings                                                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“ Complete Project Structure

anygpt/
β”œβ”€β”€ πŸ“¦ package.json
β”œβ”€β”€ 🐳 docker-compose.yml
β”œβ”€β”€ πŸ“ src/
β”‚   β”œβ”€β”€ πŸ“ app/
β”‚   β”‚   β”œβ”€β”€ πŸ“ (auth)/
β”‚   β”‚   β”‚   β”œβ”€β”€ login/
β”‚   β”‚   β”‚   β”œβ”€β”€ register/
β”‚   β”‚   β”‚   β”œβ”€β”€ forgot-password/
β”‚   β”‚   β”‚   └── layout.tsx
β”‚   β”‚   β”œβ”€β”€ πŸ“ (dashboard)/
β”‚   β”‚   β”‚   β”œβ”€β”€ layout.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ page.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“ chat/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ [conversationId]/
β”‚   β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ page.tsx
β”‚   β”‚   β”‚   β”‚   β”‚   └── loading.tsx
β”‚   β”‚   β”‚   β”‚   └── new/
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“ providers/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ configure/
β”‚   β”‚   β”‚   β”‚   └── manage/
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“ analytics/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ usage/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ costs/
β”‚   β”‚   β”‚   β”‚   └── performance/
β”‚   β”‚   β”‚   └── πŸ“ settings/
β”‚   β”‚   β”‚       β”œβ”€β”€ api-keys/
β”‚   β”‚   β”‚       β”œβ”€β”€ preferences/
β”‚   β”‚   β”‚       └── billing/
β”‚   β”‚   β”œβ”€β”€ πŸ“ api/
β”‚   β”‚   β”‚   β”œβ”€β”€ auth/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ [...nextauth]/
β”‚   β”‚   β”‚   β”‚   └── callback/
β”‚   β”‚   β”‚   β”œβ”€β”€ chat/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ route.ts (Main gateway)
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“ providers/
β”‚   β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ openai/
β”‚   β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ anthropic/
β”‚   β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ gemini/
β”‚   β”‚   β”‚   β”‚   β”‚   └── openrouter/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“ stream/
β”‚   β”‚   β”‚   β”‚   └── πŸ“ batch/
β”‚   β”‚   β”‚   β”œβ”€β”€ admin/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ providers/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ users/
β”‚   β”‚   β”‚   β”‚   └── system/
β”‚   β”‚   β”‚   β”œβ”€β”€ analytics/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ realtime/
β”‚   β”‚   β”‚   β”‚   └── historical/
β”‚   β”‚   β”‚   β”œβ”€β”€ webhooks/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ stripe/
β”‚   β”‚   β”‚   β”‚   └── monitoring/
β”‚   β”‚   β”‚   └── edge/
β”‚   β”‚   β”‚       β”œβ”€β”€ rate-limit/
β”‚   β”‚   β”‚       └── cache/
β”‚   β”‚   β”œβ”€β”€ πŸ“ admin/ (Protected)
β”‚   β”‚   β”‚   β”œβ”€β”€ dashboard/
β”‚   β”‚   β”‚   β”œβ”€β”€ provider-management/
β”‚   β”‚   β”‚   └── user-management/
β”‚   β”‚   β”œβ”€β”€ layout.tsx
β”‚   β”‚   β”œβ”€β”€ page.tsx
β”‚   β”‚   β”œβ”€β”€ globals.css
β”‚   β”‚   └── middleware.ts
β”‚   β”œβ”€β”€ πŸ“ components/
β”‚   β”‚   β”œβ”€β”€ πŸ“ ui/ (shadcn/ui)
β”‚   β”‚   β”‚   β”œβ”€β”€ button/
β”‚   β”‚   β”‚   β”œβ”€β”€ card/
β”‚   β”‚   β”‚   β”œβ”€β”€ dialog/
β”‚   β”‚   β”‚   └── ...
β”‚   β”‚   β”œβ”€β”€ πŸ“ chat/
β”‚   β”‚   β”‚   β”œβ”€β”€ ChatInterface.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ MessageList.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ MessageBubble.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ ProviderBadge.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ ModelSelector.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ TokenCounter.tsx
β”‚   β”‚   β”‚   └── StreamDisplay.tsx
β”‚   β”‚   β”œβ”€β”€ πŸ“ providers/
β”‚   β”‚   β”‚   β”œβ”€β”€ ProviderCard.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ APIToggle.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ ModelGrid.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ OpenRouterPanel.tsx
β”‚   β”‚   β”‚   └── ProviderThemes.tsx
β”‚   β”‚   β”œβ”€β”€ πŸ“ dashboard/
β”‚   β”‚   β”‚   β”œβ”€β”€ StatsOverview.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ UsageChart.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ CostCalculator.tsx
β”‚   β”‚   β”‚   └── RealTimeMetrics.tsx
β”‚   β”‚   β”œβ”€β”€ πŸ“ forms/
β”‚   β”‚   β”‚   β”œβ”€β”€ APIKeyForm.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ ProviderConfigForm.tsx
β”‚   β”‚   β”‚   └── SettingsForm.tsx
β”‚   β”‚   └── πŸ“ layout/
β”‚   β”‚       β”œβ”€β”€ Sidebar.tsx
β”‚   β”‚       β”œβ”€β”€ Header.tsx
β”‚   β”‚       └── ThemeToggle.tsx
β”‚   β”œβ”€β”€ πŸ“ lib/
β”‚   β”‚   β”œβ”€β”€ πŸ“ providers/
β”‚   β”‚   β”‚   β”œβ”€β”€ index.ts (Factory)
β”‚   β”‚   β”‚   β”œβ”€β”€ base/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ BaseProvider.ts
β”‚   β”‚   β”‚   β”‚   └── types.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ openai/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ client.ts
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ models.ts
β”‚   β”‚   β”‚   β”‚   └── adapter.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ anthropic/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ client.ts
β”‚   β”‚   β”‚   β”‚   └── adapter.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ gemini/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ client.ts
β”‚   β”‚   β”‚   β”‚   └── adapter.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ openrouter/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ client.ts
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ models.ts
β”‚   β”‚   β”‚   β”‚   └── router.ts
β”‚   β”‚   β”‚   └── custom/
β”‚   β”‚   β”‚       └── provider-builder.ts
β”‚   β”‚   β”œβ”€β”€ πŸ“ api/
β”‚   β”‚   β”‚   β”œβ”€β”€ gateway/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ router.ts
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ load-balancer.ts
β”‚   β”‚   β”‚   β”‚   └── fallback.ts
β”‚   β”‚   β”‚   └── streaming/
β”‚   β”‚   β”‚       β”œβ”€β”€ sse.ts
β”‚   β”‚   β”‚       └── web-socket.ts
β”‚   β”‚   β”œβ”€β”€ πŸ“ utils/
β”‚   β”‚   β”‚   β”œβ”€β”€ encryption.ts (AES-256)
β”‚   β”‚   β”‚   β”œβ”€β”€ token-counter.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ cost-calculator.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ rate-limiter.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ logger.ts
β”‚   β”‚   β”‚   └── validation.ts
β”‚   β”‚   β”œβ”€β”€ πŸ“ db/
β”‚   β”‚   β”‚   β”œβ”€β”€ schema/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ index.ts
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ user.ts
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ conversation.ts
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ provider.ts
β”‚   β”‚   β”‚   β”‚   └── analytics.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ queries/
β”‚   β”‚   β”‚   └── migrations/
β”‚   β”‚   β”œβ”€β”€ πŸ“ auth/
β”‚   β”‚   β”‚   β”œβ”€β”€ config.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ middleware.ts
β”‚   β”‚   β”‚   └── permissions.ts
β”‚   β”‚   └── πŸ“ types/
β”‚   β”‚       β”œβ”€β”€ global.d.ts
β”‚   β”‚       β”œβ”€β”€ chat.ts
β”‚   β”‚       β”œβ”€β”€ providers.ts
β”‚   β”‚       └── api.ts
β”‚   β”œβ”€β”€ πŸ“ hooks/
β”‚   β”‚   β”œβ”€β”€ useChat.ts
β”‚   β”‚   β”œβ”€β”€ useProviders.ts
β”‚   β”‚   β”œβ”€β”€ useAnalytics.ts
β”‚   β”‚   β”œβ”€β”€ useWebSocket.ts
β”‚   β”‚   β”œβ”€β”€ useDebounce.ts
β”‚   β”‚   └── useLocalStorage.ts
β”‚   β”œβ”€β”€ πŸ“ store/
β”‚   β”‚   β”œβ”€β”€ chat.store.ts
β”‚   β”‚   β”œβ”€β”€ provider.store.ts
β”‚   β”‚   β”œβ”€β”€ user.store.ts
β”‚   β”‚   β”œβ”€β”€ ui.store.ts
β”‚   β”‚   └── analytics.store.ts
β”‚   β”œβ”€β”€ πŸ“ styles/
β”‚   β”‚   β”œβ”€β”€ themes/
β”‚   β”‚   β”‚   β”œβ”€β”€ openai.css
β”‚   β”‚   β”‚   β”œβ”€β”€ anthropic.css
β”‚   β”‚   β”‚   β”œβ”€β”€ gemini.css
β”‚   β”‚   β”‚   └── openrouter.css
β”‚   β”‚   └── animations.css
β”‚   └── πŸ“ public/
β”‚       β”œβ”€β”€ themes/
β”‚       β”œβ”€β”€ icons/
β”‚       └── fonts/
β”œβ”€β”€ πŸ“ prisma/
β”‚   β”œβ”€β”€ schema.prisma
β”‚   β”œβ”€β”€ migrations/
β”‚   └── seed.ts
β”œβ”€β”€ πŸ“ tests/
β”‚   β”œβ”€β”€ unit/
β”‚   β”œβ”€β”€ integration/
β”‚   └── e2e/
β”œβ”€β”€ πŸ“ scripts/
β”‚   β”œβ”€β”€ deploy/
β”‚   β”œβ”€β”€ backup/
β”‚   └── monitoring/
β”œβ”€β”€ πŸ“ config/
β”‚   β”œβ”€β”€ env.ts
β”‚   β”œβ”€β”€ providers.json
β”‚   └── rate-limits.json
β”œβ”€β”€ .env.example
β”œβ”€β”€ .env.local
β”œβ”€β”€ tailwind.config.ts
β”œβ”€β”€ next.config.js
β”œβ”€β”€ tsconfig.json
└── README.md

πŸ”Œ OpenRouter Integration Architecture

OpenRouter Integration Flow:
1. User Request β†’ AnyGPT API Gateway
2. Gateway analyzes request:
   β”œβ”€β”€ Required capabilities
   β”œβ”€β”€ Cost constraints
   β”œβ”€β”€ Performance needs
   └── User preferences
3. Decision Engine:
   β”œβ”€β”€ Use direct provider (if enabled & available)
   β”œβ”€β”€ Use OpenRouter (if better match/fallback)
   └── Load balance between options
4. OpenRouter Router:
   β”œβ”€β”€ Model selection (100+ models)
   β”œβ”€β”€ Automatic retries
   β”œβ”€β”€ Cost optimization
   └── Unified response format

πŸ—„οΈ Database Schema Core Tables

// Core Models
model User {
  id            String        @id @default(cuid())
  email         String        @unique
  name          String?
  avatar        String?
  role          UserRole      @default(USER)
  tier          UserTier      @default(FREE)
  
  // Relationships
  apiKeys       ApiKey[]
  conversations Conversation[]
  settings      UserSettings?
  usage         UsageStats[]
  
  // Timestamps
  createdAt     DateTime      @default(now())
  updatedAt     DateTime      @updatedAt
}

model Provider {
  id            String        @id @default(cuid())
  name          ProviderType  // openai, anthropic, gemini, openrouter
  displayName   String
  enabled       Boolean       @default(true)
  config        Json?         // Provider-specific config
  
  // Models available for this provider
  models        Model[]
  
  // Usage tracking
  totalRequests Int           @default(0)
  totalTokens   BigInt        @default(0)
  totalCost     Float         @default(0)
  
  // Rate limiting
  rateLimit     Int           @default(1000)
  rateWindow    Int           @default(3600) // seconds
}

model ApiKey {
  id            String        @id @default(cuid())
  userId        String
  providerId    String
  name          String
  key           String        @encrypted
  enabled       Boolean       @default(true)
  lastUsed      DateTime?
  usageCount    Int           @default(0)
  
  // Relationships
  user          User          @relation(fields: [userId], references: [id])
  provider      Provider      @relation(fields: [providerId], references: [id])
  
  createdAt     DateTime      @default(now())
}

model Conversation {
  id            String        @id @default(cuid())
  userId        String
  title         String        @default("New Conversation")
  providerId    String
  model         String
  tokensUsed    Int           @default(0)
  estimatedCost Float         @default(0)
  
  // Messages (1:N)
  messages      Message[]
  
  // Metadata
  tags          String[]      @default([])
  archived      Boolean       @default(false)
  starred       Boolean       @default(false)
  
  createdAt     DateTime      @default(now())
  updatedAt     DateTime      @updatedAt
}

model Message {
  id             String       @id @default(cuid())
  conversationId String
  role           MessageRole  // system, user, assistant
  content        String
  tokens         Int
  model          String?
  provider       ProviderType?
  cost           Float?
  
  // Metadata
  processingTime Int?         // milliseconds
  error          String?
  
  createdAt      DateTime     @default(now())
}

// OpenRouter Specific
model OpenRouterRequest {
  id            String       @id @default(cuid())
  conversationId String
  model         String
  route         String       // which model was actually used
  cost          Float
  latency       Int
  success       Boolean
  fallbackUsed  Boolean      @default(false)
  
  createdAt     DateTime     @default(now())
}

// Analytics
model UsageAnalytics {
  id            String       @id @default(cuid())
  userId        String
  date          DateTime
  provider      ProviderType
  model         String
  requests      Int
  tokens        Int
  cost          Float
  
  @@index([userId, date])
}

πŸ”„ API Gateway Flow

Request Flow:
1. Request β†’ API Gateway (/api/chat)
2. Authentication & Authorization
3. Rate Limiting Check
4. Request Analysis:
   β”œβ”€β”€ Extract provider preference
   β”œβ”€β”€ Check provider availability
   β”œβ”€β”€ Validate model compatibility
   └── Apply user quotas
5. Provider Selection Logic:
   β”œβ”€β”€ If specific provider requested β†’ use it
   β”œβ”€β”€ If OpenRouter preferred β†’ route through OpenRouter
   β”œβ”€β”€ If fallback needed β†’ use OpenRouter as backup
   └── If load balancing β†’ distribute requests
6. Request Processing:
   β”œβ”€β”€ Format conversion
   β”œβ”€β”€ Token counting
   β”œβ”€β”€ Cost estimation
   └── Stream handling
7. Response:
   β”œβ”€β”€ Standardized format
   β”œβ”€β”€ Analytics logging
   β”œβ”€β”€ Real-time updates
   └── Error handling

πŸ”§ Provider Configuration System

// Provider Configuration Interface
interface ProviderConfig {
  id: 'openai' | 'anthropic' | 'gemini' | 'openrouter' | 'custom';
  name: string;
  enabled: boolean;
  apiKey?: string; // Encrypted
  baseURL?: string;
  models: {
    id: string;
    name: string;
    enabled: boolean;
    capabilities: string[]; // ['chat', 'vision', 'function-calling']
    contextWindow: number;
    maxTokens: number;
    cost: {
      input: number; // per 1K tokens
      output: number; // per 1K tokens
    };
  }[];
  rateLimit: {
    requests: number;
    tokens: number;
    window: number; // seconds
  };
  features: {
    streaming: boolean;
    vision: boolean;
    functionCalling: boolean;
    jsonMode: boolean;
  };
  ui: {
    theme: string;
    icon: string;
    color: string;
  };
}

// OpenRouter Specific Config
interface OpenRouterConfig extends ProviderConfig {
  routing: {
    strategy: 'cost' | 'performance' | 'quality';
    fallbackProviders: string[];
    costLimit: number;
    timeout: number;
  };
  models: Array<{
    id: string; // OpenRouter model ID
    provider: string; // Original provider
    name: string;
    // ... other model config
  }>;
}

🎨 Theming System Architecture

Theming Layers:
1. Base Theme (Tailwind)
2. Provider Themes:
   β”œβ”€β”€ OpenAI: Green gradient, clean
   β”œβ”€β”€ Anthropic: Purple gradient, elegant
   β”œβ”€β”€ Gemini: Blue gradient, modern
   └── OpenRouter: Orange gradient, dynamic
3. User Preferences:
   β”œβ”€β”€ Dark/Light mode
   β”œβ”€β”€ Compact/Comfortable
   └── Custom colors
4. Dynamic Elements:
   β”œβ”€β”€ Message bubbles
   β”œβ”€β”€ Loading states
   β”œβ”€β”€ Buttons & inputs
   └── Status indicators

πŸ“Š Analytics & Monitoring

Monitoring Stack:
1. Real-time Metrics:
   β”œβ”€β”€ Request rate
   β”œβ”€β”€ Token usage
   β”œβ”€β”€ Response times
   β”œβ”€β”€ Error rates
   └── Cost tracking

2. Provider Health:
   β”œβ”€β”€ Uptime monitoring
   β”œβ”€β”€ Latency tracking
   β”œβ”€β”€ Success rates
   └── Cost efficiency

3. User Analytics:
   β”œβ”€β”€ Usage patterns
   β”œβ”€β”€ Model preferences
   β”œβ”€β”€ Feature usage
   └── Retention metrics

4. Business Metrics:
   β”œβ”€β”€ Cost per user
   β”œβ”€β”€ Provider costs
   β”œβ”€β”€ ROI calculation
   └── Growth tracking

πŸ” Security Architecture

Security Layers:
1. Encryption:
   β”œβ”€β”€ API Keys (AES-256)
   β”œβ”€β”€ Database fields
   └── Communication

2. Authentication:
   β”œβ”€β”€ NextAuth.js
   β”œβ”€β”€ OAuth providers
   β”œβ”€β”€ API key authentication
   └── Session management

3. Authorization:
   β”œβ”€β”€ Role-based access
   β”œβ”€β”€ Provider permissions
   β”œβ”€β”€ Rate limiting
   └── Request validation

4. Monitoring:
   β”œβ”€β”€ Audit logs
   β”œβ”€β”€ Suspicious activity
   β”œβ”€β”€ API abuse detection
   └── Compliance tracking

πŸš€ Deployment Architecture

Multi-Environment Setup:
1. Development:
   β”œβ”€β”€ Local Docker Compose
   β”œβ”€β”€ Hot reload
   └── Mock providers

2. Staging:
   β”œβ”€β”€ Vercel Preview
   β”œβ”€β”€ Test database
   └── Limited API access

3. Production:
   β”œβ”€β”€ Vercel Production
   β”œβ”€β”€ PostgreSQL (Neon)
   β”œβ”€β”€ Redis (Upstash)
   β”œβ”€β”€ Object storage
   └── CDN for static assets

4. Backup:
   β”œβ”€β”€ Automated backups
   β”œβ”€β”€ Point-in-time recovery
   └── Disaster recovery plan

πŸ“ˆ Scaling Strategy

Horizontal Scaling:
1. Stateless API servers
2. Database connection pooling
3. Redis cache for sessions
4. CDN for static assets
5. Queue system for async tasks

Vertical Scaling:
1. Database optimization
2. Query optimization
3. Caching strategy
4. Asset optimization

Cost Optimization:
1. Provider cost comparison
2. Token usage optimization
3. Caching responses
4. Batch processing

This architecture provides a production-ready, scalable, and maintainable foundation for AnyGPT with complete OpenRouter integration and enterprise-grade features.

About

AnyGPT - Multi-API Intelligent Chat Platform

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages