first commit
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { geminiGenerate } from "@/lib/gemini";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
/**
|
||||
* Translate arbitrary text to a target language. Used for per-message
|
||||
* translation and composer "translate before send". Server-side Gemini only.
|
||||
*/
|
||||
export async function POST(req: NextRequest) {
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const text: string = (body.text ?? "").toString();
|
||||
const targetLang: string = (body.targetLang ?? "").toString().trim();
|
||||
|
||||
if (!text.trim()) return NextResponse.json({ ok: false, error: "Nothing to translate." }, { status: 400 });
|
||||
if (!targetLang) return NextResponse.json({ ok: false, error: "No target language." }, { status: 400 });
|
||||
|
||||
const result = await geminiGenerate(
|
||||
`Translate the following text into ${targetLang}. Preserve markdown, emoji, @mentions and #channel references exactly. ` +
|
||||
`Return ONLY the translated text with no quotes, notes, or preamble.\n\n${text}`,
|
||||
{
|
||||
system: "You are a professional translator. Output only the translation.",
|
||||
temperature: 0.2,
|
||||
},
|
||||
);
|
||||
|
||||
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 });
|
||||
}
|
||||
Reference in New Issue
Block a user