import { db } from './db'; export interface Role { id: string; label: string; descr: string; fixes: string; varies: string; pattern: string; n: number } export interface Area { id: string; label: string; kind: 'agent' | 'general'; n: number } export function getMatrix(opts: { src?: 'llm' | 'all' } = {}) { const llmOnly = opts.src === 'llm'; const roles = db().prepare(`SELECT r.*, (SELECT COUNT(*) FROM paper_work_types w WHERE substr(w.type_id, instr(w.type_id,'|')+1) = r.id ${llmOnly ? "AND w.source='llm'" : ''}) AS n FROM roles r ORDER BY sort`).all() as Role[]; const areas = db().prepare(`SELECT a.*, (SELECT COUNT(*) FROM paper_work_types w WHERE substr(w.type_id,1,instr(w.type_id,'|')-1) = a.id ${llmOnly ? "AND w.source='llm'" : ''}) AS n FROM areas a ORDER BY sort`).all() as Area[]; const cells = db() .prepare(`SELECT type_id AS id, COUNT(*) AS n, SUM(source='llm') AS n_llm FROM paper_work_types GROUP BY type_id`) .all() as { id: string; n: number; n_llm: number }[]; return { roles, areas: areas.filter((a) => a.n > 0), cells: Object.fromEntries(cells.map((c) => [c.id, llmOnly ? c.n_llm : c.n])) as Record }; } export interface TypeMember { id: number; title: string; authors: string | null; conf_short: string; conf: string; date: string; track: string | null; why: string | null; source: string; summary: string | null; fixes: string[]; varies: string[]; workflow: string[]; } const j = (s: string | null): string[] => { try { return s ? (JSON.parse(s) as string[]) : []; } catch { return []; } }; const DATE = "COALESCE(c.start_date, '2026-06-01')"; export function getWorkType(id: string, page: number, srcLlm: boolean) { const d = db(); const t = d.prepare(`SELECT * FROM work_types WHERE id = ?`).get(id) as { id: string; area: string; role: string; n_papers: number; n_llm_labeled: number } | undefined; if (!t) return null; const area = d.prepare('SELECT * FROM areas WHERE id = ?').get(t.area) as Area; const role = d.prepare('SELECT * FROM roles WHERE id = ?').get(t.role) as Role; const srcSql = srcLlm ? "AND pwt.source = 'llm'" : ''; const PAGE = 60; const total = (d.prepare(`SELECT COUNT(*) AS n FROM paper_work_types pwt WHERE pwt.type_id = ? ${srcSql}`).get(id) as { n: number }).n; const rows = d.prepare(` SELECT p.id, p.title, p.authors, c.short AS conf_short, c.slug AS conf, ${DATE} AS date, p.track, pwt.why, pwt.source, pm.summary, pm.fixes, pm.varies, pm.workflow FROM paper_work_types pwt JOIN papers p ON p.id = pwt.paper_id JOIN conferences c ON c.slug = p.conf LEFT JOIN paper_meta pm ON pm.paper_id = p.id WHERE pwt.type_id = ? ${srcSql} ORDER BY date, p.id LIMIT ? OFFSET ?`).all(id, PAGE, (page - 1) * PAGE) as (Omit & { fixes: string; varies: string; workflow: string })[]; const members: TypeMember[] = rows.map((r) => ({ ...r, fixes: j(r.fixes), varies: j(r.varies), workflow: j(r.workflow) })); const timeline = d.prepare(` SELECT c.short AS label, ${DATE} AS date, COUNT(*) AS n FROM paper_work_types pwt JOIN papers p ON p.id = pwt.paper_id JOIN conferences c ON c.slug = p.conf WHERE pwt.type_id = ? ${srcSql} GROUP BY c.slug ORDER BY date`).all(id) as { label: string; date: string; n: number }[]; const benchmarks = d.prepare(` SELECT b.id, b.name, COUNT(DISTINCT pb.paper_id) AS n, SUM(pb.is_new) AS n_new, (SELECT group_concat(purpose, ' | ') FROM (SELECT DISTINCT pb2.purpose AS purpose FROM paper_benchmarks pb2 JOIN paper_work_types w2 ON w2.paper_id = pb2.paper_id WHERE pb2.bench_id = b.id AND w2.type_id = ? AND pb2.purpose <> '' LIMIT 3)) AS purposes FROM paper_benchmarks pb JOIN benchmarks b ON b.id = pb.bench_id JOIN paper_work_types pwt ON pwt.paper_id = pb.paper_id WHERE pwt.type_id = ? GROUP BY b.id ORDER BY n DESC LIMIT 15`).all(id, id) as { id: number; name: string; n: number; n_new: number; purposes: string | null }[]; const results = getResults({ typeId: id }); const siblings = d.prepare(`SELECT w.id, w.role, w.n_papers, r.label, r.fixes, r.varies FROM work_types w JOIN roles r ON r.id = w.role WHERE w.area = ? ORDER BY w.n_papers DESC`).all(t.area) as { id: string; role: string; n_papers: number; label: string; fixes: string; varies: string }[]; const sameRole = d.prepare(`SELECT w.id, w.n_papers, a.label FROM work_types w JOIN areas a ON a.id = w.area WHERE w.role = ? AND w.area <> ? AND a.kind = ? ORDER BY w.n_papers DESC LIMIT 8`).all(t.role, t.area, area.kind) as { id: string; n_papers: number; label: string }[]; const note = d.prepare('SELECT * FROM type_notes WHERE type_id = ?').get(id) as { why_same: string; workflow: string; bench_summary: string; narrative: string } | undefined; const workflows = members.filter((m) => m.workflow.length).slice(0, 6).map((m) => ({ id: m.id, title: m.title, steps: m.workflow })); return { type: t, area, role, total, pageSize: PAGE, members, timeline, benchmarks, results, siblings, sameRole, note: note ? { ...note, workflow: j(note.workflow) } : null, workflows }; } export interface ResultRow { id: number; paper_id: number; title: string; system: string | null; base_model: string | null; setting: string | null; metric: string | null; score: number; unit: string | null; date: string; bench_id: number; bench: string; conf_short: string } export function getResults(f: { typeId?: string; benchId?: number }): ResultRow[] { const where: string[] = []; const params: unknown[] = []; if (f.benchId) { where.push('r.bench_id = ?'); params.push(f.benchId); } if (f.typeId) { where.push('r.paper_id IN (SELECT paper_id FROM paper_work_types WHERE type_id = ?)'); params.push(f.typeId); } return db().prepare(` SELECT r.id, r.paper_id, p.title, r.system, r.base_model, r.setting, r.metric, r.score, r.unit, ${DATE} AS date, r.bench_id, b.name AS bench, c.short AS conf_short FROM results r JOIN papers p ON p.id = r.paper_id JOIN conferences c ON c.slug = p.conf JOIN benchmarks b ON b.id = r.bench_id ${where.length ? 'WHERE ' + where.join(' AND ') : ''} ORDER BY date LIMIT 500`).all(...params) as ResultRow[]; } export function getBenchmarks(minPapers = 2) { return db().prepare(`SELECT id, name, n_papers, n_new, (SELECT COUNT(*) FROM results r WHERE r.bench_id = benchmarks.id) AS n_results FROM benchmarks WHERE n_papers >= ? ORDER BY n_papers DESC`).all(minPapers) as { id: number; name: string; n_papers: number; n_new: number; n_results: number }[]; } export function getBenchmark(id: number) { const d = db(); const b = d.prepare('SELECT * FROM benchmarks WHERE id = ?').get(id) as { id: number; name: string; n_papers: number; n_new: number } | undefined; if (!b) return null; const papers = d.prepare(` SELECT p.id, p.title, c.short AS conf_short, ${DATE} AS date, pb.purpose, pb.is_new, pb.source, pm.summary, (SELECT group_concat(w.type_id, ',') FROM paper_work_types w WHERE w.paper_id = p.id) AS types FROM paper_benchmarks pb JOIN papers p ON p.id = pb.paper_id JOIN conferences c ON c.slug = p.conf LEFT JOIN paper_meta pm ON pm.paper_id = p.id WHERE pb.bench_id = ? ORDER BY date, p.id LIMIT 300`).all(id) as { id: number; title: string; conf_short: string; date: string; purpose: string; is_new: number; source: string; summary: string | null; types: string | null }[]; const byType = d.prepare(` SELECT w.type_id AS id, COUNT(*) AS n FROM paper_benchmarks pb JOIN paper_work_types w ON w.paper_id = pb.paper_id WHERE pb.bench_id = ? GROUP BY w.type_id ORDER BY n DESC LIMIT 12`).all(id) as { id: string; n: number }[]; return { bench: b, papers, byType, results: getResults({ benchId: id }) }; } export function getPaperWork(paperId: number) { const d = db(); const types = d.prepare(`SELECT w.type_id AS id, w.why, w.source, a.label AS area_label, r.label AS role_label, r.fixes, r.varies FROM paper_work_types w JOIN areas a ON a.id = substr(w.type_id,1,instr(w.type_id,'|')-1) JOIN roles r ON r.id = substr(w.type_id,instr(w.type_id,'|')+1) WHERE w.paper_id = ?`).all(paperId) as { id: string; why: string; source: string; area_label: string; role_label: string; fixes: string; varies: string }[]; const meta = d.prepare('SELECT * FROM paper_meta WHERE paper_id = ?').get(paperId) as { summary: string; fixes: string; varies: string; workflow: string; label_source: string } | undefined; const benchmarks = d.prepare(`SELECT b.id, b.name, pb.purpose, pb.is_new FROM paper_benchmarks pb JOIN benchmarks b ON b.id = pb.bench_id WHERE pb.paper_id = ?`).all(paperId) as { id: number; name: string; purpose: string; is_new: number }[]; return { types, meta: meta ? { ...meta, fixes: j(meta.fixes), varies: j(meta.varies), workflow: j(meta.workflow) } : null, benchmarks }; }