System architecture diagram
Component interaction summary
| Component | Talks to | Protocol | Purpose |
|---|---|---|---|
| Frontend | Backend API | HTTPS REST + SSE | All user actions: chat, vote, thread management |
| Frontend | Supabase Auth | HTTPS | User login/signup, obtain JWT |
| Admin Panel | Backend API | HTTPS REST (proxied via Worker) | CRUD operations on all entities |
| Backend API | Supabase/PostgreSQL | HTTPS (PostgREST) | Data persistence for all tables |
| Backend API | Groq | HTTPS | Primary AI chat completions |
| Backend API | Bytez | HTTPS | Secondary AI chat completions |
| Backend API | Supabase Auth | JWT validation | Verify user identity on each request |
Repositories
Backend API
DualMind_Back — .NET 8 REST API with JWT auth, AI provider gateway, and Supabase data access
Frontend
DualMind UI — Vanilla JS SPA with SSE streaming, served via Cloudflare Workers
Admin Panel
DualMind Admin UI — Cloudflare Worker dashboard with full CRUD for all entities
Backend architecture
The backend is a .NET 8 ASP.NET Core Web API located atsrc/DualMind.API/.
Directory structure
- src/DualMind.API/
- AI/
- Contracts/ — IChatProvider, ChatRequest, GroqResponse, AIStreamEvent
- Gateway/ — ChatProviderFactory routes to correct provider
- Providers/ — GroqService, BytezService implementations
- Controllers/
- Api/
- ArenaController.cs — POST /api/arena/chat, /dualchat, /chat/stream
- Admin/
- AdminDashboardController.cs
- AdminAIModelsController.cs
- AdminUsersController.cs
- AdminComparisonsController.cs
- AdminModelVotesController.cs
- AdminThreadsController.cs
- AdminProvidersController.cs
- ModelsController.cs — GET /api/models
- ThreadsController.cs — CRUD /api/threads
- UsersController.cs — POST /api/users/sync
- SettingsController.cs — GET /api/settings/feature-flag/
- HealthController.cs — GET /health
- SpeechController.cs — POST /api/speech/generate
- Api/
- Core/
- Exceptions/ — ProviderExhaustedException
- Models/ — User, AIModel, Comparison, ModelVote, Thread DTOs
- Services/ — ModelSelector, ThreadsService, etc.
- Infrastructure/
- Configuration/ — EnvConfig, SupabaseSettings
- Data/ — SupabaseService, AdminSupabaseClient
- Program.cs — App bootstrap, DI registration, middleware
- Dockerfile
- AI/
Key design patterns
- :
ChatProviderFactoryroutes requests to the correct provider based on model metadata. - Automatic fallback: If a provider fails, the system falls back to Groq with
llama-3.3-70b-versatile. If Groq fails, it tries an alternative Groq model. - : Automatic rotation on auth errors, rate limits, and cooldowns.
- :
UserSyncService.EnsureUserExistsAsync()is called before any data-writing operation.
Dependency injection (Program.cs)
Frontend architecture
The frontend is a vanilla JavaScript single-page application served via Cloudflare Workers.Directory structure
- DualMind UI/
- js/
- api/
- DualMindApi.js — Main API client facade
- config/ApiConfig.js — Base URL, timeout configuration
- core/HttpClient.js — Fetch wrapper with retry, auth headers
- services/
- ArenaService.js — chat(), dualChat(), streamChat()
- ThreadService.js — CRUD threads and messages
- ModelService.js — getModels()
- UserService.js — syncUser()
- app-final.js — Main application entry point
- arena-core.js — Arena battle UI logic
- api/
- components/
- chat/ChatView.js — Chat message rendering
- ChatInput.js — User input component
- Header.js — Top navigation
- ShareModal.js — Thread sharing dialog
- css/ — Stylesheets
- config.js — Global configuration (API URL, Supabase keys)
- index.html — Main entry HTML
- worker.js — Cloudflare Worker (serves static + proxies API)
- wrangler.jsonc — Cloudflare deployment config
- js/
Key patterns
- API client facade:
DualMindApiclass wraps all HTTP calls. Services likeArenaService,ThreadServiceencapsulate endpoint-specific logic. - SSE streaming: Uses
fetch()withReadableStream(notEventSource) for streaming chat responses. - Auth flow: Supabase JS client handles OAuth/email login. JWT stored in Supabase session, injected as
Authorization: Bearerheader on every API call. - Config-driven:
window.DUALMIND_CONFIGprovides runtime configuration for API base URL, streaming settings, timeouts, and feature flags.
Admin panel architecture
The admin panel is a separate Cloudflare Workers application.- DualMind Admin UI/
- public/
- js/
- api/ — Admin API client
- pages/ — Page-specific JS (users, models, etc.)
- utils.js — Shared utilities
- partials/
- sidebar.html — Navigation sidebar
- topbar.html — Top bar
- index.html — Dashboard page
- users.html — User management
- models.html — Model management
- comparisons.html — Comparison browser
- config.js — API base URL config
- auth-gate.js — Admin authentication check
- js/
- worker.js — Cloudflare Worker entry point
- wrangler.toml — Deployment config
- package.json
- public/
Admin worker proxy pattern
The admin Cloudflare Worker intercepts all requests:/api/*routes are proxied to the backend (BACKEND_URLenv var, default:https://api.dualmindlab.tech)- Static files are served from the
public/directory via theASSETSbinding - Extensionless paths fall back to
.htmlfiles (e.g.,/users→/users.html) - SPA fallback returns
index.htmlfor unmatched routes
Environment variables
- Backend (.NET API)
- Frontend
- Admin Panel
| Variable | Required | Description |
|---|---|---|
SUPABASE_URL | Yes | Supabase project URL (e.g., https://xxx.supabase.co) |
SUPABASE_SERVICE_ROLE_KEY | Yes | Service role key for server-side DB access |
SUPABASE_KEY | Fallback | Anon key (used if service role key not set) |
JWT_SECRET | Recommended | Supabase JWT secret for HS256 token validation |
GROQ_API_KEY | Optional | Groq API key (overrides database keys) |
Next steps
Request Lifecycle
Detailed execution flow from HTTP request to response
Backend Deep Dive
Controllers, services, data access, and middleware pipeline
Data Flow Diagrams
Mermaid diagrams for every major operation
Database Schema
Complete table definitions and ER diagram