- Integrated prayer data with Prisma database schema - Updated API endpoints to use actual database - Implemented AI prayer generation with Azure OpenAI - Added user authentication for prayer creation - Moved Add Prayer button from FAB to sidebar top - Added prayer count tracking and user prayer status 🤖 Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
123 lines
5.9 KiB
TypeScript
123 lines
5.9 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { AzureOpenAI } from 'openai'
|
|
|
|
export const runtime = 'nodejs'
|
|
|
|
// Initialize Azure OpenAI client
|
|
const client = new AzureOpenAI({
|
|
apiKey: process.env.AZURE_OPENAI_KEY || '',
|
|
endpoint: process.env.AZURE_OPENAI_ENDPOINT || '',
|
|
apiVersion: process.env.AZURE_OPENAI_API_VERSION || '2024-05-01-preview',
|
|
deployment: 'gpt-4o' // Using GPT-4 for better quality prayers
|
|
})
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const { prompt, category, locale } = await request.json()
|
|
|
|
if (!prompt?.trim()) {
|
|
return NextResponse.json(
|
|
{ error: 'Prompt is required' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Call Azure OpenAI to generate a prayer
|
|
|
|
const isRomanian = locale === 'ro'
|
|
|
|
// Create system prompt for the AI
|
|
const systemPrompt = isRomanian
|
|
? `Ești un asistent creștin care ajută oamenii să formuleze rugăciuni sincere și biblice.
|
|
Creează rugăciuni care sunt:
|
|
- Respectuoase și reverenți față de Dumnezeu
|
|
- Bazate pe principii și învățături biblice
|
|
- Personale și pline de compasiune
|
|
- Încurajatoare și pline de speranță
|
|
- Scurte și concise (maxim 150 de cuvinte)
|
|
- Potrivite pentru categoria: ${category}
|
|
- În limba română`
|
|
: `You are a Christian assistant helping people formulate sincere and biblical prayers.
|
|
Create prayers that are:
|
|
- Respectful and reverent toward God
|
|
- Based on biblical principles and teachings
|
|
- Personal and compassionate
|
|
- Encouraging and hopeful
|
|
- Brief and concise (maximum 150 words)
|
|
- Appropriate for the category: ${category}
|
|
- In English`
|
|
|
|
// Create user prompt
|
|
const userPrompt = isRomanian
|
|
? `Te rog să creezi o rugăciune sinceră pentru următoarea situație: ${prompt}
|
|
Categoria: ${category}
|
|
Răspunde în format JSON cu structura: {"title": "titlul rugăciunii", "prayer": "textul rugăciunii"}`
|
|
: `Please create a sincere prayer for the following situation: ${prompt}
|
|
Category: ${category}
|
|
Respond in JSON format with structure: {"title": "prayer title", "prayer": "prayer text"}`
|
|
|
|
try {
|
|
// Call Azure OpenAI
|
|
const completion = await client.chat.completions.create({
|
|
model: 'gpt-4o',
|
|
messages: [
|
|
{ role: 'system', content: systemPrompt },
|
|
{ role: 'user', content: userPrompt }
|
|
],
|
|
temperature: 0.7,
|
|
max_tokens: 500,
|
|
response_format: { type: 'json_object' }
|
|
})
|
|
|
|
const responseText = completion.choices[0]?.message?.content || '{}'
|
|
const prayerData = JSON.parse(responseText)
|
|
|
|
return NextResponse.json({
|
|
title: prayerData.title || (isRomanian ? 'Rugăciune generată' : 'Generated Prayer'),
|
|
prayer: prayerData.prayer || (isRomanian ? 'Nu s-a putut genera rugăciunea.' : 'Could not generate prayer.'),
|
|
category,
|
|
generated: true
|
|
})
|
|
} catch (aiError) {
|
|
console.error('Error calling Azure OpenAI:', aiError)
|
|
|
|
// Fallback to template-based generation
|
|
const categoryTemplates = {
|
|
personal: isRomanian
|
|
? { title: 'Rugăciune personală', prayer: `Doamne, îți aduc înaintea Ta această cerere personală: ${prompt}. Te rog să mă îndrumi și să-mi dai pace. În numele lui Isus, Amin.` }
|
|
: { title: 'Personal Prayer', prayer: `Lord, I bring before You this personal request: ${prompt}. Please guide me and grant me peace. In Jesus' name, Amen.` },
|
|
family: isRomanian
|
|
? { title: 'Rugăciune pentru familie', prayer: `Tată ceresc, îți încredințez familia mea. ${prompt}. Binecuvântează-ne și păstrează-ne uniți în dragoste. În numele lui Isus, Amin.` }
|
|
: { title: 'Family Prayer', prayer: `Heavenly Father, I entrust my family to You. ${prompt}. Bless us and keep us united in love. In Jesus' name, Amen.` },
|
|
health: isRomanian
|
|
? { title: 'Rugăciune pentru sănătate', prayer: `Mare Vindecător, vin la Tine cu această nevoie de sănătate: ${prompt}. Te rog pentru vindecare și restaurare. În numele lui Isus, Amin.` }
|
|
: { title: 'Health Prayer', prayer: `Great Healer, I come to You with this health need: ${prompt}. I pray for healing and restoration. In Jesus' name, Amen.` },
|
|
work: isRomanian
|
|
? { title: 'Rugăciune pentru muncă', prayer: `Doamne, îți aduc situația mea profesională: ${prompt}. Îndrumă-mă și binecuvântează munca mâinilor mele. În numele lui Isus, Amin.` }
|
|
: { title: 'Work Prayer', prayer: `Lord, I bring my professional situation: ${prompt}. Guide me and bless the work of my hands. In Jesus' name, Amen.` },
|
|
ministry: isRomanian
|
|
? { title: 'Rugăciune pentru lucrare', prayer: `Doamne, îți dedic această lucrare: ${prompt}. Folosește-mă pentru gloria Ta. În numele lui Isus, Amin.` }
|
|
: { title: 'Ministry Prayer', prayer: `Lord, I dedicate this ministry to You: ${prompt}. Use me for Your glory. In Jesus' name, Amen.` },
|
|
world: isRomanian
|
|
? { title: 'Rugăciune pentru lume', prayer: `Domn Suveran, mă rog pentru această situație globală: ${prompt}. Fie voia Ta pe pământ. În numele lui Isus, Amin.` }
|
|
: { title: 'World Prayer', prayer: `Sovereign Lord, I pray for this global situation: ${prompt}. May Your will be done on earth. In Jesus' name, Amen.` }
|
|
}
|
|
|
|
const template = categoryTemplates[category as keyof typeof categoryTemplates] || categoryTemplates.personal
|
|
|
|
return NextResponse.json({
|
|
title: template.title,
|
|
prayer: template.prayer,
|
|
category,
|
|
generated: true
|
|
})
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('Error generating prayer:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to generate prayer' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
} |