Implement AI chat history system with enhanced memory (Phases 1 & 2)

Phase 1 - Database Schema & Basic API:
- Add ChatConversation and ChatMessage tables with proper relationships
- Create conversation CRUD API endpoints with authentication
- Update chat API to support persistent conversations
- Implement auto-generated conversation titles and language separation
- Add conversation soft delete and user-specific access control

Phase 2 - Enhanced Memory System:
- Implement intelligent context selection beyond simple chronological order
- Add relevance scoring for older messages based on keyword overlap and biblical references
- Create automatic message summarization for very long conversations
- Optimize token usage with smart context management (1500 token budget)
- Add biblical awareness with Romanian/English book name detection
- Implement time-based relevance decay for better context prioritization

Frontend Improvements:
- Add chat history button to floating chat header
- Create basic history panel UI with placeholder content
- Maintain backward compatibility for anonymous users

Database Changes:
- Enhanced schema with conversation relationships and message roles
- Proper indexing for performance and user-specific queries
- Migration completed successfully with prisma db push

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
andupetcu
2025-09-22 11:33:36 +03:00
parent 73a8b44f76
commit 86b7ff377b
6 changed files with 1229 additions and 27 deletions

View File

@@ -0,0 +1,266 @@
import { NextRequest, NextResponse } from 'next/server'
import { z } from 'zod'
import { PrismaClient } from '@prisma/client'
import { verifyToken } from '@/lib/auth'
const prisma = new PrismaClient()
export const runtime = 'nodejs'
const updateConversationSchema = z.object({
title: z.string().min(1).max(200).optional(),
isActive: z.boolean().optional(),
})
// GET /api/chat/conversations/[id] - Get conversation with messages
export async function GET(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
// Get user from authentication
const authHeader = request.headers.get('authorization')
if (!authHeader?.startsWith('Bearer ')) {
return NextResponse.json(
{ success: false, error: 'Authentication required' },
{ status: 401 }
)
}
const token = authHeader.substring(7)
const payload = verifyToken(token)
if (!payload) {
return NextResponse.json(
{ success: false, error: 'Invalid token' },
{ status: 401 }
)
}
const userId = payload.userId as string
const conversationId = params.id
// Get conversation with messages
const conversation = await prisma.chatConversation.findUnique({
where: {
id: conversationId,
userId, // Ensure user owns this conversation
isActive: true
},
include: {
messages: {
orderBy: { timestamp: 'asc' },
select: {
id: true,
role: true,
content: true,
timestamp: true,
metadata: true
}
}
}
})
if (!conversation) {
return NextResponse.json(
{ success: false, error: 'Conversation not found' },
{ status: 404 }
)
}
return NextResponse.json({
success: true,
conversation: {
id: conversation.id,
title: conversation.title,
language: conversation.language,
createdAt: conversation.createdAt,
lastMessageAt: conversation.lastMessageAt,
messages: conversation.messages.map(msg => ({
id: msg.id,
role: msg.role.toLowerCase(), // Convert enum to lowercase for frontend compatibility
content: msg.content,
timestamp: msg.timestamp,
metadata: msg.metadata
}))
}
})
} catch (error) {
console.error('Error fetching conversation:', error)
return NextResponse.json(
{
success: false,
error: 'Failed to fetch conversation'
},
{ status: 500 }
)
}
}
// PUT /api/chat/conversations/[id] - Update conversation
export async function PUT(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
// Get user from authentication
const authHeader = request.headers.get('authorization')
if (!authHeader?.startsWith('Bearer ')) {
return NextResponse.json(
{ success: false, error: 'Authentication required' },
{ status: 401 }
)
}
const token = authHeader.substring(7)
const payload = verifyToken(token)
if (!payload) {
return NextResponse.json(
{ success: false, error: 'Invalid token' },
{ status: 401 }
)
}
const userId = payload.userId as string
const conversationId = params.id
// Parse request body
const body = await request.json()
const updateData = updateConversationSchema.parse(body)
// Verify conversation ownership
const existingConversation = await prisma.chatConversation.findUnique({
where: {
id: conversationId,
userId
}
})
if (!existingConversation) {
return NextResponse.json(
{ success: false, error: 'Conversation not found' },
{ status: 404 }
)
}
// Update conversation
const conversation = await prisma.chatConversation.update({
where: { id: conversationId },
data: {
...updateData,
updatedAt: new Date()
},
include: {
_count: {
select: { messages: true }
}
}
})
return NextResponse.json({
success: true,
conversation: {
id: conversation.id,
title: conversation.title,
language: conversation.language,
messageCount: conversation._count.messages,
lastMessageAt: conversation.lastMessageAt,
createdAt: conversation.createdAt,
isActive: conversation.isActive
}
})
} catch (error) {
console.error('Error updating conversation:', error)
if (error instanceof z.ZodError) {
return NextResponse.json(
{
success: false,
error: 'Invalid request format',
details: error.errors
},
{ status: 400 }
)
}
return NextResponse.json(
{
success: false,
error: 'Failed to update conversation'
},
{ status: 500 }
)
}
}
// DELETE /api/chat/conversations/[id] - Delete conversation
export async function DELETE(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
// Get user from authentication
const authHeader = request.headers.get('authorization')
if (!authHeader?.startsWith('Bearer ')) {
return NextResponse.json(
{ success: false, error: 'Authentication required' },
{ status: 401 }
)
}
const token = authHeader.substring(7)
const payload = verifyToken(token)
if (!payload) {
return NextResponse.json(
{ success: false, error: 'Invalid token' },
{ status: 401 }
)
}
const userId = payload.userId as string
const conversationId = params.id
// Verify conversation ownership
const existingConversation = await prisma.chatConversation.findUnique({
where: {
id: conversationId,
userId
}
})
if (!existingConversation) {
return NextResponse.json(
{ success: false, error: 'Conversation not found' },
{ status: 404 }
)
}
// Soft delete (mark as inactive) instead of hard delete to preserve data
await prisma.chatConversation.update({
where: { id: conversationId },
data: {
isActive: false,
updatedAt: new Date()
}
})
return NextResponse.json({
success: true,
message: 'Conversation deleted successfully'
})
} catch (error) {
console.error('Error deleting conversation:', error)
return NextResponse.json(
{
success: false,
error: 'Failed to delete conversation'
},
{ status: 500 }
)
}
}

