Files
CatoandClaude Opus 5.5 6b1801359c feat(updates): reader-initiated news updates; AI drafting only on readers' own keys (#102)
* feat(ai): drafting runs only on readers' own keys; ratchet against free-tier background AI

The hourly drafter spent the site's free models. Remove the free-chain path
(freeAsk, backgroundMay); drafting now takes an Ask built from a reader key
(lib/byok-ask.ts). /api/cron/drafts is gone; /api/cron/auto-updates drafts
only for readers who opted in, on their stored key, capped per day (013).
test/no-free-background-ai walks the import graph from every cron route and
fails on any path to the free chain; proven by mutation.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* feat(updates): Update news now for every reader; AI summaries and auto-updates on the reader's own key

- Bottleneck, company and desk pages: "Update news now" sweeps the web
  (no AI, signed out too, rate-limited, 15-min cooldown) and lists the leads.
- "Summarise with AI" drafts up to three on the reader's key (browser or
  sealed); without one it explains the free AI is kept for questions and
  opens the key panel.
- /account/settings#auto-updates: opt-in hourly drafting on the stored key,
  daily cap with a token estimate (3.4k/draft, measured).
- Honest copy: /data/freshness reports drafting as on demand, /review, /data
  AI spend, desk and docs say drafting runs only on readers' keys.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-24 22:33:46 -07:00

24 lines
1.1 KiB
TypeScript

/**
* The reader's own key for one request: from the body (held in their
* browser), or — signed in, `byokStored: true` — opened from the vault.
* Shared by Ask and "Summarise with AI", so the two cannot disagree about
* whose key a request carries. The key never leaves this request.
*/
import { currentSession } from './auth';
import { byokFromBody, type ByokConfig } from './byok';
import { openStoredKey } from './byok-vault';
export type ReaderKey = { key: ByokConfig } | { key: null } | { error: string };
export async function readerKey(input: unknown): Promise<ReaderKey> {
const parsed = byokFromBody((input as { byok?: unknown } | null)?.byok);
if (parsed === 'invalid') return { error: 'Invalid key configuration.' };
if (parsed) return { key: parsed };
if ((input as { byokStored?: unknown } | null)?.byokStored !== true) return { key: null };
const session = await currentSession();
const stored = session?.actorId ? await openStoredKey(session.actorId).catch(() => null) : null;
return stored
? { key: stored }
: { error: 'Your saved key could not be opened. Add it again in AI settings.' };
}