"error", "response" => "Silence... (No input detected)"]); exit; } // 2. BUILD THE COSMIC CONTEXT (The "Brain") // We simulate the logic of get_user_cosmic_data.php here or internally curl it. // For speed, we'll query DB directly since we have the connection. $user_email = $_SESSION['user_email'] ?? 'founder@themakersstay.com'; // Fallback for dev $stmt = $pdo->prepare("SELECT * FROM users WHERE email = :email LIMIT 1"); $stmt->execute([':email' => $user_email]); $user = $stmt->fetch(); // Calculate Context Variables $dob = new DateTime($user['dob']); $now = new DateTime(); $personalDay = 5; // Placeholder logic (copy calculateReducedSum from cosmic_data if needed) $dailyCard = "The Hermit"; // Placeholder logic // 3. CONSTRUCT THE SYSTEM PROMPT $systemPrompt = << "gpt-4-turbo", "messages" => [ ["role" => "system", "content" => $systemPrompt], ["role" => "user", "content" => $userMessage] ], "temperature" => 0.7 ]; $ch = curl_init("https://api.openai.com/v1/chat/completions"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Content-Type: application/json", "Authorization: Bearer $OPENAI_KEY" ]); $response = curl_exec($ch); curl_close($ch); $aiResult = json_decode($response, true); $reply = $aiResult['choices'][0]['message']['content'] ?? "The connection is hazy. The stars are silent."; echo json_encode([ "status" => "success", "response" => $reply, "debug_context" => "Loaded Profile: " . $user['full_name'] // For testing ]); ?>