All files / web/src/lib/ai gradeWorksheet.ts

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

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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
import { readFile } from 'fs/promises'
import { SINGLE_CARRY_PATH } from '@/app/create/worksheets/progressionPath'
import { recordOpenAiResponsesUsage } from '@/lib/ai-usage/helpers'
import { AiFeature } from '@/lib/ai-usage/features'

/**
 * Grading result structure returned by GPT-5
 */
export interface GradingResult {
  problems: Array<{
    index: number
    operandA: number
    operandB: number
    correctAnswer: number
    studentAnswer: number | null
    isCorrect: boolean
    digitCount: number
    requiresRegrouping: boolean
  }>
  totalProblems: number
  correctCount: number
  accuracy: number
  errorPatterns: string[]
  currentStepEstimate: string
  suggestedStepId: string
  reasoning: string
  feedback: string
}

/**
 * Validation error structure for retry prompts
 */
interface ValidationError {
  field: string
  value?: any
  error: string
  validOptions?: string[]
  expected?: any
}

/**
 * JSON schema for GPT-5 strict mode
 */
const gradingSchema = {
  type: 'object',
  properties: {
    problems: {
      type: 'array',
      items: {
        type: 'object',
        properties: {
          index: { type: 'integer' },
          operandA: { type: 'integer' },
          operandB: { type: 'integer' },
          correctAnswer: { type: 'integer' },
          studentAnswer: { type: ['integer', 'null'] },
          isCorrect: { type: 'boolean' },
          digitCount: { type: 'integer' },
          requiresRegrouping: { type: 'boolean' },
        },
        required: [
          'index',
          'operandA',
          'operandB',
          'correctAnswer',
          'studentAnswer',
          'isCorrect',
          'digitCount',
          'requiresRegrouping',
        ],
        additionalProperties: false,
      },
    },
    totalProblems: { type: 'integer' },
    correctCount: { type: 'integer' },
    accuracy: { type: 'number' },
    errorPatterns: {
      type: 'array',
      items: { type: 'string' },
    },
    currentStepEstimate: { type: 'string' },
    suggestedStepId: { type: 'string' },
    reasoning: { type: 'string' },
    feedback: { type: 'string' },
  },
  required: [
    'problems',
    'totalProblems',
    'correctCount',
    'accuracy',
    'errorPatterns',
    'currentStepEstimate',
    'suggestedStepId',
    'reasoning',
    'feedback',
  ],
  additionalProperties: false,
}

/**
 * Build the grading prompt with progression context
 */
function buildGradingPrompt(validationError?: ValidationError): string {
  const stepDescriptions = SINGLE_CARRY_PATH.map(
    (step) => `- ${step.id}: ${step.name} - ${step.description}`
  ).join('\n')

  const validStepIds = SINGLE_CARRY_PATH.map((s) => s.id).join(', ')

  let prompt = `You are grading an elementary math worksheet to determine the student's exact position in a mastery progression path.

AVAILABLE PROGRESSION STEPS:
${stepDescriptions}
`

  // Add validation error feedback if this is a retry
  if (validationError) {
    prompt += `

PREVIOUS ATTEMPT HAD VALIDATION ERROR:
Field: ${validationError.field}
Error: ${validationError.error}
${validationError.validOptions ? `Valid options: ${validationError.validOptions.join(', ')}` : ''}
${validationError.expected !== undefined ? `Expected value: ${validationError.expected}` : ''}

Please correct this error and try again.
`
  }

  prompt += `

TASK:
1. Examine the worksheet image and identify all addition problems
2. Read the student's handwritten answers carefully
3. Grade each problem as correct or incorrect
4. Identify specific error patterns (e.g., "forgets to carry in tens place", "misaligns digits")
5. Determine which progression step ID best matches their current skill level
6. Recommend the EXACT next step ID they should practice

Return JSON matching the schema exactly.

CRITICAL: suggestedStepId MUST be one of: ${validStepIds}`

  return prompt
}

/**
 * Call GPT-5 Responses API with vision
 */
