Skip to content

SpektraBot Architecture Overview โ€‹

Comprehensive developer-facing documentation for the SpektraBot SEND Support System. This page covers the system architecture, technology stack, database design, AI/RAG pipeline, and API design.


1. Overview โ€‹

SpektraBot is a GDPR-compliant AI assistant that helps UK families and education professionals navigate the Special Educational Needs and Disabilities (SEND) system. It provides real-time, context-aware guidance grounded in authoritative sources including the SEND Code of Practice, local authority Local Offers, national charity resources, and school SEND policies.

Target users:

  • Parents and Carers -- Understanding SEND processes, EHCP applications, rights and entitlements
  • Teachers and SENCOs -- SEND Code of Practice guidance, IEP development, professional documentation
  • Administrators -- System management, analytics, knowledge base curation, crawler management, support tickets

Deployment model: Cloud-native on Azure Container Apps (UK South, UK data residency). Container images are pushed to Azure Container Registry (spektrabotacr.azurecr.io).

Brain documentation has moved. The deep technical content on prompts, message flow, agentic-RAG, retrieval, knowledge-base ingestion, safety / grounding, and evaluation now lives under The Brain. This page is now the system-wide architecture summary and keeps only the non-brain subsystems (auth, GDPR, deployment, admin).


2. Architecture โ€‹

SpektraBot follows a monorepo architecture with a Next.js frontend, Express API backend, PostgreSQL database with pgvector for semantic search, Redis for caching and WebSocket coordination, and Azure OpenAI GPT-4.1 (UK South) for language model inference.


3. Technology Stack โ€‹

LayerTechnologyVersion
FrontendNext.js (App Router)15.5.7
UI FrameworkReact19.0.1
UI Componentsshadcn/ui, Radix UI, Recharts--
Iconslucide-react0.468
StylingTailwind CSS3.4
Backend RuntimeNode.js22
Backend LanguageTypeScript5.6+
HTTP FrameworkExpress4.21
WebSocketSocket.IO + Redis Adapter4.8.1
ORMPrisma6.18
DatabasePostgreSQL + pgvector16
CacheRedis7.4
AI LLMAzure OpenAI GPT-4.1 (UK South)--
AI EmbeddingsAzure OpenAI text-embedding-3-large (1536d)--
AI FrameworkLangChain v1.x + LangGraph1.0.2
TestingVitest + Supertest2.1
DeploymentAzure Container Apps (UK South)--
Container RegistryAzure Container Registry (spektrabotacr.azurecr.io)--
EmailResend + React Email--
AnalyticsUmami (privacy-focused)--
MonitoringPrometheus + Grafanakube-prometheus-stack 81.6
Monorepopnpm workspaces9+

4. Monorepo Structure โ€‹

