Spaces:
Sleeping
Sleeping
axeronis commited on
Commit ·
a3e1c21
1
Parent(s): 597f927
refactor: remove ai_settings duplication - use direct tenant fields for ai_identity, banned_words, custom_instructions, tone
Browse files60 unmodified lines6162636465666768598 unmodified lines6676686696706716726736746756766774 unmodified lines6826836846856866876886896904 unmodified lines6956966976986997007017027032 unmodified lines7067077087097107117127137147157163 unmodified lines7207217227237247257267277282 unmodified lines7317327337347357367377387397407417427437447457467477487497507514 unmodified lines75675775875976076176276360 unmodified linesraise HTTPException(status_code=404, detail="Tenant not found")# DEBUG: Log current settingsai_settings = tenant.ai_settings or {}ai_identity = ai_settings.get("ai_identity", {})logger.info(f"🔍 Demo chat - bot_name from DB: {ai_identity.get('bot_name', 'NOT SET')}")# Build FULL context using memory manager (includes KB, products, settings)598 unmodified linesvalue = change.get("value", "")reason = change.get("reason", "")if action == "set_bot_name" and value:old_value = ai_identity.get('bot_name', 'AI Assistant')if "ai_identity" not in ai_settings:ai_settings["ai_identity"] = {}ai_settings["ai_identity"]["bot_name"] = valueapplied_changes.append({"field": "Имя бота","section": "AI Мозг → Личность",4 unmodified lineselif action == "set_bot_role" and value:old_value = ai_identity.get('bot_role', 'Менеджер')if "ai_identity" not in ai_settings:ai_settings["ai_identity"] = {}ai_settings["ai_identity"]["bot_role"] = valueapplied_changes.append({"field": "Роль бота","section": "AI Мозг → Личность",4 unmodified lineselif action == "set_bot_gender" and value in ["male", "female"]:old_value = ai_identity.get('bot_gender', 'female')if "ai_identity" not in ai_settings:ai_settings["ai_identity"] = {}ai_settings["ai_identity"]["bot_gender"] = valueapplied_changes.append({"field": "Пол бота","section": "AI Мозг → Личность",2 unmodified lines"reason": reason})elif action == "add_banned_word" and value:banned = ai_settings.get("banned_words", []) or []if value.lower() not in [w.lower() for w in banned]:banned.append(value)ai_settings["banned_words"] = bannedapplied_changes.append({"field": "Запрещённые слова","section": "AI Мозг → Фильтры",3 unmodified lines})elif action == "remove_banned_word" and value:banned = ai_settings.get("banned_words", []) or []if value.lower() in [w.lower() for w in banned]:ai_settings["banned_words"] = [w for w in banned if w.lower() != value.lower()]applied_changes.append({"field": "Запрещённые слова","section": "AI Мозг → Фильтры",2 unmodified lines"reason": reason})elif action == "add_instruction" and value:current = ai_settings.get("custom_instructions", "") or ""if value not in current:new_instructions = current + "\n" + value if current else valueai_settings["custom_instructions"] = new_instructionsapplied_changes.append({"field": "Инструкции","section": "AI Мозг → Глобальный промпт","old": "(добавлено новое)","new": value[:100] + ("..." if len(value) > 100 else ""),"full_value": value, # Full value for confirm endpoint"reason": reason})elif action == "set_tone" and value:valid_tones = ["Professional", "Friendly", "Empathetic", "Bold"]tone_map = {4 unmodified lines}mapped = tone_map.get(value.lower(), value)if mapped in valid_tones:old_value = ai_settings.get("tone_of_voice", "Professional")ai_settings["tone_of_voice"] = mappedapplied_changes.append({"field": "Тон общения","section": "AI Мозг → Стиль",60 unmodified lines61626364656667598 unmodified lines6666676686696706716726736744 unmodified lines6796806816826836844 unmodified lines6896906916926936942 unmodified lines6976986997007017027037047057063 unmodified lines7107117127137147157167172 unmodified lines7207217227237247257267277287297307317327337347357367377387397404 unmodified lines74574674774874975075160 unmodified linesraise HTTPException(status_code=404, detail="Tenant not found")# DEBUG: Log current settingsai_identity = tenant.ai_identity or {}logger.info(f"🔍 Demo chat - bot_name from DB: {ai_identity.get('bot_name', 'NOT SET')}")# Build FULL context using memory manager (includes KB, products, settings)598 unmodified linesvalue = change.get("value", "")reason = change.get("reason", "")# Bot identity - read from tenant.ai_identity (correct field)if action == "set_bot_name" and value:old_value = ai_identity.get('bot_name', 'AI Assistant')applied_changes.append({"field": "Имя бота","section": "AI Мозг → Личность",4 unmodified lineselif action == "set_bot_role" and value:old_value = ai_identity.get('bot_role', 'Менеджер')applied_changes.append({"field": "Роль бота","section": "AI Мозг → Личность",4 unmodified lineselif action == "set_bot_gender" and value in ["male", "female"]:old_value = ai_identity.get('bot_gender', 'female')applied_changes.append({"field": "Пол бота","section": "AI Мозг → Личность",2 unmodified lines"reason": reason})# Banned words - read from tenant.banned_words (correct field)elif action == "add_banned_word" and value:banned = list(tenant.banned_words or [])if value.lower() not in [w.lower() for w in banned]:applied_changes.append({"field": "Запрещённые слова","section": "AI Мозг → Фильтры",3 unmodified lines})elif action == "remove_banned_word" and value:banned = list(tenant.banned_words or [])if value.lower() in [w.lower() for w in banned]:applied_changes.append({"field": "Запрещённые слова","section": "AI Мозг → Фильтры",2 unmodified lines"reason": reason})# Custom instructions - read from tenant.custom_instructions (correct field)elif action == "add_instruction" and value:current = tenant.custom_instructions or ""if value not in current:applied_changes.append({"field": "Инструкции","section": "AI Мозг → Глобальный промпт","old": "(добавлено новое)","new": value[:100] + ("..." if len(value) > 100 else ""),"full_value": value,"reason": reason})# Tone - read from tenant.tone_of_voice (correct field)elif action == "set_tone" and value:valid_tones = ["Professional", "Friendly", "Empathetic", "Bold"]tone_map = {4 unmodified lines}mapped = tone_map.get(value.lower(), value)if mapped in valid_tones:old_value = tenant.tone_of_voice or "Professional"applied_changes.append({"field": "Тон общения","section": "AI Мозг → Стиль",
backend/app/api/demo.py+10-22