Files
ai-taxonomy-literature-anal…/lib/reviews.ts
T
2026-10-22 15:38:57 +00:00

58 lines
3.6 KiB
TypeScript

import { one, query } from './db';
export interface Review {
type_id: string; title: string; abstract: string;
perspective: { options: { name: string; pros: string; cons: string }[]; chosen: string; why: string };
outline: { section: string; purpose: string; words: number }[];
subfields: { name: string; description: string; focus: string; key_paper_ids: number[]; paper_ids: number[]; relations: string }[];
evolution: { pioneering: { id: number; why: string }[]; paths: { name: string; paper_ids: number[]; narrative: string }[]; gaps: string[]; connections: string[]; reading_order: number[] };
papers: { id: number; question: string; innovation: string; method: string; findings: string; contribution: string; limitations: string; summary: string; subfield: string }[];
methods: { families: { name: string; description: string; paper_ids: number[]; pros: string; cons: string; trend: string }[]; trends: string; suggested_new_methods: string[] };
key_data: { rows: { id: number; subject: string; metric: string; data: string; conclusion: string }[]; patterns: string; contradictions: string; missing: string };
sections: { introduction: string; body: { title: string; subfield: string; content: string }[]; comparative: string; challenges: string; future: string; conclusion: string };
novel_perspective: string;
quality: { checks: { issue: string; status: string; note: string }[]; limitations_of_this_review: string };
}
export interface RefMeta { id: number; title: string; authors: string; venue: string; date: string; url: string | null }
const CITE = /\[#(\d+)(?:\s*[,;]\s*#?(\d+))*\]/g;
/** Order of first citation across the article -> reference numbers. */
function citationOrder(r: Review): number[] {
const parts = [r.sections.introduction, ...r.sections.body.map((b) => b.content), r.sections.comparative, r.sections.challenges, r.sections.future, r.sections.conclusion];
const seen: number[] = [];
for (const t of parts) for (const m of (t ?? '').matchAll(/#(\d+)/g)) {
const id = Number(m[1]); if (!seen.includes(id)) seen.push(id);
}
return seen;
}
export async function getReview(typeId: string) {
let row: { json: string } | undefined;
try { row = await one<{ json: string }>('SELECT json FROM type_reviews WHERE type_id = ?', [typeId]); } catch { return null; }
if (!row) return null;
const review = JSON.parse(row.json) as Review;
const memberIds = review.papers.map((p) => p.id);
const order = citationOrder(review).filter((id) => memberIds.includes(id));
const all = [...order, ...memberIds.filter((id) => !order.includes(id))];
const found = all.length
? await query<RefMeta>(`SELECT p.id, p.title, p.authors, c.short || ' 2026' AS venue, COALESCE(c.start_date,'2026-06-01') AS date, p.url FROM papers p JOIN conferences c ON c.slug = p.conf WHERE p.id = ANY(?)`, [all])
: [];
const byId = new Map(found.map((r) => [r.id, r]));
const refs: RefMeta[] = all.map((id) => byId.get(id)).filter((r): r is RefMeta => !!r).map((r) => {
const a = (r.authors ?? '').split(', ');
return { ...r, authors: a[0] ? a[0] + (a.length > 1 ? ' et al.' : '') : '' };
});
return { review, refs, citeNum: Object.fromEntries(all.map((id, i) => [id, i + 1])) as Record<number, number> };
}
export async function listReviews() {
try {
return await query<{ type_id: string; title: string; abstract: string; n: number; area: string; role: string }>(
`SELECT r.type_id, r.json::json->>'title' AS title, r.json::json->>'abstract' AS abstract, w.n_llm_labeled AS n, w.area, w.role
FROM type_reviews r JOIN work_types w ON w.id = r.type_id ORDER BY w.area, w.n_llm_labeled DESC`);
} catch { return []; }
}
export { CITE };