pilot/
โ”œโ”€โ”€ packages/
โ”‚   โ”œโ”€โ”€ api/                       # Node.js backend API
โ”‚   โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ admin/             # Admin routes, service, analytics, crawler
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ auth/              # JWT auth, registration, password reset
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ chat/              # WebSocket, AI service, agentic RAG, langchain
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ config/            # Zod-validated configuration
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ crawler/           # Web crawler worker, URL validation, content extraction
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ school-policy/ # School policy crawler worker
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ gdpr/              # GDPR compliance directory
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ knowledge-base/    # Document upload, chunking, embedding, validation
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ middleware/        # Auth, rate limiting, GDPR consent
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ observability/     # Prometheus metrics, HTTP instrumentation, LLM metrics
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ preferences/       # User preference routes and service
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ prompts/           # Modular prompt system, admin management
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ services/          # Shared services (analytics, children, email, GIAS, etc.)
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ support/           # Support ticket routes and service
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ types/             # TypeScript type definitions
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ utils/             # Logger (Pino), DB client, Redis client
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ index.ts           # Express server entry point
โ”‚   โ”‚   โ”œโ”€โ”€ prisma/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ schema.prisma      # Database schema (37 models)
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ seed.ts            # Database seed script
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ migrations/        # Prisma migration history
โ”‚   โ”‚   โ””โ”€โ”€ tests/                 # Test suite (30+ test files)
โ”‚   โ”‚       โ”œโ”€โ”€ unit/              # Unit tests (auth, chat, admin, config, etc.)
โ”‚   โ”‚       โ”œโ”€โ”€ integration/       # Integration tests (RAG, schools, user isolation)
โ”‚   โ”‚       โ”œโ”€โ”€ infrastructure/    # DB persistence, pgvector tests
โ”‚   โ”‚       โ””โ”€โ”€ services/          # Service-level tests (children, email, waitlist)
โ”‚   โ”‚
โ”‚   โ””โ”€โ”€ web/                       # Next.js frontend
โ”‚       โ””โ”€โ”€ src/
โ”‚           โ”œโ”€โ”€ app/               # App Router pages (45+ routes)
โ”‚           โ”‚   โ”œโ”€โ”€ admin/         # Admin dashboard (18 sub-pages)
โ”‚           โ”‚   โ”œโ”€โ”€ chat/          # AI chat interface
โ”‚           โ”‚   โ”œโ”€โ”€ children/      # Child profile management
โ”‚           โ”‚   โ”œโ”€โ”€ settings/      # User preferences
โ”‚           โ”‚   โ”œโ”€โ”€ support/       # Support ticket pages
โ”‚           โ”‚   โ””โ”€โ”€ ...            # Public pages, auth flows
โ”‚           โ”œโ”€โ”€ components/        # Reusable components
โ”‚           โ”‚   โ”œโ”€โ”€ charts/        # Recharts-based analytics charts (7+ types)
โ”‚           โ”‚   โ”œโ”€โ”€ chat/          # Chat UI components + HelpDrawer
โ”‚           โ”‚   โ”œโ”€โ”€ onboarding/    # WelcomeDialog wizard
โ”‚           โ”‚   โ””โ”€โ”€ ui/            # shadcn/ui base components
โ”‚           โ””โ”€โ”€ lib/               # API client (110+ methods), contexts, utilities
โ”‚
โ”œโ”€โ”€ helm/
โ”‚   โ”œโ”€โ”€ spektrabot/                # Application Helm chart
โ”‚   โ”‚   โ”œโ”€โ”€ Chart.yaml
โ”‚   โ”‚   โ”œโ”€โ”€ values.yaml            # Default values
โ”‚   โ”‚   โ”œโ”€โ”€ values-civo.yaml       # Civo-specific overrides
โ”‚   โ”‚   โ”œโ”€โ”€ values-pilot.yaml      # Pilot deployment
โ”‚   โ”‚   โ”œโ”€โ”€ values-production.yaml # Production deployment
โ”‚   โ”‚   โ””โ”€โ”€ templates/             # 37 Kubernetes templates
โ”‚   โ”‚
โ”‚   โ””โ”€โ”€ monitoring/                # Monitoring Helm chart (kube-prometheus-stack)
โ”‚       โ”œโ”€โ”€ Chart.yaml
โ”‚       โ”œโ”€โ”€ values-civo.yaml       # Prometheus, Grafana, AlertManager config
โ”‚       โ”œโ”€โ”€ dashboards/            # Custom Grafana dashboards
โ”‚       โ””โ”€โ”€ templates/             # ServiceMonitors, PodMonitors, ConfigMaps
โ”‚
โ”œโ”€โ”€ docs/                          # Technical documentation
โ”œโ”€โ”€ scripts/                       # Utility scripts
โ”œโ”€โ”€ docker-compose.yml             # Local development (PostgreSQL, Redis, ClamAV)
โ”œโ”€โ”€ package.json                   # Root monorepo config
โ”œโ”€โ”€ pnpm-workspace.yaml            # Workspace definition
โ””โ”€โ”€ tsconfig.json                  # Root TypeScript config

5. Backend API โ€‹

5.1 Express Middleware Stack โ€‹

The middleware is configured in packages/api/src/index.ts:

  1. Helmet -- Security headers
  2. CORS -- Configurable origin allowlist with multi-domain support
  3. Body parsing -- JSON (10MB limit) and URL-encoded
  4. Rate limiting -- Per-user rate limiting via express-rate-limit with Redis store
  5. Authentication -- JWT verification via middleware (packages/api/src/middleware/auth.ts)
  6. Authorization -- Role-based access control (parent, teacher_senco, admin)
  7. Input validation -- express-validator on all route handlers
  8. HTTP metrics -- Prometheus instrumentation recording golden signals

5.2 API Route Groups โ€‹

All routes are mounted under the /api prefix (except /metrics).

