File size: 1,424 Bytes
af60cba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# modules/session_manager.py
import uuid
from typing import Dict, Any

class SessionManager:
    def __init__(self):
        self.sessions: Dict[str, Dict[str, Any]] = {}

    def get_or_create_session(self, session_id: str = None) -> Dict[str, Any]:
        if not session_id or session_id not in self.sessions:
            session_id = str(uuid.uuid4())[:8]
            self.sessions[session_id] = {
                "session_id": session_id,
                "destination": None,
                "duration": None,
                "persona": None,
                "stage": "greeting" # 对话状态机
            }
        return self.sessions[session_id]

    def update_session(self, session_id: str, updates: Dict[str, Any]):
        if session_id in self.sessions:
            self.sessions[session_id].update(updates)

    def format_session_info(self, session_state: dict) -> str:
        parts = [f"ID: {session_state.get('session_id', 'N/A')}"]
        if session_state.get('destination'): parts.append(f"目的地: {session_state['destination']['name']}")
        if session_state.get('duration'): parts.append(f"天数: {session_state['duration']['days']}")
        if session_state.get('persona'): parts.append(f"风格: {session_state['persona']['name']}")
        return " | ".join(parts)

    def reset(self, session_id: str):
        if session_id in self.sessions:
            del self.sessions[session_id]