74 lines
2.0 KiB
PHP
74 lines
2.0 KiB
PHP
|
|
<?php
|
||
|
|
// get_ai_result.php
|
||
|
|
// Polling endpoint для проверки статуса AI задачи
|
||
|
|
|
||
|
|
header('Content-Type: application/json');
|
||
|
|
header('Access-Control-Allow-Origin: *');
|
||
|
|
header('Access-Control-Allow-Methods: GET, OPTIONS');
|
||
|
|
header('Access-Control-Allow-Headers: Content-Type');
|
||
|
|
|
||
|
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
||
|
|
http_response_code(200);
|
||
|
|
exit();
|
||
|
|
}
|
||
|
|
|
||
|
|
error_reporting(E_ALL);
|
||
|
|
ini_set('display_errors', 1);
|
||
|
|
|
||
|
|
try {
|
||
|
|
$taskId = $_GET['task_id'] ?? null;
|
||
|
|
|
||
|
|
if (!$taskId) {
|
||
|
|
throw new Exception('Missing task_id parameter');
|
||
|
|
}
|
||
|
|
|
||
|
|
// Подключаемся к БД
|
||
|
|
include_once('config.inc.php');
|
||
|
|
|
||
|
|
$conn = new mysqli($dbconfig['db_server'], $dbconfig['db_username'], $dbconfig['db_password'], $dbconfig['db_name']);
|
||
|
|
|
||
|
|
if ($conn->connect_error) {
|
||
|
|
throw new Exception("DB connection failed: " . $conn->connect_error);
|
||
|
|
}
|
||
|
|
|
||
|
|
$conn->set_charset('utf8mb4');
|
||
|
|
|
||
|
|
// Получаем статус задачи
|
||
|
|
$stmt = $conn->prepare("SELECT status, response_data, error_message FROM ai_responses WHERE task_id = ?");
|
||
|
|
$stmt->bind_param('s', $taskId);
|
||
|
|
$stmt->execute();
|
||
|
|
$result = $stmt->get_result();
|
||
|
|
|
||
|
|
if ($row = $result->fetch_assoc()) {
|
||
|
|
$response = [
|
||
|
|
'success' => true,
|
||
|
|
'status' => $row['status'],
|
||
|
|
'taskId' => $taskId
|
||
|
|
];
|
||
|
|
|
||
|
|
if ($row['status'] === 'completed') {
|
||
|
|
$response['response'] = $row['response_data'];
|
||
|
|
} elseif ($row['status'] === 'error') {
|
||
|
|
$response['error'] = $row['error_message'];
|
||
|
|
} else {
|
||
|
|
$response['message'] = 'Обработка...';
|
||
|
|
}
|
||
|
|
|
||
|
|
echo json_encode($response, JSON_UNESCAPED_UNICODE);
|
||
|
|
} else {
|
||
|
|
throw new Exception('Task not found');
|
||
|
|
}
|
||
|
|
|
||
|
|
$stmt->close();
|
||
|
|
$conn->close();
|
||
|
|
|
||
|
|
} catch (Exception $e) {
|
||
|
|
error_log("Get AI Result Error: " . $e->getMessage());
|
||
|
|
|
||
|
|
http_response_code(500);
|
||
|
|
echo json_encode([
|
||
|
|
'success' => false,
|
||
|
|
'error' => $e->getMessage()
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
?>
|