Skip to content

feat: add /api/ai/complete Anthropic proxy for sub-apps#26

Merged
dougdevitre merged 1 commit intomainfrom
feat/ai-proxy
Apr 15, 2026
Merged

feat: add /api/ai/complete Anthropic proxy for sub-apps#26
dougdevitre merged 1 commit intomainfrom
feat/ai-proxy

Conversation

@dougdevitre
Copy link
Copy Markdown
Owner

Summary

Step 1 of the Gemini → Anthropic migration: server-side proxy so the 20+ sub-apps that currently ship @google/genai with an API key in the browser can call Anthropic through our server instead.

  • POST /api/ai/complete on both Fastify host and Vercel
  • Clerk JWT required (no API-key fallback — browsers never hold one)
  • Per-Clerk-userId rate limits via shared KV (AI_RATE_LIMIT_PER_MIN / AI_RATE_LIMIT_PER_HOUR)
  • CORS allows *.cotrackpro.com + localhost; credentials enabled
  • Input validated: message shape, ≤60 turns, ≤200k total chars, max_tokens capped at 4096

Next steps (not in this PR)

  • Shared aiClient.ts added to the auth.tsx template
  • Per-repo script to remove @google/genai and swap generateContent() calls
  • Clean up API_KEY env vars from 20 Vercel projects

Test plan

  • Deploy to preview
  • curl with a valid Clerk JWT: returns { text, model, usage }
  • curl without auth: 401
  • Exceed rate limit: 429 with Retry-After
  • Pre-existing test failures (authorizeOutbound, authorizeRecords) are not caused by this PR — they were failing on main before these changes

🤖 Generated with Claude Code

Sub-apps used to call @google/genai from the browser, shipping an API
key to every user. This proxies Anthropic through the server so the
key stays server-side and AI spend is auditable per Clerk userId.

- Clerk JWT required (no API-key fallback)
- Per-user rate limits via shared KV (AI_RATE_LIMIT_PER_MIN/HOUR)
- CORS limited to *.cotrackpro.com + localhost
- Both Fastify (long-running host) and Vercel (api/ai/complete.ts) adapters
- Input validated: messages shape, max turns, max total chars, max_tokens cap

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@dougdevitre dougdevitre merged commit 6e9d7b1 into main Apr 15, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant