feat: briefing conversation management API
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
"""Create and manage briefing conversations."""
|
||||
|
||||
import logging
|
||||
from datetime import date, datetime, timezone
|
||||
|
||||
from sqlalchemy import select
|
||||
|
||||
from fabledassistant.models import async_session
|
||||
from fabledassistant.models.conversation import Conversation, Message
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def get_or_create_today_conversation(user_id: int, model: str) -> Conversation:
|
||||
"""
|
||||
Return today's briefing conversation, creating it if it doesn't exist.
|
||||
"""
|
||||
today = date.today()
|
||||
async with async_session() as session:
|
||||
result = await session.execute(
|
||||
select(Conversation).where(
|
||||
Conversation.user_id == user_id,
|
||||
Conversation.conversation_type == "briefing",
|
||||
Conversation.briefing_date == today,
|
||||
)
|
||||
)
|
||||
conv = result.scalars().first()
|
||||
if conv is not None:
|
||||
return conv
|
||||
|
||||
conv = Conversation(
|
||||
user_id=user_id,
|
||||
title=f"Briefing — {today.isoformat()}",
|
||||
model=model,
|
||||
conversation_type="briefing",
|
||||
briefing_date=today,
|
||||
)
|
||||
session.add(conv)
|
||||
await session.commit()
|
||||
await session.refresh(conv)
|
||||
return conv
|
||||
|
||||
|
||||
async def post_message(conversation_id: int, role: str, content: str) -> Message:
|
||||
"""Append a message to a briefing conversation."""
|
||||
async with async_session() as session:
|
||||
msg = Message(
|
||||
conversation_id=conversation_id,
|
||||
role=role,
|
||||
content=content,
|
||||
status="complete",
|
||||
)
|
||||
session.add(msg)
|
||||
# Bump conversation updated_at
|
||||
conv = await session.get(Conversation, conversation_id)
|
||||
if conv:
|
||||
conv.updated_at = datetime.now(timezone.utc)
|
||||
await session.commit()
|
||||
await session.refresh(msg)
|
||||
return msg
|
||||
|
||||
|
||||
async def list_briefing_conversations(user_id: int) -> list[dict]:
|
||||
"""Return briefing conversations newest first."""
|
||||
async with async_session() as session:
|
||||
result = await session.execute(
|
||||
select(Conversation).where(
|
||||
Conversation.user_id == user_id,
|
||||
Conversation.conversation_type == "briefing",
|
||||
).order_by(Conversation.briefing_date.desc().nullslast())
|
||||
.limit(30)
|
||||
)
|
||||
convs = list(result.scalars().all())
|
||||
return [c.to_dict() for c in convs]
|
||||
Reference in New Issue
Block a user