'Message is required']); exit; } $userMsg = $data['message']; $mode = $data['mode'] ?? 'regular'; $chatId = $data['chat_id'] ?? null; $creativity = $data['creativity'] ?? 0.7; $limitsOff = $data['limits_off'] ?? 0; try { // 1. Create chat if not exists if (!$chatId) { $stmt = db()->prepare("INSERT INTO chats (mode, title) VALUES (?, ?)"); $title = mb_substr($userMsg, 0, 50) . (mb_strlen($userMsg) > 50 ? '...' : ''); $stmt->execute([$mode, $title]); $chatId = db()->lastInsertId(); } // 2. Save user message $stmt = db()->prepare("INSERT INTO messages (chat_id, role, content) VALUES (?, 'user', ?)"); $stmt->execute([$chatId, $userMsg]); // 3. Prepare AI Prompt based on mode $systemPrompt = "You are a helpful, intelligent AI assistant."; if ($mode === 'coding') { $systemPrompt = "You are an elite senior software engineer and architect. Provide clean, secure, efficient, and perfectly documented code. Explain your reasoning briefly."; } elseif ($mode === 'game' || $mode === 'app') { $systemPrompt = "You are a world-class single-file application developer. Your goal is to generate a complete, functional, and visually stunning HTML/CSS/JS application or game in a SINGLE message. Always wrap the code in a triple-backtick block starting with ```html. Ensure the app is self-contained (no external assets unless they are from reliable CDNs). Include CSS in