Spaces:
Configuration error
Configuration error
| import { NextRequest, NextResponse } from 'next/server' | |
| import { streamText } from 'ai' | |
| import { google } from '@ai-sdk/google' | |
| import { getAuthUser } from '@/lib/auth' | |
| import { checkRateLimit, getClientIdentifier, getRateLimitHeaders } from '@/lib/rate-limit' | |
| // POST /api/coach — AI Prompt Improver | |
| export async function POST(request: NextRequest) { | |
| try { | |
| const authUser = await getAuthUser() | |
| const userId = authUser?.id ?? null | |
| // Rate limiting | |
| const identifier = getClientIdentifier(request, userId ?? undefined) | |
| const rateLimit = await checkRateLimit(identifier, !!userId) | |
| if (!rateLimit.success) { | |
| return new Response( | |
| JSON.stringify({ error: rateLimit.error }), | |
| { status: 429, headers: { 'Content-Type': 'application/json' } } | |
| ) | |
| } | |
| const body = await request.json() | |
| // Support both 'prompt' (legacy) and 'template' (from PromptCoach component) | |
| const { prompt, template, action, category } = body | |
| const inputText = template || prompt | |
| if (!inputText) { | |
| return NextResponse.json({ error: 'Prompt or template is required' }, { status: 400 }) | |
| } | |
| let systemPrompt = '' | |
| switch (action) { | |
| case 'improve': | |
| systemPrompt = `You are an expert prompt engineer. Improve the following AI prompt to get better results. Make it clearer, more specific, and add any missing context or instructions. Return ONLY the improved prompt, no explanations.` | |
| break | |
| case 'shorten': | |
| systemPrompt = `You are an expert prompt engineer. Make the following AI prompt more concise while keeping its effectiveness. Remove redundancy and unnecessary words. Return ONLY the shortened prompt, no explanations.` | |
| break | |
| case 'expand': | |
| systemPrompt = `You are an expert prompt engineer. Expand the following AI prompt with more detail, context, and specific instructions to get higher quality results. Return ONLY the expanded prompt, no explanations.` | |
| break | |
| case 'analyze': | |
| default: | |
| systemPrompt = `You are an expert prompt engineer and AI coach. | |
| Give concise, actionable feedback on this prompt in under 120 words. | |
| Focus ONLY on specific weaknesses and how to fix them. Do NOT repeat what is already good. | |
| ${category ? `The prompt is for the "${category}" category.` : ''} | |
| End with one concrete rewrite suggestion for the weakest part.` | |
| } | |
| const result = await streamText({ | |
| model: google('gemini-2.0-flash'), | |
| system: systemPrompt, | |
| prompt: inputText.slice(0, 2000), | |
| }) | |
| const headers = getRateLimitHeaders(rateLimit) | |
| return result.toTextStreamResponse({ | |
| headers: Object.fromEntries(headers.entries()), | |
| }) | |
| } catch (error) { | |
| console.error('Coach error:', error) | |
| return NextResponse.json({ error: 'Failed to improve prompt' }, { status: 500 }) | |
| } | |
| } | |