Skip to main content

Technology summary

Controller map

Every controller inherits from ControllerBase (no views). Routes use attribute routing.

Public API controllers

Admin API controllers

All admin controllers are under /api/admin/ and use IAdminSupabaseClient for data access with service role key.

ArenaController — the core

The ArenaController (Controllers/Api/ArenaController.cs) is the heart of DualMind. It handles all chat operations.

Dependencies injected

Single chat flow (POST /api/arena/chat)

  1. Validate prompt is not empty
  2. Select model: if request.Model is "auto" or null, call _modelSelector.GetRandomModelAsync(); otherwise use the specified model
  3. Call ExecuteWithFallbackAsync(model, prompt, system, maxTokens, temperature)
  4. Build ChatResponse with output content, model info, usage stats, response time
  5. Log message via _messageLogger.LogMessageAsync()
  6. If request.ThreadId is set, persist to thread via _threadMessagesService.LogSingleAsync()
  7. Return response

Dual chat flow (POST /api/arena/dualchat)

  1. Validate prompt
  2. Determine selection mode:
    • Manual: Both model1 and model2 specified by client
    • Topper: _leaderboardModelSelector.GetTopperAndRandomModelAsync() — top-rated model vs random
    • Random (default): _modelSelector.GetTwoRandomModelsAsync()
  3. Execute both models in parallel via Task.WhenAll(task1, task2)
  4. Build two ChatResponse objects
  5. Log both messages and the comparison
  6. Compute arena metrics (winner by length, winner by tokens, verdict)
  7. Return { agent1, agent2, comparisonId, arena: { comparison, models } }

Fallback logic (ExecuteWithFallbackAsync)

Service layer

All business logic is in Core/Services/. Services are registered as Scoped except ModelSelector which is Singleton.

Data access layer

DualMind does not use Entity Framework. It makes direct HTTP calls to the Supabase PostgREST API.

ISupabaseService (user-facing)

Used by public controllers. Configured with service role key in HttpClient default headers.

IAdminSupabaseClient (admin-facing)

Used by admin controllers. Provides generic CRUD operations:

Middleware pipeline

Configured in Program.cs, executed in order:
  1. Exception handler — catches unhandled exceptions, returns ProblemDetails JSON
  2. Request logging — logs correlation ID, method, path, duration, status code
  3. CORSAllowAll policy (any origin, method, header)
  4. HTTPS redirection
  5. Authentication — JWT Bearer validation
  6. Authorization[Authorize] attribute enforcement
  7. Controller routingapp.MapControllers()

Error response format

All errors follow a consistent shape:
Error codes: INVALID_REQUEST, API_ERROR, STREAM_ERROR, THREADS_ERROR, THREAD_CREATE_ERROR, MODELS_ERROR, NOT_FOUND, UNAUTHORIZED, FORBIDDEN.