File size: 1,678 Bytes
b69c342
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import uuid
import threading
from utils import log

class Session:
    def __init__(self, project_name):
        self.session_id = str(uuid.uuid4())
        self.project_name = project_name
        self.last_intent = None
        self.variables = {}  # Extracted parameters
        self.awaiting_parameters = []  # Still-missing parameters
        self.state = "intent_detection"  # intent_detection, parameter_extraction, api_call, humanization
        self.chat_history = []  # [{'role': 'user'/'assistant', 'content': text}]
        self.auth_tokens = {}  # Per-API auth tokens


class SessionStore:
    def __init__(self):
        self.sessions = {}
        self.lock = threading.Lock()

    def create_session(self, project_name):
        with self.lock:
            session = Session(project_name)
            self.sessions[session.session_id] = session
            log(f"πŸ†• Created session: {session.session_id} for project: {project_name}")
            return session

    def get_session(self, session_id):
        with self.lock:
            return self.sessions.get(session_id)

    def update_session(self, session_id, **kwargs):
        with self.lock:
            session = self.sessions.get(session_id)
            if not session:
                return None
            for key, value in kwargs.items():
                setattr(session, key, value)
            log(f"πŸ”„ Updated session: {session_id} with {kwargs}")
            return session

    def remove_session(self, session_id):
        with self.lock:
            if session_id in self.sessions:
                del self.sessions[session_id]
                log(f"❌ Removed session: {session_id}")