async function callGPT5Vision(
  imageDataUrl: string,
  prompt: string,
  userId?: string
): Promise<GradingResult> {
  const response = await fetch('https://api.openai.com/v1/responses', {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      model: 'gpt-5-2025-08-07',
      input: [
        {
          role: 'user',
          content: [
            {
              type: 'input_image',
              image_url: imageDataUrl,
              detail: 'high', // High detail for handwriting recognition
            },
            {
              type: 'input_text',
              text: prompt,
            },
          ],
        },
      ],
      reasoning: {
        effort: 'high', // Use deep thinking for analysis
      },
      max_output_tokens: 4000, // Enough for 20 problems + analysis
      text: {
        verbosity: 'medium', // Balanced output
        format: {
          type: 'json_schema',
          name: 'worksheet_grading',
          schema: gradingSchema,
          strict: true, // Enforce schema strictly
        },
      },
    }),
  })

  if (!response.ok) {
    const errorText = await response.text()
    throw new Error(`OpenAI API error (${response.status}): ${errorText}`)
  }

  const apiResponse = await response.json()

  // Check for API errors
  if (apiResponse.error) {
    throw new Error(`OpenAI API error: ${apiResponse.error.message}`)
  }

  if (userId) {
    recordOpenAiResponsesUsage(apiResponse, { userId, feature: AiFeature.WORKSHEET_GRADE })
  }

  // Extract the grading result (already parsed JSON due to schema)
  return apiResponse.output[0].content[0].text as GradingResult
}

/**
 * Validate grading result and return errors if any
 */
function validateGradingResult(result: GradingResult): ValidationError | null {
  // Validate step ID
  const validStep = SINGLE_CARRY_PATH.find((s) => s.id === result.suggestedStepId)
  if (!validStep) {
    return {
      field: 'suggestedStepId',
      value: result.suggestedStepId,
      error: `Invalid step ID. Must be one of the provided progression steps.`,
      validOptions: SINGLE_CARRY_PATH.map((s) => s.id),
    }
  }

  // Validate problem count
  if (result.totalProblems !== result.problems.length) {
    return {
      field: 'totalProblems',
      error: `totalProblems (${result.totalProblems}) doesn't match problems array length (${result.problems.length})`,
      expected: result.problems.length,
    }
  }

  // Validate accuracy calculation
  const expectedAccuracy = result.correctCount / result.totalProblems
  if (Math.abs(result.accuracy - expectedAccuracy) > 0.01) {
    return {
      field: 'accuracy',
      error: `Accuracy ${result.accuracy} doesn't match correctCount/totalProblems = ${expectedAccuracy}`,
      expected: expectedAccuracy,
    }
  }

  return null
}

/**
 * Grade worksheet with GPT-5 vision and retry logic
 *
 * @param imagePath - Path to worksheet image file
 * @param maxRetries - Maximum number of retry attempts (default: 2)
 * @returns Complete grading assessment with progression recommendation
 */
export async function gradeWorksheetWithVision(
  imagePath: string,
  maxRetries = 2,
  userId?: string
): Promise<GradingResult> {
  // Read and encode image
  const imageBuffer = await readFile(imagePath)
  const base64Image = imageBuffer.toString('base64')
  const mimeType = imagePath.endsWith('.png') ? 'image/png' : 'image/jpeg'
  const dataUrl = `data:${mimeType};base64,${base64Image}`

  let lastError: Error | null = null
  let validationError: ValidationError | undefined

  for (let attempt = 0; attempt <= maxRetries; attempt++) {
    try {
      // Build prompt (includes validation error if retry)
      const prompt = buildGradingPrompt(validationError)

      // Call GPT-5
      const result = await callGPT5Vision(dataUrl, prompt, userId)

      // Validate result
      const error = validateGradingResult(result)

      if (error) {
        if (attempt < maxRetries) {
          // Retry with validation error
          console.warn(`Retry ${attempt + 1}: Validation error in ${error.field} - ${error.error}`)
          validationError = error
          continue
        }

        // Out of retries, apply fallback fixes
        console.error(`All retries exhausted. Applying fallback fixes.`)

        if (error.field === 'suggestedStepId') {
          result.suggestedStepId = SINGLE_CARRY_PATH[0].id
        } else if (error.field === 'totalProblems') {
          result.totalProblems = result.problems.length
        } else if (error.field === 'accuracy') {
          result.accuracy = result.correctCount / result.totalProblems
        }

        return result
      }

      // All validations passed
      return result
    } catch (error) {
      lastError = error as Error
      if (attempt < maxRetries) {
        console.warn(`Retry ${attempt + 1}: ${lastError.message}`)
        // Exponential backoff
        await new Promise((resolve) => setTimeout(resolve, 1000 * 2 ** attempt))
      }
    }
  }

  throw lastError || new Error('Grading failed after all retries')
}