74 lines
2.6 KiB
TypeScript
74 lines
2.6 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
|
|
const OPENROUTER_API_KEY = process.env.OPENROUTER_API_KEY;
|
|
const OPENROUTER_MODEL = 'anthropic/claude-3.5-haiku';
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const { message } = await request.json();
|
|
|
|
if (!message || typeof message !== 'string') {
|
|
return NextResponse.json(
|
|
{ error: 'Message is required and must be a string' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
if (!OPENROUTER_API_KEY) {
|
|
console.error('OPENROUTER_API_KEY is not configured');
|
|
// Fallback: return original message if API key is missing
|
|
return NextResponse.json({ rewrittenMessage: message });
|
|
}
|
|
|
|
const prompt = `Rewrite the following message to express the COMPLETE OPPOSITE meaning and opinion.
|
|
If the message is negative or critical, rewrite it to be overwhelmingly positive, praising, and appreciative.
|
|
If the message is positive, rewrite it to be critical or negative.
|
|
Maintain the original language (German or English).
|
|
Return ONLY the rewritten message text, nothing else.
|
|
|
|
Message: "${message}"`;
|
|
|
|
const response = await fetch('https://openrouter.ai/api/v1/chat/completions', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Authorization': `Bearer ${OPENROUTER_API_KEY}`,
|
|
'Content-Type': 'application/json',
|
|
'HTTP-Referer': 'https://hoerdle.elpatron.me',
|
|
'X-Title': 'Hördle Message Rewriter'
|
|
},
|
|
body: JSON.stringify({
|
|
model: OPENROUTER_MODEL,
|
|
messages: [
|
|
{
|
|
role: 'user',
|
|
content: prompt
|
|
}
|
|
],
|
|
temperature: 0.7,
|
|
max_tokens: 500
|
|
})
|
|
});
|
|
|
|
if (!response.ok) {
|
|
console.error('OpenRouter API error:', await response.text());
|
|
// Fallback: return original message
|
|
return NextResponse.json({ rewrittenMessage: message });
|
|
}
|
|
|
|
const data = await response.json();
|
|
let rewrittenMessage = data.choices?.[0]?.message?.content?.trim() || message;
|
|
|
|
// Add suffix
|
|
rewrittenMessage += " (autocorrected by Polite-Bot)";
|
|
|
|
return NextResponse.json({ rewrittenMessage });
|
|
|
|
} catch (error) {
|
|
console.error('Error rewriting message:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Internal Server Error' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|