- Added comprehensive AI Assistant system (aiassist/ directory): * Vector search and embedding capabilities * Typebot proxy integration * Elastic search functionality * Message classification and chat history * MCP proxy for external integrations - Implemented Court Status API (GetCourtStatus.php): * Real-time court document status checking * Integration with external court systems * Comprehensive error handling and logging - Enhanced S3 integration: * Improved file backup system with metadata * Batch processing capabilities * Enhanced error logging and recovery * Copy operations with URL fixing - Added Telegram contact creation API - Improved error logging across all modules - Enhanced callback system for AI responses - Extensive backup file storage with timestamps - Updated documentation and README files - File storage improvements: * Thousands of backup files with proper metadata * Fix operations for broken file references * Project-specific backup and recovery systems * Comprehensive file integrity checking Total: 26,461+ files added/modified including AWS SDK, vendor dependencies, and extensive backup system.
102 lines
4.1 KiB
PHP
102 lines
4.1 KiB
PHP
<?php
|
||
// search_endpoint.php
|
||
|
||
header('Content-Type: application/json; charset=utf-8');
|
||
|
||
// === Путь к логам ===
|
||
$logPath = '/var/www/fastuser/data/www/crm.clientright.ru/aiassist/logs/search_endpoint.log';
|
||
|
||
// === Логгер ===
|
||
function logSearch($msg) {
|
||
global $logPath;
|
||
file_put_contents($logPath, "[" . date('Y-m-d H:i:s') . "] $msg\n", FILE_APPEND);
|
||
}
|
||
|
||
// Устанавливаем локаль для корректной работы с UTF-8
|
||
setlocale(LC_ALL, 'ru_RU.UTF-8');
|
||
|
||
// === Чтение JSON из тела запроса ===
|
||
$input = file_get_contents("php://input");
|
||
logSearch("🔻 Raw input: $input");
|
||
|
||
$data = json_decode($input, true);
|
||
if (
|
||
json_last_error() !== JSON_ERROR_NONE ||
|
||
empty($data['facts_full']) ||
|
||
empty($data['facts_short'])
|
||
) {
|
||
http_response_code(400);
|
||
logSearch("❌ Ошибка: невалидный JSON или отсутствуют обязательные поля");
|
||
echo json_encode(['error' => 'Невалидный JSON или отсутствуют поля facts_full и facts_short'], JSON_UNESCAPED_UNICODE);
|
||
exit;
|
||
}
|
||
|
||
// === Данные ===
|
||
$factsFull = mb_convert_encoding(trim($data['facts_full']), 'UTF-8', 'auto');
|
||
$factsShort = mb_convert_encoding(trim($data['facts_short']), 'UTF-8', 'auto');
|
||
|
||
logSearch("📩 facts_short: $factsShort");
|
||
logSearch("📩 facts_full: " . mb_substr($factsFull, 0, 200) . "...");
|
||
|
||
// Дополнительная проверка на пустые строки
|
||
if (empty($factsFull) || empty($factsShort)) {
|
||
http_response_code(400);
|
||
logSearch("❌ Ошибка: facts_full или facts_short пустые после trim");
|
||
echo json_encode(['error' => 'facts_full или facts_short пустые'], JSON_UNESCAPED_UNICODE);
|
||
exit;
|
||
}
|
||
|
||
// === Экранирование аргументов ===
|
||
logSearch("🔍 Before escapeshellarg - facts_full: '$factsFull'");
|
||
logSearch("🔍 Before escapeshellarg - facts_short: '$factsShort'");
|
||
|
||
$escapedFull = escapeshellarg($factsFull);
|
||
$escapedShort = escapeshellarg($factsShort);
|
||
|
||
logSearch("🔍 After escapeshellarg - escaped_full: $escapedFull");
|
||
logSearch("🔍 After escapeshellarg - escaped_short: $escapedShort");
|
||
|
||
// === Полный путь к Python из виртуального окружения ===
|
||
$pythonPath = "/var/www/search/legal/bin/python3";
|
||
//$scriptPath = "/var/www/search/legal/search_by_embedding.py";
|
||
$scriptPath = "/var/www/search/legal/search_by_text.py";
|
||
|
||
// === Сборка команды ===
|
||
$command = "sudo -u fastuser $pythonPath $scriptPath $escapedFull $escapedShort";
|
||
|
||
logSearch("🚀 Executing command: $command");
|
||
|
||
// === Запуск Python-скрипта ===
|
||
exec($command . " 2>&1", $output, $return_var); // stderr тоже в stdout
|
||
|
||
if ($return_var !== 0) {
|
||
logSearch("❌ Ошибка выполнения скрипта: код $return_var");
|
||
logSearch("🔻 STDERR/STDOUT: " . implode("\n", $output));
|
||
http_response_code(500);
|
||
echo json_encode(['error' => 'Ошибка Python-скрипта', 'details' => $output], JSON_UNESCAPED_UNICODE);
|
||
exit;
|
||
}
|
||
|
||
// Проверяем, что $output не пустой и содержит валидный JSON
|
||
if (empty($output)) {
|
||
logSearch("❌ Ошибка: Python-скрипт вернул пустой вывод");
|
||
http_response_code(500);
|
||
echo json_encode(['error' => 'Python-скрипт вернул пустой вывод'], JSON_UNESCAPED_UNICODE);
|
||
exit;
|
||
}
|
||
|
||
// Склеиваем вывод без лишних переносов
|
||
$jsonOutput = implode("", $output);
|
||
$parsedOutput = json_decode($jsonOutput, true);
|
||
if (json_last_error() !== JSON_ERROR_NONE) {
|
||
logSearch("❌ Ошибка: Python-скрипт вернул невалидный JSON: " . json_last_error_msg());
|
||
logSearch("🔻 Raw output: " . $jsonOutput);
|
||
http_response_code(500);
|
||
echo json_encode(['error' => 'Невалидный JSON от Python-скрипта', 'details' => $jsonOutput], JSON_UNESCAPED_UNICODE);
|
||
exit;
|
||
}
|
||
|
||
logSearch("✅ Скрипт успешно отработал. Вывод:\n" . $jsonOutput);
|
||
|
||
// === Ответ в n8n ===
|
||
echo $jsonOutput; |