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:
@@ -0,0 +1,76 @@
|
||||
import { apiV2Data, apiV2Raw } from "@/lib/apiV2";
|
||||
import type { V2Result } from "@/lib/apiV2";
|
||||
|
||||
/**
|
||||
* AI helpers (2.0). The site calls AI through the Cross Events Hub, which keeps a monthly
|
||||
* allowance in rand per licence. One status per page load is shared by every AI button
|
||||
* (`useAiStatus`), and every helper call refreshes it — so the buttons disable straight after the
|
||||
* call that used up the allowance.
|
||||
*/
|
||||
|
||||
export type AiStatus = V2Result<"/ai/status", "get">["data"];
|
||||
export type AiHelper = keyof AiStatus["helpers"];
|
||||
|
||||
const OFF: AiStatus = {
|
||||
enabled: false, capReached: false, available: true, capCents: 0, usedCents: 0, usedPercent: 0, renewsOn: null,
|
||||
helpers: { draft_event: false, summarise_report: false, draft_message: false, summarise_feedback: false, improve_text: false },
|
||||
};
|
||||
|
||||
let current: AiStatus | null = null;
|
||||
let loading: Promise<AiStatus> | null = null;
|
||||
const listeners = new Set<(s: AiStatus) => void>();
|
||||
|
||||
function publish(s: AiStatus) {
|
||||
current = s;
|
||||
listeners.forEach(l => l(s));
|
||||
}
|
||||
|
||||
/** The status, fetched once per page load (staff never see AI, so they never ask). */
|
||||
export function loadAiStatus(authToken: string, { fresh = false } = {}): Promise<AiStatus> {
|
||||
if (current && !fresh) return Promise.resolve(current);
|
||||
if (!loading || fresh) {
|
||||
loading = (apiV2Data("get", "/ai/status", { authToken }) as Promise<AiStatus>)
|
||||
.then(s => { publish(s); return s; })
|
||||
.catch(() => { publish(OFF); return OFF; })
|
||||
.finally(() => { loading = null; });
|
||||
}
|
||||
return loading;
|
||||
}
|
||||
|
||||
export function subscribeAiStatus(fn: (s: AiStatus) => void): () => void {
|
||||
listeners.add(fn);
|
||||
return () => { listeners.delete(fn); };
|
||||
}
|
||||
|
||||
export function currentAiStatus(): AiStatus | null {
|
||||
return current;
|
||||
}
|
||||
|
||||
/** "1 December" from "2026-12-01". */
|
||||
export function renewsText(renewsOn: string | null): string {
|
||||
if (!renewsOn) return "next month";
|
||||
return new Date(`${renewsOn}T12:00:00Z`).toLocaleDateString("en-ZA", { day: "numeric", month: "long" });
|
||||
}
|
||||
|
||||
/** What the button's tooltip says when it can't be used. */
|
||||
export function disabledReason(s: AiStatus, helper: AiHelper): string | null {
|
||||
if (s.capReached) return `This month's AI allowance is used up — it renews on ${renewsText(s.renewsOn)}.`;
|
||||
if (!s.available) return "AI is unavailable right now. Try again in a few minutes.";
|
||||
if (!s.helpers[helper]) return "This AI helper is switched off in Site settings → AI.";
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a helper (`POST /api/v2/ai/<helper>`). Resolves to its answer; throws the API error (with
|
||||
* a message written for people). Either way the shared status is refreshed.
|
||||
*/
|
||||
export async function runAi<T>(helper: AiHelper, body: object, authToken: string): Promise<T> {
|
||||
try {
|
||||
const res = await apiV2Raw<{ output: T; status: AiStatus }>("post", `/ai/${helper.replace(/_/g, "-")}`, { authToken, body });
|
||||
publish(res.status);
|
||||
return res.output;
|
||||
} catch (e) {
|
||||
loadAiStatus(authToken, { fresh: true }).catch(() => {});
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user