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

@@ -30,9 +30,11 @@ import {
ThumbDown,
Minimize,
Launch,
History,
} from '@mui/icons-material'
import { useState, useRef, useEffect } from 'react'
import { useTranslations, useLocale } from 'next-intl'
import ReactMarkdown from 'react-markdown'
interface ChatMessage {
id: string
@@ -48,6 +50,7 @@ export default function FloatingChat() {
const [isOpen, setIsOpen] = useState(false)
const [isMinimized, setIsMinimized] = useState(false)
const [isFullscreen, setIsFullscreen] = useState(false)
const [showHistory, setShowHistory] = useState(false)
const [messages, setMessages] = useState<ChatMessage[]>([
{
id: '1',
@@ -240,6 +243,14 @@ export default function FloatingChat() {
</Box>
</Box>
<Box>
<IconButton
size="small"
onClick={() => setShowHistory(!showHistory)}
sx={{ color: 'white', mr: 0.5 }}
title="Chat History"
>
<History />
</IconButton>
<IconButton
size="small"
onClick={minimizeChat}
@@ -265,6 +276,39 @@ export default function FloatingChat() {
</Box>
{!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 */}
<Box sx={{ p: 2, borderBottom: 1, borderColor: 'divider' }}>
@@ -338,15 +382,62 @@ export default function FloatingChat() {
maxWidth: '100%',
}}
>
<Typography
variant="body2"
sx={{
whiteSpace: 'pre-wrap',
lineHeight: 1.4,
}}
>
{message.content}
</Typography>
{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
variant="body2"
sx={{
whiteSpace: 'pre-wrap',
lineHeight: 1.4,
}}
>
{message.content}
</Typography>
)}
{message.role === 'assistant' && (
<Box sx={{ display: 'flex', gap: 0.5, mt: 1, justifyContent: 'flex-end' }}>