Spaces:
Building
Building
Update session.py
Browse files- session.py +38 -40
session.py
CHANGED
@@ -1,72 +1,70 @@
|
|
1 |
-
"""
|
2 |
-
|
3 |
-
|
|
|
|
|
4 |
"""
|
5 |
|
6 |
-
import
|
7 |
-
import threading
|
8 |
from typing import Dict, List
|
9 |
from utils import log
|
10 |
|
11 |
|
12 |
class Session:
|
13 |
-
"""
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
self.chat_history: List[Dict[str, str]] = []
|
23 |
-
self.auth_tokens: Dict[str, str] = {}
|
24 |
|
25 |
-
#
|
26 |
-
def add_turn(self, role: str, content: str)
|
27 |
self.chat_history.append({"role": role, "content": content})
|
28 |
-
if len(self.chat_history) > 20:
|
29 |
self.chat_history.pop(0)
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
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
|
45 |
|
46 |
-
def __init__(self)
|
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 |
-
|
53 |
-
self._sessions[
|
54 |
-
log(f"🆕
|
55 |
-
return
|
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
|
62 |
with self._lock:
|
63 |
-
|
64 |
-
del self._sessions[session_id]
|
65 |
-
log(f"❌ Removed session: {session_id}")
|
66 |
|
67 |
-
def __contains__(self,
|
68 |
-
return self.get_session(
|
69 |
|
70 |
|
71 |
-
# Global singleton
|
72 |
session_store = SessionStore()
|
|
|
1 |
+
"""
|
2 |
+
Flare – Session Management
|
3 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
4 |
+
• thread-safe SessionStore
|
5 |
+
• state-machine alanları
|
6 |
"""
|
7 |
|
8 |
+
from __future__ import annotations
|
9 |
+
import threading, uuid
|
10 |
from typing import Dict, List
|
11 |
from utils import log
|
12 |
|
13 |
|
14 |
class Session:
|
15 |
+
"""Single chat session."""
|
16 |
|
17 |
def __init__(self, project_name: str):
|
18 |
self.session_id: str = str(uuid.uuid4())
|
19 |
self.project_name: str = project_name
|
20 |
+
|
21 |
+
# flow state
|
22 |
+
self.state: str = "idle" # idle | await_param | call_api | humanize
|
23 |
self.last_intent: str | None = None
|
|
|
24 |
self.awaiting_parameters: List[str] = []
|
25 |
+
self.missing_ask_count: int = 0
|
26 |
+
|
27 |
+
# data
|
28 |
+
self.variables: Dict[str, str] = {}
|
29 |
+
self.auth_tokens: Dict[str, Dict] = {} # api_name -> {token, expiry}
|
30 |
+
|
31 |
+
# history
|
32 |
self.chat_history: List[Dict[str, str]] = []
|
|
|
33 |
|
34 |
+
# -------- helper ----------
|
35 |
+
def add_turn(self, role: str, content: str):
|
36 |
self.chat_history.append({"role": role, "content": content})
|
37 |
+
if len(self.chat_history) > 20:
|
38 |
self.chat_history.pop(0)
|
39 |
|
40 |
+
# -------- reset flow ------
|
41 |
+
def reset_flow(self):
|
42 |
+
self.state = "idle"
|
43 |
+
self.last_intent = None
|
44 |
+
self.awaiting_parameters.clear()
|
45 |
+
self.missing_ask_count = 0
|
|
|
|
|
|
|
|
|
46 |
|
47 |
|
48 |
class SessionStore:
|
49 |
+
"""Thread-safe global store."""
|
50 |
|
51 |
+
def __init__(self):
|
|
|
52 |
self._lock = threading.Lock()
|
53 |
+
self._sessions: Dict[str, Session] = {}
|
54 |
|
55 |
def create_session(self, project_name: str) -> Session:
|
56 |
with self._lock:
|
57 |
+
s = Session(project_name)
|
58 |
+
self._sessions[s.session_id] = s
|
59 |
+
log(f"🆕 Session created {s.session_id}")
|
60 |
+
return s
|
|
|
|
|
|
|
|
|
61 |
|
62 |
+
def get_session(self, sid: str) -> Session | None:
|
63 |
with self._lock:
|
64 |
+
return self._sessions.get(sid)
|
|
|
|
|
65 |
|
66 |
+
def __contains__(self, sid: str) -> bool:
|
67 |
+
return self.get_session(sid) is not None
|
68 |
|
69 |
|
|
|
70 |
session_store = SessionStore()
|