WIP Hub 1.6.0: AI proxy for Cross Events 2.0

Signed /installs/ai/status and /installs/ai/complete (protocol 3b):
the Hub holds the Claude key, holds each call's worst case against the
licence's monthly cap in a locked transaction, calls Claude Haiku 5.5
and meters real cost and charged amount (markup) in rand. Settings ->
AI, licence markup override, AI use on the install page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-30 09:16:04 +02:00
co-authored by Claude Opus 5.5
parent 0f4d1c360a
commit 49f62aeb75
20 changed files with 1041 additions and 27 deletions
@@ -0,0 +1,35 @@
-- CreateEnum
CREATE TYPE "AiUsageStatus" AS ENUM ('reserved', 'done', 'failed');
-- AlterTable
ALTER TABLE "Licence" ADD COLUMN "aiMarkupPercent" INTEGER;
-- CreateTable
CREATE TABLE "AiUsage" (
"id" TEXT NOT NULL,
"installId" TEXT NOT NULL,
"helper" TEXT NOT NULL,
"model" TEXT NOT NULL,
"month" TEXT NOT NULL,
"status" "AiUsageStatus" NOT NULL DEFAULT 'reserved',
"inputTokens" INTEGER NOT NULL DEFAULT 0,
"outputTokens" INTEGER NOT NULL DEFAULT 0,
"cacheReadTokens" INTEGER NOT NULL DEFAULT 0,
"cacheWriteTokens" INTEGER NOT NULL DEFAULT 0,
"realMillicents" INTEGER NOT NULL DEFAULT 0,
"chargedMillicents" INTEGER NOT NULL DEFAULT 0,
"markupPercent" INTEGER NOT NULL,
"usdZar" DOUBLE PRECISION NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "AiUsage_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE INDEX "AiUsage_installId_month_idx" ON "AiUsage"("installId", "month");
-- CreateIndex
CREATE INDEX "AiUsage_month_idx" ON "AiUsage"("month");
-- AddForeignKey
ALTER TABLE "AiUsage" ADD CONSTRAINT "AiUsage_installId_fkey" FOREIGN KEY ("installId") REFERENCES "Install"("id") ON DELETE CASCADE ON UPDATE CASCADE;
+38 -1
View File
@@ -55,8 +55,10 @@ model Licence {
// Null = no end date.
expiresAt DateTime?
graceDays Int @default(14)
// Cross Events 2.0: monthly AI spend cap (in cents, rand). 0 = AI off.
// Cross Events 2.0: monthly AI spend cap (in cents, rand), counted in charged rand. 0 = AI off.
aiMonthlyCapCents Int @default(0)
// Markup on the real AI cost, in %. Null = the Hub default (Settings → AI).
aiMarkupPercent Int?
notes String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@ -90,10 +92,45 @@ model Install {
releasedAt DateTime?
heartbeats Heartbeat[]
events InstallEvent[]
aiUsage AiUsage[]
@@index([lastHeartbeatAt])
}
enum AiUsageStatus {
// The worst case is held against the cap while the call runs.
reserved
done
failed
}
// One AI call from an install (Cross Events 2.0). No prompt or response text is kept — only what's
// needed to meter it. Amounts are in millicents (1/1000 of a cent, rand): a call costs a few cents.
model AiUsage {
id String @id @default(uuid())
installId String
install Install @relation(fields: [installId], references: [id], onDelete: Cascade)
helper String
model String
// The billing month, YYYY-MM in South African time.
month String
status AiUsageStatus @default(reserved)
inputTokens Int @default(0)
outputTokens Int @default(0)
cacheReadTokens Int @default(0)
cacheWriteTokens Int @default(0)
// What Claude cost (real) and what the church is charged (real × (1 + markup)).
realMillicents Int @default(0)
chargedMillicents Int @default(0)
markupPercent Int
// Rand per US dollar used for this call.
usdZar Float
createdAt DateTime @default(now())
@@index([installId, month])
@@index([month])
}
// Every heartbeat for 30 days, then one per install per day.
model Heartbeat {
id String @id @default(uuid())