feat(journal): run_for_user orchestrates closeout extraction
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -7,6 +7,20 @@ the bullets to user_profiles.observations_raw via append_observations.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime
|
||||
import logging
|
||||
|
||||
from sqlalchemy import or_, select
|
||||
|
||||
from fabledassistant.config import Config
|
||||
from fabledassistant.models import async_session
|
||||
from fabledassistant.models.conversation import Conversation, Message
|
||||
from fabledassistant.services.llm import generate_completion
|
||||
from fabledassistant.services.settings import get_setting
|
||||
from fabledassistant.services.user_profile import append_observations
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Message kinds whose content must NEVER be sent to the closeout LLM.
|
||||
# These are assistant-authored auto-blocks that would otherwise dominate
|
||||
# attention and leak back into "what the assistant has learned."
|
||||
@@ -54,3 +68,66 @@ SYSTEM_PROMPT = (
|
||||
"- Write 2-5 short bullet points. Be specific and factual.\n"
|
||||
"- If nothing notable, output only: (nothing to note)"
|
||||
)
|
||||
|
||||
|
||||
async def run_for_user(user_id: int, yesterday: datetime.date) -> None:
|
||||
"""Extract preference observations from yesterday's journal conversation.
|
||||
|
||||
Skips silently when there is nothing meaningful to extract.
|
||||
"""
|
||||
async with async_session() as session:
|
||||
conv_result = await session.execute(
|
||||
select(Conversation).where(
|
||||
Conversation.user_id == user_id,
|
||||
Conversation.conversation_type == "journal",
|
||||
Conversation.day_date == yesterday,
|
||||
)
|
||||
)
|
||||
conv = conv_result.scalar_one_or_none()
|
||||
if conv is None:
|
||||
logger.debug("closeout: no journal conv for user %d on %s", user_id, yesterday)
|
||||
return
|
||||
|
||||
msg_result = await session.execute(
|
||||
select(Message)
|
||||
.where(
|
||||
Message.conversation_id == conv.id,
|
||||
Message.role.in_(("user", "assistant")),
|
||||
or_(
|
||||
Message.msg_metadata.is_(None),
|
||||
~Message.msg_metadata["kind"].astext.in_(EXCLUDED_KINDS),
|
||||
),
|
||||
)
|
||||
.order_by(Message.created_at)
|
||||
)
|
||||
messages = list(msg_result.scalars().all())
|
||||
|
||||
# Defensive second-pass filter (covers any message with metadata the
|
||||
# SQL JSON path can't reach, e.g. older rows where kind nesting differs).
|
||||
messages = _filter_messages(messages)
|
||||
|
||||
if len(messages) < 2:
|
||||
logger.debug("closeout: not enough messages for user %d (%d)", user_id, len(messages))
|
||||
return
|
||||
|
||||
transcript = _build_transcript(messages)
|
||||
model = await get_setting(user_id, "background_model", Config.OLLAMA_BACKGROUND_MODEL)
|
||||
|
||||
try:
|
||||
output = (await generate_completion(
|
||||
[
|
||||
{"role": "system", "content": SYSTEM_PROMPT},
|
||||
{"role": "user", "content": transcript},
|
||||
],
|
||||
model,
|
||||
)).strip()
|
||||
except Exception:
|
||||
logger.warning("closeout LLM failed for user %d", user_id, exc_info=True)
|
||||
return
|
||||
|
||||
if not output or "(nothing to note)" in output.lower():
|
||||
logger.debug("closeout: nothing to note for user %d", user_id)
|
||||
return
|
||||
|
||||
await append_observations(user_id, output)
|
||||
logger.info("closeout: appended observations for user %d (%s)", user_id, yesterday)
|
||||
|
||||
Reference in New Issue
Block a user