import { NextRequest, NextResponse } from 'next/server'; import UTASChatBot from '@/lib/chatbot'; export async function POST(request: NextRequest) { try { const { message, mode = 'general', history = [], conversationHistory = [], systemPrompt = null, language = 'en' } = await request.json(); if (!message || typeof message !== 'string') { return NextResponse.json({ error: 'Message is required and must be a string' }, { status: 400 }); } // Initialize the chatbot with API key from environment const apiKey = process.env.OPENROUTER_API_KEY || ''; const chatbot = new UTASChatBot(apiKey); // Generate AI response using OpenRouter const response = await chatbot.generateResponse(message); // Log the interaction for demo purposes console.log('UTAS Chat:', { timestamp: new Date().toISOString(), message: message.substring(0, 100), response: response.substring(0, 100) }); return NextResponse.json({ message: response, response: response, // for backward compatibility timestamp: new Date().toISOString(), source: 'UTAS AI Assistant' }); } catch (error) { console.error('Chat API Error:', error); return NextResponse.json({ message: "I apologize, but I'm experiencing technical difficulties. Please try again or contact UTAS directly at +61 3 6226 6200 or info@utas.edu.au for immediate assistance.", timestamp: new Date().toISOString(), source: 'UTAS AI Assistant', error: true }, { status: 500 }); } } export async function GET() { return NextResponse.json({ service: 'UTAS AI Chat Assistant', status: 'active', features: [ 'RAG-powered responses using UTAS knowledge base', 'OpenRouter AI integration (when API key provided)', 'Real-time course and program information', 'Application guidance and support', 'Campus and research information' ], endpoints: { POST: 'Send message and conversation history', GET: 'Service status and information' } }); }