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:
2025-10-12 19:37:24 +00:00
parent b3ec31a265
commit a01377b21a
20 changed files with 3022 additions and 130 deletions

View 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 }
)
}
}