Files
biblical-guide.com/app/api/chat/conversations/[id]/route.ts
andupetcu 86b7ff377b 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>
2025-09-22 11:33:36 +03:00

266 lines
6.5 KiB
TypeScript

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