#33
Add Smart Compose AI writing assistance
Summary
- add opt-in Smart Compose using the existing OpenAI, Anthropic, and Google BYOK connection
- offer responsive inline completions after a 100–280 ms pause, with Tab to accept and Escape to dismiss
- keep one useful request alive while typing, reuse matching completion text instantly, and retrigger from new caret positions and lines
- add tone-based reply drafting plus selection-aware or full-draft Improve writing, Shorten, Expand, and Fix spelling & grammar actions
- keep rich text, line breaks, editor focus, and the Redakt footer intact while applying suggestions
- keep mail delivery user-controlled and load recent thread context on the server
- add separate consent-aware settings, request throttling, migration, tests, and legal documentation
Validation
corepack pnpm test(162 passed)corepack pnpm exec tsc --noEmit- focused ESLint on every changed TypeScript/TSX/MJS file
corepack pnpm build- browser QA at desktop and 320px, using mocked compose responses so no provider tokens were consumed
- verified responsive autocomplete timing, matching-text carry-forward, Enter retriggering from a new line, Tab chaining, selected-text and full-draft refinements, footer preservation, focus restoration, and responsive menu layout
Activity
26mikeyreviewed2mo ago
d35ec66 into main2mo ago
Review summary
Reviewed the full diff (29 files, +2448/-126): API route (
app/api/mail/compose/route.ts), prompt construction and input parsing (lib/ai/compose.ts), provider fan-out (lib/ai/provider.ts), settings/consent gating (lib/ai/settings.ts,lib/data/ai-settings.ts), the new rate limiter (lib/ai/composer-rate-limit.ts,lib/data/composer-rate-limit.ts), the DB migration/schema change, and the client-side editor integration (components/mail/smart-compose.tsx,rich-text-editor.tsx, both composers).Findings:
accountIdis resolved viaresolveMailAccount(user, accountId), which scopes lookups touser.id(lib/mail/accounts.ts), so there's no cross-account thread/account access via the new compose endpoint.getEnabledAiCredential(user, "composer")requirescomposerEnabled, a stored API key, and a currentconsentVersion— consistent with the existing Focus gate, andAI_CONSENT_VERSIONwas bumped to 2 so existing users must re-consent under the new copy that discloses Smart Compose data use.consumeComposerRateLimituses a single atomicUPDATE ... WHEREwith a SQLCASEto reset/increment the window, gated by the same condition in theWHEREclause, so concurrent requests can't race past the 100/10min cap. The demo-mode in-memory path uses matching semantics and both are covered bycomposer-rate-limit.test.mjs.parseComposeAssistInputrejects unknown keys, bounds every string field, and enforces minimum content length before contacting a provider, preventing empty/oversized calls.COMPOSE_SYSTEM_PROMPTexplicitly instructs the model to treat thread/draft content as untrusted data and never follow embedded instructions; output is constrained to a JSON schema (composeResponseSchema, max 5000 chars) and further normalized/truncated innormalizeComposeResponse.selection.insertText/editor state rather thandangerouslySetInnerHTMLor raw HTML injection, so provider output can't execute as markup.FocusProviderErrorwas cleanly renamed toAiProviderErrorand reused for both Focus and Compose call sites (app/api/mail/focus/route.tsupdated to match); the settings UI, migration, and Drizzle schema additions (composer_enabled,composer_request_count,composer_window_started_at) are all consistent with each other.No blocking correctness or security issues found in this pass. CI (
Validate application) is green.Non-blocking observations (not required for merge):
app/api/mail/compose/route.ts, the composer rate limit is consumed viaconsumeComposerRateLimitbeforegetMailProvider/resolveMailAccountvalidatesaccountId. A request with a bogusaccountIdstill burns one unit of the caller's own rate limit before failing with a generic 502. Low impact (self-inflicted, same user/window), but worth confirming it's intentional.