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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 14x 14x 14x 3x 3x 11x 11x 11x 11x 11x 11x 11x 11x 14x 15x 15x 13x 13x 15x 11x 11x 11x 1x 1x 1x 1x 14x 14x 14x 14x 14x 17x 17x 11x 11x 11x 17x 3x 3x 3x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 1610x 1610x 1610x 1610x 2x 2x 2x 1608x 1610x 2x 2x 2x 1606x 1606x 1610x 194x 194x 194x 1412x 1412x 1610x 679x 32x 17x 17x 32x 679x 38x 38x 15x 15x 15x 15x 15x 38x 679x 1610x 11x 11x 11x 1x 1x 1x 1x 15x 15x 15x 15x 15x 15x 15x 15x 14x 14x 14x 13x 15x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 15x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x | 'use client'
/**
* Partial JSON Parser for Streaming LLM Output
*
* Parses incomplete JSON streams to extract completed problem objects
* as they stream in. Used to provide progressive visual feedback during
* worksheet parsing.
*
* @example
* ```typescript
* const completedProblems = extractCompletedProblems(partialJson)
* // Returns: [{ problemNumber: 1, problemBoundingBox: {...} }, ...]
* ```
*/
import type { BoundingBox } from '@/lib/worksheet-parsing'
/** Minimal problem data needed for progressive highlighting */
export interface CompletedProblem {
problemNumber: number
problemBoundingBox: BoundingBox
}
/**
* Extract completed problem objects from a partial JSON stream
*
* The LLM streams JSON character-by-character. This function parses
* the partial output to find complete `{...}` objects within the
* `"problems": [...]` array.
*
* It uses brace depth tracking to find complete objects, then attempts
* to parse each one for the required fields.
*
* @param partialJson - The accumulated partial JSON string from streaming
* @returns Array of completed problems that have been fully streamed
*/
export function extractCompletedProblems(partialJson: string): CompletedProblem[] {
const problems: CompletedProblem[] = []
// Find the start of the problems array
const problemsArrayStart = findProblemsArrayStart(partialJson)
if (problemsArrayStart === -1) {
return problems
}
// Extract just the content inside the problems array
const arrayContent = partialJson.slice(problemsArrayStart)
// Find complete objects within the array
const completeObjects = findCompleteObjects(arrayContent)
// Try to parse each complete object
for (const objStr of completeObjects) {
const problem = tryParseProblem(objStr)
if (problem) {
problems.push(problem)
}
}
return problems
}
/**
* Find the start index of the problems array content (after the opening '[')
*/
function findProblemsArrayStart(json: string): number {
// Look for "problems": [ or "problems":[
const patterns = [/"problems"\s*:\s*\[/, /"problems":\[/]
for (const pattern of patterns) {
const match = json.match(pattern)
if (match && match.index !== undefined) {
// Return position right after the opening bracket
return match.index + match[0].length
}
}
return -1
}
/**
* Find complete JSON objects within array content using brace depth tracking
*/
function findCompleteObjects(arrayContent: string): string[] {
const objects: string[] = []
let depth = 0
let objectStart = -1
let inString = false
let escapeNext = false
for (let i = 0; i < arrayContent.length; i++) {
const char = arrayContent[i]
// Handle escape sequences in strings
if (escapeNext) {
escapeNext = false
continue
}
if (char === '\\' && inString) {
escapeNext = true
continue
}
// Track string boundaries
if (char === '"' && !escapeNext) {
inString = !inString
continue
}
// Only process braces outside of strings
if (!inString) {
if (char === '{') {
if (depth === 0) {
objectStart = i
}
depth++
} else if (char === '}') {
depth--
if (depth === 0 && objectStart !== -1) {
// Found a complete object
const objStr = arrayContent.slice(objectStart, i + 1)
objects.push(objStr)
objectStart = -1
}
}
}
}
return objects
}
/**
* Try to parse a JSON object string as a problem with the required fields
*/
function tryParseProblem(objStr: string): CompletedProblem | null {
try {
const obj = JSON.parse(objStr)
// Check for required fields
if (
typeof obj.problemNumber === 'number' &&
obj.problemBoundingBox &&
typeof obj.problemBoundingBox.x === 'number' &&
typeof obj.problemBoundingBox.y === 'number' &&
typeof obj.problemBoundingBox.width === 'number' &&
typeof obj.problemBoundingBox.height === 'number'
) {
return {
problemNumber: obj.problemNumber,
problemBoundingBox: {
x: obj.problemBoundingBox.x,
y: obj.problemBoundingBox.y,
width: obj.problemBoundingBox.width,
height: obj.problemBoundingBox.height,
},
}
}
} catch {
// JSON.parse failed - object is not yet complete or malformed
}
return null
}
/**
* Get the count of problems that have been fully streamed
*
* Useful for progress indicators: "Parsing... 5/24 problems"
*/
export function getCompletedProblemCount(partialJson: string): number {
return extractCompletedProblems(partialJson).length
}
/**
* Estimate total problem count from partial JSON
*
* Looks for "totalProblems" or counts expected based on row indicators.
* Returns null if cannot determine.
*/
export function estimateTotalProblems(partialJson: string): number | null {
// Look for pageMetadata.totalProblems if it exists
const totalMatch = partialJson.match(/"totalProblems"\s*:\s*(\d+)/)
if (totalMatch) {
return parseInt(totalMatch[1], 10)
}
// Look for problems array length hint (may be inaccurate during streaming)
// This is a rough estimate
return null
}
|