Route GroupMount PathKey EndpointsAuth
Health/api/healthGET /None
Metrics/metricsPrometheus scrape endpointNone
Auth/api/authPOST /register, POST /login, PUT /consent, GET /me, POST /forgot-password, POST /reset-password, PATCH /profileMixed
Chat/api/chatPOST / (sync), Socket.IO for streamingUser
Preferences/api/preferencesGET /, PATCH /, POST /resetUser
Children/api/childrenCRUD for child profiles, school searchUser
Support (User)/api/supportPOST /tickets, GET /tickets, GET /tickets/:id, POST /tickets/:id/messages, POST /tickets/:id/rateUser
Waitlist/api/waitlistEmail verification + admin approval + account creationMixed
Analytics (public)/api/analyticsUmami script proxy and event collectionMixed
Admin/api/adminUsers CRUD, stats, audit-logs, RAG trace, eval usersAdmin
Admin Support/api/admin/supportTicket management, stats, internal notes, assignmentAdmin
Knowledge Base/api/admin/knowledge-baseUpload, upload-zip, list, stats, quality-stats, search, validate, bulk-validate, chunks, tags, deleteAdmin
Prompts/api/admin/promptsTemplates CRUD + activate, LLM settings CRUD + activate, Modules list/update/toggle, previewAdmin
Crawler/api/admin/crawlerLA crawl trigger, batch crawl, job status, queue status, coverageAdmin
School Crawler/api/admin/school-crawlerSchool crawl trigger, batch by LA, job status, queue statusAdmin
Schools & GIAS/api/admin/schoolsSchool listing/search/detail, stats, GIAS sync trigger/status/history, CSV uploadAdmin
Analytics (admin)/api/admin/analyticsKPI, new-users, conversations, messages, conversation-volume, feedback-summary, LA coverage, user roles, persona distributionAdmin

Total: 20+ route groups, 110+ REST endpoints + real-time WebSocket

5.3 Chat Message Flow โ€‹

Moved to Brain โ€” Message Flow. Full sequence diagram, layer boundaries (socket.ts โ†’ ai-service.ts โ†’ langchain.ts โ†’ agentic-rag.ts), streaming internals and latency budget live there.

5.4 WebSocket Events โ€‹

Client to Server:

EventPayloadDescription
message:send{ conversationId?, content, attachments? }Send a message (creates conversation if needed)
conversations:list--Request conversation list
conversation:history{ conversationId }Request message history
conversation:delete{ conversationId }Delete a conversation
typing:start{ conversationId }Typing indicator start
typing:stop{ conversationId }Typing indicator stop
message:feedback{ messageId, feedback }Thumbs up (1) / down (0) / remove (null)

Server to Client:

EventPayloadDescription
message:created{ conversationId, message }User message saved
message:ai:start{ conversationId }AI generation starting
message:ai:chunk{ conversationId, content }Streaming token chunk
message:ai:end{ conversationId, message }AI response complete (includes sources)
rag:stage{ conversationId, stage }RAG pipeline stage update
conversations:listConversation[]Conversation list response
conversation:history{ conversationId, messages }Message history response
conversation:deleted{ conversationId }Deletion confirmed
conversation:title:updated{ conversationId, title }Auto-generated title
error{ message }Error notification

6. Agentic RAG Pipeline โ€‹

Moved to Brain โ€” Agentic RAG. The canonical state diagram (9 nodes, 10 conditional edges), state shape, entry point, default env vars, routing logic, tool calling, streaming and self-correction loop all live there.

Related: Retrieval covers the retrieve node (hybrid search + RRF + LA bias + chunk expansion). Safety & Grounding covers the post-generation grounding pass.


7. Knowledge Base System โ€‹

Implemented in packages/api/src/knowledge-base/.

7.1 Document Upload โ€‹

  • Supported formats: PDF, DOCX, TXT, Markdown, HTML, CSV, ZIP (bulk upload)
  • Upload via admin API with multer middleware
  • File size limit: 50MB per file, 200MB per ZIP
  • ZIP extraction for batch processing

7.2 Chunking Strategy โ€‹

  • Recursive text splitting via LangChain RecursiveCharacterTextSplitter
  • Chunk size: configurable (default ~1000 characters with overlap)
  • SHA-256 content hashing for deduplication
  • Chunk index tracking for document reconstruction

7.3 Embedding Generation โ€‹

  • Model: Azure OpenAI text-embedding-3-large (1536 dimensions)
  • Embeddings stored in pgvector columns on knowledge_base table
  • L2 normalization for accurate cosine similarity
  • Validation to prevent SQL injection via embedding arrays

