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:
266
app/api/chat/conversations/[id]/route.ts
Normal file
266
app/api/chat/conversations/[id]/route.ts
Normal 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 }
|
||||
)
|
||||
}
|
||||
}
|
||||
206
app/api/chat/conversations/route.ts
Normal file
206
app/api/chat/conversations/route.ts
Normal 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 }
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user