"""Generate a short session name from user messages via LLM."""
from navi.llm.base import LLMBackend, Message
_SYSTEM = (
"You are a session title generator. "
"Given the user's messages from a conversation, produce ONE short title (3–6 words, no punctuation at the end). "
"The title must reflect the actual topic. "
"Respond in the same language as the user's messages. "
"If the messages contain no clear topic yet (e.g. only greetings, very short or ambiguous text), "
"reply with exactly: NO_TITLE"
)
async def generate_session_name(
user_messages: list[str],
backend: LLMBackend,
model: str,
) -> str | None:
"""Return a short title or None if content isn't substantial enough."""
if not user_messages:
return None
combined = "\n".join(f"- {m}" for m in user_messages[:10])
messages = [
Message(role="system", content=_SYSTEM),
Message(role="user", content=f"User messages:\n{combined}"),
]
resp = await backend.complete(messages, tools=[], model=model, temperature=0.4)
text = (resp.content or "").strip()
if not text or text.upper() == "NO_TITLE" or len(text) > 80:
return None
# Strip surrounding quotes if the model added them
return text.strip('"\'')