ciyidogan commited on
Commit
d8516ac
·
verified ·
1 Parent(s): 665b6e6

Update session.py

Browse files
Files changed (1) hide show
  1. session.py +38 -40
session.py CHANGED
@@ -1,72 +1,70 @@
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()
 
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()