feat: add /api/ai/complete Anthropic proxy for sub-apps#26
Merged
dougdevitre merged 1 commit intomainfrom Apr 15, 2026
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Step 1 of the Gemini → Anthropic migration: server-side proxy so the 20+ sub-apps that currently ship
@google/genaiwith an API key in the browser can call Anthropic through our server instead.POST /api/ai/completeon both Fastify host and VercelAI_RATE_LIMIT_PER_MIN/AI_RATE_LIMIT_PER_HOUR)*.cotrackpro.com+ localhost; credentials enabledmax_tokenscapped at 4096Next steps (not in this PR)
aiClient.tsadded to theauth.tsxtemplate@google/genaiand swapgenerateContent()callsAPI_KEYenv vars from 20 Vercel projectsTest plan
{ text, model, usage }Retry-AfterauthorizeOutbound,authorizeRecords) are not caused by this PR — they were failing on main before these changes🤖 Generated with Claude Code