'Missing required data']); exit; } $username = $data['username']; $comment_author = $data['author'] ?? 'Anonymous'; $comment_text = $data['comment']; // Prompt for AI $prompt = "You are a helpful and funny AI assistant for a TikTok live stream. The streamer is '$username'. The user '$comment_author' just said: '$comment_text'. Respond to them in a short, engaging, and friendly way (max 20 words). Keep it suitable for a live stream audience."; $resp = LocalAIApi::createResponse([ 'input' => [ ['role' => 'system', 'content' => 'TikTok Live AI Assistant'], ['role' => 'user', 'content' => $prompt], ], ]); $ai_reply = ''; if (!empty($resp['success'])) { $ai_reply = LocalAIApi::extractText($resp); } else { $ai_reply = "Thanks for your comment, $comment_author!"; } // Persist to DB try { $stmt = db()->prepare("INSERT INTO tiktok_history (username, comment_author, comment_text, ai_reply) VALUES (?, ?, ?, ?)"); $stmt->execute([$username, $comment_author, $comment_text, $ai_reply]); } catch (Exception $e) { // Log error but continue } echo json_encode([ 'success' => true, 'reply' => $ai_reply, 'author' => $comment_author, 'comment' => $comment_text ]);