smart-bot-factory 1.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. smart_bot_factory/__init__.py +3 -0
  2. smart_bot_factory/admin/__init__.py +18 -0
  3. smart_bot_factory/admin/admin_events.py +1223 -0
  4. smart_bot_factory/admin/admin_logic.py +553 -0
  5. smart_bot_factory/admin/admin_manager.py +156 -0
  6. smart_bot_factory/admin/admin_tester.py +157 -0
  7. smart_bot_factory/admin/timeout_checker.py +547 -0
  8. smart_bot_factory/aiogram_calendar/__init__.py +14 -0
  9. smart_bot_factory/aiogram_calendar/common.py +64 -0
  10. smart_bot_factory/aiogram_calendar/dialog_calendar.py +259 -0
  11. smart_bot_factory/aiogram_calendar/schemas.py +99 -0
  12. smart_bot_factory/aiogram_calendar/simple_calendar.py +224 -0
  13. smart_bot_factory/analytics/analytics_manager.py +414 -0
  14. smart_bot_factory/cli.py +806 -0
  15. smart_bot_factory/config.py +258 -0
  16. smart_bot_factory/configs/growthmed-october-24/prompts/1sales_context.txt +16 -0
  17. smart_bot_factory/configs/growthmed-october-24/prompts/2product_info.txt +582 -0
  18. smart_bot_factory/configs/growthmed-october-24/prompts/3objection_handling.txt +66 -0
  19. smart_bot_factory/configs/growthmed-october-24/prompts/final_instructions.txt +212 -0
  20. smart_bot_factory/configs/growthmed-october-24/prompts/help_message.txt +28 -0
  21. smart_bot_factory/configs/growthmed-october-24/prompts/welcome_message.txt +8 -0
  22. smart_bot_factory/configs/growthmed-october-24/reports/test_20250924_064229.txt +818 -0
  23. smart_bot_factory/configs/growthmed-october-24/reports/test_20250924_064335.txt +32 -0
  24. smart_bot_factory/configs/growthmed-october-24/reports/test_20250924_064638.txt +35 -0
  25. smart_bot_factory/configs/growthmed-october-24/tests/quick_scenarios.yaml +133 -0
  26. smart_bot_factory/configs/growthmed-october-24/tests/realistic_scenarios.yaml +108 -0
  27. smart_bot_factory/configs/growthmed-october-24/tests/scenario_examples.yaml +46 -0
  28. smart_bot_factory/configs/growthmed-october-24/welcome_file/welcome_file_msg.txt +16 -0
  29. smart_bot_factory/configs/growthmed-october-24/welcome_file//342/225/250/320/267/342/225/250/342/225/241/342/225/250/342/225/221 /342/225/250/342/225/227/342/225/250/342/225/225/342/225/244/320/221/342/225/244/320/222 /342/225/250/342/224/220/342/225/250/342/225/233 152/342/225/250/320/264/342/225/250/320/247 /342/225/250/342/225/225 323/342/225/250/320/264/342/225/250/320/247 /342/225/250/342/224/244/342/225/250/342/225/227/342/225/244/320/237 /342/225/250/342/225/235/342/225/250/342/225/241/342/225/250/342/224/244/342/225/250/342/225/225/342/225/244/320/226/342/225/250/342/225/225/342/225/250/342/225/234/342/225/244/320/233.pdf +0 -0
  30. smart_bot_factory/core/bot_utils.py +1108 -0
  31. smart_bot_factory/core/conversation_manager.py +653 -0
  32. smart_bot_factory/core/decorators.py +2464 -0
  33. smart_bot_factory/core/message_sender.py +729 -0
  34. smart_bot_factory/core/router.py +347 -0
  35. smart_bot_factory/core/router_manager.py +218 -0
  36. smart_bot_factory/core/states.py +27 -0
  37. smart_bot_factory/creation/__init__.py +7 -0
  38. smart_bot_factory/creation/bot_builder.py +1093 -0
  39. smart_bot_factory/creation/bot_testing.py +1122 -0
  40. smart_bot_factory/dashboard/__init__.py +3 -0
  41. smart_bot_factory/event/__init__.py +7 -0
  42. smart_bot_factory/handlers/handlers.py +2013 -0
  43. smart_bot_factory/integrations/langchain_openai.py +542 -0
  44. smart_bot_factory/integrations/openai_client.py +513 -0
  45. smart_bot_factory/integrations/supabase_client.py +1678 -0
  46. smart_bot_factory/memory/__init__.py +8 -0
  47. smart_bot_factory/memory/memory_manager.py +299 -0
  48. smart_bot_factory/memory/static_memory.py +214 -0
  49. smart_bot_factory/message/__init__.py +56 -0
  50. smart_bot_factory/rag/__init__.py +5 -0
  51. smart_bot_factory/rag/decorators.py +29 -0
  52. smart_bot_factory/rag/router.py +54 -0
  53. smart_bot_factory/rag/templates/__init__.py +3 -0
  54. smart_bot_factory/rag/templates/create_table.sql +7 -0
  55. smart_bot_factory/rag/templates/create_table_and_function_template.py +94 -0
  56. smart_bot_factory/rag/templates/match_function.sql +61 -0
  57. smart_bot_factory/rag/templates/match_services_template.py +82 -0
  58. smart_bot_factory/rag/vectorstore.py +449 -0
  59. smart_bot_factory/router/__init__.py +10 -0
  60. smart_bot_factory/setup_checker.py +512 -0
  61. smart_bot_factory/supabase/__init__.py +7 -0
  62. smart_bot_factory/supabase/client.py +631 -0
  63. smart_bot_factory/utils/__init__.py +11 -0
  64. smart_bot_factory/utils/debug_routing.py +114 -0
  65. smart_bot_factory/utils/prompt_loader.py +529 -0
  66. smart_bot_factory/utils/tool_router.py +68 -0
  67. smart_bot_factory/utils/user_prompt_loader.py +55 -0
  68. smart_bot_factory/utm_link_generator.py +123 -0
  69. smart_bot_factory-1.1.1.dist-info/METADATA +1135 -0
  70. smart_bot_factory-1.1.1.dist-info/RECORD +73 -0
  71. smart_bot_factory-1.1.1.dist-info/WHEEL +4 -0
  72. smart_bot_factory-1.1.1.dist-info/entry_points.txt +2 -0
  73. smart_bot_factory-1.1.1.dist-info/licenses/LICENSE +24 -0
