feat(ai): resolve an AI usage level per record and publish it as ai.usage

Three levels (frugal, standard, full) with an operator ceiling and default
and a per-account choice within them, resolved in the capability layer for
the record and published on GET /api/auth/me as ai.usage: the level in
effect, the stored choice, the bounds, why (user, default, admin_cap), the
three options with whether each is allowed and an estimate of tokens per
day scaled from the record's last seven full days of coach_usage, and the
measured mean. A delegate sees the level, never the owner's spend.

PUT /api/auth/me/ai-usage stores the account's own choice or null for the
default, refusing a level above the ceiling with 422 ai.usage.aboveCeiling
and meta.maxLevel. The new supplementLabel capability needs standard or
above; below it the capability is unavailable with the new usage_level
reason, refused with 403 ai.usage.levelTooLow and meta.requiredLevel.

Refs #1217
This commit is contained in:
Marc-André Bombeck committed 2026-10-15 22:49:01 +02:00
1 parent e573f28ae8
commit 6f673a7b84
22 files changed
+1342 -84

No files matched your search

@@ -19,6 +19,7 @@ import { describe, expect, it } from "vitest";
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { AI_CAPABILITY_KEYS } from "@/lib/ai/capabilities/types";
import { DOCUMENT_AI_RUN_KINDS } from "@/lib/documents/ai-runs/types";
import type { runStatusCompletion } from "@/lib/insights/status-provider";
@@ -283,3 +284,29 @@ describe("the status chokepoint's capability argument", () => {
expect(withCapability.capability).toBe("statusText");
});
});
/**
* v1.43 (#1217) — capabilities whose model call has not landed yet. The
* capability is published (clients can render its state) before the route
* that reads a label photo exists; until then that route must not reach a
* provider at all. When it does, it becomes an egress site above, re-checking
* its capability at the wire like every document read, and leaves this list.
*/
const CAPABILITY_EGRESS_PENDING: Record<string, string> = {
supplementLabel: "app/api/medications/[id]/ingredients/extract/route.ts",
};
describe("capabilities whose model call is still to come", () => {
it.each(Object.entries(CAPABILITY_EGRESS_PENDING))(
"%s is a published capability whose route does not call a model yet",
(key, rel) => {
expect(AI_CAPABILITY_KEYS as readonly string[]).toContain(key);
const text = code(rel);
expect(
EGRESS_CALL.test(text),
`${rel} calls a provider now: list it in EGRESS_SITES with its re-check and drop it here`,
).toBe(false);
expect(/\bnotImplemented\s*\(/.test(text)).toBe(true);
},
);
});
@@ -39,7 +39,6 @@ const CONTRACT_STUBS: Readonly<Record<string, readonly string[]>> = {
],
"anamnesis/life-circumstances/route.ts": ["anamnesis.life_circumstances.get"],
"auth/claim/sso/route.ts": ["profile_claim.sso.start"],
"auth/me/ai-usage/route.ts": ["auth.me.ai_usage.put"],
"biomarkers/[id]/merge/route.ts": ["labs.biomarker.merge"],
"dawarich/connect/route.ts": ["dawarich.connect"],
"dawarich/disconnect/route.ts": ["dawarich.disconnect"],
@@ -64,7 +63,7 @@ const CONTRACT_STUBS: Readonly<Record<string, readonly string[]>> = {
};
/** The size of the register when it was written; see the header. */
const REGISTER_SIZE = 24;
const REGISTER_SIZE = 23;
function routeFiles(): string[] {
return walkSourceFiles(API_ROOT, { floor: 400, extensions: [".ts"] })