import { one, query } from './db'; import { listReviews } from './reviews'; import { Area, Role } from './worktypes'; import type { ContentNode } from './content-tree'; export async function getContent(node: ContentNode) { const areas = await query('SELECT a.*, 0 AS n FROM areas a WHERE a.id = ANY(?) ORDER BY sort', [node.areas]); const [roles, byRole, benchmarks, taxonomy, papers, total, reviews] = await Promise.all([ query('SELECT r.*, 0 AS n FROM roles r ORDER BY sort'), query<{ type_id: string; n: number; n_llm: number }>( `SELECT type_id, COUNT(*) AS n, SUM((source='llm')::int) AS n_llm FROM paper_work_types WHERE split_part(type_id,'|',1) = ANY(?) GROUP BY type_id`, [node.areas]), query<{ id: number; name: string; n: number; n_new: number }>( `SELECT b.id, b.name, COUNT(DISTINCT pb.paper_id) AS n, SUM(pb.is_new) AS n_new FROM paper_benchmarks pb JOIN benchmarks b ON b.id = pb.bench_id JOIN paper_work_types w ON w.paper_id = pb.paper_id WHERE split_part(w.type_id,'|',1) = ANY(?) GROUP BY b.id ORDER BY n DESC, b.id LIMIT 12`, [node.areas]), query<{ id: string; label: string; n: number }>( `SELECT pt.node_id AS id, t.label, COUNT(DISTINCT pt.paper_id) AS n FROM paper_taxonomy pt JOIN taxonomy t ON t.id = pt.node_id WHERE pt.node_id = ANY(?) GROUP BY pt.node_id, t.label`, [node.taxonomy]), query<{ id: number; title: string; conf_short: string; date: string; summary: string | null }>( `SELECT p.id, p.title, c.short AS conf_short, COALESCE(c.start_date,'2026-06-01') AS date, pm.summary FROM papers p JOIN conferences c ON c.slug = p.conf LEFT JOIN paper_meta pm ON pm.paper_id = p.id WHERE p.id IN (SELECT paper_id FROM paper_work_types WHERE split_part(type_id,'|',1) = ANY(?) AND source = 'llm') ORDER BY date DESC, p.id LIMIT 20`, [node.areas]), one<{ n: number }>(`SELECT COUNT(DISTINCT paper_id) AS n FROM paper_work_types WHERE split_part(type_id,'|',1) = ANY(?)`, [node.areas]), listReviews(), ]); const cells = new Map(byRole.map((r) => [r.type_id, r])); return { areas, total: total?.n ?? 0, roles: roles .map((r) => ({ ...r, cells: node.areas.map((a) => ({ id: `${a}|${r.id}`, ...(cells.get(`${a}|${r.id}`) ?? { n: 0, n_llm: 0 }) })) })) .filter((r) => r.cells.some((c) => c.n > 0)), benchmarks, taxonomy, papers, reviews: reviews.filter((r) => node.areas.includes(r.area)), }; }