Skip to content

SpektraBot Development Guide โ€‹

Getting started with local development, testing, deployment procedures, CI/CD, and troubleshooting for SpektraBot.

Deployment section below is historical. The Helm / Civo Kubernetes content describes the retired infrastructure. Current deployment is Azure Container Apps โ€” see Architecture ยง17 for the live build-and-deploy workflow. The sections below are retained for archaeology until we rewrite this page.


1. Deployment (historical โ€” retired Civo Kubernetes) โ€‹

1.1 Helm Chart Structure โ€‹

The application Helm chart is in helm/spektrabot/ with 37 templates:

TemplateDescription
api-deployment.yamlAPI backend pods
web-deployment.yamlNext.js frontend pods
redis-deployment.yamlRedis cache
cloudnativepg-cluster.yamlCloudNativePG PostgreSQL cluster
cloudnativepg-objectstore.yamlBackup object store config
cloudnativepg-scheduledbackup.yamlScheduled database backups (CNPG barman)
pg-backup-s3-cronjob.yamlStandalone S3 backup CronJob (every 6h)
pg-backup-s3-configmap.yamlBackup script configuration
ingress.yamlNGINX ingress for HTTP traffic
ingress-socketio.yamlWebSocket ingress with sticky sessions
hpa.yamlHorizontal Pod Autoscaler
networkpolicy.yamlNetwork policies for pod isolation
cronjob-gias-sync.yamlDaily GIAS school data sync
umami-deployment.yamlPrivacy-focused analytics
secret.yamlKubernetes secrets
configmap.yamlConfiguration
rbac.yamlRBAC rules
servicemonitor-api.yamlPrometheus ServiceMonitor for API
podmonitor-cnpg.yamlPrometheus PodMonitor for PostgreSQL
cnpg-custom-queries-cm.yamlCustom PostgreSQL metric queries

1.2 Values Configuration โ€‹

FilePurpose
values.yamlDefault values (local development)
values-civo.yamlCivo Kubernetes overrides (storage class: civo-volume)
values-pilot.yaml20-user pilot sizing
values-production.yamlProduction configuration

1.3 Kubernetes Architecture โ€‹

1.4 Backup Strategy (Dual-Layer) โ€‹

Primary: Standalone S3 CronJob (every 6 hours)

  • pg_dump in custom format uploaded to Civo Object Storage
  • Independent of kubelet exec (works even when cluster has proxy issues)
  • Integrity verification: rejects files <1MB, validates with pg_restore --list
  • Retains last 7 backups with automatic pruning
  • Civo S3 quirks: signature v2, multipart threshold 600MB

Secondary: CNPG Barman Cloud (continuous + daily)

  • WAL archiving for point-in-time recovery
  • Daily base backups at 2 AM UTC
  • 30-day retention
  • Depends on kubelet exec (can fail during cluster issues)

1.5 Docker Multi-Stage Builds โ€‹

Both API and Web packages use multi-stage Docker builds:

  1. deps -- Install pnpm dependencies with hoisting
  2. builder -- Compile TypeScript / build Next.js (standalone output)
  3. runner -- Minimal production image

Key considerations:

  • --platform linux/amd64 required when building on ARM (Mac) for Civo (AMD64)
  • --no-cache to avoid cached ARM layers
  • pnpm-workspace.yaml and .npmrc must be copied for monorepo resolution
  • Next.js requires output: "standalone" in next.config.ts

2. Development Setup โ€‹

Prerequisites โ€‹

  • Node.js 22+
  • pnpm 9+
  • Docker and Docker Compose

Quick Start โ€‹

bash
# 1. Install dependencies
pnpm install

# 2. Start database services
docker-compose up -d

# 3. Configure environment
cp .env.example .env
# Edit .env with your Azure OpenAI API keys and other settings

# 4. Generate Prisma client
pnpm db:generate

# 5. Run database migrations
pnpm db:migrate

# 6. Seed database (optional)
pnpm db:seed

# 7. Start development servers
pnpm dev

Key Development Scripts โ€‹

ScriptDescription
pnpm devStart all services in development mode
pnpm buildBuild all packages for production
pnpm testRun all tests
pnpm lintLint all packages
pnpm type-checkTypeScript type checking
pnpm db:generateGenerate Prisma client
pnpm db:migrateRun database migrations
pnpm db:studioOpen Prisma Studio GUI
pnpm db:seedSeed database with test data

3. Testing โ€‹

The test suite uses Vitest with ~30+ test files across multiple layers.

Test Structure โ€‹

DirectoryTypeExamples
tests/unit/Unit testsAuth service, chat handlers, admin service, prompt assembly, config validation
tests/unit/chat/Chat unit testsAgentic RAG, clarification, conversation history, validators
tests/integration/Integration testsAzure OpenAI flow, RAG knowledge base, user isolation, admin schools, prompt quality
tests/infrastructure/Infrastructure testsDatabase persistence, pgvector indexes
tests/services/Service testsChildren CRUD, email service, waitlist

Running Tests โ€‹

bash
# Run all tests
pnpm test

# Run integration tests only
pnpm test:integration

# Run with coverage
pnpm test:ci

# Run specific test file
pnpm --filter @spektrabot/api test -- tests/unit/chat/agentic-rag.test.ts

4. Key Configuration โ€‹

Environment Variables โ€‹

VariableRequiredDescription
DATABASE_URLYesPostgreSQL connection string
REDIS_URLYesRedis connection string
JWT_SECRETYesJWT signing secret (32+ chars)
AZURE_OPENAI_API_KEYYesAzure OpenAI API key
AZURE_OPENAI_ENDPOINTYesAzure OpenAI endpoint URL
AZURE_OPENAI_API_VERSIONNoAzure OpenAI API version (default: 2024-10-21)
AZURE_OPENAI_DEPLOYMENT_NAMENoChat model deployment (default: gpt-4o)
AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAMENoEmbedding model deployment (default: text-embedding-3-large)
AI_MODELNoAI model (default: gpt-4o)
CORS_ORIGINYesAllowed CORS origins (comma-separated)
INVITE_CODEYesRegistration invite code
RESEND_API_KEYNoResend email service API key
CIVO_ACCESS_KEYNoCivo object storage access key
ENABLE_AGENTIC_RAGNoEnable agentic RAG (default: true)
RELEVANCE_THRESHOLDNoRAG relevance threshold (default: 0.3)
SKIP_HALLUCINATION_CHECKNoSkip hallucination checking (default: false)
DATA_RETENTION_DAYSNoGDPR data retention period (default: 90)

5. Further Reading โ€‹

Confidential ยท Spectrum Dynamics CIC