@@ -0,0 +1,631 @@
1
+ """
2
+ Supabase клиент с автоматической загрузкой настроек из .env файла
3
+ """
4
+
5
+ import logging
6
+ import os
7
+ from datetime import datetime, timedelta
8
+ from pathlib import Path
9
+ from typing import Any, Dict, List, Optional
10
+
11
+ from dotenv import load_dotenv
12
+ from postgrest.exceptions import APIError
13
+ from project_root_finder import root
14
+ from supabase import create_client
15
+
16
+ PROJECT_ROOT = root
17
+
18
+ logger = logging.getLogger(__name__)
19
+
20
+
21
+ class SupabaseClient:
22
+ """Клиент для работы с Supabase с автоматической загрузкой настроек из .env"""
23
+
24
+ def __init__(self, bot_id: str):
25
+ """
26
+ Инициализация клиента Supabase с автоматической загрузкой настроек
27
+
28
+ Args:
29
+ bot_id: Идентификатор бота (обязательно) - код сам найдет все настройки
30
+ """
31
+ self.bot_id = bot_id
32
+
33
+ # Автоматически загружаем настройки из .env
34
+ self._load_env_config()
35
+
36
+ # Инициализируем клиент СИНХРОННО прямо в __init__
37
+ self.client = create_client(self.url, self.key)
38
+
39
+ logger.info(f"✅ SupabaseClient инициализирован для bot_id: {self.bot_id}")
40
+
41
+ def _load_env_config(self):
42
+ """Загружает конфигурацию из .env файла"""
43
+ try:
44
+ # Автоматический поиск .env файла
45
+ env_path = self._find_env_file()
46
+
47
+ if not env_path or not env_path.exists():
48
+ raise FileNotFoundError(f".env файл не найден: {env_path}")
49
+
50
+ # Загружаем переменные окружения
51
+ load_dotenv(env_path)
52
+
53
+ # Получаем настройки Supabase
54
+ self.url = os.getenv("SUPABASE_URL")
55
+ self.key = os.getenv("SUPABASE_KEY")
56
+
57
+ if not self.url or not self.key:
58
+ missing_vars = []
59
+ if not self.url:
60
+ missing_vars.append("SUPABASE_URL")
61
+ if not self.key:
62
+ missing_vars.append("SUPABASE_KEY")
63
+ raise ValueError(
64
+ f"Отсутствуют обязательные переменные в .env: {', '.join(missing_vars)}"
65
+ )
66
+
67
+ logger.info(f"✅ Настройки Supabase загружены из {env_path}")
68
+
69
+ except Exception as e:
70
+ logger.error(f"❌ Ошибка загрузки конфигурации Supabase: {e}")
71
+ raise
72
+
73
+ def _find_env_file(self) -> Optional[Path]:
74
+ """Автоматически находит .env файл для указанного бота"""
75
+ # Ищем .env файл в папке конкретного бота
76
+ bot_env_path = PROJECT_ROOT / "bots" / self.bot_id / ".env"
77
+
78
+ if bot_env_path.exists():
79
+ logger.info(f"🔍 Найден .env файл для бота {self.bot_id}: {bot_env_path}")
80
+ return bot_env_path
81
+
82
+ logger.error(f"❌ .env файл не найден для бота {self.bot_id}")
83
+ logger.error(f" Искали в: {bot_env_path}")
84
+ return None
85
+
86
+ # =============================================================================
87
+ # МЕТОДЫ ДЛЯ РАБОТЫ С ПОЛЬЗОВАТЕЛЯМИ
88
+ # =============================================================================
89
+
90
+ async def create_or_get_user(self, user_data: Dict[str, Any]) -> int:
91
+ """Создает или получает пользователя с учетом bot_id (если указан)"""
92
+ try:
93
+ # Если bot_id указан, фильтруем по нему
94
+ query = (
95
+ self.client.table("sales_users")
96
+ .select("telegram_id")
97
+ .eq("telegram_id", user_data["telegram_id"])
98
+ )
99
+ if self.bot_id:
100
+ query = query.eq("bot_id", self.bot_id)
101
+
102
+ response = query.execute()
103
+
104
+ if response.data:
105
+ # Обновляем данные существующего пользователя
106
+ update_query = (
107
+ self.client.table("sales_users")
108
+ .update(
109
+ {
110
+ "username": user_data.get("username"),
111
+ "first_name": user_data.get("first_name"),
112
+ "last_name": user_data.get("last_name"),
113
+ "language_code": user_data.get("language_code"),
114
+ "updated_at": datetime.now().isoformat(),
115
+ "is_active": True,
116
+ }
117
+ )
118
+ .eq("telegram_id", user_data["telegram_id"])
119
+ )
120
+
121
+ if self.bot_id:
122
+ update_query = update_query.eq("bot_id", self.bot_id)
123
+
124
+ update_query.execute()
125
+
126
+ logger.info(
127
+ f"✅ Обновлен пользователь {user_data['telegram_id']}{f' для bot_id {self.bot_id}' if self.bot_id else ''}"
128
+ )
129
+ return user_data["telegram_id"]
130
+ else:
131
+ # Создаем нового пользователя с bot_id (если указан)
132
+ user_insert_data = {
133
+ "telegram_id": user_data["telegram_id"],
134
+ "username": user_data.get("username"),
135
+ "first_name": user_data.get("first_name"),
136
+ "last_name": user_data.get("last_name"),
137
+ "language_code": user_data.get("language_code"),
138
+ "is_active": True,
139
+ "source": user_data.get("source"),
140
+ "medium": user_data.get("medium"),
141
+ "campaign": user_data.get("campaign"),
142
+ "content": user_data.get("content"),
143
+ "term": user_data.get("term"),
144
+ }
145
+ if self.bot_id:
146
+ user_insert_data["bot_id"] = self.bot_id
147
+
148
+ response = (
149
+ self.client.table("sales_users").insert(user_insert_data).execute()
150
+ )
151
+
152
+ logger.info(
153
+ f"✅ Создан новый пользователь {user_data['telegram_id']}{f' для bot_id {self.bot_id}' if self.bot_id else ''}"
154
+ )
155
+ return user_data["telegram_id"]
156
+
157
+ except APIError as e:
158
+ logger.error(f"❌ Ошибка при работе с пользователем: {e}")
159
+ raise
160
+
161
+ # =============================================================================
162
+ # МЕТОДЫ ДЛЯ РАБОТЫ С СЕССИЯМИ
163
+ # =============================================================================
164
+
165
+ async def create_chat_session(
166
+ self, user_data: Dict[str, Any], system_prompt: str
167
+ ) -> str:
168
+ """Создает новую сессию чата с учетом bot_id (если указан)"""
169
+ try:
170
+ # Создаем или обновляем пользователя
171
+ user_id = await self.create_or_get_user(user_data)
172
+
173
+ # Завершаем активные сессии пользователя (с учетом bot_id)
174
+ await self.close_active_sessions(user_id)
175
+
176
+ # Создаем новую сессию с bot_id (если указан)
177
+ session_data = {
178
+ "user_id": user_id,
179
+ "system_prompt": system_prompt,
180
+ "status": "active",
181
+ "current_stage": "introduction",
182
+ "lead_quality_score": 5,
183
+ "metadata": {
184
+ "user_agent": user_data.get("user_agent", ""),
185
+ "start_timestamp": datetime.now().isoformat(),
186
+ },
187
+ }
188
+ if self.bot_id:
189
+ session_data["bot_id"] = self.bot_id
190
+ session_data["metadata"]["bot_id"] = self.bot_id
191
+
192
+ response = (
193
+ self.client.table("sales_chat_sessions").insert(session_data).execute()
194
+ )
195
+
196
+ session_id = response.data[0]["id"]
197
+
198
+ # Создаем запись аналитики
199
+ await self.create_session_analytics(session_id)
200
+
201
+ logger.info(
202
+ f"✅ Создана новая сессия {session_id} для пользователя {user_id}{f', bot_id {self.bot_id}' if self.bot_id else ''}"
203
+ )
204
+ return session_id
205
+
206
+ except APIError as e:
207
+ logger.error(f"❌ Ошибка при создании сессии: {e}")
208
+ raise
209
+
210
+ async def close_active_sessions(self, user_id: int):
211
+ """Закрывает активные сессии пользователя с учетом bot_id (если указан)"""
212
+ try:
213
+ # Закрываем только сессии этого бота (если bot_id указан)
214
+ query = (
215
+ self.client.table("sales_chat_sessions")
216
+ .update(
217
+ {"status": "completed", "updated_at": datetime.now().isoformat()}
218
+ )
219
+ .eq("user_id", user_id)
220
+ .eq("status", "active")
221
+ )
222
+
223
+ if self.bot_id:
224
+ query = query.eq("bot_id", self.bot_id)
225
+
226
+ query.execute()
227
+
228
+ logger.info(
229
+ f"✅ Закрыты активные сессии для пользователя {user_id}{f', bot_id {self.bot_id}' if self.bot_id else ''}"
230
+ )
231
+
232
+ except APIError as e:
233
+ logger.error(f"❌ Ошибка при закрытии сессий: {e}")
234
+ raise
235
+
236
+ async def get_active_session(self, telegram_id: int) -> Optional[Dict[str, Any]]:
237
+ """Получает активную сессию пользователя с учетом bot_id (если указан)"""
238
+ try:
239
+ # Ищем активную сессию с учетом bot_id (если указан)
240
+ query = (
241
+ self.client.table("sales_chat_sessions")
242
+ .select(
243
+ "id",
244
+ "system_prompt",
245
+ "created_at",
246
+ "current_stage",
247
+ "lead_quality_score",
248
+ )
249
+ .eq("user_id", telegram_id)
250
+ .eq("status", "active")
251
+ )
252
+
253
+ if self.bot_id:
254
+ query = query.eq("bot_id", self.bot_id)
255
+
256
+ response = query.execute()
257
+
258
+ if response.data:
259
+ session_info = response.data[0]
260
+ logger.info(
261
+ f"✅ Найдена активная сессия {session_info['id']} для пользователя {telegram_id}{f', bot_id {self.bot_id}' if self.bot_id else ''}"
262
+ )
263
+ return session_info
264
+
265
+ return None
266
+
267
+ except APIError as e:
268
+ logger.error(f"❌ Ошибка при поиске активной сессии: {e}")
269
+ return None
270
+
271
+ # =============================================================================
272
+ # МЕТОДЫ ДЛЯ РАБОТЫ С СООБЩЕНИЯМИ
273
+ # =============================================================================
274
+
275
+ async def add_message(
276
+ self,
277
+ session_id: str,
278
+ role: str,
279
+ content: str,
280
+ message_type: str = "text",
281
+ tokens_used: int = 0,
282
+ processing_time_ms: int = 0,
283
+ metadata: Dict[str, Any] = None,
284
+ ai_metadata: Dict[str, Any] = None,
285
+ ) -> int:
286
+ """Добавляет сообщение в базу данных"""
287
+ try:
288
+ response = (
289
+ self.client.table("sales_messages")
290
+ .insert(
291
+ {
292
+ "session_id": session_id,
293
+ "role": role,
294
+ "content": content,
295
+ "message_type": message_type,
296
+ "tokens_used": tokens_used,
297
+ "processing_time_ms": processing_time_ms,
298
+ "metadata": metadata or {},
299
+ "ai_metadata": ai_metadata or {},
300
+ }
301
+ )
302
+ .execute()
303
+ )
304
+
305
+ message_id = response.data[0]["id"]
306
+
307
+ # Обновляем аналитику сессии
308
+ await self.update_session_analytics(
309
+ session_id, tokens_used, processing_time_ms
310
+ )
311
+
312
+ logger.debug(f"✅ Добавлено сообщение {message_id} в сессию {session_id}")
313
+ return message_id
314
+
315
+ except APIError as e:
316
+ logger.error(f"❌ Ошибка при добавлении сообщения: {e}")
317
+ raise
318
+
319
+ async def get_chat_history(
320
+ self, session_id: str, limit: int = 50
321
+ ) -> List[Dict[str, Any]]:
322
+ """Получает историю сообщений для сессии"""
323
+ try:
324
+ response = (
325
+ self.client.table("sales_messages")
326
+ .select(
327
+ "id",
328
+ "role",
329
+ "content",
330
+ "message_type",
331
+ "created_at",
332
+ "metadata",
333
+ "ai_metadata",
334
+ )
335
+ .eq("session_id", session_id)
336
+ .order("created_at", desc=True)
337
+ .limit(limit)
338
+ .execute()
339
+ )
340
+
341
+ # Фильтруем системные сообщения из истории
342
+ messages = [msg for msg in response.data if msg["role"] != "system"]
343
+
344
+ # Переворачиваем в хронологический порядок (старые -> новые)
345
+ messages.reverse()
346
+
347
+ logger.debug(
348
+ f"✅ Получено {len(messages)} сообщений для сессии {session_id}"
349
+ )
350
+ return messages
351
+
352
+ except APIError as e:
353
+ logger.error(f"❌ Ошибка при получении истории: {e}")
354
+ raise
355
+
356
+ # =============================================================================
357
+ # МЕТОДЫ ДЛЯ АНАЛИТИКИ
358
+ # =============================================================================
359
+
360
+ async def create_session_analytics(self, session_id: str):
361
+ """Создает запись аналитики для сессии"""
362
+ try:
363
+ self.client.table("sales_session_analytics").insert(
364
+ {
365
+ "session_id": session_id,
366
+ "total_messages": 0,
367
+ "total_tokens": 0,
368
+ "average_response_time_ms": 0,
369
+ "conversion_stage": "initial",
370
+ "lead_quality_score": 5,
371
+ }
372
+ ).execute()
373
+
374
+ logger.debug(f"✅ Создана аналитика для сессии {session_id}")
375
+
376
+ except APIError as e:
377
+ logger.error(f"❌ Ошибка при создании аналитики: {e}")
378
+ raise
379
+
380
+ async def update_session_analytics(
381
+ self, session_id: str, tokens_used: int = 0, processing_time_ms: int = 0
382
+ ):
383
+ """Обновляет аналитику сессии"""
384
+ try:
385
+ # Получаем текущую аналитику
386
+ response = (
387
+ self.client.table("sales_session_analytics")
388
+ .select("total_messages", "total_tokens", "average_response_time_ms")
389
+ .eq("session_id", session_id)
390
+ .execute()
391
+ )
392
+
393
+ if response.data:
394
+ current = response.data[0]
395
+ new_total_messages = current["total_messages"] + 1
396
+ new_total_tokens = current["total_tokens"] + tokens_used
397
+
398
+ # Вычисляем среднее время ответа
399
+ if processing_time_ms > 0:
400
+ current_avg = current["average_response_time_ms"]
401
+ new_avg = (
402
+ (current_avg * (new_total_messages - 1)) + processing_time_ms
403
+ ) / new_total_messages
404
+ else:
405
+ new_avg = current["average_response_time_ms"]
406
+
407
+ # Обновляем аналитику
408
+ self.client.table("sales_session_analytics").update(
409
+ {
410
+ "total_messages": new_total_messages,
411
+ "total_tokens": new_total_tokens,
412
+ "average_response_time_ms": int(new_avg),
413
+ "updated_at": datetime.now().isoformat(),
414
+ }
415
+ ).eq("session_id", session_id).execute()
416
+
417
+ except APIError as e:
418
+ logger.error(f"❌ Ошибка при обновлении аналитики: {e}")
419
+ # Не прерываем выполнение, аналитика не критична
420
+
421
+ async def update_session_stage(
422
+ self, session_id: str, stage: str = None, quality_score: int = None
423
+ ):
424
+ """Обновляет этап сессии и качество лида"""
425
+ try:
426
+ update_data = {"updated_at": datetime.now().isoformat()}
427
+
428
+ if stage:
429
+ update_data["current_stage"] = stage
430
+ if quality_score is not None:
431
+ update_data["lead_quality_score"] = quality_score
432
+
433
+ # Дополнительная проверка bot_id при обновлении (если указан)
434
+ if self.bot_id:
435
+ response = (
436
+ self.client.table("sales_chat_sessions")
437
+ .select("bot_id")
438
+ .eq("id", session_id)
439
+ .execute()
440
+ )
441
+ if response.data and response.data[0].get("bot_id") != self.bot_id:
442
+ logger.warning(
443
+ f"⚠️ Попытка обновления сессии {session_id} другого бота"
444
+ )
445
+ return
446
+
447
+ self.client.table("sales_chat_sessions").update(update_data).eq(
448
+ "id", session_id
449
+ ).execute()
450
+
451
+ logger.debug(
452
+ f"✅ Обновлен этап сессии {session_id}: stage={stage}, quality={quality_score}"
453
+ )
454
+
455
+ except APIError as e:
456
+ logger.error(f"❌ Ошибка при обновлении этапа сессии: {e}")
457
+ raise
458
+
459
+ # =============================================================================
460
+ # МЕТОДЫ ДЛЯ РАБОТЫ С ФАЙЛАМИ
461
+ # =============================================================================
462
+
463
+ async def get_sent_files(self, user_id: int) -> List[str]:
464
+ """Получает список отправленных файлов для пользователя"""
465
+ try:
466
+ query = (
467
+ self.client.table("sales_users")
468
+ .select("files")
469
+ .eq("telegram_id", user_id)
470
+ )
471
+
472
+ if self.bot_id:
473
+ query = query.eq("bot_id", self.bot_id)
474
+
475
+ response = query.execute()
476
+
477
+ if response.data and response.data[0].get("files"):
478
+ files_str = response.data[0]["files"]
479
+ return [f.strip() for f in files_str.split(",") if f.strip()]
480
+
481
+ return []
482
+
483
+ except Exception as e:
484
+ logger.error(
485
+ f"❌ Ошибка получения отправленных файлов для пользователя {user_id}: {e}"
486
+ )
487
+ return []
488
+
489
+ async def add_sent_files(self, user_id: int, files_list: List[str]):
490
+ """Добавляет файлы в список отправленных для пользователя"""
491
+ try:
492
+ logger.info(
493
+ f"📁 Добавление файлов для пользователя {user_id}: {files_list}"
494
+ )
495
+
496
+ # Получаем текущий список
497
+ current_files = await self.get_sent_files(user_id)
498
+ logger.info(f"📁 Текущие файлы в БД: {current_files}")
499
+
500
+ # Объединяем с новыми файлами (без дубликатов)
501
+ all_files = list(set(current_files + files_list))
502
+ logger.info(f"📁 Объединенный список файлов: {all_files}")
503
+
504
+ # Сохраняем обратно
505
+ files_str = ", ".join(all_files)
506
+ logger.info(f"📁 Сохраняем строку: {files_str}")
507
+
508
+ query = (
509
+ self.client.table("sales_users")
510
+ .update({"files": files_str})
511
+ .eq("telegram_id", user_id)
512
+ )
513
+
514
+ if self.bot_id:
515
+ query = query.eq("bot_id", self.bot_id)
516
+ logger.info(f"📁 Фильтр по bot_id: {self.bot_id}")
517
+
518
+ response = query.execute()
519
+ logger.info(f"📁 Ответ от БД: {response.data}")
520
+
521
+ logger.info(
522
+ f"✅ Добавлено {len(files_list)} файлов для пользователя {user_id}"
523
+ )
524
+
525
+ except Exception as e:
526
+ logger.error(
527
+ f"❌ Ошибка добавления отправленных файлов для пользователя {user_id}: {e}"
528
+ )
529
+ logger.exception("Полный стек ошибки:")
530
+
531
+ # =============================================================================
532
+ # МЕТОДЫ ДЛЯ АНАЛИТИКИ И СТАТИСТИКИ
533
+ # =============================================================================
534
+
535
+ async def get_analytics_summary(self, days: int = 7) -> Dict[str, Any]:
536
+ """Получает сводку аналитики за последние дни с учетом bot_id (если указан)"""
537
+ try:
538
+ cutoff_date = datetime.now() - timedelta(days=days)
539
+
540
+ # Получаем сессии с учетом bot_id (если указан)
541
+ query = (
542
+ self.client.table("sales_chat_sessions")
543
+ .select("id", "current_stage", "lead_quality_score", "created_at")
544
+ .gte("created_at", cutoff_date.isoformat())
545
+ )
546
+
547
+ if self.bot_id:
548
+ query = query.eq("bot_id", self.bot_id)
549
+
550
+ sessions_response = query.execute()
551
+
552
+ sessions = sessions_response.data
553
+ total_sessions = len(sessions)
554
+
555
+ # Группировка по этапам
556
+ stages = {}
557
+ quality_scores = []
558
+
559
+ for session in sessions:
560
+ stage = session.get("current_stage", "unknown")
561
+ stages[stage] = stages.get(stage, 0) + 1
562
+
563
+ score = session.get("lead_quality_score", 5)
564
+ if score:
565
+ quality_scores.append(score)
566
+
567
+ avg_quality = (
568
+ sum(quality_scores) / len(quality_scores) if quality_scores else 5
569
+ )
570
+
571
+ return {
572
+ "bot_id": self.bot_id,
573
+ "period_days": days,
574
+ "total_sessions": total_sessions,
575
+ "stages": stages,
576
+ "average_lead_quality": round(avg_quality, 1),
577
+ "generated_at": datetime.now().isoformat(),
578
+ }
579
+
580
+ except APIError as e:
581
+ logger.error(f"❌ Ошибка при получении аналитики: {e}")
582
+ return {
583
+ "bot_id": self.bot_id,
584
+ "error": str(e),
585
+ "generated_at": datetime.now().isoformat(),
586
+ }
587
+
588
+ # =============================================================================
589
+ # МЕТОДЫ СОВМЕСТИМОСТИ
590
+ # =============================================================================
591
+
592
+ async def update_conversion_stage(
593
+ self, session_id: str, stage: str, quality_score: int = None
594
+ ):
595
+ """Обновляет этап конверсии и качество лида (для совместимости)"""
596
+ await self.update_session_stage(session_id, stage, quality_score)
597
+
598
+ async def get_session_info(self, session_id: str) -> Optional[Dict[str, Any]]:
599
+ """Получает информацию о сессии с проверкой bot_id (если указан)"""
600
+ try:
601
+ response = (
602
+ self.client.table("sales_chat_sessions")
603
+ .select(
604
+ "id",
605
+ "user_id",
606
+ "bot_id",
607
+ "system_prompt",
608
+ "status",
609
+ "created_at",
610
+ "metadata",
611
+ "current_stage",
612
+ "lead_quality_score",
613
+ )
614
+ .eq("id", session_id)
615
+ .execute()
616
+ )
617
+
618
+ if response.data:
619
+ session = response.data[0]
620
+ # Дополнительная проверка bot_id для безопасности (если указан)
621
+ if self.bot_id and session.get("bot_id") != self.bot_id:
622
+ logger.warning(
623
+ f"⚠️ Попытка доступа к сессии {session_id} другого бота: {session.get('bot_id')} != {self.bot_id}"
624
+ )
625
+ return None
626
+ return session
627
+ return None
628
+
629
+ except APIError as e:
630
+ logger.error(f"❌ Ошибка при получении информации о сессии: {e}")
631
+ raise
@@ -0,0 +1,11 @@
1
+ """
2
+ Utils модули smart_bot_factory
3
+ """
4
+
5
+ from .user_prompt_loader import UserPromptLoader
6
+ from .tool_router import ToolRouter
7
+
8
+ __all__ = [ # Базовый класс (для библиотеки)
9
+ "UserPromptLoader", # Для пользователей (автопоиск prompts_dir)
10
+ "ToolRouter",
11
+ ]