View File

@@ -0,0 +1,206 @@
import { NextRequest, NextResponse } from 'next/server'
import { z } from 'zod'
import { PrismaClient } from '@prisma/client'
import { verifyToken } from '@/lib/auth'
const prisma = new PrismaClient()
export const runtime = 'nodejs'
const createConversationSchema = z.object({
title: z.string().min(1).max(200),
language: z.enum(['ro', 'en']).default('ro'),
})
const getConversationsSchema = z.object({
language: z.enum(['ro', 'en']).optional(),
limit: z.string().transform(Number).pipe(z.number().min(1).max(50)).optional().default(20),
offset: z.string().transform(Number).pipe(z.number().min(0)).optional().default(0),
})
// GET /api/chat/conversations - List user's conversations
export async function GET(request: NextRequest) {
try {
// Get user from authentication
const authHeader = request.headers.get('authorization')
if (!authHeader?.startsWith('Bearer ')) {
return NextResponse.json(
{ success: false, error: 'Authentication required' },
{ status: 401 }
)
}
const token = authHeader.substring(7)
const payload = verifyToken(token)
if (!payload) {
return NextResponse.json(
{ success: false, error: 'Invalid token' },
{ status: 401 }
)
}
const userId = payload.userId as string
// Parse query parameters
const url = new URL(request.url)
const queryParams = Object.fromEntries(url.searchParams.entries())
const { language, limit, offset } = getConversationsSchema.parse(queryParams)
// Build where clause
const where: any = {
userId,
isActive: true
}
if (language) {
where.language = language
}
// Get conversations with message count
const conversations = await prisma.chatConversation.findMany({
where,
orderBy: { lastMessageAt: 'desc' },
take: limit,
skip: offset,
include: {
_count: {
select: { messages: true }
},
messages: {
take: 1,
orderBy: { timestamp: 'desc' },
select: {
content: true,
role: true,
timestamp: true
}
}
}
})
// Get total count for pagination
const total = await prisma.chatConversation.count({ where })
return NextResponse.json({
success: true,
conversations: conversations.map(conv => ({
id: conv.id,
title: conv.title,
language: conv.language,
messageCount: conv._count.messages,
lastMessage: conv.messages[0] || null,
lastMessageAt: conv.lastMessageAt,
createdAt: conv.createdAt
})),
pagination: {
total,
limit,
offset,
hasMore: offset + limit < total
}
})
} catch (error) {
console.error('Error fetching conversations:', error)
if (error instanceof z.ZodError) {
return NextResponse.json(
{
success: false,
error: 'Invalid request parameters',
details: error.errors
},
{ status: 400 }
)
}
return NextResponse.json(
{
success: false,
error: 'Failed to fetch conversations'
},
{ status: 500 }
)
}
}
// POST /api/chat/conversations - Create new conversation
export async function POST(request: NextRequest) {
try {
// Get user from authentication
const authHeader = request.headers.get('authorization')
if (!authHeader?.startsWith('Bearer ')) {
return NextResponse.json(
{ success: false, error: 'Authentication required' },
{ status: 401 }
)
}
const token = authHeader.substring(7)
const payload = verifyToken(token)
if (!payload) {
return NextResponse.json(
{ success: false, error: 'Invalid token' },
{ status: 401 }
)
}
const userId = payload.userId as string
// Parse request body
const body = await request.json()
const { title, language } = createConversationSchema.parse(body)
// Create new conversation
const conversation = await prisma.chatConversation.create({
data: {
userId,
title,
language,
lastMessageAt: new Date()
},
include: {
_count: {
select: { messages: true }
}
}
})
return NextResponse.json({
success: true,
conversation: {
id: conversation.id,
title: conversation.title,
language: conversation.language,
messageCount: conversation._count.messages,
lastMessage: null,
lastMessageAt: conversation.lastMessageAt,
createdAt: conversation.createdAt
}
}, { status: 201 })
} catch (error) {
console.error('Error creating conversation:', error)
if (error instanceof z.ZodError) {
return NextResponse.json(
{
success: false,
error: 'Invalid request format',
details: error.errors
},
{ status: 400 }
)
}
return NextResponse.json(
{
success: false,
error: 'Failed to create conversation'
},
{ status: 500 }
)
}
}

