import { NextRequest, NextResponse } from "next/server"; import { geminiGenerate } from "@/lib/gemini"; export const dynamic = "force-dynamic"; /** * AI assistant. The UI sends a user prompt plus optional message context * (e.g. a copied message to summarize / draft a reply to). The Gemini key * never leaves the server. */ export async function POST(req: NextRequest) { const body = await req.json().catch(() => ({})); const prompt: string = (body.prompt ?? "").toString().trim(); const context: string | undefined = body.context ? String(body.context) : undefined; if (!prompt && !context) { return NextResponse.json({ ok: false, error: "Nothing to ask." }, { status: 400 }); } const parts: string[] = []; if (context) parts.push(`Here is a chat message for context:\n"""\n${context}\n"""`); parts.push(prompt || "Summarize the message above."); const result = await geminiGenerate(parts.join("\n\n"), { system: "You are a concise, helpful assistant embedded in a team-messaging app. " + "Answer directly in a professional but friendly tone. When drafting a reply, write it in first person, ready to send. Keep responses tight.", temperature: 0.5, }); if (!result.ok) { return NextResponse.json({ ok: false, error: result.error, retryable: result.retryable }, { status: result.status || 502 }); } return NextResponse.json({ ok: true, text: result.text }); }