feat: Auto-rename DM rooms by default with improved title prompt
- DM rooms (1:1 with bot) now auto-rename after first response without needing !ai auto-rename on - Group rooms still require explicit opt-in - Skip rename if room already has a meaningful name - Improved prompt: emoji prefix, 3-5 words, same language as chat (inspired by Open WebUI title generation) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
34
bot.py
34
bot.py
@@ -484,8 +484,13 @@ class Bot:
|
||||
reply = resp.choices[0].message.content
|
||||
await self._send_text(room.room_id, reply)
|
||||
# Auto-rename room after first AI response
|
||||
if (room.room_id in self.auto_rename_rooms
|
||||
and room.room_id not in self.renamed_rooms):
|
||||
# In DMs: always auto-rename. In groups: only if explicitly enabled.
|
||||
is_dm = room.member_count == 2
|
||||
should_rename = (
|
||||
room.room_id not in self.renamed_rooms
|
||||
and (is_dm or room.room_id in self.auto_rename_rooms)
|
||||
)
|
||||
if should_rename:
|
||||
await self._auto_rename_room(room, user_message, reply)
|
||||
except Exception:
|
||||
logger.exception("LLM call failed")
|
||||
@@ -529,24 +534,31 @@ class Bot:
|
||||
return user_message
|
||||
|
||||
async def _auto_rename_room(self, room, user_message: str, ai_reply: str):
|
||||
"""Generate a short topic title and set it as the room name."""
|
||||
"""Generate a short topic title and set it as the room name (Open WebUI style)."""
|
||||
# Skip if room already has a meaningful name (not default)
|
||||
current_name = getattr(room, "name", "") or ""
|
||||
if current_name and current_name.lower() not in ("", "ai", "empty room", "new chat"):
|
||||
self.renamed_rooms.add(room.room_id)
|
||||
return
|
||||
|
||||
try:
|
||||
resp = await self.llm.chat.completions.create(
|
||||
model=self.room_models.get(room.room_id, DEFAULT_MODEL),
|
||||
messages=[
|
||||
{"role": "system", "content": (
|
||||
"Generate a very short room title (3-6 words, no quotes) "
|
||||
"that captures the topic of this conversation. "
|
||||
"Reply with ONLY the title, nothing else."
|
||||
)},
|
||||
{"role": "user", "content": user_message},
|
||||
{"role": "assistant", "content": ai_reply[:200]},
|
||||
{"role": "user", "content": "What is a good short title for this conversation?"},
|
||||
{"role": "assistant", "content": ai_reply[:300]},
|
||||
{"role": "user", "content": (
|
||||
"Generate a concise, 3-5 word title with an emoji as prefix "
|
||||
"that summarizes the conversation above. "
|
||||
"Use the same language as the conversation. "
|
||||
"Do not use quotation marks or formatting. "
|
||||
"Respond with ONLY the title, nothing else."
|
||||
)},
|
||||
],
|
||||
max_tokens=30,
|
||||
)
|
||||
title = resp.choices[0].message.content.strip().strip('"\'')
|
||||
if not title or len(title) > 80:
|
||||
if not title or len(title) > 80 or len(title) < 3:
|
||||
return
|
||||
await self.client.room_put_state(
|
||||
room.room_id, "m.room.name",
|
||||
|
||||
Reference in New Issue
Block a user