All files / web/src/app/api/vision-training/preflight route.ts

0% Statements 0/103
0% Branches 0/1
0% Functions 0/1
0% Lines 0/103

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104                                                                                                                                                                                                               
import { NextResponse } from 'next/server'
import { checkDependencies, ensureVenvReady, isPlatformSupported, TRAINING_PYTHON } from '../config'
import { withAuth } from '@/lib/auth/withAuth'

// Force dynamic rendering - this route checks system dependencies at runtime
export const dynamic = 'force-dynamic'

export interface PreflightResult {
  ready: boolean
  platform: {
    supported: boolean
    reason?: string
  }
  venv: {
    exists: boolean
    python: string
    isAppleSilicon: boolean
    hasGpu: boolean
    error?: string
  }
  dependencies: {
    allInstalled: boolean
    installed: { name: string; pipName: string }[]
    missing: { name: string; pipName: string }[]
    error?: string
  }
}

/**
 * GET /api/vision-training/preflight
 *
 * Performs a complete preflight check for training readiness:
 * 1. Platform support (TensorFlow availability)
 * 2. Venv existence and setup
 * 3. All required Python dependencies installed
 *
 * Returns a structured result indicating if training can proceed.
 */
export const GET = withAuth(
  async () => {
    // Check platform support first
    const platformCheck = isPlatformSupported()
    if (!platformCheck.supported) {
      return NextResponse.json({
        ready: false,
        platform: platformCheck,
        venv: {
          exists: false,
          python: TRAINING_PYTHON,
          isAppleSilicon: false,
          hasGpu: false,
        },
        dependencies: {
          allInstalled: false,
          installed: [],
          missing: [],
          error: 'Platform not supported',
        },
      })
    }

    // Check/setup venv
    const venvResult = await ensureVenvReady()

    if (!venvResult.success) {
      return NextResponse.json({
        ready: false,
        platform: { supported: true },
        venv: {
          exists: false,
          python: venvResult.python,
          isAppleSilicon: venvResult.isAppleSilicon,
          hasGpu: venvResult.hasGpu,
          error: venvResult.error,
        },
        dependencies: {
          allInstalled: false,
          installed: [],
          missing: [],
          error: 'Venv setup failed',
        },
      })
    }

    // Check all dependencies
    const depResult = await checkDependencies()

    const ready = depResult.allInstalled

    return NextResponse.json({
      ready,
      platform: { supported: true },
      venv: {
        exists: true,
        python: venvResult.python,
        isAppleSilicon: venvResult.isAppleSilicon,
        hasGpu: venvResult.hasGpu,
      },
      dependencies: depResult,
    })
  },
  { role: 'admin' }
)