From c45f98160a90907db7a973cb02642a5ddf0bbe93 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 12 Nov 2025 22:34:39 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=D0=BD=D0=BE=D0=B2=D0=B0=D1=8F=20=D1=84?= =?UTF-8?q?=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app.js | 1 + backend/services/ai-assistant.js | 4 ++-- backend/services/profileAnalysisService.js | 2 +- backend/services/userContextService.js | 27 +++++++++++++++++++--- frontend/nginx-local.conf | 6 +++++ frontend/nginx-simple.conf | 6 +++++ 6 files changed, 40 insertions(+), 6 deletions(-) diff --git a/backend/app.js b/backend/app.js index f71ac37..c6c9268 100644 --- a/backend/app.js +++ b/backend/app.js @@ -115,6 +115,7 @@ const app = express(); // Указываем хост явно app.set('host', '0.0.0.0'); app.set('port', process.env.PORT || 8000); +app.set('trust proxy', true); // Настройка CORS const corsOrigins = process.env.NODE_ENV === 'production' diff --git a/backend/services/ai-assistant.js b/backend/services/ai-assistant.js index 2b25286..7438919 100644 --- a/backend/services/ai-assistant.js +++ b/backend/services/ai-assistant.js @@ -112,7 +112,7 @@ class AIAssistant { let userNameForProfile = null; let shouldAskForName = false; let profileAnalysis = null; - if (userId && (typeof userId !== 'string' || !userId.toString().startsWith('guest_'))) { + if (userId && !userContextService.isGuestId(userId)) { try { profileAnalysis = await profileAnalysisService.analyzeUserMessage(userId, userQuestion); const tagsDisplay = profileAnalysis.currentTagNames && profileAnalysis.currentTagNames.length > 0 @@ -266,7 +266,7 @@ class AIAssistant { // 5. Генерируем LLM ответ const { generateLLMResponse } = require('./ragService'); // Получаем актуальную информацию о пользователе для LLM - if (!userNameForProfile && userId && (typeof userId !== 'string' || !userId.toString().startsWith('guest_'))) { + if (!userNameForProfile && userId && !userContextService.isGuestId(userId)) { try { const userContext = await userContextService.getUserContext(userId); if (userContext) { diff --git a/backend/services/profileAnalysisService.js b/backend/services/profileAnalysisService.js index fdf8500..496db24 100644 --- a/backend/services/profileAnalysisService.js +++ b/backend/services/profileAnalysisService.js @@ -426,7 +426,7 @@ async function analyzeUserMessage(userId, message) { logger.info(`[ProfileAnalysis] Сообщение пользователя ${userId}: "${message.substring(0, 100)}${message.length > 100 ? '...' : ''}"`); const DEFAULT_TAG_NAME = 'Без лицензии'; - const isGuest = typeof userId === 'string' && userId.startsWith('guest_'); + const isGuest = userContextService.isGuestId(userId); let currentContext = null; let currentName = null; diff --git a/backend/services/userContextService.js b/backend/services/userContextService.js index c684bce..f95fe14 100644 --- a/backend/services/userContextService.js +++ b/backend/services/userContextService.js @@ -40,7 +40,7 @@ async function getUserTags(userId) { } // Гостевые пользователи не имеют тегов - if (typeof userId === 'string' && userId.startsWith('guest_')) { + if (isGuestId(userId)) { return []; } @@ -164,7 +164,7 @@ async function getUserContext(userId) { } // Гостевые пользователи - if (typeof userId === 'string' && userId.startsWith('guest_')) { + if (isGuestId(userId)) { return { id: userId, name: null, @@ -227,6 +227,26 @@ async function getUserContext(userId) { } } +/** + * Проверяет, является ли идентификатор гостевым (строковым) + * @param {unknown} userId + * @returns {boolean} + */ +function isGuestId(userId) { + if (typeof userId !== 'string') { + return false; + } + + const normalized = userId.trim(); + return ( + normalized.startsWith('guest_') || + normalized.startsWith('web:guest_') || + normalized.startsWith('telegram:guest_') || + normalized.startsWith('email:guest_') || + normalized.includes(':guest_') + ); +} + /** * Инвалидация кэша для пользователя * @param {number} userId - ID пользователя @@ -270,6 +290,7 @@ module.exports = { getUserContext, invalidateUserCache, clearCache, - getCacheStats + getCacheStats, + isGuestId }; diff --git a/frontend/nginx-local.conf b/frontend/nginx-local.conf index d9da413..e94a609 100644 --- a/frontend/nginx-local.conf +++ b/frontend/nginx-local.conf @@ -54,6 +54,9 @@ http { limit_req zone=api_limit_per_ip burst=100 nodelay; proxy_pass http://${BACKEND_CONTAINER}:8000/api/; + proxy_connect_timeout 120s; + proxy_send_timeout 120s; + proxy_read_timeout 600s; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; @@ -71,6 +74,9 @@ http { proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; + proxy_connect_timeout 120s; + proxy_send_timeout 120s; + proxy_read_timeout 600s; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; diff --git a/frontend/nginx-simple.conf b/frontend/nginx-simple.conf index 2409c46..cfa9090 100644 --- a/frontend/nginx-simple.conf +++ b/frontend/nginx-simple.conf @@ -135,6 +135,9 @@ http { limit_req zone=api_limit_per_ip burst=10 nodelay; proxy_pass http://${BACKEND_CONTAINER}:8000/api/; + proxy_connect_timeout 120s; + proxy_send_timeout 120s; + proxy_read_timeout 600s; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; @@ -152,6 +155,9 @@ http { proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; + proxy_connect_timeout 120s; + proxy_send_timeout 120s; + proxy_read_timeout 600s; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;