An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
feat: Add quality mode rate limiting with user-friendly error handling
Yoshiki Miuraa0891caecbde
diff --git a/.env.local.example b/.env.local.exampleindex ec5f4710..bce78d30 100644--- a/.env.local.example+++ b/.env.local.example@@ -75,6 +75,14 @@ NEXT_PUBLIC_SUPABASE_ANON_KEY=[YOUR_SUPABASE_ANON_KEY]# Enable additional functionality as needed# =============================================================================+# -----------------------------------------------------------------------------+# Cloud Deployment Configuration+# -----------------------------------------------------------------------------+# Enable rate limiting for cloud deployments+# When enabled, quality mode chat requests are limited to 5 per day+# Requires UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN to be configured+# MORPHIC_CLOUD_DEPLOYMENT=true+# -----------------------------------------------------------------------------# File Upload Support (Cloudflare R2)# Enable file and image upload functionality@@ -130,8 +138,5 @@ NEXT_PUBLIC_SUPABASE_ANON_KEY=[YOUR_SUPABASE_ANON_KEY]# -----------------------------------------------------------------------------# Advanced Configuration# ------------------------------------------------------------------------------# Model Configuration Profile-# MORPHIC_MODELS_PROFILE=default # Options: default, cloud, or custom profile-# Feedback Notifications (Slack)# SLACK_WEBHOOK_URL=[YOUR_SLACK_WEBHOOK_URL]diff --git a/app/api/chat/route.ts b/app/api/chat/route.tsindex 93d1e17c..67f0f473 100644--- a/app/api/chat/route.ts+++ b/app/api/chat/route.ts@@ -2,6 +2,7 @@ import { revalidateTag } from 'next/cache'import { cookies } from 'next/headers'import { getCurrentUserId } from '@/lib/auth/get-current-user'+import { checkAndEnforceQualityLimit } from '@/lib/rate-limit/quality-limit'import { createChatStreamResponse } from '@/lib/streaming/create-chat-stream-response'import { SearchMode } from '@/lib/types/search'import { selectModel } from '@/lib/utils/model-selection'@@ -93,6 +94,14 @@ export async function POST(req: Request) {)}+ // Check rate limit for quality mode+ const modelTypeCookie = cookieStore.get('modelType')?.value+ const rateLimitResponse = await checkAndEnforceQualityLimit(+ userId,+ modelTypeCookie === 'quality'+ )+ if (rateLimitResponse) return rateLimitResponse+const streamStart = performance.now()perfLog(`createChatStreamResponse - Start: model=${selectedModel.providerId}:${selectedModel.id}, searchMode=${searchMode}`diff --git a/bun.lock b/bun.lockindex a7dbf413..8442f041 100644--- a/bun.lock+++ b/bun.lock@@ -36,7 +36,7 @@"@supabase/ssr": "^0.6.1","@supabase/supabase-js": "^2.49.4","@tailwindcss/typography": "^0.5.12",- "@upstash/redis": "^1.34.0",+ "@upstash/redis": "^1.35.5","@vercel/analytics": "^1.5.0","@vercel/otel": "^1.13.0","ai": "^5.0.39",@@ -964,7 +964,7 @@"@unrs/resolver-binding-win32-x64-msvc": ["@unrs/[email protected]", "", { "os": "win32", "cpu": "x64" }, "sha512-lrW200hZdbfRtztbygyaq/6jP6AKE8qQN2KvPcJ+x7wiD038YtnYtZ82IMNJ69GJibV7bwL3y9FgK+5w/pYt6g=="],- "@upstash/redis": ["@upstash/[email protected]", "", { "dependencies": { "uncrypto": "^0.1.3" } }, "sha512-hSjv66NOuahW3MisRGlSgoszU2uONAY2l5Qo3Sae8OT3/Tng9K+2/cBRuyPBX8egwEGcNNCF9+r0V6grNnhL+w=="],+ "@upstash/redis": ["@upstash/[email protected]", "", { "dependencies": { "uncrypto": "^0.1.3" } }, "sha512-KdLdNAspQGOTGeC++o2LDBzNbMXrfInnmW5nUJfNXabnVh8X4NPrlJ0X4j75cBUShiMpXB3uI1ql4KpFQeqrHQ=="],"@vercel/analytics": ["@vercel/[email protected]", "", { "peerDependencies": { "@remix-run/react": "^2", "@sveltejs/kit": "^1 || ^2", "next": ">= 13", "react": "^18 || ^19 || ^19.0.0-rc", "svelte": ">= 4", "vue": "^3", "vue-router": "^4" }, "optionalPeers": ["@remix-run/react", "@sveltejs/kit", "next", "react", "svelte", "vue", "vue-router"] }, "sha512-MYsBzfPki4gthY5HnYN7jgInhAZ7Ac1cYDoRWFomwGHWEX7odTEzbtg9kf/QSo7XEsEAqlQugA6gJ2WS2DEa3g=="],diff --git a/components/chat-error.tsx b/components/chat-error.tsxindex 838c2182..0cf3ac63 100644--- a/components/chat-error.tsx+++ b/components/chat-error.tsx@@ -9,7 +9,20 @@ interface ChatErrorProps {export function ChatError({ error }: ChatErrorProps) {if (!error) return null- const errorMessage = error instanceof Error ? error.message : error+ let errorMessage = error instanceof Error ? error.message : error++ // Try to parse JSON error response and extract user-friendly message+ try {+ const jsonMatch = errorMessage?.match(/\{.*\}/)+ if (jsonMatch) {+ const parsed = JSON.parse(jsonMatch[0])+ if (parsed.error) {+ errorMessage = parsed.error+ }+ }+ } catch {+ // If parsing fails, use the original error message+ }return (<Card className="border-destructive bg-destructive/10 p-4">diff --git a/components/chat.tsx b/components/chat.tsxindex 1a7c0907..2685ce59 100644--- a/components/chat.tsx+++ b/components/chat.tsx@@ -110,13 +110,29 @@ export function Chat({const isRateLimit =error.message?.includes('429') ||errorMessage.includes('rate limit') ||- errorMessage.includes('too many requests')+ errorMessage.includes('too many requests') ||+ errorMessage.includes('daily limit')if (isRateLimit) {+ // Try to parse JSON error response for quality mode rate limit+ let parsedError: { error?: string; resetAt?: number; remaining?: number } = {}+ try {+ // Extract JSON from error message if it exists+ const jsonMatch = error.message?.match(/\{.*\}/)+ if (jsonMatch) {+ parsedError = JSON.parse(jsonMatch[0])+ }+ } catch {+ // Ignore parse errors+ }++ // Use parsed error message or fallback+ const userMessage = parsedError.error || 'You have reached your daily limit for quality mode chat requests.'+setErrorModal({open: true,type: 'rate-limit',- message: error.message,+ message: userMessage,details: undefined})} else if (diff --git a/components/error-modal.tsx b/components/error-modal.tsxindex 1c628735..38227e5c 100644--- a/components/error-modal.tsx+++ b/components/error-modal.tsx@@ -57,7 +57,7 @@ export function ErrorModal({const getErrorDescription = () => {switch (error.type) {case 'rate-limit':- return 'You have made too many requests. Please wait a moment before trying again.'+ return error.message || 'You have made too many requests. Please wait a moment before trying again.'case 'auth':return 'You need to sign in to continue using this feature.'case 'forbidden':@@ -71,7 +71,7 @@ export function ErrorModal({const getErrorDetails = () => {if (error.type === 'rate-limit') {- return 'Our rate limiting helps ensure fair usage and maintain service quality for all users. The limit will reset shortly.'+ return 'The limit will reset at midnight UTC. You can continue using speed mode without restrictions.'}return error.details}diff --git a/lib/config/load-models-config.ts b/lib/config/load-models-config.tsindex 4a4080f9..fca0f0aa 100644--- a/lib/config/load-models-config.ts+++ b/lib/config/load-models-config.ts@@ -21,7 +21,8 @@ const VALID_MODEL_TYPES: ModelType[] = ['speed', 'quality']const VALID_SEARCH_MODES: SearchMode[] = ['quick', 'adaptive', 'planning']function resolveConfigPath(): string {- const profile = process.env.MORPHIC_MODELS_PROFILE?.trim() || 'default'+ const profile =+ process.env.MORPHIC_CLOUD_DEPLOYMENT === 'true' ? 'cloud' : 'default'const file = `${profile}.json`const configPath = path.resolve(process.cwd(), 'config', 'models', file)return configPath@@ -68,7 +69,8 @@ function validateModelsConfigStructure(}export async function loadModelsConfig(): Promise<ModelsConfig> {- const profile = process.env.MORPHIC_MODELS_PROFILE?.trim() || 'default'+ const profile =+ process.env.MORPHIC_CLOUD_DEPLOYMENT === 'true' ? 'cloud' : 'default'if (cachedConfig && cachedProfile === profile) {return cachedConfig@@ -106,7 +108,8 @@ export async function loadModelsConfig(): Promise<ModelsConfig> {// Synchronous load (for code paths that need sync access)export function loadModelsConfigSync(): ModelsConfig {- const profile = process.env.MORPHIC_MODELS_PROFILE?.trim() || 'default'+ const profile =+ process.env.MORPHIC_CLOUD_DEPLOYMENT === 'true' ? 'cloud' : 'default'if (cachedConfig && cachedProfile === profile) {return cachedConfig}diff --git a/lib/rate-limit/quality-limit.ts b/lib/rate-limit/quality-limit.tsnew file mode 100644index 00000000..1004eef8--- /dev/null+++ b/lib/rate-limit/quality-limit.ts@@ -0,0 +1,128 @@+import { Redis } from '@upstash/redis'++import { perfLog } from '@/lib/utils/perf-logging'++const DAILY_QUALITY_LIMIT = 5++/**+ * Internal function to check rate limit using Upstash Redis+ */+async function checkQualityChatLimit(userId: string): Promise<{+ allowed: boolean+ remaining: number+ resetAt: number+}> {+ // If not in cloud deployment mode, allow unlimited requests+ if (process.env.MORPHIC_CLOUD_DEPLOYMENT !== 'true') {+ return { allowed: true, remaining: Infinity, resetAt: 0 }+ }++ // If Upstash is not configured, allow unlimited requests+ if (+ !process.env.UPSTASH_REDIS_REST_URL ||+ !process.env.UPSTASH_REDIS_REST_TOKEN+ ) {+ return { allowed: true, remaining: Infinity, resetAt: 0 }+ }++ try {+ const redis = new Redis({+ url: process.env.UPSTASH_REDIS_REST_URL,+ token: process.env.UPSTASH_REDIS_REST_TOKEN+ })++ // Create daily key with date+ const dateKey = new Date().toISOString().split('T')[0] // YYYY-MM-DD+ const key = `rl:quality:chat:${userId}:${dateKey}`++ // Increment counter with timeout+ const count = await Promise.race([+ redis.incr(key),+ new Promise<number>((_, reject) =>+ setTimeout(() => reject(new Error('Redis timeout')), 3000)+ )+ ])++ // Set expiry on first request of the day+ if (count === 1) {+ const secondsUntilMidnight = getSecondsUntilMidnight()+ await redis.expire(key, secondsUntilMidnight)+ }++ const remaining = Math.max(0, DAILY_QUALITY_LIMIT - count)+ const resetAt = getNextMidnightTimestamp()++ return {+ allowed: count <= DAILY_QUALITY_LIMIT,+ remaining,+ resetAt+ }+ } catch (error) {+ // On error, allow request to proceed (availability over strict limiting)+ console.error('Rate limit check failed:', error)+ return { allowed: true, remaining: Infinity, resetAt: 0 }+ }+}++/**+ * Get seconds until next midnight UTC+ */+function getSecondsUntilMidnight(): number {+ const now = new Date()+ const midnight = new Date(now)+ midnight.setUTCHours(24, 0, 0, 0)+ return Math.floor((midnight.getTime() - now.getTime()) / 1000)+}++/**+ * Get timestamp of next midnight UTC+ */+function getNextMidnightTimestamp(): number {+ const now = new Date()+ const midnight = new Date(now)+ midnight.setUTCHours(24, 0, 0, 0)+ return midnight.getTime()+}++/**+ * Check and enforce quality chat rate limit+ * Returns a 429 Response if limit is exceeded, null if allowed+ */+export async function checkAndEnforceQualityLimit(+ userId: string,+ isQualityMode: boolean+): Promise<Response | null> {+ // Only check limit for quality mode+ if (!isQualityMode) {+ return null+ }++ const result = await checkQualityChatLimit(userId)++ if (!result.allowed) {+ return new Response(+ JSON.stringify({+ error: `You've reached your daily limit of ${DAILY_QUALITY_LIMIT} quality mode requests.`,+ remaining: 0,+ resetAt: result.resetAt,+ limit: DAILY_QUALITY_LIMIT+ }),+ {+ status: 429,+ headers: {+ 'Content-Type': 'application/json',+ 'X-RateLimit-Limit': String(DAILY_QUALITY_LIMIT),+ 'X-RateLimit-Remaining': '0',+ 'X-RateLimit-Reset': String(result.resetAt)+ }+ }+ )+ }++ // Log usage for debugging+ perfLog(+ `Quality chat usage: ${DAILY_QUALITY_LIMIT - result.remaining}/${DAILY_QUALITY_LIMIT}`+ )++ return null+}diff --git a/package.json b/package.jsonindex c5e900dc..f267af0b 100644--- a/package.json+++ b/package.json@@ -49,7 +49,7 @@"@supabase/ssr": "^0.6.1","@supabase/supabase-js": "^2.49.4","@tailwindcss/typography": "^0.5.12",- "@upstash/redis": "^1.34.0",+ "@upstash/redis": "^1.35.5","@vercel/analytics": "^1.5.0","@vercel/otel": "^1.13.0","ai": "^5.0.39",