Spaces:
Sleeping

axeronis commited on
Commit
25a2e3d
·
1 Parent(s): 4bd34ff

fix: sync demo-chat corrections to correct tenant fields (ai_identity, custom_instructions)

Browse files
Files changed (1) hide show
  1. backend/app/api/demo.py +19 -16
backend/app/api/demo.py CHANGED
@@ -278,12 +278,11 @@ async def confirm_smart_correction(
278
  if not tenant:
279
  raise HTTPException(status_code=404, detail="Tenant not found")
280
 
281
- # CRITICAL: Use deepcopy so SQLAlchemy detects changes to JSONB field
282
  import copy
283
- ai_settings = copy.deepcopy(tenant.ai_settings or {})
284
- if "ai_identity" not in ai_settings:
285
- ai_settings["ai_identity"] = {}
286
-
287
 
288
  applied_count = 0
289
 
@@ -292,26 +291,28 @@ async def confirm_smart_correction(
292
  new_value = change.get("new", "")
293
 
294
  # Map field names to settings paths
 
295
  if "Имя бота" in field:
296
- ai_settings["ai_identity"]["bot_name"] = new_value
297
  applied_count += 1
298
  elif "Роль бота" in field:
299
- ai_settings["ai_identity"]["bot_role"] = new_value
300
  applied_count += 1
301
  elif "Пол бота" in field:
302
- ai_settings["ai_identity"]["bot_gender"] = "female" if "Женский" in new_value else "male"
303
  applied_count += 1
 
304
  elif "Тон общения" in field:
305
- ai_settings["tone_of_voice"] = new_value
306
  applied_count += 1
307
  elif "Методология продаж" in field:
308
- ai_settings["sales_methodology"] = new_value
309
  applied_count += 1
310
  elif "Запрещённые" in field:
311
  if new_value.startswith("+"):
312
- banned = ai_settings.get("banned_words", []) or []
313
  banned.append(new_value[1:])
314
- ai_settings["banned_words"] = banned
315
  applied_count += 1
316
  elif "Инструкции" in field or "инструкц" in field.lower():
317
  if "..." in new_value:
@@ -319,7 +320,7 @@ async def confirm_smart_correction(
319
  else:
320
  full_value = new_value
321
 
322
- current = ai_settings.get("custom_instructions", "") or ""
323
 
324
  # Smart instruction handling: detect and replace contradictions
325
  # Common contradicting patterns
@@ -352,7 +353,7 @@ async def confirm_smart_correction(
352
  if len(lines) > 10:
353
  lines = lines[-10:]
354
 
355
- ai_settings["custom_instructions"] = "\n".join(filter(None, lines))
356
  applied_count += 1
357
 
358
  elif "Идеальный клиент" in field or "клиент" in field.lower():
@@ -446,9 +447,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_settings: {ai_settings.get('ai_identity', {})}")
 
 
 
450
 
451
- tenant.ai_settings = ai_settings
452
  session.add(tenant)
453
  await session.commit()
454
 
 
278
  if not tenant:
279
  raise HTTPException(status_code=404, detail="Tenant not found")
280
 
281
+ # CRITICAL: Use deepcopy so SQLAlchemy detects changes to JSONB fields
282
  import copy
283
+
284
+ # tenant.ai_identity is the correct field for bot identity (not ai_settings['ai_identity'])
285
+ ai_identity = copy.deepcopy(tenant.ai_identity or {})
 
286
 
287
  applied_count = 0
288
 
 
291
  new_value = change.get("new", "")
292
 
293
  # Map field names to settings paths
294
+ # Bot identity goes to tenant.ai_identity (NOT ai_settings)
295
  if "Имя бота" in field:
296
+ ai_identity["bot_name"] = new_value
297
  applied_count += 1
298
  elif "Роль бота" in field:
299
+ ai_identity["bot_role"] = new_value
300
  applied_count += 1
301
  elif "Пол бота" in field:
302
+ ai_identity["bot_gender"] = "female" if "Женский" in new_value else "male"
303
  applied_count += 1
304
+ # These go to direct tenant fields
305
  elif "Тон общения" in field:
306
+ tenant.tone_of_voice = new_value
307
  applied_count += 1
308
  elif "Методология продаж" in field:
309
+ tenant.sales_methodology = new_value
310
  applied_count += 1
311
  elif "Запрещённые" in field:
312
  if new_value.startswith("+"):
313
+ banned = list(tenant.banned_words or [])
314
  banned.append(new_value[1:])
315
+ tenant.banned_words = banned
316
  applied_count += 1
317
  elif "Инструкции" in field or "инструкц" in field.lower():
318
  if "..." in new_value:
 
320
  else:
321
  full_value = new_value
322
 
323
+ current = tenant.custom_instructions or ""
324
 
325
  # Smart instruction handling: detect and replace contradictions
326
  # Common contradicting patterns
 
353
  if len(lines) > 10:
354
  lines = lines[-10:]
355
 
356
+ tenant.custom_instructions = "\n".join(filter(None, lines))
357
  applied_count += 1
358
 
359
  elif "Идеальный клиент" in field or "клиент" in field.lower():
 
447
 
448
  if applied_count > 0:
449
  # DEBUG: Log what we're saving
450
+ logger.info(f"🔧 Saving ai_identity: {ai_identity}")
451
+
452
+ # Save ai_identity to the correct tenant field
453
+ tenant.ai_identity = ai_identity
454
 
 
455
  session.add(tenant)
456
  await session.commit()
457