26 lines
909 B
Python
26 lines
909 B
Python
"""Инжект системных оповещений в чат без role=assistant (не ломает LLM-историю)."""
|
|
|
|
from sqlalchemy import select
|
|
|
|
from app.db.base import SessionLocal
|
|
from app.db.models import ChatSession, Message
|
|
|
|
|
|
def post_notice_to_latest_chat(content: str) -> int | None:
|
|
"""Сохраняет notice в последний активный чат. Возвращает session_id."""
|
|
db = SessionLocal()
|
|
try:
|
|
session = db.scalar(
|
|
select(ChatSession).order_by(ChatSession.updated_at.desc()).limit(1)
|
|
)
|
|
if not session:
|
|
session = ChatSession(title="Уведомления")
|
|
db.add(session)
|
|
db.commit()
|
|
db.refresh(session)
|
|
db.add(Message(session_id=session.id, role="notice", content=content))
|
|
db.commit()
|
|
return session.id
|
|
finally:
|
|
db.close()
|