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>
206 lines
5.1 KiB
TypeScript
206 lines
5.1 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 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 }
|
|
)
|
|
}
|
|
} |