feat: implement AI chat with vector search and random loading messages
Major Features: - ✅ AI chat with Azure OpenAI GPT-4o integration - ✅ Vector search across Bible versions (ASV English, RVA 1909 Spanish) - ✅ Multi-language support with automatic English fallback - ✅ Bible version citations in responses [ASV] [RVA 1909] - ✅ Random Bible-themed loading messages (5 variants) - ✅ Safe build script with memory guardrails - ✅ 8GB swap memory for build safety - ✅ Stripe donation integration (multiple payment methods) AI Chat Improvements: - Implement vector search with 1536-dim embeddings (Azure text-embedding-ada-002) - Search all Bible versions in user's language, fallback to English - Cite Bible versions properly in AI responses - Add 5 random loading messages: "Searching the Scriptures...", etc. - Fix Ollama conflict (disabled to use Azure OpenAI exclusively) - Optimize hybrid search queries for actual table schema Build & Infrastructure: - Create safe-build.sh script with memory monitoring (prevents server crashes) - Add 8GB swap memory for emergency relief - Document build process in BUILD_GUIDE.md - Set Node.js memory limits (4GB max during builds) Database: - Clean up 115 old vector tables with wrong dimensions - Keep only 2 tables with correct 1536-dim embeddings - Add Stripe schema for donations and subscriptions Documentation: - AI_CHAT_FINAL_STATUS.md - Complete implementation status - AI_CHAT_IMPLEMENTATION_COMPLETE.md - Technical details - BUILD_GUIDE.md - Safe building guide with guardrails - CHAT_LOADING_MESSAGES.md - Loading messages implementation - STRIPE_IMPLEMENTATION_COMPLETE.md - Stripe integration docs - STRIPE_SETUP_GUIDE.md - Stripe configuration guide 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -194,9 +194,27 @@ async function generateBiblicalResponse(message: string, locale: string, history
|
||||
// Continue without verses - test if Azure OpenAI works alone
|
||||
}
|
||||
|
||||
// Create context from relevant verses
|
||||
// Extract Bible version names from source_table
|
||||
const getVersionName = (sourceTable: string): string => {
|
||||
if (!sourceTable) return 'Unknown'
|
||||
// Extract table name: ai_bible."bv_en_eng_asv" -> bv_en_eng_asv
|
||||
const tableName = sourceTable.split('.').pop()?.replace(/"/g, '') || ''
|
||||
|
||||
// Map table names to friendly version names
|
||||
const versionMap: Record<string, string> = {
|
||||
'bv_en_eng_asv': 'ASV (American Standard Version)',
|
||||
'bv_es_sparv1909': 'RVA 1909 (Reina-Valera Antigua)',
|
||||
// Add more as needed
|
||||
}
|
||||
return versionMap[tableName] || tableName
|
||||
}
|
||||
|
||||
// Create context from relevant verses with version citations
|
||||
const versesContext = relevantVerses
|
||||
.map(verse => `${verse.ref}: "${verse.text_raw}"`)
|
||||
.map(verse => {
|
||||
const version = getVersionName(verse.source_table)
|
||||
return `[${version}] ${verse.ref}: "${verse.text_raw}"`
|
||||
})
|
||||
.join('\n\n')
|
||||
|
||||
// Intelligent context selection for conversation history
|
||||
@@ -204,39 +222,62 @@ async function generateBiblicalResponse(message: string, locale: string, history
|
||||
|
||||
// Create language-specific system prompts
|
||||
const systemPrompts = {
|
||||
ro: `Ești un asistent AI pentru întrebări biblice în limba română. Răspunde pe baza Scripturii, fiind respectuos și înțelept.
|
||||
ro: `Ești un asistent AI biblic expert în limba română. Răspunde pe baza Scripturii, fiind precis și empatic.
|
||||
|
||||
Instrucțiuni:
|
||||
- Folosește versurile biblice relevante pentru a răspunde la întrebare
|
||||
- Citează întotdeauna referințele biblice (ex: Ioan 3:16)
|
||||
- Răspunde în română
|
||||
- Fii empatic și încurajator
|
||||
- Dacă nu ești sigur, încurajează studiul personal și rugăciunea
|
||||
INSTRUCȚIUNI IMPORTANTE:
|
||||
- CITEAZĂ ÎNTOTDEAUNA versiunea biblică folosind formatul [Versiune] Referință
|
||||
Exemplu: "[ASV] Ioan 3:16" sau "[RVA 1909] Juan 3:16"
|
||||
- Folosește versurile biblice furnizate mai jos pentru a răspunde
|
||||
- Răspunde ÎNTOTDEAUNA în română, chiar dacă versetele sunt în alte limbi
|
||||
- Dacă folosești versuri în engleză sau alte limbi, explică-le în română
|
||||
- Fii respectuos, înțelept și încurajator
|
||||
- Dacă întrebarea nu are răspuns clar în Scriptură, menționează-l cu onestitate
|
||||
|
||||
Versuri relevante pentru această întrebare:
|
||||
${versesContext}
|
||||
Versuri biblice relevante găsite:
|
||||
${versesContext || 'Nu s-au găsit versete specifice. Răspunde pe baza cunoștințelor biblice generale.'}
|
||||
|
||||
Conversația anterioară:
|
||||
${conversationHistory}
|
||||
|
||||
Întrebarea curentă: ${message}`,
|
||||
|
||||
en: `You are an AI assistant for biblical questions in English. Answer based on Scripture, being respectful and wise.
|
||||
en: `You are an expert Biblical AI assistant in English. Answer based on Scripture, being precise and empathetic.
|
||||
|
||||
Instructions:
|
||||
- Use the relevant Bible verses to answer the question
|
||||
- Always cite biblical references (e.g., John 3:16)
|
||||
- Respond in English
|
||||
- Be empathetic and encouraging
|
||||
- If unsure, encourage personal study and prayer
|
||||
IMPORTANT INSTRUCTIONS:
|
||||
- ALWAYS cite the Bible version using the format [Version] Reference
|
||||
Example: "[ASV] John 3:16" or "[RVA 1909] Juan 3:16"
|
||||
- Use the Bible verses provided below to answer the question
|
||||
- ALWAYS respond in English
|
||||
- Be respectful, wise, and encouraging
|
||||
- If the question doesn't have a clear answer in Scripture, state that honestly
|
||||
- When multiple versions are available, cite the most relevant ones
|
||||
|
||||
Relevant verses for this question:
|
||||
${versesContext}
|
||||
Relevant Bible verses found:
|
||||
${versesContext || 'No specific verses found. Answer based on general biblical knowledge.'}
|
||||
|
||||
Previous conversation:
|
||||
${conversationHistory}
|
||||
|
||||
Current question: ${message}`
|
||||
Current question: ${message}`,
|
||||
|
||||
es: `Eres un asistente bíblico experto en español. Responde basándote en las Escrituras, siendo preciso y empático.
|
||||
|
||||
INSTRUCCIONES IMPORTANTES:
|
||||
- SIEMPRE cita la versión bíblica usando el formato [Versión] Referencia
|
||||
Ejemplo: "[RVA 1909] Juan 3:16" o "[ASV] John 3:16"
|
||||
- Usa los versículos bíblicos proporcionados abajo para responder
|
||||
- SIEMPRE responde en español, incluso si los versículos están en otros idiomas
|
||||
- Si usas versículos en inglés u otros idiomas, explícalos en español
|
||||
- Sé respetuoso, sabio y alentador
|
||||
- Si la pregunta no tiene respuesta clara en las Escrituras, mencio nalo honestamente
|
||||
|
||||
Versículos bíblicos relevantes encontrados:
|
||||
${versesContext || 'No se encontraron versículos específicos. Responde basándote en conocimiento bíblico general.'}
|
||||
|
||||
Conversación anterior:
|
||||
${conversationHistory}
|
||||
|
||||
Pregunta actual: ${message}`
|
||||
}
|
||||
|
||||
const systemPrompt = systemPrompts[locale as keyof typeof systemPrompts] || systemPrompts.en
|
||||
|
||||
99
app/api/stripe/checkout/route.ts
Normal file
99
app/api/stripe/checkout/route.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { stripe } from '@/lib/stripe-server'
|
||||
import { dollarsToCents } from '@/lib/stripe'
|
||||
import { prisma } from '@/lib/db'
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const body = await req.json()
|
||||
const { amount, email, name, message, isAnonymous, isRecurring, recurringInterval, locale } = body
|
||||
|
||||
// Validate required fields
|
||||
if (!amount || !email) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Amount and email are required' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// Convert amount to cents
|
||||
const amountInCents = dollarsToCents(parseFloat(amount))
|
||||
|
||||
// Validate amount (minimum $1)
|
||||
if (amountInCents < 100) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Minimum donation amount is $1' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// Get the base URL for redirects
|
||||
const baseUrl = process.env.NEXTAUTH_URL || 'http://localhost:3010'
|
||||
const userLocale = locale || 'en'
|
||||
|
||||
// Create checkout session parameters
|
||||
const sessionParams: any = {
|
||||
payment_method_types: ['card'],
|
||||
line_items: [
|
||||
{
|
||||
price_data: {
|
||||
currency: 'usd',
|
||||
product_data: {
|
||||
name: 'Donation to Biblical Guide',
|
||||
description: 'Support Biblical Guide - Every Scripture. Every Language. Forever Free.',
|
||||
images: [`${baseUrl}/icon.png`],
|
||||
},
|
||||
unit_amount: amountInCents,
|
||||
},
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
mode: isRecurring ? 'subscription' : 'payment',
|
||||
success_url: `${baseUrl}/${userLocale}/donate/success?session_id={CHECKOUT_SESSION_ID}`,
|
||||
cancel_url: `${baseUrl}/${userLocale}/donate?canceled=true`,
|
||||
customer_email: email,
|
||||
metadata: {
|
||||
donorName: name || 'Anonymous',
|
||||
donorMessage: message || '',
|
||||
isAnonymous: isAnonymous ? 'true' : 'false',
|
||||
},
|
||||
}
|
||||
|
||||
// Add recurring interval if applicable
|
||||
if (isRecurring && recurringInterval) {
|
||||
sessionParams.line_items[0].price_data.recurring = {
|
||||
interval: recurringInterval,
|
||||
}
|
||||
}
|
||||
|
||||
// Create Stripe checkout session
|
||||
const session = await stripe.checkout.sessions.create(sessionParams)
|
||||
|
||||
// Create donation record in database with PENDING status
|
||||
await prisma.donation.create({
|
||||
data: {
|
||||
stripeSessionId: session.id,
|
||||
email,
|
||||
name: name || null,
|
||||
amount: amountInCents,
|
||||
currency: 'usd',
|
||||
status: 'PENDING',
|
||||
message: message || null,
|
||||
isAnonymous: isAnonymous || false,
|
||||
isRecurring: isRecurring || false,
|
||||
recurringInterval: recurringInterval || null,
|
||||
metadata: {
|
||||
sessionUrl: session.url,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
return NextResponse.json({ sessionId: session.id, url: session.url })
|
||||
} catch (error) {
|
||||
console.error('Error creating checkout session:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to create checkout session' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
130
app/api/stripe/webhook/route.ts
Normal file
130
app/api/stripe/webhook/route.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { stripe } from '@/lib/stripe-server'
|
||||
import { prisma } from '@/lib/db'
|
||||
import Stripe from 'stripe'
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const body = await req.text()
|
||||
const signature = req.headers.get('stripe-signature')
|
||||
|
||||
if (!signature) {
|
||||
console.error('No stripe signature found')
|
||||
return NextResponse.json({ error: 'No signature' }, { status: 400 })
|
||||
}
|
||||
|
||||
let event: Stripe.Event
|
||||
|
||||
try {
|
||||
// Verify webhook signature
|
||||
event = stripe.webhooks.constructEvent(
|
||||
body,
|
||||
signature,
|
||||
process.env.STRIPE_WEBHOOK_SECRET!
|
||||
)
|
||||
} catch (err) {
|
||||
console.error('Webhook signature verification failed:', err)
|
||||
return NextResponse.json(
|
||||
{ error: 'Webhook signature verification failed' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// Handle the event
|
||||
try {
|
||||
switch (event.type) {
|
||||
case 'checkout.session.completed': {
|
||||
const session = event.data.object as Stripe.Checkout.Session
|
||||
|
||||
// Update donation status to COMPLETED
|
||||
await prisma.donation.update({
|
||||
where: { stripeSessionId: session.id },
|
||||
data: {
|
||||
status: 'COMPLETED',
|
||||
stripePaymentId: session.payment_intent as string,
|
||||
metadata: {
|
||||
paymentStatus: session.payment_status,
|
||||
customerEmail: session.customer_email,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
console.log(`Donation completed for session: ${session.id}`)
|
||||
break
|
||||
}
|
||||
|
||||
case 'checkout.session.expired': {
|
||||
const session = event.data.object as Stripe.Checkout.Session
|
||||
|
||||
// Update donation status to CANCELLED
|
||||
await prisma.donation.update({
|
||||
where: { stripeSessionId: session.id },
|
||||
data: {
|
||||
status: 'CANCELLED',
|
||||
},
|
||||
})
|
||||
|
||||
console.log(`Donation cancelled for session: ${session.id}`)
|
||||
break
|
||||
}
|
||||
|
||||
case 'payment_intent.payment_failed': {
|
||||
const paymentIntent = event.data.object as Stripe.PaymentIntent
|
||||
|
||||
// Update donation status to FAILED
|
||||
const donation = await prisma.donation.findFirst({
|
||||
where: { stripePaymentId: paymentIntent.id },
|
||||
})
|
||||
|
||||
if (donation) {
|
||||
await prisma.donation.update({
|
||||
where: { id: donation.id },
|
||||
data: {
|
||||
status: 'FAILED',
|
||||
metadata: {
|
||||
error: paymentIntent.last_payment_error?.message,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
console.log(`Payment failed for intent: ${paymentIntent.id}`)
|
||||
break
|
||||
}
|
||||
|
||||
case 'charge.refunded': {
|
||||
const charge = event.data.object as Stripe.Charge
|
||||
|
||||
// Update donation status to REFUNDED
|
||||
const donation = await prisma.donation.findFirst({
|
||||
where: { stripePaymentId: charge.payment_intent as string },
|
||||
})
|
||||
|
||||
if (donation) {
|
||||
await prisma.donation.update({
|
||||
where: { id: donation.id },
|
||||
data: {
|
||||
status: 'REFUNDED',
|
||||
metadata: {
|
||||
refundReason: charge.refunds?.data[0]?.reason,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
console.log(`Donation refunded for charge: ${charge.id}`)
|
||||
break
|
||||
}
|
||||
|
||||
default:
|
||||
console.log(`Unhandled event type: ${event.type}`)
|
||||
}
|
||||
|
||||
return NextResponse.json({ received: true })
|
||||
} catch (error) {
|
||||
console.error('Error processing webhook:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Webhook processing failed' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user