Spaces:
Sleeping

axeronis commited on
Commit
d990df1
·
1 Parent(s): 17d53fb

fix: ALL AI modules now read ai_identity from ai_settings where settings/brain saves it

Browse files
backend/app/api/demo.py CHANGED
@@ -61,7 +61,8 @@ async def demo_chat(
61
  raise HTTPException(status_code=404, detail="Tenant not found")
62
 
63
  # DEBUG: Log current settings
64
- ai_identity = tenant.ai_identity or {}
 
65
  logger.info(f"🔍 Demo chat - bot_name from DB: {ai_identity.get('bot_name', 'NOT SET')}")
66
 
67
  # Build FULL context using memory manager (includes KB, products, settings)
@@ -280,8 +281,9 @@ async def confirm_smart_correction(
280
  # CRITICAL: Use deepcopy so SQLAlchemy detects changes to JSONB fields
281
  import copy
282
 
283
- # tenant.ai_identity is the correct field for bot identity (not ai_settings['ai_identity'])
284
- ai_identity = copy.deepcopy(tenant.ai_identity or {})
 
285
 
286
  applied_count = 0
287
 
@@ -290,7 +292,7 @@ async def confirm_smart_correction(
290
  new_value = change.get("new", "")
291
 
292
  # Map field names to settings paths
293
- # Bot identity goes to tenant.ai_identity (NOT ai_settings)
294
  if "Имя бота" in field:
295
  ai_identity["bot_name"] = new_value
296
  applied_count += 1
@@ -446,10 +448,11 @@ async def confirm_smart_correction(
446
 
447
  if applied_count > 0:
448
  # DEBUG: Log what we're saving
449
- logger.info(f"🔧 Saving ai_identity: {ai_identity}")
450
 
451
- # Save ai_identity to the correct tenant field
452
- tenant.ai_identity = ai_identity
 
453
 
454
  session.add(tenant)
455
  await session.commit()
@@ -686,7 +689,7 @@ async def smart_correct(
686
  value = change.get("value", "")
687
  reason = change.get("reason", "")
688
 
689
- # Bot identity - read from tenant.ai_identity (correct field)
690
  if action == "set_bot_name" and value:
691
  old_value = ai_identity.get('bot_name', 'AI Assistant')
692
  applied_changes.append({
 
61
  raise HTTPException(status_code=404, detail="Tenant not found")
62
 
63
  # DEBUG: Log current settings
64
+ # FIXED: ai_identity is stored in ai_settings by settings/brain page
65
+ ai_identity = (tenant.ai_settings or {}).get("ai_identity", {})
66
  logger.info(f"🔍 Demo chat - bot_name from DB: {ai_identity.get('bot_name', 'NOT SET')}")
67
 
68
  # Build FULL context using memory manager (includes KB, products, settings)
 
281
  # CRITICAL: Use deepcopy so SQLAlchemy detects changes to JSONB fields
282
  import copy
283
 
284
+ # FIXED: ai_identity is stored INSIDE ai_settings by settings/brain page
285
+ ai_settings = copy.deepcopy(tenant.ai_settings or {})
286
+ ai_identity = ai_settings.get("ai_identity", {})
287
 
288
  applied_count = 0
289
 
 
292
  new_value = change.get("new", "")
293
 
294
  # Map field names to settings paths
295
+ # Bot identity goes to ai_settings.ai_identity
296
  if "Имя бота" in field:
297
  ai_identity["bot_name"] = new_value
298
  applied_count += 1
 
448
 
449
  if applied_count > 0:
450
  # DEBUG: Log what we're saving
451
+ logger.info(f"🔧 Saving ai_identity to ai_settings: {ai_identity}")
452
 
453
+ # FIXED: Save ai_identity INSIDE ai_settings where settings/brain reads from
454
+ ai_settings["ai_identity"] = ai_identity
455
+ tenant.ai_settings = ai_settings
456
 
457
  session.add(tenant)
458
  await session.commit()
 
689
  value = change.get("value", "")
690
  reason = change.get("reason", "")
691
 
692
+ # Bot identity - now correctly read from ai_settings.ai_identity
693
  if action == "set_bot_name" and value:
694
  old_value = ai_identity.get('bot_name', 'AI Assistant')
695
  applied_changes.append({
backend/app/services/ai_core/lead_analyzer.py CHANGED
@@ -244,7 +244,8 @@ async def check_and_notify_qualification_complete(
244
 
245
  ai_settings = tenant.ai_settings or {}
246
  business_description = ai_settings.get("business_description", "")[:100]
247
- ai_identity = tenant.ai_identity or ai_settings.get("ai_identity", {})
 
248
  bot_role = ai_identity.get("bot_role", "менеджер")
249
 
250
  # Build qualification summary
 
244
 
245
  ai_settings = tenant.ai_settings or {}
246
  business_description = ai_settings.get("business_description", "")[:100]
247
+ # FIXED: ai_identity stored inside ai_settings by settings/brain
248
+ ai_identity = ai_settings.get("ai_identity", {})
249
  bot_role = ai_identity.get("bot_role", "менеджер")
250
 
251
  # Build qualification summary
backend/app/services/ai_core/prompt_builder.py CHANGED
@@ -168,9 +168,9 @@ def build_system_prompt(
168
  - Booking/order systems
169
  """
170
 
171
- # Extract bot identity - try tenant.ai_identity first (new way), fallback to ai_settings (legacy)
172
  ai_settings = tenant.ai_settings or {}
173
- ai_identity = getattr(tenant, 'ai_identity', None) or ai_settings.get("ai_identity", {}) or {}
174
  bot_name = ai_identity.get("bot_name", "")
175
  bot_role = ai_identity.get("bot_role", "")
176
  company_name = tenant.business_name or tenant.name or ""
 
168
  - Booking/order systems
169
  """
170
 
171
+ # Extract bot identity - FIXED: ai_identity stored inside ai_settings by settings/brain
172
  ai_settings = tenant.ai_settings or {}
173
+ ai_identity = ai_settings.get("ai_identity", {})
174
  bot_name = ai_identity.get("bot_name", "")
175
  bot_role = ai_identity.get("bot_role", "")
176
  company_name = tenant.business_name or tenant.name or ""
backend/app/services/ai_core/settings_assistant.py CHANGED
@@ -39,10 +39,11 @@ async def get_settings_context(session: AsyncSession, tenant: Tenant) -> str:
39
  Build a context string showing ALL current tenant settings.
40
  This is injected into AI prompt so it knows what can be changed.
41
  """
42
- # AI Identity
43
- ai_identity = tenant.ai_identity or {}
44
- bot_name = ai_identity.get("name", "Ассистент")
45
- bot_role = ai_identity.get("role", "Продажник")
 
46
 
47
  # Core settings
48
  tone = tenant.tone_of_voice or "не задан"
@@ -158,12 +159,15 @@ async def execute_setting_change(
158
 
159
  try:
160
  # Handle nested paths (e.g., ai_identity.name)
 
161
  if path.startswith("ai_identity."):
162
  field = path.split(".")[1]
163
- ai_identity = tenant.ai_identity or {}
 
164
  old_value = ai_identity.get(field, "")
165
  ai_identity[field] = new_value
166
- tenant.ai_identity = ai_identity
 
167
 
168
  elif path.startswith("ai_settings."):
169
  field = path.split(".")[1]
 
39
  Build a context string showing ALL current tenant settings.
40
  This is injected into AI prompt so it knows what can be changed.
41
  """
42
+ # AI Identity - stored INSIDE ai_settings by settings/brain page
43
+ ai_settings_json = tenant.ai_settings or {}
44
+ ai_identity = ai_settings_json.get("ai_identity", {})
45
+ bot_name = ai_identity.get("bot_name", "Ассистент") # Field is bot_name, not name
46
+ bot_role = ai_identity.get("bot_role", "Продажник") # Field is bot_role, not role
47
 
48
  # Core settings
49
  tone = tenant.tone_of_voice or "не задан"
 
159
 
160
  try:
161
  # Handle nested paths (e.g., ai_identity.name)
162
+ # FIXED: ai_identity is stored INSIDE ai_settings, not as tenant.ai_identity
163
  if path.startswith("ai_identity."):
164
  field = path.split(".")[1]
165
+ ai_settings = tenant.ai_settings or {}
166
+ ai_identity = ai_settings.get("ai_identity", {})
167
  old_value = ai_identity.get(field, "")
168
  ai_identity[field] = new_value
169
+ ai_settings["ai_identity"] = ai_identity
170
+ tenant.ai_settings = ai_settings
171
 
172
  elif path.startswith("ai_settings."):
173
  field = path.split(".")[1]
backend/app/services/ai_memory_manager.py CHANGED
@@ -156,8 +156,9 @@ class AIMemoryManager:
156
  # PRIORITY: Direct tenant fields (from /settings/brain) FIRST, fallback to ai_settings JSON (legacy)
157
  ai_settings = tenant.ai_settings or {}
158
 
159
- # UNIFIED AI IDENTITY: Check tenant.ai_identity first (new), fallback to ai_settings (legacy)
160
- ai_identity = getattr(tenant, 'ai_identity', None) or ai_settings.get("ai_identity", {}) or {}
 
161
  bot_name = ai_identity.get("bot_name", "AI Assistant")
162
  bot_role = ai_identity.get("bot_role", "Эксперт по продажам") # User-defined role
163
  bot_gender = ai_identity.get("bot_gender", "male") # male or female
 
156
  # PRIORITY: Direct tenant fields (from /settings/brain) FIRST, fallback to ai_settings JSON (legacy)
157
  ai_settings = tenant.ai_settings or {}
158
 
159
+ # FIXED: ai_identity is stored INSIDE ai_settings by settings/brain page
160
+ # (tenant.ai_identity field doesn't exist in the model)
161
+ ai_identity = ai_settings.get("ai_identity", {})
162
  bot_name = ai_identity.get("bot_name", "AI Assistant")
163
  bot_role = ai_identity.get("bot_role", "Эксперт по продажам") # User-defined role
164
  bot_gender = ai_identity.get("bot_gender", "male") # male or female