Spaces:
Building
Building
Update session.py
Browse files- session.py +58 -33
session.py
CHANGED
@@ -1,47 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import uuid
|
2 |
import threading
|
|
|
3 |
from utils import log
|
4 |
|
|
|
5 |
class Session:
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
self.
|
10 |
-
self.
|
11 |
-
self.
|
12 |
-
self.
|
13 |
-
self.
|
14 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
|
17 |
class SessionStore:
|
18 |
-
|
19 |
-
self.sessions = {}
|
20 |
-
self.lock = threading.Lock()
|
21 |
|
22 |
-
def
|
23 |
-
|
|
|
|
|
|
|
|
|
24 |
session = Session(project_name)
|
25 |
-
self.
|
26 |
-
log(f"🆕 Created session: {session.session_id}
|
27 |
return session
|
28 |
|
29 |
-
def get_session(self, session_id):
|
30 |
-
with self.
|
31 |
-
return self.
|
32 |
-
|
33 |
-
def update_session(self, session_id, **kwargs):
|
34 |
-
with self.lock:
|
35 |
-
session = self.sessions.get(session_id)
|
36 |
-
if not session:
|
37 |
-
return None
|
38 |
-
for key, value in kwargs.items():
|
39 |
-
setattr(session, key, value)
|
40 |
-
log(f"🔄 Updated session: {session_id} with {kwargs}")
|
41 |
-
return session
|
42 |
|
43 |
-
def remove_session(self, session_id):
|
44 |
-
with self.
|
45 |
-
if session_id in self.
|
46 |
-
del self.
|
47 |
log(f"❌ Removed session: {session_id}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Flare – Session & SessionStore
|
2 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
3 |
+
Basit, thread-safe, in-memory oturum yöneticisi.
|
4 |
+
"""
|
5 |
+
|
6 |
import uuid
|
7 |
import threading
|
8 |
+
from typing import Dict, List
|
9 |
from utils import log
|
10 |
|
11 |
+
|
12 |
class Session:
|
13 |
+
"""Tek kullanıcılık sohbet durumu."""
|
14 |
+
|
15 |
+
def __init__(self, project_name: str):
|
16 |
+
self.session_id: str = str(uuid.uuid4())
|
17 |
+
self.project_name: str = project_name
|
18 |
+
self.last_intent: str | None = None
|
19 |
+
self.variables: Dict[str, str] = {}
|
20 |
+
self.awaiting_parameters: List[str] = []
|
21 |
+
self.state: str = "intent_detection"
|
22 |
+
self.chat_history: List[Dict[str, str]] = []
|
23 |
+
self.auth_tokens: Dict[str, str] = {}
|
24 |
+
|
25 |
+
# --------- helpers ----------
|
26 |
+
def add_turn(self, role: str, content: str) -> None:
|
27 |
+
self.chat_history.append({"role": role, "content": content})
|
28 |
+
if len(self.chat_history) > 20: # hafıza koruması
|
29 |
+
self.chat_history.pop(0)
|
30 |
+
|
31 |
+
def to_dict(self) -> Dict:
|
32 |
+
return {
|
33 |
+
"session_id": self.session_id,
|
34 |
+
"project_name": self.project_name,
|
35 |
+
"last_intent": self.last_intent,
|
36 |
+
"variables": self.variables,
|
37 |
+
"awaiting_parameters": self.awaiting_parameters,
|
38 |
+
"state": self.state,
|
39 |
+
"chat_history": self.chat_history,
|
40 |
+
}
|
41 |
|
42 |
|
43 |
class SessionStore:
|
44 |
+
"""Thread-safe oturum havuzu."""
|
|
|
|
|
45 |
|
46 |
+
def __init__(self) -> None:
|
47 |
+
self._sessions: Dict[str, Session] = {}
|
48 |
+
self._lock = threading.Lock()
|
49 |
+
|
50 |
+
def create_session(self, project_name: str) -> Session:
|
51 |
+
with self._lock:
|
52 |
session = Session(project_name)
|
53 |
+
self._sessions[session.session_id] = session
|
54 |
+
log(f"🆕 Created session: {session.session_id} (project: {project_name})")
|
55 |
return session
|
56 |
|
57 |
+
def get_session(self, session_id: str) -> Session | None:
|
58 |
+
with self._lock:
|
59 |
+
return self._sessions.get(session_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
def remove_session(self, session_id: str) -> None:
|
62 |
+
with self._lock:
|
63 |
+
if session_id in self._sessions:
|
64 |
+
del self._sessions[session_id]
|
65 |
log(f"❌ Removed session: {session_id}")
|
66 |
+
|
67 |
+
def __contains__(self, session_id: str) -> bool:
|
68 |
+
return self.get_session(session_id) is not None
|
69 |
+
|
70 |
+
|
71 |
+
# Global singleton
|
72 |
+
session_store = SessionStore()
|