Summarise this report on every report (2.0.1)

The eight reports that list people get the AI summary too, sent as
anonymised totals built on the screen (aiReportData.ts): no names,
contact details, notes or comments; consent and medical only as counts.
The server refuses personal columns on those reports and logs each
summary in the access log (ai_report; sensitive for consent and medical).

Roadmap §18: 2.0.1, then crosscode.co.za, then 2.1.0 (AI helpers on the
Consent log and Access log).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-02 18:42:27 +02:00
co-authored by Claude Opus 5.5
parent 8d7269a1e2
commit 40fa8b2dba
20 changed files with 359 additions and 48 deletions
+22 -3
View File
@@ -164,10 +164,26 @@ export async function draftEvent(userId: string, notes: string, now = new Date()
// ─── 2. Summarise a report ────────────────────────────────────────────────────
/** Reports with no people in them: only these can be summarised (keys from ReportCatalog). */
/** Reports with no people in them: sent as they are on screen (keys from ReportCatalog). */
export const REPORTS_WITHOUT_PEOPLE = ['seriesComparison', 'regTypes', 'usage', 'revenue', 'regStatus', 'donations', 'funnel', 'discounts', 'cashup', 'financeReport', 'profitReport'] as const;
/**
* Reports that list people: the screen sends these as anonymised totals instead (no names, contact
* details, notes or comments; medical and consent only as counts). The server checks the columns too.
*/
export const REPORTS_WITH_PEOPLE = ['payments', 'attendees', 'revenueDetailed', 'masterOrders', 'cashupAudit', 'consentStatus', 'feedbackSummary', 'medicalList'] as const;
export const REPORT_KEYS = [...REPORTS_WITHOUT_PEOPLE, ...REPORTS_WITH_PEOPLE] as const;
/** Children's or special information: an AI summary of one is a sensitive access-log entry. */
export const SENSITIVE_REPORTS: readonly string[] = ['consentStatus', 'medicalList'];
/** Column headings that would carry a person: refused on a report with people in it. */
const PERSONAL_COLUMN = /^(user|name|e-?mail|phone|cell|attendee|donor)$|^(parent|guardian|emergency|performed by|recorded by|registration ?id|date of birth|dob|medical$|notes?$|comments?$|signed)/i;
const MAX_ROWS = 400;
/** The columns of a people report that would carry a person (none means it's safe to send). */
export function personalColumns(r: { reportKey: string; columns: string[] }): string[] {
if (!(REPORTS_WITH_PEOPLE as readonly string[]).includes(r.reportKey)) return [];
return r.columns.filter(c => PERSONAL_COLUMN.test(c.trim()));
}
export type ReportInput = {
reportKey: string;
title: string;
@@ -187,6 +203,7 @@ const REPORT_SYSTEM = (now: Date) => [
'- A one-sentence headline, then 3–5 insights: trends, changes against earlier periods, outliers, and anything that needs action.',
'- Use the report\'s numbers; work out percentages and differences where they help, and get them right. Never invent figures or causes; when you suggest a cause, say it is only a possibility.',
'- Mind the dates against today: an event that has already happened can\'t still be promoted or paid for before it starts.',
'- Reports about people come as anonymised totals: names, contact details and notes are left out on purpose, so don\'t ask for them or guess who is who.',
'- Plain, friendly South African English. Amounts in rand like R18 900.',
].join('\n');
@@ -202,8 +219,10 @@ export function reportText(r: ReportInput): string {
}
export async function summariseReport(userId: string, report: ReportInput, now = new Date()): Promise<{ summary: ReportSummary; status: AiStatus }> {
if (!(REPORTS_WITHOUT_PEOPLE as readonly string[]).includes(report.reportKey)) {
throw Object.assign(new ApiError('FORBIDDEN', 'This report lists people, so it can\'t be sent to the AI. Summaries work on totals-only reports.'), { reason: 'AI_REPORT_HAS_PEOPLE' });
if (!(REPORT_KEYS as readonly string[]).includes(report.reportKey)) throw new ApiError('VALIDATION_FAILED', `There is no report called ${report.reportKey}.`);
const personal = personalColumns(report);
if (personal.length) {
throw Object.assign(new ApiError('FORBIDDEN', `This report still has people in it (${personal.join(', ')}), so it can't be sent to the AI. Only anonymised totals are summarised.`), { reason: 'AI_REPORT_HAS_PEOPLE' });
}
const p = protect(reportText(report));
const { output, status } = await runHelper('summarise_report', userId, { system: REPORT_SYSTEM(now), input: p.text, schema: REPORT_SCHEMA, maxTokens: 2000, effort: 'low' });