WIP 2.0.0: AI helper plumbing (status, usage, Site settings -> AI)

The site calls AI through the Hub (protocol 3b, Hub 1.6.0): src/ai
works out whether the AI buttons show (licence token first, then the
Hub, cached a minute), runs a helper call with per-helper switches and
a per-person daily limit, logs AiCall rows, and emails admins at 80%.
v2 /ai/status, /ai/usage, /ai/settings; useAiStatus, AiButton, AiTab.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-30 09:25:36 +02:00
co-authored by Claude Opus 5.5
parent 838a6b3c3d
commit dbec47aae3
19 changed files with 1760 additions and 19 deletions
@@ -0,0 +1,24 @@
"use client";
import { useEffect, useState } from "react";
import { useAuth } from "@/hooks/useAuth";
import { currentAiStatus, loadAiStatus, subscribeAiStatus, type AiStatus } from "@/lib/ai";
/**
* Whether to show the AI buttons, shared across the page (one request per page load). Null while
* loading, and for anyone who isn't a supervisor or admin — AI helpers are theirs only.
*/
export function useAiStatus(): AiStatus | null {
const { user, token } = useAuth();
const allowed = !!token && (user?.role === "admin" || user?.role === "supervisor");
const [status, setStatus] = useState<AiStatus | null>(() => (allowed ? currentAiStatus() : null));
useEffect(() => {
if (!allowed || !token) return;
const off = subscribeAiStatus(setStatus);
loadAiStatus(token).then(setStatus).catch(() => {});
return off;
}, [allowed, token]);
return allowed ? status : null;
}