/** * Server-side extraction of usable assets from a note BEFORE any slide LLM call. * Inspired by PPTAgent (retrieve relevant content per slide) and DeckForge (data_needs). * Critical for STEM notes: formulas must be harvested, not hoped for from the model. */ export interface SourceAssets { formulas: string[] numbers: Array<{ label: string; value: number; raw: string }> keySentences: string[] hasMath: boolean hasNumbers: boolean wordCount: number } /** Extract LaTeX / equation-like fragments from note plain text or HTML. */ export function extractFormulas(raw: string): string[] { if (!raw) return [] const found: string[] = [] const push = (s: string) => { const t = s.replace(/\s+/g, ' ').trim() if (t.length >= 2 && t.length <= 280 && !found.includes(t)) found.push(t) } // $$ ... $$ for (const m of raw.matchAll(/\$\$([\s\S]+?)\$\$/g)) push(m[1] || '') // \[ ... \] for (const m of raw.matchAll(/\\\[([\s\S]+?)\\\]/g)) push(m[1] || '') // \( ... \) for (const m of raw.matchAll(/\\\(([\s\S]+?)\\\)/g)) push(m[1] || '') // $ ... $ (single line) for (const m of raw.matchAll(/(? { if (!raw) return [] const out: Array<{ label: string; value: number; raw: string }> = [] const re = /(?:^|[^\d])((?:≈|~)?\s*-?\d+(?:[.,]\d+)?)\s*(%|€|\$|k|m|bn|ms|s|°C)?\b/gi let m: RegExpExecArray | null while ((m = re.exec(raw)) !== null && out.length < 20) { const numStr = (m[1] || '').replace(/[≈~\s]/g, '').replace(',', '.') const value = parseFloat(numStr) if (!Number.isFinite(value)) continue const unit = m[2] || '' // crude label: 40 chars before const start = Math.max(0, m.index - 40) const ctx = raw.slice(start, m.index).replace(/\s+/g, ' ').trim() const label = ctx.split(/[.;:!?\n]/).pop()?.trim().slice(-32) || `n${out.length + 1}` out.push({ label, value: unit === '%' ? value : value, raw: `${numStr}${unit}` }) } return out } export function extractKeySentences(raw: string, max = 16): string[] { if (!raw) return [] const plain = raw .replace(/<[^>]+>/g, ' ') .replace(/\$\$[\s\S]+?\$\$/g, ' ') .replace(/\\\[[\s\S]+?\\\]/g, ' ') .replace(/\s+/g, ' ') .trim() const parts = plain .split(/(?<=[.!?])\s+/) .map((s) => s.trim()) .filter((s) => s.length >= 40 && s.length <= 220) const out: string[] = [] for (const p of parts) { if (out.length >= max) break if (!out.some((x) => x.slice(0, 40) === p.slice(0, 40))) out.push(p) } return out } export function extractSourceAssets(raw: string): SourceAssets { const formulas = extractFormulas(raw) const numbers = extractNumbers(raw) const keySentences = extractKeySentences(raw) const wordCount = raw.replace(/<[^>]+>/g, ' ').split(/\s+/).filter(Boolean).length return { formulas, numbers, keySentences, hasMath: formulas.length > 0 || /équat|equat|différen|differen|dériv|deriv|intégr|integr|EDO|ODE|PDE|latex/i.test(raw), hasNumbers: numbers.length >= 2, wordCount, } }