WIP 2.0.0: AI buttons on the five screens, eval set, privacy notice

Describe your event (new event), Improve/Shorter/Fix mistakes (event
description, banner, email and WhatsApp boxes), Draft with AI (email and
WhatsApp attendees), Summarise this report (people-free reports) and
Summarise feedback (per event). Built-in privacy notice text for AI
(2.0.0 entry in BUILT_IN_UPDATES). npm run ai:eval runs the trial cases
through the Hub with fact checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ls1vMeckMWrKPfc1MMyMJF
This commit is contained in:
2026-10-30 18:19:46 +02:00
co-authored by Claude Opus 5.5
parent d4f54f2cc8
commit 4924d1ed76
14 changed files with 620 additions and 1 deletions
@@ -0,0 +1,71 @@
"use client";
import React, { useState } from "react";
import { useAuth } from "@/hooks/useAuth";
import { useAiStatus } from "@/hooks/useAiStatus";
import { runAi } from "@/lib/ai";
import type { V2Result } from "@/lib/apiV2";
import { errorMessage } from "@/lib/utils";
import { AiButton, AiNotice } from "@/components/shared/AiButton";
type Summary = V2Result<"/ai/summarise-feedback", "post">["data"]["output"];
const Themes = ({ title, list }: { title: string; list: { theme: string; mentions: number }[] }) => (
<div>
<p className="text-xs font-medium uppercase tracking-wide text-gray-500">{title}</p>
{list.length === 0 ? <p className="text-gray-500">—</p> : (
<ul className="mt-1 space-y-0.5">
{list.map(t => <li key={t.theme} className="flex justify-between gap-3"><span>{t.theme}</span><span className="shrink-0 tabular-nums text-gray-500">{t.mentions}</span></li>)}
</ul>
)}
</div>
);
/**
* "Summarise feedback" for one event: how it went, what people liked and want improved (with how
* many said so), a few quotes and suggestions. The server sends the answers without who gave them
* and records it in the access log. Hidden on sites without AI.
*/
export function AiFeedbackSummary({ eventId, responses }: { eventId: string; responses: number }) {
const { token } = useAuth();
const status = useAiStatus();
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
const [summary, setSummary] = useState<Summary | null>(null);
if (!status?.enabled || responses < 1) return null;
const run = async () => {
if (!token) return;
setBusy(true);
setError(null);
try { setSummary(await runAi<Summary>("summarise_feedback", { eventId }, token)); } catch (e) { setError(errorMessage(e) || "The AI couldn't summarise this feedback."); }
setBusy(false);
};
return (
<div className="mb-3 space-y-2">
{!summary && <AiButton helper="summarise_feedback" size="sm" busy={busy} onClick={run}>{busy ? "Summarising…" : "Summarise feedback"}</AiButton>}
{error && <p className="text-sm text-red-700" role="alert">{error}</p>}
{summary && (
<div className="space-y-3 rounded-lg border border-violet-200 bg-violet-50/60 p-3 text-sm">
<AiNotice />
<p>{summary.overall}</p>
<div className="grid gap-3 sm:grid-cols-2">
<Themes title="Liked" list={summary.liked} />
<Themes title="To improve" list={summary.improve} />
</div>
{summary.quotes.length > 0 && (
<div className="space-y-1">{summary.quotes.map(q => <blockquote key={q} className="border-l-2 border-violet-300 pl-2 italic text-gray-700">{q}</blockquote>)}</div>
)}
{summary.nextTime.length > 0 && (
<div>
<p className="text-xs font-medium uppercase tracking-wide text-gray-500">Next time</p>
<ul className="ml-4 mt-1 list-disc space-y-0.5">{summary.nextTime.map(n => <li key={n}>{n}</li>)}</ul>
</div>
)}
<button type="button" className="text-xs text-gray-600 underline" onClick={() => setSummary(null)}>Hide the summary</button>
</div>
)}
</div>
);
}
@@ -0,0 +1,68 @@
"use client";
import React, { useState } from "react";
import { AlertTriangle } from "lucide-react";
import { useAuth } from "@/hooks/useAuth";
import { useAiStatus } from "@/hooks/useAiStatus";
import { runAi } from "@/lib/ai";
import type { V2Body, V2Result } from "@/lib/apiV2";
import type { ReportPdfPayload } from "@/lib/export";
import { errorMessage } from "@/lib/utils";
import { AiButton, AiNotice } from "@/components/shared/AiButton";
type Summary = V2Result<"/ai/summarise-report", "post">["data"]["output"];
type ReportKey = V2Body<"/ai/summarise-report", "post">["reportKey"];
/** Reports without people in them: only these are sent to the AI (the server checks too). */
export const SUMMARISABLE_REPORTS: readonly string[] = ["seriesComparison", "regTypes", "usage", "revenue", "regStatus", "donations", "funnel", "discounts", "cashup", "financeReport", "profitReport"] satisfies ReportKey[];
/**
* "Summarise this report": a headline, a few insights and anything to act on, from the report as
* it is on screen (the same data as Print and Excel). Only on reports without people in them;
* hidden on sites without AI.
*/
export function AiReportSummary({ reportKey, getPayload }: { reportKey: string; getPayload: () => ReportPdfPayload | null }) {
const { token } = useAuth();
const status = useAiStatus();
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
const [summary, setSummary] = useState<Summary | null>(null);
if (!status?.enabled || !SUMMARISABLE_REPORTS.includes(reportKey)) return null;
const run = async () => {
const p = getPayload();
if (!token || !p?.table) { setError("This report has no figures to summarise yet."); return; }
setBusy(true);
setError(null);
try {
setSummary(await runAi<Summary>("summarise_report", {
reportKey, title: p.title, subtitle: p.subtitle,
stats: (p.stats || []).map(s => ({ label: s.label, value: s.value })),
columns: p.table.columns, rows: p.table.rows,
}, token));
} catch (e) { setError(errorMessage(e) || "The AI couldn't summarise this report."); }
setBusy(false);
};
return (
<div className="mb-4 space-y-2" data-testid="ai-report-summary">
{!summary && <AiButton helper="summarise_report" busy={busy} onClick={run}>{busy ? "Summarising…" : "Summarise this report"}</AiButton>}
{error && <p className="text-sm text-red-700" role="alert">{error}</p>}
{summary && (
<div className="space-y-2 rounded-lg border border-violet-200 bg-violet-50/60 p-4 text-sm">
<AiNotice />
<p className="font-medium text-gray-900">{summary.headline}</p>
<ul className="space-y-1.5">
{summary.insights.map(i => <li key={i.title}><span className="font-medium">{i.title}.</span> {i.detail}</li>)}
</ul>
{summary.watchOut.length > 0 && (
<ul className="space-y-1 rounded border border-amber-200 bg-amber-50 p-2 text-amber-900" aria-label="Needs attention">
{summary.watchOut.map(w => <li key={w} className="flex gap-1.5"><AlertTriangle className="mt-0.5 h-3.5 w-3.5 shrink-0" aria-hidden />{w}</li>)}
</ul>
)}
<button type="button" className="text-xs text-gray-600 underline" onClick={() => setSummary(null)}>Hide the summary</button>
</div>
)}
</div>
);
}
@@ -9,6 +9,8 @@ import { Printer, Mail, FileSpreadsheet, MessageCircle, ShoppingCart, CreditCard
import { REPORTS, type ReportKey } from "./ReportCatalog";
import ReportsShell from "./ReportsShell";
import ReportViewerModal, { ReportActionButton } from "./ReportViewerModal";
import { AiReportSummary } from "./AiReportSummary";
import { AiFeedbackSummary } from "./AiFeedbackSummary";
import { StatTile, StatTileRow } from "./StatTile";
import { HorizontalBarChart } from "./charts/HorizontalBarChart";
import { AreaTrendChart } from "@/components/charts/AreaTrendChart";
@@ -1566,6 +1568,7 @@ export default function ReportsV2({ backHref }: { backHref?: string } = {}) {
{!ready && <div className="text-sm text-gray-400">Loading…</div>}
{ready && (
<>
<AiReportSummary key={report} reportKey={report} getPayload={buildReportPayload} />
{report === "payments" && (
<div>
{paymentRows.length === 0 ? (
@@ -2015,6 +2018,7 @@ export default function ReportsV2({ backHref }: { backHref?: string } = {}) {
{f.sentAt ? `${f.responses} of ${f.invited} responded${f.responseRate != null ? ` (${f.responseRate}%)` : ""}` : "Not sent yet"}{f.anonymous ? " · anonymous" : ""}
</div>
</div>
<AiFeedbackSummary eventId={f.eventId} responses={f.responses || 0} />
<div className="space-y-4">
{(f.questions || []).map((q) => (
<div key={q.fieldId}>