65 lines
2.0 KiB
PHP
65 lines
2.0 KiB
PHP
<?php
|
|
// aiassist/chat_history.php
|
|
// Загрузка истории чата для AI Drawer
|
|
|
|
header('Content-Type: application/json');
|
|
header('Access-Control-Allow-Origin: *');
|
|
header('Access-Control-Allow-Methods: POST, OPTIONS');
|
|
header('Access-Control-Allow-Headers: Content-Type');
|
|
|
|
// Обработка preflight запросов
|
|
if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
|
http_response_code(200);
|
|
exit();
|
|
}
|
|
|
|
try {
|
|
// Получение данных из POST запроса
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (!$input) {
|
|
throw new Exception('Invalid JSON input');
|
|
}
|
|
|
|
$context = $input['context'] ?? [];
|
|
$limit = $input['limit'] ?? 10;
|
|
|
|
// Создаем уникальный ключ для истории на основе контекста
|
|
$historyKey = md5(json_encode([
|
|
'module' => $context['currentModule'] ?? '',
|
|
'record' => $context['projectId'] ?? '',
|
|
'user' => $context['userId'] ?? ''
|
|
]));
|
|
|
|
// Путь к файлу истории
|
|
$historyFile = __DIR__ . '/logs/chat_history_' . $historyKey . '.json';
|
|
|
|
$history = [];
|
|
|
|
// Загружаем историю если файл существует
|
|
if (file_exists($historyFile)) {
|
|
$historyData = file_get_contents($historyFile);
|
|
$decodedHistory = json_decode($historyData, true);
|
|
|
|
if ($decodedHistory && isset($decodedHistory['messages'])) {
|
|
// Берем последние $limit сообщений
|
|
$history = array_slice($decodedHistory['messages'], -$limit);
|
|
}
|
|
}
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'history' => $history,
|
|
'count' => count($history),
|
|
'historyKey' => $historyKey
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
http_response_code(500);
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => $e->getMessage()
|
|
]);
|
|
}
|
|
?>
|