feat: Re-rename DM rooms after 5min gap for new conversation topics
Matrix reuses a single DM room per user pair, so 'new' DMs jump back to the old thread. Now the bot re-renames the room if >5min has passed since the last rename, reflecting the new topic. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
18
bot.py
18
bot.py
@@ -141,7 +141,7 @@ class Bot:
|
|||||||
self.llm = AsyncOpenAI(base_url=LITELLM_URL, api_key=LITELLM_KEY) if LITELLM_URL else None
|
self.llm = AsyncOpenAI(base_url=LITELLM_URL, api_key=LITELLM_KEY) if LITELLM_URL else None
|
||||||
self.room_models: dict[str, str] = {} # room_id -> model name
|
self.room_models: dict[str, str] = {} # room_id -> model name
|
||||||
self.auto_rename_rooms: set[str] = set() # rooms with auto-rename enabled
|
self.auto_rename_rooms: set[str] = set() # rooms with auto-rename enabled
|
||||||
self.renamed_rooms: set[str] = set() # rooms already renamed this session
|
self.renamed_rooms: dict[str, float] = {} # room_id -> timestamp of last rename
|
||||||
self._loaded_rooms: set[str] = set() # rooms where we've loaded state
|
self._loaded_rooms: set[str] = set() # rooms where we've loaded state
|
||||||
self._sync_token_received = False
|
self._sync_token_received = False
|
||||||
self._verifications: dict[str, dict] = {} # txn_id -> verification state
|
self._verifications: dict[str, dict] = {} # txn_id -> verification state
|
||||||
@@ -483,12 +483,14 @@ class Bot:
|
|||||||
)
|
)
|
||||||
reply = resp.choices[0].message.content
|
reply = resp.choices[0].message.content
|
||||||
await self._send_text(room.room_id, reply)
|
await self._send_text(room.room_id, reply)
|
||||||
# Auto-rename room after first AI response
|
# Auto-rename room after first AI response or after a conversation gap
|
||||||
# In DMs: always auto-rename. In groups: only if explicitly enabled.
|
# In DMs: always auto-rename. In groups: only if explicitly enabled.
|
||||||
is_dm = room.member_count == 2
|
is_dm = room.member_count == 2
|
||||||
|
last_rename = self.renamed_rooms.get(room.room_id, 0)
|
||||||
|
gap_seconds = time.time() - last_rename if last_rename else float("inf")
|
||||||
should_rename = (
|
should_rename = (
|
||||||
room.room_id not in self.renamed_rooms
|
(is_dm or room.room_id in self.auto_rename_rooms)
|
||||||
and (is_dm or room.room_id in self.auto_rename_rooms)
|
and gap_seconds > 300 # Re-rename after 5min gap (new topic)
|
||||||
)
|
)
|
||||||
if should_rename:
|
if should_rename:
|
||||||
await self._auto_rename_room(room, user_message, reply)
|
await self._auto_rename_room(room, user_message, reply)
|
||||||
@@ -535,11 +537,7 @@ class Bot:
|
|||||||
|
|
||||||
async def _auto_rename_room(self, room, user_message: str, ai_reply: str):
|
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 (Open WebUI style)."""
|
"""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)
|
# Skip rename check — always generate fresh title based on current conversation
|
||||||
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:
|
try:
|
||||||
resp = await self.llm.chat.completions.create(
|
resp = await self.llm.chat.completions.create(
|
||||||
@@ -564,7 +562,7 @@ class Bot:
|
|||||||
room.room_id, "m.room.name",
|
room.room_id, "m.room.name",
|
||||||
{"name": title}, state_key="",
|
{"name": title}, state_key="",
|
||||||
)
|
)
|
||||||
self.renamed_rooms.add(room.room_id)
|
self.renamed_rooms[room.room_id] = time.time()
|
||||||
logger.info("Auto-renamed room %s to: %s", room.room_id, title)
|
logger.info("Auto-renamed room %s to: %s", room.room_id, title)
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.debug("Auto-rename failed", exc_info=True)
|
logger.debug("Auto-rename failed", exc_info=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user