MCPcopy Create free account
hub / github.com/pollinations/pollinations / SessionManager

Class SessionManager

apps/polly/src/context/manager.py:11–88  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9
10
11class SessionManager:
12 def __init__(self, max_sessions: int = MAX_SESSIONS):
13 self._sessions: dict[int, ConversationSession] = {}
14 self._max_sessions = max_sessions
15
16 def get_session(self, thread_id: int) -> ConversationSession | None:
17 session = self._sessions.get(thread_id)
18
19 if session and session.is_expired(SESSION_TIMEOUT):
20 self._cleanup_session(thread_id)
21 return None
22
23 return session
24
25 def create_session(
26 self,
27 channel_id: int,
28 thread_id: int,
29 user_id: int,
30 user_name: str,
31 initial_message: str,
32 topic_summary: str,
33 image_urls: list[str] | None = None,
34 ) -> ConversationSession:
35 if len(self._sessions) >= self._max_sessions:
36 self._evict_oldest()
37
38 session = ConversationSession(channel_id=channel_id, thread_id=thread_id, topic_summary=topic_summary)
39 session.add_message("user", initial_message, user_name, user_id, image_urls)
40
41 self._sessions[thread_id] = session
42
43 logger.info(f"Created session for thread {thread_id} - topic: '{topic_summary}'")
44 return session
45
46 def add_to_session(
47 self,
48 session: ConversationSession,
49 role: str,
50 content: str,
51 author: str,
52 author_id: int,
53 image_urls: list[str] | None = None,
54 ):
55 session.add_message(role, content, author, author_id, image_urls)
56
57 def clear_session(self, session: ConversationSession):
58 self._cleanup_session(session.thread_id)
59 logger.info(f"Cleared session for thread {session.thread_id}")
60
61 def _cleanup_session(self, thread_id: int):
62 if thread_id in self._sessions:
63 del self._sessions[thread_id]
64
65 def _evict_oldest(self, count: int = 10):
66 if not self._sessions:
67 return
68

Callers 1

manager.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected