Spaces:
Configuration error
Configuration error
Upload stratego\web\config\session_manager.py with huggingface_hub
Browse files
stratego//web//config//session_manager.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Session state management for Streamlit"""
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from typing import Any, Optional
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class SessionManager:
|
| 8 |
+
"""Manage Streamlit session state across reruns"""
|
| 9 |
+
|
| 10 |
+
@staticmethod
|
| 11 |
+
def init():
|
| 12 |
+
"""Initialize default session state values"""
|
| 13 |
+
if "current_screen" not in st.session_state:
|
| 14 |
+
st.session_state.current_screen = 0 # Welcome screen
|
| 15 |
+
|
| 16 |
+
if "game_controller" not in st.session_state:
|
| 17 |
+
st.session_state.game_controller = None
|
| 18 |
+
|
| 19 |
+
if "game_mode" not in st.session_state:
|
| 20 |
+
st.session_state.game_mode = "Standard (10x10)"
|
| 21 |
+
|
| 22 |
+
if "ai_agent" not in st.session_state:
|
| 23 |
+
st.session_state.ai_agent = None
|
| 24 |
+
|
| 25 |
+
if "ai_config" not in st.session_state:
|
| 26 |
+
st.session_state.ai_config = {
|
| 27 |
+
"backend": "huggingface",
|
| 28 |
+
"model": "mistral:7b",
|
| 29 |
+
"prompt": "base"
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
if "game_active" not in st.session_state:
|
| 33 |
+
st.session_state.game_active = False
|
| 34 |
+
|
| 35 |
+
if "game_log" not in st.session_state:
|
| 36 |
+
st.session_state.game_log = []
|
| 37 |
+
|
| 38 |
+
if "last_error" not in st.session_state:
|
| 39 |
+
st.session_state.last_error = None
|
| 40 |
+
|
| 41 |
+
if "last_move" not in st.session_state:
|
| 42 |
+
st.session_state.last_move = None
|
| 43 |
+
|
| 44 |
+
if "current_observation" not in st.session_state:
|
| 45 |
+
st.session_state.current_observation = ""
|
| 46 |
+
|
| 47 |
+
@staticmethod
|
| 48 |
+
def set_game_controller(controller: Any) -> None:
|
| 49 |
+
"""Set the game controller in session state"""
|
| 50 |
+
st.session_state.game_controller = controller
|
| 51 |
+
|
| 52 |
+
@staticmethod
|
| 53 |
+
def get_game_controller() -> Optional[Any]:
|
| 54 |
+
"""Get game controller from session state"""
|
| 55 |
+
return st.session_state.get("game_controller")
|
| 56 |
+
|
| 57 |
+
@staticmethod
|
| 58 |
+
def set_screen(screen_number: int) -> None:
|
| 59 |
+
"""Set current screen"""
|
| 60 |
+
st.session_state.current_screen = screen_number
|
| 61 |
+
|
| 62 |
+
@staticmethod
|
| 63 |
+
def get_screen() -> int:
|
| 64 |
+
"""Get current screen"""
|
| 65 |
+
return st.session_state.get("current_screen", 0)
|
| 66 |
+
|
| 67 |
+
@staticmethod
|
| 68 |
+
def set_ai_config(backend: str, model: str, prompt: str) -> None:
|
| 69 |
+
"""Set AI configuration"""
|
| 70 |
+
st.session_state.ai_config = {
|
| 71 |
+
"backend": backend,
|
| 72 |
+
"model": model,
|
| 73 |
+
"prompt": prompt
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
@staticmethod
|
| 77 |
+
def get_ai_config() -> dict:
|
| 78 |
+
"""Get AI configuration"""
|
| 79 |
+
return st.session_state.get("ai_config", {})
|
| 80 |
+
|
| 81 |
+
@staticmethod
|
| 82 |
+
def set_error(error_message: str) -> None:
|
| 83 |
+
"""Set the last error message"""
|
| 84 |
+
st.session_state.last_error = error_message
|
| 85 |
+
|
| 86 |
+
@staticmethod
|
| 87 |
+
def get_error() -> Optional[str]:
|
| 88 |
+
"""Get the last error message"""
|
| 89 |
+
return st.session_state.get("last_error")
|
| 90 |
+
|
| 91 |
+
@staticmethod
|
| 92 |
+
def clear_error() -> None:
|
| 93 |
+
"""Clear error message"""
|
| 94 |
+
st.session_state.last_error = None
|
| 95 |
+
|
| 96 |
+
@staticmethod
|
| 97 |
+
def set_game_mode(mode: str) -> None:
|
| 98 |
+
"""Set selected game mode"""
|
| 99 |
+
st.session_state.game_mode = mode
|
| 100 |
+
|
| 101 |
+
@staticmethod
|
| 102 |
+
def get_game_mode() -> str:
|
| 103 |
+
"""Get selected game mode"""
|
| 104 |
+
return st.session_state.get("game_mode", "Standard (10x10)")
|
| 105 |
+
|
| 106 |
+
@staticmethod
|
| 107 |
+
def reset_all() -> None:
|
| 108 |
+
"""Reset all session state to defaults (for new game)"""
|
| 109 |
+
st.session_state.game_controller = None
|
| 110 |
+
st.session_state.game_active = False
|
| 111 |
+
st.session_state.game_log = []
|
| 112 |
+
st.session_state.last_error = None
|
| 113 |
+
st.session_state.last_move = None
|
| 114 |
+
st.session_state.current_observation = ""
|
| 115 |
+
st.session_state.current_screen = 0
|