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 { NextRequest, NextResponse } from 'next/server'
import { z } from 'zod' import { z } from 'zod'
import { PrismaClient, ChatMessageRole } from '@prisma/client'
import { searchBibleHybrid, BibleVerse } from '@/lib/vector-search' import { searchBibleHybrid, BibleVerse } from '@/lib/vector-search'
import { verifyToken } from '@/lib/auth'
const prisma = new PrismaClient()
export const runtime = 'nodejs' export const runtime = 'nodejs'
const chatRequestSchema = z.object({ const chatRequestSchema = z.object({
message: z.string().min(1), message: z.string().min(1),
conversationId: z.string().optional(),
locale: z.string().optional().default('ro'), locale: z.string().optional().default('ro'),
// Keep history for backward compatibility with frontend
history: z.array(z.object({ history: z.array(z.object({
id: z.string(), id: z.string(),
role: z.enum(['user', 'assistant']), role: z.enum(['user', 'assistant']),
@@ -18,15 +24,115 @@ const chatRequestSchema = z.object({
export async function POST(request: NextRequest) { export async function POST(request: NextRequest) {
try { try {
const body = await request.json() 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 // Try to get user from authentication (optional for backward compatibility)
const response = await generateBiblicalResponse(message, locale, history) 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({ return NextResponse.json({
success: true, success: true,
response response: aiResponse,
conversationId: finalConversationId
}) })
} catch (error) { } catch (error) {
console.error('Error in chat API:', 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}"`) .map(verse => `${verse.ref}: "${verse.text_raw}"`)
.join('\n\n') .join('\n\n')
// Create conversation history for context // Intelligent context selection for conversation history
const conversationHistory = history const conversationHistory = buildSmartContext(history, message, locale)
.slice(-3) // Last 3 messages for context
.map(msg => `${msg.role}: ${msg.content}`)
.join('\n')
// Create language-specific system prompts // Create language-specific system prompts
const systemPrompts = { const systemPrompts = {
@@ -126,7 +229,7 @@ Current question: ${message}`
content: message content: message
} }
], ],
max_tokens: 800, max_tokens: 2000,
temperature: 0.7, temperature: 0.7,
top_p: 0.9 top_p: 0.9
}), }),
@@ -161,3 +264,148 @@ Current question: ${message}`
return fallbackResponses[locale as keyof typeof fallbackResponses] || fallbackResponses.en 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')
}

View File

@@ -30,9 +30,11 @@ import {
ThumbDown, ThumbDown,
Minimize, Minimize,
Launch, Launch,
History,
} from '@mui/icons-material' } from '@mui/icons-material'
import { useState, useRef, useEffect } from 'react' import { useState, useRef, useEffect } from 'react'
import { useTranslations, useLocale } from 'next-intl' import { useTranslations, useLocale } from 'next-intl'
import ReactMarkdown from 'react-markdown'
interface ChatMessage { interface ChatMessage {
id: string id: string
@@ -48,6 +50,7 @@ export default function FloatingChat() {
const [isOpen, setIsOpen] = useState(false) const [isOpen, setIsOpen] = useState(false)
const [isMinimized, setIsMinimized] = useState(false) const [isMinimized, setIsMinimized] = useState(false)
const [isFullscreen, setIsFullscreen] = useState(false) const [isFullscreen, setIsFullscreen] = useState(false)
const [showHistory, setShowHistory] = useState(false)
const [messages, setMessages] = useState<ChatMessage[]>([ const [messages, setMessages] = useState<ChatMessage[]>([
{ {
id: '1', id: '1',
@@ -240,6 +243,14 @@ export default function FloatingChat() {
</Box> </Box>
</Box> </Box>
<Box> <Box>
<IconButton
size="small"
onClick={() => setShowHistory(!showHistory)}
sx={{ color: 'white', mr: 0.5 }}
title="Chat History"
>
<History />
</IconButton>
<IconButton <IconButton
size="small" size="small"
onClick={minimizeChat} onClick={minimizeChat}
@@ -265,6 +276,39 @@ export default function FloatingChat() {
</Box> </Box>
{!isMinimized && ( {!isMinimized && (
<>
{/* Chat History Panel */}
{showHistory && (
<Box sx={{
p: 2,
borderBottom: 1,
borderColor: 'divider',
bgcolor: 'grey.50',
maxHeight: '300px',
overflowY: 'auto'
}}>
<Typography variant="h6" sx={{ mb: 2 }}>
Chat History
</Typography>
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 1 }}>
<Typography variant="body2" color="text.secondary" textAlign="center" sx={{ py: 2 }}>
📝 Chat history will appear here
</Typography>
<Typography variant="body2" color="text.secondary" textAlign="center">
Sign in to save your conversations
</Typography>
<Button
variant="outlined"
size="small"
sx={{ mt: 1 }}
onClick={() => setShowHistory(false)}
>
Close History
</Button>
</Box>
</Box>
)}
<> <>
{/* Suggested Questions */} {/* Suggested Questions */}
<Box sx={{ p: 2, borderBottom: 1, borderColor: 'divider' }}> <Box sx={{ p: 2, borderBottom: 1, borderColor: 'divider' }}>
@@ -338,6 +382,52 @@ export default function FloatingChat() {
maxWidth: '100%', maxWidth: '100%',
}} }}
> >
{message.role === 'assistant' ? (
<ReactMarkdown
components={{
p: ({ children }) => (
<Typography
variant="body2"
sx={{ mb: 1, lineHeight: 1.4 }}
>
{children}
</Typography>
),
h3: ({ children }) => (
<Typography
variant="h6"
sx={{ fontWeight: 'bold', mt: 2, mb: 1 }}
>
{children}
</Typography>
),
strong: ({ children }) => (
<Typography
component="span"
sx={{ fontWeight: 'bold' }}
>
{children}
</Typography>
),
ul: ({ children }) => (
<Box component="ul" sx={{ pl: 2, mb: 1 }}>
{children}
</Box>
),
li: ({ children }) => (
<Typography
component="li"
variant="body2"
sx={{ mb: 0.5 }}
>
{children}
</Typography>
),
}}
>
{message.content}
</ReactMarkdown>
) : (
<Typography <Typography
variant="body2" variant="body2"
sx={{ sx={{
@@ -347,6 +437,7 @@ export default function FloatingChat() {
> >
{message.content} {message.content}
</Typography> </Typography>
)}
{message.role === 'assistant' && ( {message.role === 'assistant' && (
<Box sx={{ display: 'flex', gap: 0.5, mt: 1, justifyContent: 'flex-end' }}> <Box sx={{ display: 'flex', gap: 0.5, mt: 1, justifyContent: 'flex-end' }}>

View File

@@ -24,6 +24,7 @@ model User {
chapterBookmarks ChapterBookmark[] chapterBookmarks ChapterBookmark[]
notes Note[] notes Note[]
chatMessages ChatMessage[] chatMessages ChatMessage[]
chatConversations ChatConversation[]
prayerRequests PrayerRequest[] prayerRequests PrayerRequest[]
userPrayers UserPrayer[] userPrayers UserPrayer[]
readingHistory ReadingHistory[] readingHistory ReadingHistory[]
@@ -126,17 +127,43 @@ model BiblePassage {
@@index([testament]) @@index([testament])
} }
model ChatConversation {
id String @id @default(uuid())
userId String? // Optional for anonymous users
title String // Auto-generated from first message
language String // 'ro' or 'en'
isActive Boolean @default(true)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
lastMessageAt DateTime @default(now())
user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
messages ChatMessage[]
@@index([userId, language, lastMessageAt])
@@index([isActive, lastMessageAt])
}
model ChatMessage { model ChatMessage {
id String @id @default(uuid()) id String @id @default(uuid())
userId String conversationId String
role String // 'user' or 'assistant' userId String? // Keep for backward compatibility
role ChatMessageRole
content String @db.Text content String @db.Text
metadata Json? // Store verse references, etc. metadata Json? // Store verse references, etc.
createdAt DateTime @default(now()) timestamp DateTime @default(now())
user User @relation(fields: [userId], references: [id], onDelete: Cascade) conversation ChatConversation @relation(fields: [conversationId], references: [id], onDelete: Cascade)
user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
@@index([userId, createdAt]) @@index([conversationId, timestamp])
@@index([userId, timestamp])
}
enum ChatMessageRole {
USER
ASSISTANT
SYSTEM
} }
model Bookmark { model Bookmark {

View File

@@ -0,0 +1,364 @@
# AI Chat Improvements Plan
## Overview
Enhance the AI chat system with persistent chat history and conversation memory to provide a more engaging and contextual user experience.
## Current State Analysis
- ✅ Basic AI chat with Azure OpenAI integration
- ✅ ReactMarkdown rendering for formatted responses
- ✅ Floating chat component with fullscreen mode
- ✅ Language-specific responses (Romanian/English)
- ❌ No chat persistence between sessions
- ❌ Limited conversation memory (only last 3 messages)
- ❌ No user-specific chat history
- ❌ No chat management UI
## Goals
1. **Persistent Chat History**: Store chat conversations in database per user and language
2. **Enhanced Memory**: Maintain longer conversation context for better AI responses
3. **Chat Management**: Allow users to view, organize, and manage their chat history
4. **Multi-language Support**: Separate chat histories for Romanian and English
## Technical Implementation Plan
### 1. Database Schema Extensions
#### 1.1 Chat Conversations Table
```prisma
model ChatConversation {
id String @id @default(cuid())
userId String? // Optional for anonymous users
title String // Auto-generated from first message
language String // 'ro' or 'en'
isActive Boolean @default(true)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
lastMessageAt DateTime @default(now())
user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
messages ChatMessage[]
@@index([userId, language, lastMessageAt])
@@index([isActive, lastMessageAt])
}
model ChatMessage {
id String @id @default(cuid())
conversationId String
role ChatMessageRole
content String @db.Text
timestamp DateTime @default(now())
metadata Json? // For storing additional context
conversation ChatConversation @relation(fields: [conversationId], references: [id], onDelete: Cascade)
@@index([conversationId, timestamp])
}
enum ChatMessageRole {
USER
ASSISTANT
SYSTEM
}
```
#### 1.2 Update User Model
```prisma
model User {
// ... existing fields
chatConversations ChatConversation[]
}
```
### 2. API Endpoints
#### 2.1 Chat Conversations API (`/api/chat/conversations`)
- **GET**: List user's conversations (paginated, filtered by language)
- **POST**: Create new conversation
- **DELETE /:id**: Delete conversation
#### 2.2 Enhanced Chat API (`/api/chat`)
- **Modify existing POST**: Include conversation management
- **GET /:conversationId**: Get conversation history
- **PUT /:conversationId**: Update conversation (rename, etc.)
#### 2.3 Chat Messages API (`/api/chat/:conversationId/messages`)
- **GET**: Get all messages in conversation (paginated)
- **POST**: Add message to conversation
### 3. Frontend Components
#### 3.1 Enhanced Floating Chat Component
```typescript
interface FloatingChatProps {
conversationId?: string
initialMessage?: string
}
// New state management
const [conversations, setConversations] = useState<Conversation[]>([])
const [activeConversationId, setActiveConversationId] = useState<string | null>(null)
const [messages, setMessages] = useState<ChatMessage[]>([])
const [isHistoryOpen, setIsHistoryOpen] = useState(false)
```
#### 3.2 Chat History Sidebar
```typescript
interface ChatHistorySidebarProps {
conversations: Conversation[]
activeConversationId: string | null
onSelectConversation: (id: string) => void
onNewConversation: () => void
onDeleteConversation: (id: string) => void
language: string
}
```
#### 3.3 Conversation List Item
```typescript
interface ConversationListItemProps {
conversation: Conversation
isActive: boolean
onClick: () => void
onDelete: () => void
onRename: (newTitle: string) => void
}
```
### 4. Implementation Phases
#### Phase 1: Database Schema & Basic API
1. **Create Prisma migrations** for new tables
2. **Implement conversation CRUD APIs**
3. **Add database seed scripts** for testing
4. **Update existing chat API** to work with conversations
#### Phase 2: Enhanced Memory System
1. **Modify chat API** to include more conversation context
2. **Implement intelligent context selection** (last 10-15 messages)
3. **Add conversation summarization** for very long chats
4. **Optimize token usage** with smart context management
#### Phase 3: Frontend Chat Management
1. **Add conversation state management** to floating chat
2. **Implement chat history sidebar**
3. **Add conversation creation/deletion** functionality
4. **Implement conversation switching** within chat
#### Phase 4: Advanced Features
1. **Auto-generate conversation titles** from first message
2. **Add conversation search/filtering**
3. **Implement conversation sharing** (optional)
4. **Add conversation export** functionality
### 5. Detailed Implementation Steps
#### 5.1 Database Setup
```bash
# Create migration
npx prisma migrate dev --name add-chat-conversations
# Update database
npx prisma db push
# Generate client
npx prisma generate
```
#### 5.2 API Implementation Strategy
**Chat API Enhancement (`/api/chat/route.ts`)**:
```typescript
// Modified request schema
const chatRequestSchema = z.object({
message: z.string().min(1),
conversationId: z.string().optional(),
locale: z.string().default('ro'),
userId: z.string().optional()
})
// Enhanced response
interface ChatResponse {
success: boolean
response: string
conversationId: string
messageId: string
}
```
**Conversation Management Logic**:
```typescript
async function handleChatMessage(
message: string,
conversationId?: string,
locale: string = 'ro',
userId?: string
) {
// 1. Get or create conversation
const conversation = conversationId
? await getConversation(conversationId)
: await createConversation(userId, locale, message)
// 2. Get conversation history (last 15 messages)
const history = await getConversationHistory(conversation.id, 15)
// 3. Generate AI response with full context
const aiResponse = await generateBiblicalResponse(message, locale, history)
// 4. Save both user message and AI response
await saveMessages(conversation.id, [
{ role: 'user', content: message },
{ role: 'assistant', content: aiResponse }
])
// 5. Update conversation metadata
await updateConversationActivity(conversation.id)
return { response: aiResponse, conversationId: conversation.id }
}
```
#### 5.3 Frontend State Management
**Enhanced Floating Chat Hook**:
```typescript
function useFloatingChat() {
const [conversations, setConversations] = useState<Conversation[]>([])
const [activeConversation, setActiveConversation] = useState<Conversation | null>(null)
const [messages, setMessages] = useState<ChatMessage[]>([])
const [isLoading, setIsLoading] = useState(false)
const loadConversations = useCallback(async () => {
// Fetch user's conversations
}, [])
const switchConversation = useCallback(async (conversationId: string) => {
// Load conversation messages
}, [])
const startNewConversation = useCallback(() => {
// Reset state for new conversation
}, [])
const sendMessage = useCallback(async (message: string) => {
// Send message with conversation context
}, [activeConversation])
return {
conversations,
activeConversation,
messages,
isLoading,
loadConversations,
switchConversation,
startNewConversation,
sendMessage
}
}
```
### 6. UI/UX Enhancements
#### 6.1 Chat History Sidebar Layout
```
┌─────────────────┬──────────────────────┐
│ Chat History │ Active Chat │
│ │ │
│ ○ New Chat │ ┌─ Messages ─────┐ │
│ │ │ │ │
│ ╔═ Today ═══ │ │ User: Question │ │
│ ║ ○ Bible Q&A │ │ AI: Response │ │
│ ║ ○ Prayer Help │ │ ... │ │
│ ╚═══════════ │ │ │ │
│ │ └────────────────┘ │
│ ╔═ Yesterday ═ │ ┌─ Input ──────────┐ │
│ ║ ○ Verse Study │ │ [Type message... ]│ │
│ ╚═══════════ │ └──────────────────┘ │
└─────────────────┴──────────────────────┘
```
#### 6.2 Mobile-Responsive Design
- **Mobile**: Stack history as overlay/drawer
- **Tablet**: Side-by-side with collapsed history
- **Desktop**: Full side-by-side layout
### 7. Performance Considerations
#### 7.1 Database Optimization
- **Indexes**: conversation lookups, message pagination
- **Pagination**: Limit conversation and message queries
- **Cleanup**: Archive old conversations after 6 months
#### 7.2 Frontend Optimization
- **Lazy loading**: Load conversations on demand
- **Virtualization**: For large message lists
- **Caching**: Cache conversation metadata
#### 7.3 AI Context Management
- **Smart truncation**: Summarize old messages if context too long
- **Relevance scoring**: Prioritize recent and relevant messages
- **Token optimization**: Balance context richness vs cost
### 8. Security & Privacy
#### 8.1 Data Protection
- **User isolation**: Strict user-based access control
- **Encryption**: Sensitive conversation content
- **Retention policy**: Auto-delete after configurable period
#### 8.2 Anonymous Users
- **Session-based storage**: For non-authenticated users
- **Migration path**: Convert anonymous chats to user accounts
### 9. Testing Strategy
#### 9.1 Unit Tests
- Conversation CRUD operations
- Message history management
- AI context generation
#### 9.2 Integration Tests
- Full chat flow with persistence
- Conversation switching
- Multi-language handling
#### 9.3 User Testing
- Chat history navigation
- Conversation management
- Mobile responsiveness
### 10. Deployment Considerations
#### 10.1 Migration Strategy
- **Backward compatibility**: Existing chat continues to work
- **Data migration**: Convert existing session data if applicable
- **Feature flags**: Gradual rollout of new features
#### 10.2 Monitoring
- **Conversation metrics**: Creation, length, engagement
- **Performance monitoring**: API response times
- **Error tracking**: Failed chat operations
## Success Metrics
1. **User Engagement**: Longer chat sessions, return conversations
2. **Conversation Quality**: Better AI responses with context
3. **User Satisfaction**: Positive feedback on chat experience
4. **Technical Performance**: Fast conversation loading, reliable persistence
## Timeline Estimate
- **Phase 1** (Database & API): 3-4 days
- **Phase 2** (Enhanced Memory): 2-3 days
- **Phase 3** (Frontend Management): 4-5 days
- **Phase 4** (Advanced Features): 3-4 days
**Total**: ~2-3 weeks for complete implementation
## Next Steps
1. Review and approve this plan
2. Begin with Phase 1: Database schema and basic API
3. Implement incremental improvements
4. Test thoroughly at each phase
5. Gather user feedback and iterate