sar / src /app /api /chat /route.ts
Mafia2008's picture
feat: full CORS support + OPTIONS preflight + interactive /api-docs reference page
fdd8ef0
Raw
History Blame Contribute Delete
4.97 kB
// ═══════════════════════════════════════════════════════════════════════════
// CHAT API - Send and retrieve chat messages
// ═══════════════════════════════════════════════════════════════════════════
// POST /api/chat - Send a chat message
// GET /api/chat?sessionId=xxx&limit=100 - Get chat history
// ═══════════════════════════════════════════════════════════════════════════
import { NextRequest, NextResponse } from 'next/server';
import { sessionManager } from '@/lib/session-manager';
export { OPTIONS } from '@/lib/cors';
export const runtime = 'nodejs';
export const dynamic = 'force-dynamic';
// ═══════════════════════════════════════════════════════════════════════════
// GET CHAT HISTORY
// ═══════════════════════════════════════════════════════════════════════════
// GET /api/chat?sessionId=xxx&limit=100
// ═══════════════════════════════════════════════════════════════════════════
export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url);
const sessionId = searchParams.get('sessionId');
const limit = parseInt(searchParams.get('limit') || '100', 10);
if (!sessionId) {
return NextResponse.json({
success: false,
error: 'Missing required parameter: sessionId'
}, { status: 400 });
}
const session = sessionManager.getSession(sessionId);
if (!session) {
return NextResponse.json({
success: false,
error: 'Session not found'
}, { status: 404 });
}
const messages = sessionManager.getChatMessages(sessionId, limit);
return NextResponse.json({
success: true,
data: {
messages,
count: messages.length
}
});
}
// ═══════════════════════════════════════════════════════════════════════════
// SEND CHAT MESSAGE
// ═══════════════════════════════════════════════════════════════════════════
// POST /api/chat
// Body: {
// sessionId: string, // Required: Session ID
// userId: string, // Required: User's unique ID
// userName: string, // Required: User's display name
// message: string, // Required: Chat message content
// isTeacher: boolean // Required: Whether the sender is a teacher
// }
// ═══════════════════════════════════════════════════════════════════════════
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { sessionId, userId, userName, message, isTeacher } = body;
// Validate required fields
if (!sessionId || !userId || !userName || !message) {
return NextResponse.json({
success: false,
error: 'Missing required fields: sessionId, userId, userName, message'
}, { status: 400 });
}
// Validate message content
if (typeof message !== 'string' || message.trim().length === 0) {
return NextResponse.json({
success: false,
error: 'Message cannot be empty'
}, { status: 400 });
}
// Check session exists
const session = sessionManager.getSession(sessionId);
if (!session) {
return NextResponse.json({
success: false,
error: 'Session not found'
}, { status: 404 });
}
// Send message
const chatMessage = await sessionManager.sendChatMessage(
sessionId,
userId,
userName,
message.trim(),
isTeacher || false
);
return NextResponse.json({
success: true,
data: chatMessage,
message: 'Message sent successfully'
});
} catch (error) {
return NextResponse.json({
success: false,
error: 'Invalid JSON body'
}, { status: 400 });
}
}