'required']); if ($errors) { json_error('النص مطلوب', 422); } $apiKey = env('XAI_API_KEY'); // Ensure this is set in .env if (!$apiKey) { json_error('xAI API Key غير متوفر', 500); } $text = $data['text']; $systemPrompt = << 'grok-1', // Update to the correct Grok model when available 'messages' => [ ['role' => 'system', 'content' => $systemPrompt], ['role' => 'user', 'content' => $text] ], 'response_format' => ['type' => 'json_object'], 'temperature' => 0.2 ]; $url = "https://api.x.ai/v1/chat/completions"; $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode($payload), CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => [ 'Content-Type: application/json', 'Authorization: Bearer ' . $apiKey ] ]); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $error = curl_error($ch); curl_close($ch); if ($httpCode !== 200) { error_log("Grok Error: $response | $error"); json_error('فشل في تحليل الأمر بواسطة Grok', 500); } $respData = json_decode($response, true); if (!isset($respData['choices'][0]['message']['content'])) { json_error('رد غير متوقع من Grok AI', 500); } $jsonText = $respData['choices'][0]['message']['content']; $parsed = json_decode($jsonText, true); if (!$parsed) { json_error('فشل في تحليل الرد كـ JSON', 500); } json_success($parsed, 'تم تحليل الأمر بواسطة Grok');