View File

@@ -1,12 +1,18 @@
import { NextRequest, NextResponse } from 'next/server'
import { z } from 'zod'
import { PrismaClient, ChatMessageRole } from '@prisma/client'
import { searchBibleHybrid, BibleVerse } from '@/lib/vector-search'
import { verifyToken } from '@/lib/auth'
const prisma = new PrismaClient()
export const runtime = 'nodejs'
const chatRequestSchema = z.object({
message: z.string().min(1),
conversationId: z.string().optional(),
locale: z.string().optional().default('ro'),
// Keep history for backward compatibility with frontend
history: z.array(z.object({
id: z.string(),
role: z.enum(['user', 'assistant']),
@@ -18,15 +24,115 @@ const chatRequestSchema = z.object({
export async function POST(request: NextRequest) {
try {
const body = await request.json()
const { message, locale, history } = chatRequestSchema.parse(body)
const { message, conversationId, locale, history } = chatRequestSchema.parse(body)
// Generate response using Azure OpenAI with vector search
const response = await generateBiblicalResponse(message, locale, history)
// Try to get user from authentication (optional for backward compatibility)
let userId: string | null = null
const authHeader = request.headers.get('authorization')
if (authHeader?.startsWith('Bearer ')) {
try {
const token = authHeader.substring(7)
const payload = verifyToken(token)
userId = payload.userId
} catch (error) {
// Continue without authentication for backward compatibility
console.log('Chat without authentication')
}
}
// Handle conversation logic
let finalConversationId = conversationId
let conversationHistory: any[] = []
if (userId) {
// User is authenticated - use conversation system
if (conversationId) {
// Load existing conversation
const conversation = await prisma.chatConversation.findUnique({
where: {
id: conversationId,
userId,
isActive: true
},
include: {
messages: {
orderBy: { timestamp: 'desc' },
take: 15, // Last 15 messages for context
select: {
role: true,
content: true,
timestamp: true
}
}
}
})
if (conversation) {
conversationHistory = conversation.messages
.reverse() // Oldest first for context
.map(msg => ({
role: msg.role.toLowerCase(),
content: msg.content,
timestamp: msg.timestamp.toISOString()
}))
}
} else {
// Create new conversation
const conversation = await prisma.chatConversation.create({
data: {
userId,
title: generateConversationTitle(message),
language: locale,
lastMessageAt: new Date()
}
})
finalConversationId = conversation.id
}
} else {
// Anonymous user - use provided history for backward compatibility
conversationHistory = history
}
// Generate AI response
const aiResponse = await generateBiblicalResponse(message, locale, conversationHistory)
// Save messages to database if user is authenticated
if (userId && finalConversationId) {
await prisma.$transaction([
// Save user message
prisma.chatMessage.create({
data: {
conversationId: finalConversationId,
userId,
role: ChatMessageRole.USER,
content: message,
timestamp: new Date()
}
}),
// Save AI response
prisma.chatMessage.create({
data: {
conversationId: finalConversationId,
userId,
role: ChatMessageRole.ASSISTANT,
content: aiResponse,
timestamp: new Date()
}
}),
// Update conversation last message time
prisma.chatConversation.update({
where: { id: finalConversationId },
data: { lastMessageAt: new Date() }
})
])
}
return NextResponse.json({
success: true,
response
response: aiResponse,
conversationId: finalConversationId
})
} catch (error) {
console.error('Error in chat API:', error)
@@ -61,11 +167,8 @@ async function generateBiblicalResponse(message: string, locale: string, history
.map(verse => `${verse.ref}: "${verse.text_raw}"`)
.join('\n\n')
// Create conversation history for context
const conversationHistory = history
.slice(-3) // Last 3 messages for context
.map(msg => `${msg.role}: ${msg.content}`)
.join('\n')
// Intelligent context selection for conversation history
const conversationHistory = buildSmartContext(history, message, locale)
// Create language-specific system prompts
const systemPrompts = {
@@ -126,7 +229,7 @@ Current question: ${message}`
content: message
}
],
max_tokens: 800,
max_tokens: 2000,
temperature: 0.7,
top_p: 0.9
}),
@@ -161,3 +264,148 @@ Current question: ${message}`
return fallbackResponses[locale as keyof typeof fallbackResponses] || fallbackResponses.en
}
}
function generateConversationTitle(message: string): string {
// Generate a title from the first message (max 50 characters)
const title = message.length > 47
? message.substring(0, 47) + '...'
: message
return title
}
function buildSmartContext(history: any[], currentMessage: string, locale: string): string {
if (history.length === 0) return ''
const MAX_CONTEXT_TOKENS = 1500 // Reserve tokens for context
const RECENT_MESSAGES_COUNT = 6 // Always include last 6 messages
// Step 1: Always include the most recent messages for immediate context
const recentMessages = history.slice(-RECENT_MESSAGES_COUNT)
// Step 2: Calculate relevance scores for older messages
const olderMessages = history.slice(0, -RECENT_MESSAGES_COUNT)
const relevantOlderMessages = findRelevantMessages(olderMessages, currentMessage, locale)
// Step 3: Combine recent + relevant older messages
const selectedMessages = [...relevantOlderMessages, ...recentMessages]
// Step 4: Apply token-based truncation if needed
const optimizedContext = optimizeContextForTokens(selectedMessages, MAX_CONTEXT_TOKENS)
// Step 5: Format for AI consumption
return formatContextForAI(optimizedContext)
}
function findRelevantMessages(messages: any[], currentMessage: string, locale: string): any[] {
if (messages.length === 0) return []
// Score messages based on relevance to current question
const scoredMessages = messages.map(msg => ({
...msg,
relevanceScore: calculateMessageRelevance(msg, currentMessage, locale)
}))
// Sort by relevance and take top 3-5 most relevant older messages
return scoredMessages
.filter(msg => msg.relevanceScore > 0.3) // Only include somewhat relevant messages
.sort((a, b) => b.relevanceScore - a.relevanceScore)
.slice(0, 5) // Max 5 relevant older messages
}
function calculateMessageRelevance(message: any, currentMessage: string, locale: string): number {
const msgContent = message.content.toLowerCase()
const currentContent = currentMessage.toLowerCase()
let score = 0
// Keyword overlap scoring
const currentWords = currentContent.split(/\s+/).filter(word => word.length > 3)
const messageWords = msgContent.split(/\s+/)
for (const word of currentWords) {
if (messageWords.some(mWord => mWord.includes(word) || word.includes(mWord))) {
score += 0.2
}
}
// Biblical reference detection (higher relevance)
const biblicalPatterns = locale === 'ro'
? [/\b(geneza|exod|levitic|numeri|deuteronom|iosua|judecători|rut|samuel|regi|cronici|ezra|neemia|estera|iov|psalmi|proverbe|ecclesiast|cântarea|isaia|ieremia|plângeri|ezechiel|daniel|osea|ioel|amos|obadia|iona|mica|naum|habacuc|țefania|hagai|zaharia|maleahi|matei|marcu|luca|ioan|faptele|romani|corinteni|galateni|efeseni|filipeni|coloseni|tesaloniceni|timotei|tit|filimon|evrei|iacov|petru|ioan|iuda|apocalipsa)\s*\d+/gi]
: [/\b(genesis|exodus|leviticus|numbers|deuteronomy|joshua|judges|ruth|samuel|kings|chronicles|ezra|nehemiah|esther|job|psalm|proverbs|ecclesiastes|song|isaiah|jeremiah|lamentations|ezekiel|daniel|hosea|joel|amos|obadiah|jonah|micah|nahum|habakkuk|zephaniah|haggai|zechariah|malachi|matthew|mark|luke|john|acts|romans|corinthians|galatians|ephesians|philippians|colossians|thessalonians|timothy|titus|philemon|hebrews|james|peter|jude|revelation)\s*\d+/gi]
if (biblicalPatterns.some(pattern => pattern.test(msgContent))) {
score += 0.4
}
// Recent user questions get higher relevance
if (message.role === 'user') {
score += 0.1
}
// Time decay (older messages get slightly lower scores)
const messageAge = Date.now() - new Date(message.timestamp).getTime()
const hoursAge = messageAge / (1000 * 60 * 60)
const timeDecay = Math.max(0.5, 1 - (hoursAge / 168)) // Decay over a week
return Math.min(1.0, score * timeDecay)
}
function optimizeContextForTokens(messages: any[], maxTokens: number): any[] {
// Rough token estimation (1 token ≈ 4 characters in English, 3 in Romanian)
let currentTokens = 0
const optimizedMessages = []
// Start from most recent and work backwards
for (let i = messages.length - 1; i >= 0; i--) {
const message = messages[i]
const estimatedTokens = Math.ceil(message.content.length / 3.5)
if (currentTokens + estimatedTokens <= maxTokens) {
optimizedMessages.unshift(message) // Add to beginning to maintain order
currentTokens += estimatedTokens
} else {
// If this message would exceed limit, try to include a summary instead
if (i > 0 && optimizedMessages.length < 3) {
const summary = summarizeMessage(message)
const summaryTokens = Math.ceil(summary.length / 3.5)
if (currentTokens + summaryTokens <= maxTokens) {
optimizedMessages.unshift({
...message,
content: summary,
isSummary: true
})
currentTokens += summaryTokens
}
}
break
}
}
return optimizedMessages
}
function summarizeMessage(message: any): string {
const content = message.content
if (content.length <= 100) return content
// Extract key points and questions
const sentences = content.split(/[.!?]+/).filter(s => s.trim().length > 10)
if (sentences.length <= 2) return content
// Keep first and last sentence, or most important parts
const summary = sentences.length > 3
? `${sentences[0].trim()}... ${sentences[sentences.length - 1].trim()}`
: sentences.slice(0, 2).join('. ').trim()
return `[Summary] ${summary}`
}
function formatContextForAI(messages: any[]): string {
if (messages.length === 0) return ''
return messages.map(msg => {
const prefix = msg.isSummary ? '[Summary] ' : ''
return `${prefix}${msg.role}: ${msg.content}`
}).join('\n')
}