Prevent duplicate Twitch session prompts
This commit is contained in:
@@ -88,6 +88,47 @@ class AgentOrchestrator:
|
||||
logger.info(f"Started session {session_id} for {channel_name}")
|
||||
return session_id
|
||||
|
||||
async def ensure_single_active_session_for_channel(self, channel_name: str) -> str:
|
||||
"""Return one active session for a channel and end duplicate active sessions."""
|
||||
normalized_channel = channel_name.lower()
|
||||
matching_sessions = [
|
||||
(session_id, session)
|
||||
for session_id, session in self.active_sessions.items()
|
||||
if session.get("channel_name", "").lower() == normalized_channel
|
||||
]
|
||||
|
||||
if not matching_sessions:
|
||||
return await self.start_session(channel_name)
|
||||
|
||||
keep_session_id, _ = max(
|
||||
matching_sessions,
|
||||
key=lambda item: item[1].get("started_at", datetime.min),
|
||||
)
|
||||
duplicate_session_ids = [
|
||||
session_id
|
||||
for session_id, _ in matching_sessions
|
||||
if session_id != keep_session_id
|
||||
]
|
||||
|
||||
if duplicate_session_ids:
|
||||
async for db_session in get_session():
|
||||
repo = Repository(db_session)
|
||||
for session_id in duplicate_session_ids:
|
||||
await repo.end_session(session_id)
|
||||
|
||||
for session_id in duplicate_session_ids:
|
||||
self.active_sessions.pop(session_id, None)
|
||||
self.chat_activity.clear_activity(session_id)
|
||||
|
||||
logger.warning(
|
||||
"Ended %s duplicate active session(s) for channel %s; keeping %s",
|
||||
len(duplicate_session_ids),
|
||||
channel_name,
|
||||
keep_session_id,
|
||||
)
|
||||
|
||||
return keep_session_id
|
||||
|
||||
async def restore_active_sessions(self) -> int:
|
||||
"""Restore active sessions from the database after app startup."""
|
||||
restored_count = 0
|
||||
|
||||
Reference in New Issue
Block a user