#33

Add Smart Compose AI writing assistance

MergedstylesshOwnermerged intofrom

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

26
mikeyreviewed2mo ago
mikey2mo 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:

  • Authorization: accountId is resolved via resolveMailAccount(user, accountId), which scopes lookups to user.id (lib/mail/accounts.ts), so there's no cross-account thread/account access via the new compose endpoint.
  • Consent/enablement gating: getEnabledAiCredential(user, "composer") requires composerEnabled, a stored API key, and a current consentVersion — consistent with the existing Focus gate, and AI_CONSENT_VERSION was bumped to 2 so existing users must re-consent under the new copy that discloses Smart Compose data use.
  • Rate limiting: consumeComposerRateLimit uses a single atomic UPDATE ... WHERE with a SQL CASE to reset/increment the window, gated by the same condition in the WHERE clause, so concurrent requests can't race past the 100/10min cap. The demo-mode in-memory path uses matching semantics and both are covered by composer-rate-limit.test.mjs.
  • Input validation: parseComposeAssistInput rejects unknown keys, bounds every string field, and enforces minimum content length before contacting a provider, preventing empty/oversized calls.
  • Prompt injection posture: COMPOSE_SYSTEM_PROMPT explicitly 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 in normalizeComposeResponse.
  • XSS: AI text is inserted through Lexical's selection.insertText/editor state rather than dangerouslySetInnerHTML or raw HTML injection, so provider output can't execute as markup.
  • Consistency: FocusProviderError was cleanly renamed to AiProviderError and reused for both Focus and Compose call sites (app/api/mail/focus/route.ts updated 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):

  • In app/api/mail/compose/route.ts, the composer rate limit is consumed via consumeComposerRateLimit before getMailProvider/resolveMailAccount validates accountId. A request with a bogus accountId still 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.
stylessh merged commit d35ec66 into main2mo ago

Branch stylessh/smart-compose has been merged.

Checking branch