7.4 Quality Scoring and Validation โ€‹

  • Content length tracking for quality assessment
  • Admin validation workflow (pending/approved/rejected/flagged)
  • Bulk validation interface with keyboard shortcuts
  • LA-specific tagging for location-aware retrieval
  • SEND tag classification
  • Cross-LA deduplication to prevent duplicate content

7.5 Admin Approval Flow โ€‹

  1. Documents uploaded (status: pending)
  2. Admin reviews in validation interface (grouped by Local Authority)
  3. Admin approves or rejects (status: approved/rejected)
  4. Approved documents available for RAG retrieval

8. Web Crawler โ€‹

Implemented in packages/api/src/crawler/.

8.1 LA-Specific Document Discovery โ€‹

  • Seed URLs configured per Local Authority (152 English LAs)
  • Additional seed URLs for national charity content (10+ domains)
  • Depth-controlled crawling (configurable max depth)
  • Breadth-first link discovery

8.2 Change Detection and Re-crawling โ€‹

  • HTTP caching headers (ETag, Last-Modified)
  • SHA-256 content hashing for change detection
  • Version history tracking on crawled URLs
  • Scheduled re-crawl via admin interface

8.3 Content Quality โ€‹

  • SEND relevance scoring on discovered pages
  • robots.txt compliance checking
  • Content type filtering
  • Devolved nation URL exclusion (England-only content)
  • Base64 image stripping
  • Boilerplate text filtering
  • Minimum content length thresholds

8.4 Crawler Worker โ€‹

  • Background worker process (packages/api/src/crawler/crawler.worker.ts)
  • Bull queue for job management
  • Playwright-based browser rendering for JavaScript-heavy sites
  • Browser-like user agent headers
  • Rate limiting per domain

8.5 School Policy Crawler โ€‹

A parallel crawler system in packages/api/src/crawler/school-policy/:

  • Discovers SEND-relevant policies from school websites across England
  • Targets 52,000+ schools in the GIAS database
  • Admin UI for managing crawl jobs (single school, batch by LA, all schools)
  • Discovered documents ingested into the knowledge base for RAG retrieval
  • Separate tracking models (SchoolCrawlJob) for independent management

9. Data Sync Services โ€‹

9.1 GIAS Schools Sync โ€‹

Implemented in packages/api/src/services/gias-sync.service.ts:

  • Daily synchronisation with DfE Get Information About Schools API
  • ~52,000 English schools with comprehensive SEND metadata
  • Derived fields: isOpen, isSpecialSchool, sendScore, SEN unit/resourced provision
  • CronJob runs at 3 AM UTC (after database backups at 2 AM)

9.2 Ofsted Ratings Sync โ€‹

Implemented in packages/api/src/services/ofsted-sync.service.ts:

  • Ofsted inspection ratings for all rated schools
  • Framework version tracking (pre-2024, 2024, 2025)
  • Grade comparison with previous inspections
  • Stored in OfstedRating model linked to schools

9.3 EHCP/SEN2 Statistics Sync โ€‹

Implemented in packages/api/src/services/ehcp-stats-sync.service.ts:

  • EHCP assessment requests and completions per LA per year
  • 20-week timeliness metrics
  • Tribunal appeal counts
  • Stored in LaEhcpStatistics model

9.4 SEN Primary Need Breakdown Sync โ€‹

Implemented in packages/api/src/services/sen-needs-sync.service.ts:

  • Primary need prevalence per LA (ASD, SEMH, SLCN, MLD, etc.)
  • Provision type breakdown (EHC plan vs SEN Support)
  • Annual data per local authority
  • Stored in LaSenNeedBreakdown model

9.5 SEND Tribunal Statistics Sync โ€‹

Implemented in packages/api/src/services/tribunal-stats-sync.service.ts:

  • Appeal rates and outcomes per LA
  • Family success rates
  • Mediation statistics
  • Stored in LaTribunalStatistics model

9.6 Area SEND Inspections Sync โ€‹

Implemented in packages/api/src/services/send-inspections-sync.service.ts:

  • Joint Ofsted/CQC area SEND inspection outcomes
  • LA-level inspection results
  • Stored in LaSendInspection model

9.7 Postcode-to-LA Lookup โ€‹

