/** * Mechanical checks for the procedure "ai-assistants-everyday-tasks" v1. * Each check returns one line per pass rule: ok (true/false) or "tester" when a * person must confirm it (for example, opening a cited link). A task passes only * when every rule is ok, and any "tester" rules were confirmed by the tester. */ import { HyperFormula } from "hyperformula"; export type RuleResult = { rule: string; ok: boolean | "tester"; detail?: string }; export const words = (s: string) => (s.match(/[A-Za-z0-9$€£][\w'’.,$%€£-]*/g) ?? []).length; const norm = (s: string) => s.replace(/[’‘]/g, "'").replace(/ /g, " "); function syllables(word: string): number { const w = word.toLowerCase().replace(/[^a-z]/g, ""); if (!w) return 0; if (w.length <= 3) return 1; const trimmed = w.replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, "").replace(/^y/, ""); const groups = trimmed.match(/[aeiouy]{1,2}/g); return Math.max(1, groups ? groups.length : 1); } /** Flesch-Kincaid grade level (standard formula). Bullet lines count as sentences. */ export function fkGrade(text: string): number { const clean = norm(text).replace(/[*#_`>]/g, " "); const sentences = clean .split(/(?<=[.!?])\s+|\n+/) .map((s) => s.trim()) .filter((s) => /[A-Za-z]/.test(s)); const ws = clean.match(/[A-Za-z]+(?:'[a-z]+)?/g) ?? []; const syl = ws.reduce((n, w) => n + syllables(w), 0); if (!sentences.length || !ws.length) return 99; return Math.round((0.39 * (ws.length / sentences.length) + 11.8 * (syl / ws.length) - 15.59) * 10) / 10; } /** Pull the first JSON array out of an answer (code fences allowed). */ export function extractJson(text: string): unknown { const fenced = text.match(/```(?:json)?\s*([\s\S]*?)```/); const body = fenced ? fenced[1]! : text.slice(text.indexOf("["), text.lastIndexOf("]") + 1); return JSON.parse(body); } /** Evaluate a formula against the fixed A1:C7 sheet of the spreadsheet task. */ export function evalSalesFormula(formula: string): unknown { const d = (y: number, m: number, day: number) => `=DATE(${y},${m},${day})`; const sheet = [ ["Region", "Date", "Sales"], ["East", d(2025, 12, 15), 400], ["West", d(2026, 1, 5), 250], ["East", d(2026, 1, 10), 300], ["East", d(2026, 2, 1), 540], ["West", d(2026, 2, 3), 610], ["East", d(2026, 3, 12), 400], ]; const hf = HyperFormula.buildFromArray(sheet, { licenseKey: "gpl-v3", useArrayArithmetic: true, dateFormats: ["YYYY-MM-DD", "MM/DD/YYYY", "DD/MM/YYYY"] }); const id = hf.getSheetId(hf.getSheetNames()[0]!)!; hf.setCellContents({ sheet: id, row: 0, col: 5 }, [[formula.trim().startsWith("=") ? formula.trim() : `=${formula.trim()}`]]); const v = hf.getCellValue({ sheet: id, row: 0, col: 5 }); hf.destroy(); return typeof v === "object" && v !== null && "value" in v ? `error ${(v as { value: string }).value}` : v; } /** Find formulas in the answer: code spans/blocks or lines that start with "=". */ export function findFormulas(text: string): string[] { const out = new Set(); for (const m of norm(text).matchAll(/`{1,3}(?:excel)?\s*(=[^`]+?)\s*`{1,3}/g)) out.add(m[1]!.trim()); for (const m of norm(text).matchAll(/^\s*(=[A-Z]+\(.*\))\s*$/gm)) out.add(m[1]!.trim()); for (const m of norm(text).matchAll(/(=SUM(?:IFS|PRODUCT)?\((?:[^()]|\([^()]*\))*\))/g)) out.add(m[1]!.trim()); return [...out]; } const has = (t: string, re: RegExp) => re.test(norm(t)); const tester = (rule: string, detail?: string): RuleResult => ({ rule, ok: "tester", detail }); export const CHECKS: Record RuleResult[]> = { "summarize-pdf": (o) => [ { rule: "100 words or fewer", ok: words(o) <= 100, detail: `${words(o)} words` }, { rule: "$2.4 million", ok: has(o, /\$\s?2\.4\s?(million|m\b)/i) }, { rule: "June 2027", ok: has(o, /June\s+2027/i) }, { rule: "Oak Street Community Center", ok: has(o, /Oak Street Community Cent(er|re)/i) }, tester("states nothing the PDF contradicts"), ], "email-from-notes": (o) => { // "Subject: text" on one line, or a "Subject" label line followed by the subject text. const subjectRe = /^[ \t*#_]*subject[ \t*_]*(?::.*|\r?\n.*)$/im; const body = o.replace(subjectRe, ""); return [ { rule: "subject line", ok: subjectRe.test(norm(o)) }, { rule: "Thursday 14 May", ok: has(o, /Thursday,?\s+(14(th)?\s+May|May\s+14(th)?)/i) }, { rule: "12:30", ok: has(o, /12[:.]30/) }, { rule: "Rosa's Kitchen, 18 Elm Road", ok: has(o, /Rosa's Kitchen/i) && has(o, /18 Elm R(oa)?d/i) }, { rule: "up to $25", ok: has(o, /\$\s?25/) }, { rule: "reply by Monday 11 May", ok: has(o, /Monday,?\s+(11(th)?\s+May|May\s+11(th)?)/i) }, { rule: "body 150 words or fewer", ok: words(body) <= 150, detail: `${words(body)} words` }, tester("adds no date, time, place or amount not in the notes"), ]; }, "cited-answer": (o) => [ { rule: "says 1990", ok: has(o, /\b1990\b/) }, { rule: "has a web link", ok: has(o, /https?:\/\/\S+|\b[a-z0-9-]+\.(gov|org|com|edu)\b/i) }, tester("a linked page states Hubble launched in 1990"), ], "table-to-json": (o) => { const want = [ { name: "Ana Ruiz", city: "Austin", joined: "2025-02-03", plan: "Pro" }, { name: "Ben Okafor", city: "Leeds", joined: "2024-11-17", plan: "Free" }, { name: "Chen Wei", city: "Toronto", joined: "2026-01-09", plan: "Team" }, { name: "Dara Singh", city: "Perth", joined: "2025-06-30", plan: "Pro" }, ]; let got: unknown; try { got = extractJson(o); } catch (e) { return [{ rule: "JSON parses", ok: false, detail: String(e) }]; } const arr = Array.isArray(got) ? (got as Record[]) : []; const keysOk = arr.length === 4 && arr.every((x) => Object.keys(x).sort().join() === "city,joined,name,plan"); const valuesOk = keysOk && want.every((w, i) => Object.entries(w).every(([k, v]) => arr[i]![k] === v)); return [ { rule: "JSON parses", ok: true }, { rule: "4 objects with the 4 keys", ok: keysOk, detail: `${arr.length} objects` }, { rule: "all 16 values match", ok: valuesOk }, ]; }, "spreadsheet-formula": (o) => { const fs = findFormulas(o); const results = fs.map((f) => ({ f, v: evalSalesFormula(f) })); const good = results.find((r) => r.v === 1240); return [ { rule: "formula returns 1240", ok: !!good, detail: results.map((r) => `${r.f} -> ${r.v}`).join(" | ") || "no formula found" }, { rule: "states 1,240", ok: has(o, /\b1,?240\b/) }, ]; }, "plain-language": (o) => { const g = fkGrade(o); return [ { rule: "Flesch-Kincaid grade 8.0 or lower", ok: g <= 8, detail: `grade ${g}` }, { rule: "30 days", ok: has(o, /\b(30|thirty)\)?\s+(\(30\)\s+)?days\b/i) }, { rule: "$45", ok: has(o, /\$\s?45\b/) }, { rule: "1 March 2026", ok: has(o, /(1(st)?\s+March|March\s+1(st)?),?\s+2026/i) }, tester("says members who joined before 1 March 2026 can get a partial refund"), ]; }, "translate-spanish": (o) => [ ...[["farmacia", "farmacia"], ["domingo", "domingos?"], ["lunes", "lunes"], ["10", "10"], ["seguro", "seguro"]].map(([w, re]) => ({ rule: `contains ${w}`, ok: has(o, new RegExp(`\\b${re}\\b`, "i")) })), tester("Spanish only, all three sentences' meaning kept"), ], "extract-dates": (o) => { const days = [...norm(o).matchAll(/\b(\d{1,2})(?:st|nd|rd|th)?\s+June|June\s+(\d{1,2})\b/gi)].map((m) => Number(m[1] ?? m[2])); const set = [...new Set(days)].sort((a, b) => a - b); return [ { rule: "exactly 2, 5, 6, 8 and 12 June", ok: set.join() === "2,5,6,8,12", detail: `found ${set.join(", ")}` }, tester("each date has the right purpose"), ]; }, "constrained-writing": (o) => { const bullets = norm(o) .split("\n") .filter((l) => /^\s*([-*•]|\d+[.)])\s+/.test(l)) .map((l) => l.replace(/^\s*([-*•]|\d+[.)])\s+/, "").replace(/\*\*/g, "")); return [ { rule: "exactly 3 bullets", ok: bullets.length === 3, detail: `${bullets.length} bullets` }, { rule: "each bullet 11 words or fewer", ok: bullets.length > 0 && bullets.every((b) => words(b) <= 11), detail: bullets.map(words).join(", ") }, { rule: "no exclamation mark", ok: !o.includes("!") }, { rule: "no 'perfect'", ok: !/perfect/i.test(o) }, ]; }, "bill-split": (o) => [{ rule: "$33.24 per person", ok: has(o, /\$\s?33\.24\b/) }], }; export function gradeTask(task: string, output: string): { rules: RuleResult[]; mechanicalPass: boolean; needsTester: boolean } { const check = CHECKS[task]; if (!check) throw new Error(`no checks for task ${task}`); const rules = check(output); return { rules, mechanicalPass: rules.every((r) => r.ok !== false), needsTester: rules.some((r) => r.ok === "tester"), }; }