Three pieces built together tonight since they're naturally linked (the
server-class proxy is the real entitlement enforcement chokepoint):
1. Multi-key BYOK (public class): several named provider profiles
(name/baseUrl/model), each with its own key in lib/ai/key-store.ts
(keyed by profile id, not a single fixed 'public' slot). The "Try it"
pane lets you pick which saved profile answers each question - not one
fixed default.
2. `server` class, real: app/api/ai/server/{models,chat} proxy through
this app's own backend to AI_SERVER_BASE_URL - same-origin from the
browser, no CORS/OLLAMA_ORIGINS story at all, standing in tonight for
VNC's EU/CH-hosted infra with the real Ollama on this Mac (swapping to
the real instance tomorrow is a config change).
3. Real entitlement enforcement (lib/ai/entitlement.ts), scoped to `server`
only (not local/public, per the 2026-08-05 decisions): checkAndAssignSeat()
re-validates on every /api/ai/server/chat call - first use auto-assigns a
seat if any remain, further calls from an unlicensed user get a 402 with
a specific reason. recordUsage() appends to an append-only metering
ledger (timestamp/user/model/tokens/latency) that IS the billing record.
Admin data endpoints at /api/admin/ai/entitlement (seat total, revoke) -
the visual admin console is a separate, not-yet-built task.
Two real bugs found and fixed during verification, not just claimed fixed:
- /api/ai/policy never actually added 'server' to entitlement.classes even
when AI_SERVER_BASE_URL was set (only the type comment was updated) - the
Server radio option silently never appeared until this was caught live.
- The new routes used readStalwartAuthContext(0) (hardcoded slot, SSO/reauth-
specific) instead of getStalwartCredentials() (the general multi-slot
session resolver every other authenticated route uses) - reachable but
wrong, and would have hidden a real auth gap behind "works on my slot".
Verified end-to-end for real: built + ran the actual server, logged in via
the real (non-demo) auth flow, selected Server, listed the real Ollama
models through the proxy, asked "Reply with exactly the words: SERVER CLASS
WORKS" and got back exactly that - plus confirmed on disk (not just in the
UI) that data/admin-state/ai-entitlement.json recorded the seat assignment
and ai-metering.jsonl recorded real prompt/completion token counts and
latency from the actual model call. Rejection-path logic (seat limit
reached, zero seats configured, revocation) covered by 5 new unit tests
rather than a second live round trip. Full suite: typecheck clean, lint
clean, translations 48/48, production build succeeds.
33 lines
1.5 KiB
TypeScript
33 lines
1.5 KiB
TypeScript
// Client-held storage for the user's own public-provider API keys (BYOK).
|
|
//
|
|
// Decision 2026-08-05 (reverses docs/AI-ASSISTANT-CONCEPT.md decision #1's
|
|
// server-side-custody design): the user brings and holds their own keys,
|
|
// client-side, not VNC. This is the same custody model as
|
|
// vncmail-native's lib/ai-key-store.ts (expo-secure-store there; this repo
|
|
// has no OS keychain access from a browser tab, so localStorage is the
|
|
// honest equivalent here — plain, not hidden behind a false sense of
|
|
// "secure storage").
|
|
//
|
|
// Decision 2026-08-05 (later same night): several keys, not one — a user may
|
|
// hold multiple named provider profiles (different models, different
|
|
// providers) and pick which one answers a given question. Keys are stored
|
|
// separately from `lib/ai/local-settings.ts`'s profile metadata (name, base
|
|
// URL, model) so a profile can be exported/shared without its secret, and so
|
|
// clearing one key can't accidentally corrupt the profile list.
|
|
const KEY_PREFIX = 'vncmail:ai:key:';
|
|
|
|
export function getAiApiKey(profileId: string): string | null {
|
|
if (typeof window === 'undefined') return null;
|
|
return window.localStorage.getItem(KEY_PREFIX + profileId);
|
|
}
|
|
|
|
export function setAiApiKey(profileId: string, key: string): void {
|
|
if (typeof window === 'undefined') return;
|
|
window.localStorage.setItem(KEY_PREFIX + profileId, key);
|
|
}
|
|
|
|
export function clearAiApiKey(profileId: string): void {
|
|
if (typeof window === 'undefined') return;
|
|
window.localStorage.removeItem(KEY_PREFIX + profileId);
|
|
}
|