Implemented in packages/api/src/services/postcode-lookup.service.ts and location.service.ts:

  • Reverse postcode lookup to determine local authority
  • Cache management via PostcodeLaCache model
  • Integration with child profiles: school URN resolves to LA context
  • Enables LA-specific RAG retrieval for personalised guidance

10. Support Ticket System โ€‹

Implemented in packages/api/src/support/.

10.1 Architecture โ€‹

  • Ticket model -- Core ticket with category, status, priority, assignment
  • TicketMessage model -- Conversation thread on each ticket (user and staff messages)
  • 6 categories: SUPPORT, FEEDBACK, BUG_REPORT, FEATURE_REQUEST, BILLING, OTHER
  • 5 statuses: OPEN, IN_PROGRESS, WAITING_ON_USER, RESOLVED, CLOSED
  • 4 priority levels: LOW, MEDIUM, HIGH, URGENT (admin-only)

10.2 User Flow โ€‹

  1. User creates ticket via /api/support/tickets with category and description
  2. User views their tickets with status filtering
  3. User can reply to tickets with messages
  4. User rates satisfaction (1-5 scale + comment) after resolution

10.3 Admin Flow โ€‹

  1. Admin views all tickets with advanced filtering (status, category, priority, assignee, search)
  2. Admin updates ticket status and priority
  3. Admin assigns tickets to staff members
  4. Admin adds public replies or internal notes (not visible to users)
  5. Admin views aggregated support statistics (open count, resolution time, satisfaction)

11. Database Schema โ€‹

The database uses PostgreSQL 16 with the pgvector extension for vector similarity search. 37 models across the full data lifecycle.

11.1 Key Models โ€‹

ModelTableDescription
UserusersUser accounts with role, persona, GDPR consent
UserPreferencesuser_preferencesAccessibility settings, theme, chat preferences
ChildchildrenChild profiles with SEND diagnosis, linked school
ConversationconversationsChat sessions with detected persona and intent
MessagemessagesChat messages with RAG metadata (JSON)
MessageFeedbackmessage_feedbackThumbs up/down per message
KnowledgeBaseknowledge_baseRAG chunks with 1024d vector embeddings
DocumentdocumentsParent documents (pre-chunking)
DocumentChunkdocument_chunksChunked content with embeddings
DocumentSharedocument_sharesUser-specific document sharing with expiration
SchoolschoolsGIAS school data (~52,000 records)
OfstedRatingofsted_ratingsInspection results with framework version tracking
LocalAuthoritylocal_authorities152 English LAs with seed URLs
LaEhcpStatisticsla_ehcp_statisticsEHCP/SEN2 statistics per LA per year
LaSenNeedBreakdownla_sen_need_breakdownsPrimary need percentages per LA per year
LaTribunalStatisticsla_tribunal_statisticsTribunal appeal outcomes per LA
LaSendInspectionla_send_inspectionsArea SEND joint inspection results
PostcodeLaCachepostcode_la_cachePostcode-to-LA lookup cache
CrawlJobcrawl_jobsLA crawler execution tracking
CrawledUrlcrawled_urlsDiscovered URLs with change detection
SchoolCrawlJobschool_crawl_jobsSchool policy crawler execution tracking
TicketticketsSupport tickets (6 categories, 5 statuses, 4 priorities)
TicketMessageticket_messagesMessages within support tickets (user/staff, public/internal)
PromptModuleprompt_modulesModular prompt building blocks
PromptTemplateprompt_templatesVersioned system prompts
LLMSettingsllm_settingsModel parameter configurations

| AuditLog | audit_logs | GDPR audit trail (30+ action types) | | DataSyncLog | data_sync_logs | GIAS/Ofsted/EHCP sync job history | | SystemConfig | system_config | Admin-adjustable settings | | WaitlistEntry | waitlist_entries | Waitlist with email verification | | PasswordResetToken | password_reset_tokens | SHA-256 hashed reset tokens | | UserEmbeddingCache | user_embedding_cache | Query embedding cache per user | | VectorSearchStats | vector_search_stats | Vector search performance tracking |

11.2 Vector Indexes โ€‹

The knowledge_base and document_chunks tables use pgvector's vector(1536) column type (Azure OpenAI text-embedding-3-large) with the cosine-distance operator (<=>). Both tables have HNSW indexes (m=16, ef_construction=64, vector_cosine_ops). See Brain โ€” Knowledge Base for the full index list and the 2026-03-05 dimension migration notes.


12. Frontend Application โ€‹

