v1.8.2: AGENTS.md, consent revocation, delete bot users, dialogues fix, intent improvements, CI for max_bot
Tests / test (push) Has been cancelled
Tests / test-max-bot (push) Has been cancelled

This commit is contained in:
2026-06-02 18:22:45 +03:00
parent a64a274829
commit 4c3026a80f
17 changed files with 572 additions and 54 deletions
+94 -27
View File
@@ -33,8 +33,6 @@ async def handle_greeting(
f"Чем я могу быть вам полезна?"
)
await max_api.send_message(user_id, greeting_text)
async with async_session() as db:
existing = await db.execute(
select(BotUser).where(BotUser.id == user_id)
@@ -92,6 +90,8 @@ async def handle_greeting(
await db.commit()
await db.refresh(conv)
await max_api.send_message(user_id, greeting_text, conversation_id=conv.id)
async def handle_message(user_id: int, text: str) -> None:
async with async_session() as db:
@@ -107,11 +107,24 @@ async def handle_message(user_id: int, text: str) -> None:
await handle_greeting(user_id, text)
return
now = moscow_now()
stale_threshold = now - datetime.timedelta(minutes=10)
if conv.created_at and conv.created_at < stale_threshold:
conv = BotConversation(
user_id=user_id,
state="awaiting_input",
created_at=now,
)
db.add(conv)
await db.commit()
await db.refresh(conv)
msg = BotMessage(
conversation_id=conv.id,
direction="incoming",
text=text,
created_at=moscow_now(),
created_at=now,
)
db.add(msg)
await db.commit()
@@ -135,9 +148,16 @@ async def handle_message(user_id: int, text: str) -> None:
f"{phone_text}\n"
f"📧 {email}\n\n"
"Мы обязательно ответим и поможем!",
conversation_id=conv.id,
)
return
revoke_words = ["отозвать согласие", "отозвать", "отменить согласие"]
if any(w in text_lower for w in revoke_words):
from app.handlers.consent import handle_revoke_consent
await handle_revoke_consent(user_id, conv.id)
return
if state in ("greeting", "analyzing", "awaiting_input"):
await analyze_and_respond(user_id, conv.id, text)
elif state in ("awaiting_consent", "consent_refused", "awaiting_contact"):
@@ -175,27 +195,19 @@ async def handle_message(user_id: int, text: str) -> None:
from app.handlers.inquiry import handle_inquiry
await handle_inquiry(user_id, conv.id, text)
elif state in ("completed", "escalated"):
async with async_session() as db:
conv_new = BotConversation(
user_id=user_id,
state="analyzing",
created_at=moscow_now(),
)
db.add(conv_new)
await db.commit()
await db.refresh(conv_new)
await analyze_and_respond(user_id, conv_new.id, text)
await analyze_and_respond(user_id, conv.id, text)
async def _get_user_history_context(user_id: int) -> str:
from app.models import BotTicket
async with async_session() as db:
ten_min_ago = moscow_now() - datetime.timedelta(minutes=10)
recent_msgs = await db.execute(
select(BotMessage)
.join(BotConversation)
.where(BotConversation.user_id == user_id)
.where(BotMessage.created_at >= ten_min_ago)
.order_by(BotMessage.id.desc())
.limit(10)
)
messages = list(reversed(recent_msgs.scalars().all()))
@@ -233,11 +245,53 @@ async def _select_ai():
async def analyze_and_respond(user_id: int, conv_id: int, text: str) -> None:
try:
logger.info(f"analyze_and_respond: user={user_id} conv={conv_id} text={text[:80]}")
ai = await _select_ai()
logger.info(f"analyze_and_respond: ai={ai}")
text_lower = text.strip().lower()
ticket_quick = [
"записаться", "заявк", "заказать", "оставить заявку", "нужна помощь",
"хочу заказать", "хочу записаться", "хочу оставить", "оформить заявку",
"связаться", "обратный звонок", "перезвоните", "перезвонить",
]
if any(p in text_lower for p in ticket_quick):
async with async_session() as db:
conv = await db.get(BotConversation, conv_id)
if not conv:
return
conv.intent = "ticket"
conv.state = "awaiting_consent"
await db.commit()
consent_text = (
"Для обработки Вашего запроса нам нужны ваши контактные данные "
"для обратной связи. Даёте согласие на обработку "
"персональных данных?"
)
await max_api.send_message(
user_id, consent_text, attachments=consent_keyboard(), conversation_id=conv_id
)
return
greeting_words = [
"привет", "здравствуйте", "добрый день", "доброе утро", "добрый вечер",
"хай", "хей", "йо", "здарова", "салют",
]
words_only = set(text_lower.split())
if words_only & set(greeting_words):
async with async_session() as db:
conv = await db.get(BotConversation, conv_id)
if conv:
conv.intent = "greeting"
await db.commit()
assistant_name = await settings_cache.get("assistant_name", "София")
await max_api.send_message(
user_id,
f"Здравствуйте! Я {assistant_name}, чем могу помочь?",
conversation_id=conv_id,
)
return
ai = await _select_ai()
if not ai:
logger.info("analyze_and_respond: no AI available, limited mode")
await _limited_mode(user_id, conv_id)
return
@@ -252,19 +306,22 @@ async def analyze_and_respond(user_id: int, conv_id: int, text: str) -> None:
conv = result.scalar_one_or_none()
if not conv:
return
prev_intent = conv.intent
conv.intent = intent
if intent == "question":
kb_context = await _get_knowledge_base_context(text)
answer = await ai.generate_answer(text, kb_context, history_context)
if answer:
await max_api.send_message(user_id, answer)
await max_api.send_message(user_id, answer, conversation_id=conv_id)
else:
await _save_unknown_question(user_id, text)
await max_api.send_message(user_id,
"По данному вопросу рекомендую связаться с нашим специалистом "
"для получения исчерпывающего ответа. "
"Контакты: +7 (861) 203-33-30, mail@aegisone.ru"
"Контакты: +7 (861) 203-33-30, mail@aegisone.ru",
conversation_id=conv_id,
)
conv.state = "completed"
@@ -276,19 +333,29 @@ async def analyze_and_respond(user_id: int, conv_id: int, text: str) -> None:
"персональных данных?"
)
await max_api.send_message(
user_id, consent_text, attachments=consent_keyboard()
user_id, consent_text, attachments=consent_keyboard(), conversation_id=conv_id
)
else:
clarification = await ai.clarify_intent(text, history_context)
await max_api.send_message(user_id, clarification)
conv.state = "awaiting_input"
if prev_intent == "unknown":
kb_context = await _get_knowledge_base_context(text)
answer = await ai.generate_answer(text, kb_context, history_context)
if answer:
await max_api.send_message(user_id, answer, conversation_id=conv_id)
conv.state = "completed"
else:
await _send_contacts(user_id, conv_id)
conv.state = "completed"
else:
clarification = await ai.clarify_intent(text, history_context)
await max_api.send_message(user_id, clarification, conversation_id=conv_id)
conv.state = "awaiting_input"
await db.commit()
except Exception as e:
logger.exception(f"analyze_and_respond FAILED: {e}")
await _send_contacts(user_id)
await _send_contacts(user_id, conv_id)
async def _limited_mode(user_id: int, conv_id: int) -> None:
@@ -301,10 +368,10 @@ async def _limited_mode(user_id: int, conv_id: int) -> None:
conv.state = "completed"
await db.commit()
await _send_contacts(user_id)
await _send_contacts(user_id, conv_id)
async def _send_contacts(user_id: int) -> None:
async def _send_contacts(user_id: int, conv_id: int = None) -> None:
phones = config_reader.get_phones()
email = config_reader.get_site_mail()
@@ -316,7 +383,7 @@ async def _send_contacts(user_id: int) -> None:
f"📧 {email}"
)
await max_api.send_message(user_id, message)
await max_api.send_message(user_id, message, conversation_id=conv_id)
async def _get_knowledge_base_context(query: str) -> str: