apiUrl = $apiUrl; } else { $this->apiUrl = \App\Utils\Config::get('DAIAPI_URL', 'http://192.168.122.10:9001/run'); } } /** * Sends a prompt to the DAIAPI and returns the raw answer string. */ public function sendRequest(string $prompt): ?string { $payload = json_encode([ 'prompt' => $prompt ]); if ($payload === false) { error_log("DAIClient Error: Failed to encode JSON payload."); return null; } $ch = curl_init($this->apiUrl); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_HTTPHEADER => [ 'Content-Type: application/json', 'Content-Length: ' . strlen($payload) ], CURLOPT_POSTFIELDS => $payload, CURLOPT_TIMEOUT => $this->timeout, ]); $response = curl_exec($ch); $error = curl_error($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($response === false) { error_log("DAIClient Error: cURL error: $error"); return null; } if ($httpCode !== 200) { error_log("DAIClient Error: API returned HTTP code $httpCode. Response: $response"); return null; } $data = json_decode($response, true); if (!is_array($data) || empty($data['success']) || !isset($data['answer'])) { error_log("DAIClient Error: Invalid API response format: " . $response); return null; } return $data['answer']; } }