The frontend is a Next.js 15 application using the App Router, located in packages/web/.

12.1 Pages and Routes โ€‹

Public pages:

RouteDescription
/Landing page
/aboutAbout SpektraBot
/audienceTarget audience information
/faqFrequently asked questions
/how-it-worksPlatform walkthrough
/for-parentsParent-specific information
/for-teachersTeacher/SENCO information
/glossarySEND terminology glossary
/resourcesSEND resources directory
/trustTrust and safety information
/policies/privacyPrivacy policy
/policies/termsTerms of service

Authentication:

RouteDescription
/loginUser login
/signupWaitlist signup
/registerAccount creation (invite code required)
/forgot-passwordPassword reset request
/reset-passwordPassword reset form
/professional-setupProfessional user school linking
/waitlist/verifyEmail verification

User pages:

RouteDescription
/chatAI chat interface (main application)
/childrenChild profile management
/settingsUser preferences and accessibility
/supportSupport ticket creation and history
/support/[id]Individual support ticket conversation

Admin dashboard (18 pages):

RouteDescription
/adminDashboard with KPI cards and analytics charts
/admin/usersUser management (CRUD, role assignment)
/admin/schoolsSchool database browser (52,000+ records)
/admin/waitlistWaitlist management and approval
/admin/audit-logsGDPR audit trail viewer
/admin/analyticsDetailed analytics views
/admin/knowledge-baseKnowledge base document management
/admin/knowledge-base/validateContent validation workflow
/admin/document-qualityDocument quality metrics
/admin/la-coverageLocal Authority coverage map
/admin/promptsPrompt module and LLM settings management
/admin/rag-workflowRAG pipeline visualizer (live trace via SSE)
/admin/crawlerWeb crawler management
/admin/school-policiesSchool policy crawler and document viewer
/admin/supportSupport ticket management (admin view)
/admin/support/[id]Admin support ticket detail

| /admin/evaluation | Evaluation framework management |

12.2 Admin Dashboard Components โ€‹

12.3 Real-Time Features โ€‹

  • WebSocket streaming -- AI responses stream token-by-token via Socket.IO
  • Typing indicators -- Real-time typing status
  • RAG stage events -- Live pipeline progress (retrieving, grading, generating)
  • Title updates -- Auto-generated conversation titles pushed in real-time
  • Conversation sync -- List updates when conversations are created/deleted
  • RAG trace streaming -- Admin RAG workflow visualizer streams via SSE

12.4 Help System & Onboarding โ€‹

  • WelcomeDialog -- Three-step onboarding wizard (persona-aware: parent vs professional tips)
  • HelpDrawer -- In-app help panel with tips, limitations, keyboard shortcuts, and external links
  • Links to SENDIASS, IPSEA, and Spectrum Dynamics for further support
  • Completion tracked via localStorage

12.5 Accessibility โ€‹

  • WCAG 2.2 compliance with keyboard navigation and screen reader support
  • Universal Design for Learning (UDL) principles
  • Configurable font size, high contrast, reduced motion
  • Theme support (light/dark/auto)
  • Message density options (compact/comfortable/spacious)
  • Copy button on AI responses
  • Focus trapping in modals and drawers

13. Authentication and Authorization โ€‹

13.1 JWT Flow โ€‹

  1. User registers or logs in via /api/auth/register or /api/auth/login
  2. Server generates JWT with { userId, email, name, role } payload
  3. Token returned in response body
  4. Frontend stores token and sends via Authorization: Bearer <token> header
  5. Socket.IO authentication via handshake auth token

13.2 Role-Based Access โ€‹

RoleValueAccess
Parent/CarerparentChat, children profiles, preferences, GDPR, support tickets
Teacher/SENCOteacher_sencoChat (professional mode), preferences, school linking, support tickets
AdminadminFull system access including admin dashboard

Authorization is enforced at the route level via authorize() middleware.

13.3 Waitlist and Invite System โ€‹

  1. User submits email to waitlist (/api/waitlist/signup)
  2. Verification email sent via Resend
  3. User verifies email (status: VERIFIED_PENDING_APPROVAL)
  4. Admin approves in waitlist dashboard (status: APPROVED)
  5. Approved user receives approval token for account creation
  6. Registration requires valid INVITE_CODE environment variable

13.4 Password Reset Flow โ€‹

  1. User requests reset via /api/auth/forgot-password
  2. Token generated, SHA-256 hashed before database storage
  3. Reset email sent via Resend with token link
  4. User submits new password with token via /api/auth/reset-password
  5. Token verified, marked as used, password updated

