File size: 2,344 Bytes
525e493
 
 
 
 
b69c342
 
525e493
b69c342
 
525e493
b69c342
525e493
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b69c342
 
 
525e493
b69c342
525e493
 
 
 
 
 
b69c342
525e493
 
b69c342
 
525e493
 
 
b69c342
525e493
 
 
 
b69c342
525e493
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""Flare – Session & SessionStore
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Basit, thread-safe, in-memory oturum yöneticisi.
"""

import uuid
import threading
from typing import Dict, List
from utils import log


class Session:
    """Tek kullanıcılık sohbet durumu."""

    def __init__(self, project_name: str):
        self.session_id: str = str(uuid.uuid4())
        self.project_name: str = project_name
        self.last_intent: str | None = None
        self.variables: Dict[str, str] = {}
        self.awaiting_parameters: List[str] = []
        self.state: str = "intent_detection"
        self.chat_history: List[Dict[str, str]] = []
        self.auth_tokens: Dict[str, str] = {}

    # --------- helpers ----------
    def add_turn(self, role: str, content: str) -> None:
        self.chat_history.append({"role": role, "content": content})
        if len(self.chat_history) > 20:          # hafıza koruması
            self.chat_history.pop(0)

    def to_dict(self) -> Dict:
        return {
            "session_id": self.session_id,
            "project_name": self.project_name,
            "last_intent": self.last_intent,
            "variables": self.variables,
            "awaiting_parameters": self.awaiting_parameters,
            "state": self.state,
            "chat_history": self.chat_history,
        }


class SessionStore:
    """Thread-safe oturum havuzu."""

    def __init__(self) -> None:
        self._sessions: Dict[str, Session] = {}
        self._lock = threading.Lock()

    def create_session(self, project_name: str) -> Session:
        with self._lock:
            session = Session(project_name)
            self._sessions[session.session_id] = session
            log(f"🆕 Created session: {session.session_id} (project: {project_name})")
            return session

    def get_session(self, session_id: str) -> Session | None:
        with self._lock:
            return self._sessions.get(session_id)

    def remove_session(self, session_id: str) -> None:
        with self._lock:
            if session_id in self._sessions:
                del self._sessions[session_id]
                log(f"❌ Removed session: {session_id}")

    def __contains__(self, session_id: str) -> bool:
        return self.get_session(session_id) is not None


# Global singleton
session_store = SessionStore()