Spaces:
Sleeping
Sleeping
| import fs from 'fs' | |
| import path from 'path' | |
| import { BenchmarkData } from './types' | |
| import { BenchmarkDataSchema } from './schema' | |
| let cached: BenchmarkData | null = null | |
| export async function loadBenchmarks(): Promise<BenchmarkData> { | |
| if (cached) return cached | |
| const filePath = path.join(process.cwd(), 'data/benchmarks.json') | |
| const raw = JSON.parse(fs.readFileSync(filePath, 'utf-8')) | |
| // Sanitize patterns: Ollama sometimes returns arrays instead of strings, nulls, or missing fields | |
| if (Array.isArray(raw.patterns)) { | |
| raw.patterns = raw.patterns | |
| .map((p: Record<string, unknown>) => ({ | |
| ...p, | |
| signature: Array.isArray(p.signature) ? p.signature.join(', ') : p.signature, | |
| interpretation: Array.isArray(p.interpretation) ? p.interpretation.join(' ') : p.interpretation, | |
| improvements: Array.isArray(p.improvements) | |
| ? p.improvements.map((i: unknown) => (Array.isArray(i) ? i.join(', ') : String(i))) | |
| : [], | |
| })) | |
| // Drop patterns with missing or null required string fields | |
| .filter((p: Record<string, unknown>) => | |
| typeof p.id === 'string' && p.id && | |
| typeof p.signature === 'string' && p.signature && | |
| typeof p.interpretation === 'string' && p.interpretation | |
| ) | |
| } | |
| cached = BenchmarkDataSchema.parse(raw) | |
| return cached | |
| } | |