14. GDPR Compliance โ€‹

  • GDPR consent collected at registration (terms, privacy policy, data processing)
  • Consent stored as JSONB with version, timestamp, and IP address
  • Consent can be updated via /api/auth/consent
  • Consent verification middleware on protected routes

14.2 Data Retention โ€‹

  • 90-day automatic data retention policy
  • Soft delete via deletedAt timestamp on User model
  • Cascade deletion: User -> Conversations -> Messages -> Attachments -> Feedback
  • Audit log preserved after deletion (denormalized email for trail preservation)

14.3 Audit Logging โ€‹

30+ tracked actions including:

  • User authentication (login, logout)
  • Consent updates
  • Data export and deletion
  • Conversation and message creation
  • File uploads
  • Admin actions (role changes, user deletion)
  • Knowledge base operations
  • Fine-tuning operations
  • Password reset events
  • Waitlist events
  • Support ticket operations

14.4 User Data Isolation โ€‹

  • Every database query includes userId filter (enforced in service layer)
  • WebSocket connections join user-specific rooms
  • Conversation access verified before any read/write operation
  • Admin-only routes gated by role middleware

15. Modular Prompt System โ€‹

Moved to Brain โ€” Prompt System (assembly order, crisis override, admin management) and Brain โ€” Prompt Modules (per-module reference for all 12 modules).


16. Monitoring and Observability โ€‹

16.1 Application Metrics โ€‹

Implemented in packages/api/src/observability/:

  • HTTP metrics middleware -- Records golden signals (latency, traffic, errors, saturation) per endpoint
  • Custom LLM metrics -- Tracks LLM call counts, token usage, and latency
  • Vector search stats -- Records search performance via VectorSearchStats model
  • Prometheus endpoint -- /metrics exposed without authentication for scraping

16.2 Monitoring Stack โ€‹

SpektraBot runs on Azure Container Apps; Azure-native monitoring provides the infrastructure layer (Container Apps metrics + Log Analytics workspace). Application-level metrics are exposed at /metrics on the API container and scraped into Azure Monitor. Historical references to Prometheus / Grafana / kube-prometheus-stack in this repository relate to the retired Civo Kubernetes setup and are no longer active.

16.3 Application metrics โ€‹

  • http_request_duration_seconds โ€” golden signals per route
  • llm_call_duration_seconds, llm_tokens_total โ€” per-model Azure OpenAI usage
  • vector_search_duration_seconds โ€” RAG retrieval latency
  • statutory_mismatch_detected_total{fact_name} โ€” overclaim regressions (see Safety & Grounding)
  • Custom counters for eval runs, crawler jobs, Bull queue state

17. Deployment โ€‹

All services run on Azure Container Apps in resource group spektrabot-rg (UK South).

ServiceContainer AppNotes
APIspektrabot-apiNode.js 22, Express + Socket.IO, :3001
Webspektrabot-webNext.js 15 standalone output, :3000
Workerspektrabot-workerBull queue consumer (crawlers, ingestion, eval spawning)
Docsspektrabot-docsThis VitePress site

Container registry: spektrabotacr.azurecr.io (Azure Container Registry).

Build + deploy pattern:

bash
# Build locally (ARM Mac โ†’ AMD64 target)
docker buildx build --platform linux/amd64 --no-cache \
  -t spektrabotacr.azurecr.io/<service>:<sha> \
  -f <path>/Dockerfile --push .

# Deploy
az containerapp update -g spektrabot-rg -n <service> \
  --image spektrabotacr.azurecr.io/<service>:<sha>

Image tagging: always the git commit SHA โ€” never mutable tags like latest / staging / main (operational memory).

Web build gotcha: packages/web must be built with NEXT_PUBLIC_API_URL=https://api.spektrabot.co.uk (not the root domain) because next.config.ts rewrites are baked at build time. Using same-origin creates an infinite proxy loop (HTTP 431).

Database and Redis: managed Azure Postgres Flexible Server with pgvector, and managed Redis. UK South for data residency.

Backups: scheduled pg_dump โ†’ Azure Blob Storage, plus PITR on the managed Postgres.

DB operations note: az containerapp exec has been unreliable on this account; use Azure Container App Jobs for migrations and one-off scripts.


Confidential ยท Spectrum Dynamics CIC