awacke1 commited on
Commit
1f793e3
·
verified ·
1 Parent(s): a64ee01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +496 -638
app.py CHANGED
@@ -1,7 +1,6 @@
1
- # app.py (Full Code - Corrected Multi-statement/Indentation Errors)
2
  import streamlit as st
3
  import asyncio
4
- import websockets # Re-added
5
  import uuid
6
  from datetime import datetime
7
  import os
@@ -22,13 +21,14 @@ import threading
22
  import json
23
  import zipfile
24
  from dotenv import load_dotenv
25
- # from streamlit_marquee import streamlit_marquee # Import if needed
26
- from collections import defaultdict, Counter, deque
27
- from streamlit_js_eval import streamlit_js_eval # Correct import
28
- from PIL import Image
 
29
 
30
  # ==============================================================================
31
- # 1. ⚙️ Configuration & Constants
32
  # ==============================================================================
33
 
34
  # 🛠️ Patch asyncio for nesting
@@ -36,62 +36,71 @@ nest_asyncio.apply()
36
 
37
  # 🎨 Page Config
38
  st.set_page_config(
39
- page_title="🏗️ Live World Builder ",
40
  page_icon="🏗️",
41
  layout="wide",
42
  initial_sidebar_state="expanded"
43
  )
44
 
45
  # General Constants
46
- Site_Name = '🏗️ Live World Builder ⚡'
47
- MEDIA_DIR = "."
48
- STATE_FILE = "user_state.txt"
49
- DEFAULT_TTS_VOICE = "en-US-AriaNeural"
50
-
51
- # Directories
52
- CHAT_DIR = "chat_logs"
53
- AUDIO_CACHE_DIR = "audio_cache"
54
- AUDIO_DIR = "audio_logs"
55
- SAVED_WORLDS_DIR = "saved_worlds"
56
-
57
- # World Builder Constants
58
- PLOT_WIDTH = 50.0
59
- PLOT_DEPTH = 50.0
60
- WORLD_STATE_FILE_MD_PREFIX = "🌌_" # Keep prefix for saved files
61
- MAX_ACTION_LOG_SIZE = 30
62
 
63
  # User/Chat Constants
64
  FUN_USERNAMES = {
65
  "BuilderBot 🤖": "en-US-AriaNeural", "WorldWeaver 🕸️": "en-US-JennyNeural",
66
  "Terraformer 🌱": "en-GB-SoniaNeural", "SkyArchitect ☁️": "en-AU-NatashaNeural",
67
  "PixelPainter 🎨": "en-CA-ClaraNeural", "VoxelVortex 🌪️": "en-US-GuyNeural",
68
- } # Simplified list
 
 
69
  EDGE_TTS_VOICES = list(set(FUN_USERNAMES.values()))
 
 
 
 
 
 
 
 
 
 
 
70
 
71
  # File Emojis
72
- FILE_EMOJIS = {"md": "📜", "mp3": "🎵", "png": "🖼️", "mp4": "🎥", "zip": "📦", "json": "📄"}
73
 
74
- # Primitives Map
 
75
  PRIMITIVE_MAP = {
76
- "Tree": "🌳", "Rock": "🗿", "Simple House": "🏛️", "Pine Tree": "🌲", "Brick Wall": "🧱",
77
- "Sphere": "🔵", "Cube": "📦", "Cylinder": "🧴", "Cone": "🍦", "Torus": "🍩",
78
- "Mushroom": "🍄", "Cactus": "🌵", "Campfire": "🔥", "Star": "", "Gem": "💎",
79
- "Tower": "🗼", "Barrier": "🚧", "Fountain": "", "Lantern": "🏮", "Sign Post": ""
 
80
  }
81
- TOOLS_MAP = {"None": "🚫"}
82
- TOOLS_MAP.update({name: emoji for emoji, name in PRIMITIVE_MAP.items()})
83
 
 
84
  for d in [CHAT_DIR, AUDIO_DIR, AUDIO_CACHE_DIR, SAVED_WORLDS_DIR]:
85
  os.makedirs(d, exist_ok=True)
 
 
86
  load_dotenv()
 
 
87
 
88
- # --- Global State (WebSocket Client Tracking Only) ---
89
- clients_lock = threading.Lock()
90
- connected_clients = set() # Holds client_id strings (websocket.id)
 
91
 
92
  # ==============================================================================
93
- # 2. ✨ Utility Functions
94
  # ==============================================================================
 
95
  def get_current_time_str(tz='UTC'):
96
  """Gets formatted timestamp string in specified timezone (default UTC)."""
97
  try:
@@ -100,144 +109,89 @@ def get_current_time_str(tz='UTC'):
100
  except pytz.UnknownTimeZoneError:
101
  now_aware = datetime.now(pytz.utc)
102
  except Exception as e:
103
- print(f"Timezone error ({tz}), using UTC. Error: {e}")
104
- now_aware = datetime.now(pytz.utc)
105
  return now_aware.strftime('%Y%m%d_%H%M%S')
106
 
107
- def clean_filename_part(text, max_len=25):
 
108
  """Cleans a string part for use in a filename."""
109
  if not isinstance(text, str): text = "invalid_name"
110
- text = re.sub(r'\s+', '_', text)
111
- text = re.sub(r'[^\w\-.]', '', text)
112
  return text[:max_len]
113
 
114
  def run_async(async_func, *args, **kwargs):
115
- """Runs an async function safely from a sync context using create_task or asyncio.run."""
116
  try:
117
  loop = asyncio.get_running_loop()
118
  return loop.create_task(async_func(*args, **kwargs))
119
- except RuntimeError:
120
- try: return asyncio.run(async_func(*args, **kwargs))
121
- except Exception as e: print(f"❌ Error run_async new loop: {e}"); return None
122
- except Exception as e: print(f"❌ Error run_async schedule task: {e}"); return None
 
 
 
 
 
 
123
 
124
  def ensure_dir(dir_path):
125
- """Creates directory if it doesn't exist."""
126
- os.makedirs(dir_path, exist_ok=True)
 
 
 
 
127
 
128
  # ==============================================================================
129
- # 3. 🌍 World State Manager (Using st.cache_resource)
130
  # ==============================================================================
131
 
132
- def get_saved_worlds(): # Define this before it's used in load_initial_world_from_file
133
- """Scans the saved worlds directory for world MD files and parses them."""
134
- try:
135
- ensure_dir(SAVED_WORLDS_DIR);
136
- world_files = glob.glob(os.path.join(SAVED_WORLDS_DIR, f"{WORLD_STATE_FILE_MD_PREFIX}*.md"))
137
- parsed_worlds = [parse_world_filename(f) for f in world_files] # parse_world_filename needs to be defined below
138
- parsed_worlds.sort(key=lambda x: x.get('dt') if x.get('dt') else datetime.min.replace(tzinfo=pytz.utc), reverse=True)
139
- return parsed_worlds
140
- except Exception as e: print(f"❌ Error scanning saved worlds: {e}"); st.error(f"Could not scan saved worlds: {e}"); return []
141
 
142
- def parse_world_filename(filename): # Define this before get_saved_worlds uses it indirectly via load_initial
143
  """Extracts info from filename if possible, otherwise returns defaults."""
144
  basename = os.path.basename(filename)
 
145
  if basename.startswith(WORLD_STATE_FILE_MD_PREFIX) and basename.endswith(".md"):
146
- core = basename[len(WORLD_STATE_FILE_MD_PREFIX):-3]; parts = core.split('_')
147
- if len(parts) >= 5 and parts[-3] == "by":
148
- timestamp_str = parts[-2]; username = parts[-4]; world_name = " ".join(parts[:-4]); dt_obj = None
149
- try: dt_obj = pytz.utc.localize(datetime.strptime(timestamp_str, '%Y%m%d_%H%M%S'))
150
- except Exception: dt_obj = None
151
- return {"name": world_name or "Untitled", "user": username, "timestamp": timestamp_str, "dt": dt_obj, "filename": filename}
152
-
153
- # Fallback for unknown format or failed parse
154
- # print(f"Using fallback parsing for filename: {basename}") # Debug log
155
- dt_fallback = None # Initialize on its own line
156
- try: # Start try block on its own line
157
- # Indented block under try
158
- mtime = os.path.getmtime(filename)
159
- dt_fallback = datetime.fromtimestamp(mtime, tz=pytz.utc)
160
- except Exception: # Statement aligned with try
161
- # Indented block under except
162
- pass # Statement on its own line
163
- # Return statement aligned with the start of the fallback logic block
164
- return {"name": basename.replace('.md','').replace(WORLD_STATE_FILE_MD_PREFIX, ''), "user": "Unknown", "timestamp": "Unknown", "dt": dt_fallback, "filename": filename}
165
-
166
-
167
- def load_initial_world_from_file():
168
- """Loads the state from the most recent MD file found."""
169
- print(f"[{time.time():.1f}] ⏳ Attempting to load initial world state from files...")
170
- loaded_state = defaultdict(dict)
171
- saved_worlds = get_saved_worlds()
172
- if saved_worlds:
173
- latest_world_file_basename = os.path.basename(saved_worlds[0]['filename'])
174
- print(f"⏳ Found most recent file: {latest_world_file_basename}")
175
- load_path = os.path.join(SAVED_WORLDS_DIR, latest_world_file_basename)
176
- if os.path.exists(load_path):
177
- try:
178
- with open(load_path, 'r', encoding='utf-8') as f: content = f.read()
179
- json_match = re.search(r"```json\s*(\{[\s\S]*?\})\s*```", content, re.IGNORECASE)
180
- if json_match:
181
- world_data_dict = json.loads(json_match.group(1))
182
- for k, v in world_data_dict.items(): loaded_state[str(k)] = v
183
- print(f"✅ Successfully loaded {len(loaded_state)} objects for initial state.")
184
- # Store the initially loaded file basename in session state here?
185
- st.session_state._initial_world_file_loaded = latest_world_file_basename
186
- else: print("⚠️ No JSON block found in initial file.")
187
- except Exception as e: print(f"❌ Error parsing initial world file {latest_world_file_basename}: {e}")
188
- else: print(f"⚠️ Most recent file {latest_world_file_basename} not found at path {load_path}.")
189
- else: print("🌫️ No saved world files found to load initial state.")
190
- return loaded_state
191
-
192
- @st.cache_resource(ttl=3600) # Cache resource for 1 hour
193
- def get_world_state_manager():
194
- """
195
- Initializes and returns the shared world state dictionary and its lock.
196
- Loads initial state from the most recent file on first creation.
197
- """
198
- print(f"[{time.time():.1f}] --- ✨ Initializing/Retrieving Shared World State Resource ---")
199
- manager = {
200
- "lock": threading.Lock(),
201
- "state": load_initial_world_from_file() # Load initial state here
202
- }
203
- # Set initial current_world_file if state was loaded successfully
204
- if manager["state"]:
205
- saved_worlds = get_saved_worlds()
206
- if saved_worlds:
207
- latest_world_file_basename = os.path.basename(saved_worlds[0]['filename'])
208
- if 'current_world_file' not in st.session_state: # Initialize only if not set
209
- st.session_state.current_world_file = latest_world_file_basename
210
- print(f"🐍 Set initial current_world_file state to: {latest_world_file_basename}")
211
-
212
- return manager
213
-
214
- def get_current_world_state_copy():
215
- """Safely gets a copy of the current world state dictionary."""
216
- manager = get_world_state_manager()
217
- with manager["lock"]:
218
- return dict(manager["state"]) # Return a copy
219
 
220
- # ==============================================================================
221
- # 4. 💾 World State File Handling (Save/Load - Refactored for Cached State)
222
- # ==============================================================================
223
- def generate_world_save_filename(username="User", world_name="World"):
224
- timestamp = get_current_time_str(); clean_user = clean_filename_part(username, 15);
225
- clean_world = clean_filename_part(world_name, 20);
226
- rand_hash = hashlib.md5(str(time.time()).encode()+username.encode()+world_name.encode()).hexdigest()[:4]
227
- return f"{WORLD_STATE_FILE_MD_PREFIX}{clean_world}_by_{clean_user}_{timestamp}_{rand_hash}.md"
228
 
229
  def save_world_state_to_md(target_filename_base):
230
- """Saves the current cached world state to a specific MD file."""
231
- manager = get_world_state_manager() # Get resource
232
  save_path = os.path.join(SAVED_WORLDS_DIR, target_filename_base)
233
- print(f"💾 Acquiring lock to save world state to: {save_path}...")
234
  success = False
235
- with manager["lock"]: # Use resource's lock
236
- world_data_dict = dict(manager["state"]) # Get copy from resource state
237
- print(f"💾 Saving {len(world_data_dict)} objects...")
238
- parsed_info = parse_world_filename(save_path)
239
  timestamp_save = get_current_time_str()
240
- md_content = f"""# World State: {parsed_info['name']} by {parsed_info['user']}
241
  * **File Saved:** {timestamp_save} (UTC)
242
  * **Source Timestamp:** {parsed_info['timestamp']}
243
  * **Objects:** {len(world_data_dict)}
@@ -246,101 +200,111 @@ def save_world_state_to_md(target_filename_base):
246
  {json.dumps(world_data_dict, indent=2)}
247
  ```"""
248
  try:
249
- ensure_dir(SAVED_WORLDS_DIR);
250
  with open(save_path, 'w', encoding='utf-8') as f: f.write(md_content)
251
- print(f"World state saved successfully to {target_filename_base}")
252
  success = True
253
- except Exception as e: print(f"❌ Error saving world state to {save_path}: {e}")
 
254
  return success
255
 
 
256
  def load_world_state_from_md(filename_base):
257
- """Loads world state from MD, updates cached state, returns success bool."""
258
- manager = get_world_state_manager() # Get resource
259
  load_path = os.path.join(SAVED_WORLDS_DIR, filename_base)
260
- print(f"📜 Loading world state from MD file: {load_path}...")
261
  if not os.path.exists(load_path): st.error(f"World file not found: {filename_base}"); return False
 
262
  try:
263
  with open(load_path, 'r', encoding='utf-8') as f: content = f.read()
264
- json_match = re.search(r"```json\s*(\{[\s\S]*?\})\s*```", content, re.IGNORECASE)
265
- if not json_match: st.error(f"Could not find JSON block in {filename_base}"); return False
 
266
  world_data_dict = json.loads(json_match.group(1))
267
 
268
- print(f"⚙️ Acquiring lock to update cached world state from {filename_base}...")
269
- with manager["lock"]: # Use resource's lock
270
- manager["state"].clear() # Clear the existing cached state dict
271
- for k, v in world_data_dict.items(): manager["state"][str(k)] = v # Update with loaded data
272
- loaded_count = len(manager["state"])
273
- print(f"Loaded {loaded_count} objects into cached state. Lock released.")
274
- st.session_state.current_world_file = filename_base # Track loaded file
275
  return True
276
 
277
- except json.JSONDecodeError as e: st.error(f"Invalid JSON in {filename_base}: {e}"); return False
278
  except Exception as e: st.error(f"Error loading world state from {filename_base}: {e}"); st.exception(e); return False
279
 
 
 
 
 
 
 
 
 
 
 
 
280
  # ==============================================================================
281
- # 5. 👤 User State & Session Init
282
  # ==============================================================================
 
283
  def save_username(username):
284
  try:
285
  with open(STATE_FILE, 'w') as f: f.write(username)
286
- except Exception as e: print(f"Failed save username: {e}")
287
 
288
  def load_username():
289
  if os.path.exists(STATE_FILE):
290
  try:
291
  with open(STATE_FILE, 'r') as f: return f.read().strip()
292
- except Exception as e: print(f"Failed load username: {e}")
293
  return None
294
 
295
  def init_session_state():
296
  """Initializes Streamlit session state variables."""
297
  defaults = {
298
  'server_running_flag': False, 'server_instance': None, 'server_task': None,
299
- 'active_connections': defaultdict(dict), # Stores websocket objects by client_id
300
- 'last_chat_update': 0, 'message_input': "", 'audio_cache': {},
301
- 'tts_voice': DEFAULT_TTS_VOICE, 'chat_history': [], 'enable_audio': True,
302
- 'download_link_cache': {}, 'username': None, 'autosend': False,
303
- 'last_message': "",
304
- 'selected_object': 'None',
 
305
  'current_world_file': None, # Track loaded world filename (basename)
306
- 'new_world_name': "MyDreamscape",
307
- 'action_log': deque(maxlen=MAX_ACTION_LOG_SIZE),
308
- # Removed temporary state holders 'world_to_load_data', 'js_object_placed_data'
309
  }
310
  for k, v in defaults.items():
311
- if k not in st.session_state:
312
- if isinstance(v, (deque, dict, list)): st.session_state[k] = v.copy()
313
- else: st.session_state[k] = v
314
- # Ensure complex types are correctly initialized
315
  if not isinstance(st.session_state.active_connections, defaultdict): st.session_state.active_connections = defaultdict(dict)
316
  if not isinstance(st.session_state.chat_history, list): st.session_state.chat_history = []
 
317
  if not isinstance(st.session_state.audio_cache, dict): st.session_state.audio_cache = {}
318
  if not isinstance(st.session_state.download_link_cache, dict): st.session_state.download_link_cache = {}
319
- if not isinstance(st.session_state.action_log, deque): st.session_state.action_log = deque(maxlen=MAX_ACTION_LOG_SIZE)
320
 
321
  # ==============================================================================
322
- # 6. 📝 Action Log Helper
323
  # ==============================================================================
324
- def add_action_log(message, emoji="➡️"):
325
- """Adds a timestamped message with emoji to the session's action log."""
326
- if 'action_log' not in st.session_state or not isinstance(st.session_state.action_log, deque):
327
- st.session_state.action_log = deque(maxlen=MAX_ACTION_LOG_SIZE)
328
- timestamp = datetime.now().strftime("%H:%M:%S")
329
- st.session_state.action_log.appendleft(f"{emoji} [{timestamp}] {message}")
330
 
331
- # ==============================================================================
332
- # 7. 🎧 Audio / TTS / Chat / File Handling Helpers
333
- # ==============================================================================
334
  def clean_text_for_tts(text):
335
  if not isinstance(text, str): return "No text"
336
  text = re.sub(r'\[([^\]]+)\]\([^\)]+\)', r'\1', text); text = re.sub(r'[#*_`!]', '', text)
337
  text = ' '.join(text.split()); return text[:250] or "No text"
 
338
  def create_file(content, username, file_type="md", save_path=None):
339
  if not save_path: filename = generate_filename(content, username, file_type); save_path = os.path.join(MEDIA_DIR, filename)
340
  ensure_dir(os.path.dirname(save_path))
341
  try:
342
- with open(save_path, 'w', encoding='utf-8') as f: f.write(content); return save_path
343
- except Exception as e: print(f" Error creating file {save_path}: {e}"); return None
 
 
 
344
  def get_download_link(file_path, file_type="md"):
345
  if not file_path or not os.path.exists(file_path): basename = os.path.basename(file_path) if file_path else "N/A"; return f"<small>Not found: {basename}</small>"
346
  try: mtime = os.path.getmtime(file_path)
@@ -354,8 +318,10 @@ def get_download_link(file_path, file_type="md"):
354
  basename = os.path.basename(file_path)
355
  link_html = f'<a href="data:{mime_types.get(file_type, "application/octet-stream")};base64,{b64}" download="{basename}" title="Download {basename}">{FILE_EMOJIS.get(file_type, "📄")}</a>'
356
  st.session_state.download_link_cache[cache_key] = link_html
357
- except Exception as e: print(f"Error generating DL link for {file_path}: {e}"); return f"<small>Err</small>"
358
  return st.session_state.download_link_cache.get(cache_key, "<small>CacheErr</small>")
 
 
359
  async def async_edge_tts_generate(text, voice, username):
360
  if not text: return None
361
  cache_key = hashlib.md5(f"{text[:150]}_{voice}".encode()).hexdigest();
@@ -369,107 +335,119 @@ async def async_edge_tts_generate(text, voice, username):
369
  try:
370
  communicate = edge_tts.Communicate(text_cleaned, voice); await communicate.save(save_path);
371
  if os.path.exists(save_path) and os.path.getsize(save_path) > 0: st.session_state.audio_cache[cache_key] = save_path; return save_path
372
- else: print(f"Audio file {save_path} failed generation."); return None
373
- except Exception as e: print(f"Edge TTS Error: {e}"); return None
 
374
  def play_and_download_audio(file_path):
375
  if file_path and os.path.exists(file_path):
376
  try:
377
  st.audio(file_path)
378
  file_type = file_path.split('.')[-1]
379
  st.markdown(get_download_link(file_path, file_type), unsafe_allow_html=True)
380
- except Exception as e: st.error(f"Audio display error for {os.path.basename(file_path)}: {e}")
 
 
381
  async def save_chat_entry(username, message, voice, is_markdown=False):
382
  if not message.strip(): return None, None
383
  timestamp_str = get_current_time_str();
384
  entry = f"[{timestamp_str}] {username} ({voice}): {message}" if not is_markdown else f"[{timestamp_str}] {username} ({voice}):\n```markdown\n{message}\n```"
385
  md_filename_base = generate_filename(message, username, "md"); md_file_path = os.path.join(CHAT_DIR, md_filename_base);
386
- md_file = create_file(entry, username, "md", save_path=md_file_path)
387
  if 'chat_history' not in st.session_state: st.session_state.chat_history = [];
388
- st.session_state.chat_history.append(entry)
389
  audio_file = None;
390
  if st.session_state.get('enable_audio', True):
391
- tts_message = message
392
  audio_file = await async_edge_tts_generate(tts_message, voice, username)
393
  return md_file, audio_file
 
394
  async def load_chat_history():
395
- if 'chat_history' not in st.session_state: st.session_state.chat_history = []
396
- if not st.session_state.chat_history:
397
- ensure_dir(CHAT_DIR); print("📜 Loading chat history from files...")
398
- chat_files = sorted(glob.glob(os.path.join(CHAT_DIR, "*.md")), key=os.path.getmtime); loaded_count = 0
399
- temp_history = []
400
- for f_path in chat_files:
401
- try:
402
- with open(f_path, 'r', encoding='utf-8') as file: temp_history.append(file.read().strip()); loaded_count += 1
403
- except Exception as e: print(f"❌ Err read chat {f_path}: {e}")
404
- st.session_state.chat_history = temp_history
405
- print(f"✅ Loaded {loaded_count} chat entries from files.")
406
- return st.session_state.chat_history
 
 
 
407
  def create_zip_of_files(files_to_zip, prefix="Archive"):
408
- if not files_to_zip: st.warning("💨 Nothing to gather into an archive."); return None
409
  timestamp = format_timestamp_prefix(f"Zip_{prefix}"); zip_name = f"{prefix}_{timestamp}.zip"
410
  try:
411
- print(f"📦 Creating zip archive: {zip_name}...");
412
  with zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) as z:
413
  for f in files_to_zip:
414
- if os.path.exists(f): z.write(f, os.path.basename(f))
415
- else: print(f"💨 Skip zip missing file: {f}")
416
- print("Zip archive created successfully."); st.success(f"Created {zip_name}"); return zip_name
417
- except Exception as e: print(f"Zip creation failed: {e}"); st.error(f"Zip creation failed: {e}"); return None
 
418
  def delete_files(file_patterns, exclude_files=None):
 
419
  protected = [STATE_FILE, "app.py", "index.html", "requirements.txt", "README.md"]
420
- current_world_base = st.session_state.get('current_world_file')
421
- if current_world_base: protected.append(current_world_base)
422
  if exclude_files: protected.extend(exclude_files)
 
423
  deleted_count = 0; errors = 0
424
  for pattern in file_patterns:
425
- pattern_path = pattern
426
- print(f"🗑️ Attempting to delete files matching: {pattern_path}")
427
  try:
428
  files_to_delete = glob.glob(pattern_path)
429
- if not files_to_delete: print(f"💨 No files found for pattern: {pattern}"); continue
430
  for f_path in files_to_delete:
431
  basename = os.path.basename(f_path)
432
  if os.path.isfile(f_path) and basename not in protected:
433
- try: os.remove(f_path); print(f"🗑️ Deleted: {f_path}"); deleted_count += 1
434
- except Exception as e: print(f"Failed delete {f_path}: {e}"); errors += 1
435
- #else: print(f"🚫 Skipping protected/directory: {f_path}")
436
- except Exception as glob_e: print(f"Error matching pattern {pattern}: {glob_e}"); errors += 1
437
- msg = f"✅ Successfully deleted {deleted_count} files." if errors == 0 and deleted_count > 0 else f"Deleted {deleted_count} files."
438
  if errors > 0: msg += f" Encountered {errors} errors."; st.warning(msg)
439
  elif deleted_count > 0: st.success(msg)
440
- else: st.info("💨 No matching unprotected files found to delete.")
441
  st.session_state['download_link_cache'] = {}; st.session_state['audio_cache'] = {}
 
 
 
442
  async def save_pasted_image(image, username):
443
  if not image: return None
444
- try: img_hash = hashlib.md5(image.tobytes()).hexdigest()[:8]; timestamp = format_timestamp_prefix(username); filename = f"{timestamp}_pasted_{img_hash}.png"; filepath = os.path.join(MEDIA_DIR, filename); image.save(filepath, "PNG"); print(f"🖼️ Pasted image saved: {filepath}"); return filepath
445
- except Exception as e: print(f"❌ Failed image save: {e}"); return None
 
 
 
446
  def paste_image_component():
447
  pasted_img = None; img_type = None
448
- paste_input_value = st.text_area("📋 Paste Image Data Here", key="paste_input_area", height=50, value=st.session_state.get('paste_image_base64_input', ""), help="Paste image data directly (e.g., from clipboard)")
449
- if st.button("🖼️ Process Pasted Image", key="process_paste_button"):
450
- st.session_state.paste_image_base64_input = paste_input_value
451
- if paste_input_value and paste_input_value.startswith('data:image'):
452
  try:
453
- mime_type = paste_input_value.split(';')[0].split(':')[1]; base64_str = paste_input_value.split(',')[1]; img_bytes = base64.b64decode(base64_str); pasted_img = Image.open(io.BytesIO(img_bytes)); img_type = mime_type.split('/')[1]
454
- st.image(pasted_img, caption=f"🖼️ Pasted ({img_type.upper()})", width=150); st.session_state.paste_image_base64 = base64_str
455
- st.session_state.paste_image_base64_input = ""
456
- st.rerun()
457
- except ImportError: st.error("⚠️ Pillow library needed.")
458
- except Exception as e: st.error(f"❌ Img decode err: {e}"); st.session_state.paste_image_base64 = ""; st.session_state.paste_image_base64_input = paste_input_value
459
- else: st.warning("⚠️ No valid image data pasted."); st.session_state.paste_image_base64 = ""; st.session_state.paste_image_base64_input = paste_input_value
460
- processed_b64 = st.session_state.get('paste_image_base64', '')
461
- if processed_b64:
462
- try: img_bytes = base64.b64decode(processed_b64); return Image.open(io.BytesIO(img_bytes))
463
- except Exception: return None
464
- return None
465
  # --- PDF Processing ---
466
  class AudioProcessor:
467
  def __init__(self): self.cache_dir=AUDIO_CACHE_DIR; ensure_dir(self.cache_dir); self.metadata=json.load(open(f"{self.cache_dir}/metadata.json", 'r')) if os.path.exists(f"{self.cache_dir}/metadata.json") else {}
468
  def _save_metadata(self):
469
  try:
470
  with open(f"{self.cache_dir}/metadata.json", 'w') as f: json.dump(self.metadata, f, indent=2)
471
- except Exception as e: print(f"Failed metadata save: {e}")
472
- async def create_audio(self, text, voice=DEFAULT_TTS_VOICE):
473
  cache_key=hashlib.md5(f"{text[:150]}:{voice}".encode()).hexdigest(); cache_path=os.path.join(self.cache_dir, f"{cache_key}.mp3");
474
  if cache_key in self.metadata and os.path.exists(cache_path): return cache_path
475
  text_cleaned=clean_text_for_tts(text);
@@ -479,403 +457,298 @@ class AudioProcessor:
479
  communicate=edge_tts.Communicate(text_cleaned,voice); await communicate.save(cache_path)
480
  if os.path.exists(cache_path) and os.path.getsize(cache_path) > 0: self.metadata[cache_key]={'timestamp': datetime.now().isoformat(), 'text_length': len(text_cleaned), 'voice': voice}; self._save_metadata(); return cache_path
481
  else: return None
482
- except Exception as e: print(f"TTS Create Audio Error: {e}"); return None
483
 
484
  def process_pdf_tab(pdf_file, max_pages, voice):
485
- st.subheader("📜 PDF Processing Results")
486
- if pdf_file is None: st.info("⬆️ Upload a PDF file and click 'Process PDF' to begin."); return
487
  audio_processor = AudioProcessor()
488
  try:
489
- reader=PdfReader(pdf_file);
490
- if reader.is_encrypted: st.warning("🔒 PDF is encrypted."); return
491
- total_pages_in_pdf = len(reader.pages); pages_to_process = min(total_pages_in_pdf, max_pages);
492
- st.write(f"Processing first {pages_to_process} of {total_pages_in_pdf} pages from '{pdf_file.name}'...")
493
  texts, audios={}, {}; page_threads = []; results_lock = threading.Lock()
494
 
495
  def process_page_sync(page_num, page_text):
496
  async def run_async_audio(): return await audio_processor.create_audio(page_text, voice)
497
- try: audio_path = asyncio.run(run_async_audio()) # Use asyncio.run in thread
 
 
 
 
 
 
 
498
  if audio_path:
499
- with results_lock: audios[page_num] = audio_path
500
- except Exception as page_e: print(f"Err process page {page_num+1}: {page_e}")
501
 
502
  # Start threads
503
- for i in range(pages_to_process):
504
- try: # Start try block for page processing
505
- page = reader.pages[i]
506
- text = page.extract_text() # Attempt text extraction
507
- if text and text.strip(): # Check extracted text
508
- texts[i]=text # Store text
509
- thread = threading.Thread(target=process_page_sync, args=(i, text)) # Create thread
510
- page_threads.append(thread) # Append thread
511
- thread.start() # Start thread
512
- else: # Handle empty extraction
513
- texts[i] = "[📄 No text extracted or page empty]"
514
- # Correctly indented except block
515
- except Exception as extract_e:
516
- texts[i] = f"[❌ Error extract: {extract_e}]" # Store error message
517
- print(f"Error page {i+1} extract: {extract_e}") # Log error
518
 
519
  # Wait for threads and display progress
520
- progress_bar = st.progress(0.0, text=" Transmuting pages to sound...")
521
- total_threads = len(page_threads); start_join_time = time.time()
 
522
  while any(t.is_alive() for t in page_threads):
523
- completed_threads = total_threads - sum(t.is_alive() for t in page_threads); progress = completed_threads / total_threads if total_threads > 0 else 1.0
524
- progress_bar.progress(min(progress, 1.0), text=f"✨ Processed {completed_threads}/{total_threads} pages...")
525
- if time.time() - start_join_time > 600: print("⌛ PDF processing timed out."); st.warning("Processing timed out."); break
 
526
  time.sleep(0.5)
527
- progress_bar.progress(1.0, text="Processing complete.")
528
 
529
  # Display results
530
- st.write("🎶 Results:")
531
- for i in range(pages_to_process):
532
  with st.expander(f"Page {i+1}"):
533
- st.markdown(texts.get(i, "[Error getting text]"))
534
  audio_file = audios.get(i)
535
  if audio_file: play_and_download_audio(audio_file)
536
- else:
537
- page_text = texts.get(i,"")
538
- if page_text.strip() and not page_text.startswith("["): st.caption("🔇 Audio generation failed or timed out.")
539
-
540
- except ImportError: st.error("⚠️ PyPDF2 library needed.")
541
- except Exception as pdf_e: st.error(f"❌ Error reading PDF '{pdf_file.name}': {pdf_e}"); st.exception(pdf_e)
542
 
 
543
 
544
  # ==============================================================================
545
- # 8. 🕸️ WebSocket Server Logic
546
  # ==============================================================================
547
 
548
  async def register_client(websocket):
549
- """Adds client to tracking structures, ensuring thread safety."""
550
- client_id = str(websocket.id);
551
- with clients_lock: # Use the dedicated lock for client structures
552
- connected_clients.add(client_id);
553
- if 'active_connections' not in st.session_state: st.session_state.active_connections = defaultdict(dict);
554
- st.session_state.active_connections[client_id] = websocket;
555
- print(f"✅ Client registered: {client_id}. Total: {len(connected_clients)}")
556
 
557
  async def unregister_client(websocket):
558
- """Removes client from tracking structures, ensuring thread safety."""
559
- client_id = str(websocket.id);
560
- with clients_lock:
561
- connected_clients.discard(client_id);
562
- if 'active_connections' in st.session_state: st.session_state.active_connections.pop(client_id, None);
563
- print(f"🔌 Client unregistered: {client_id}. Remaining: {len(connected_clients)}")
564
 
565
  async def send_safely(websocket, message, client_id):
566
- """Wrapper to send message and handle potential connection errors."""
567
- try:
568
- await websocket.send(message)
569
- except websockets.ConnectionClosed:
570
- print(f"❌ WS Send failed (Closed) client {client_id}")
571
- raise # Re-raise to be caught by gather
572
- except RuntimeError as e:
573
- print(f"❌ WS Send failed (Runtime {e}) client {client_id}")
574
- raise
575
- except Exception as e:
576
- print(f"❌ WS Send failed (Other {e}) client {client_id}")
577
- raise
578
 
579
  async def broadcast_message(message, exclude_id=None):
580
- """Sends a message to all connected clients except the excluded one."""
581
- # Create local copies under lock for thread safety
582
- with clients_lock:
583
- if not connected_clients:
584
- return
585
- current_client_ids = list(connected_clients)
586
- # Ensure active_connections exists and make a copy
587
- if 'active_connections' in st.session_state:
588
- active_connections_copy = st.session_state.active_connections.copy()
589
- else:
590
- active_connections_copy = {} # Should not happen if init runs first
591
-
592
- tasks = []
593
  for client_id in current_client_ids:
594
- if client_id == exclude_id:
595
- continue
596
- websocket = active_connections_copy.get(client_id) # Use copy
597
- if websocket:
598
- tasks.append(asyncio.create_task(send_safely(websocket, message, client_id)))
599
-
600
- if tasks:
601
- await asyncio.gather(*tasks, return_exceptions=True) # Wait and ignore errors here
602
 
603
  async def broadcast_world_update():
604
- """Broadcasts the current world state (from cache) to all clients."""
605
- world_state_copy = get_current_world_state_copy() # Safely get a copy
606
- update_msg = json.dumps({"type": "initial_state", "payload": world_state_copy})
607
- print(f"📡 Broadcasting full world update ({len(world_state_copy)} objects)...")
608
- await broadcast_message(update_msg) # Send to all connected clients
609
 
610
  async def websocket_handler(websocket, path):
611
- """Handles WebSocket connections and messages using cached world state."""
612
  await register_client(websocket); client_id = str(websocket.id);
613
- # Use username from main session state - might require adjustments if session state isn't thread-safe across contexts
614
  username = st.session_state.get('username', f"User_{client_id[:4]}")
615
-
616
  try: # Send initial state
617
- initial_state_payload = get_current_world_state_copy() # Get state using cached helper
618
- initial_state_msg = json.dumps({"type": "initial_state", "payload": initial_state_payload});
619
- await websocket.send(initial_state_msg)
620
- print(f"✅ Sent initial state ({len(initial_state_payload)} objs) to {client_id}")
621
- # Announce join after state sent
622
  await broadcast_message(json.dumps({"type": "user_join", "payload": {"username": username, "id": client_id}}), exclude_id=client_id)
623
- except Exception as e: print(f"Error during initial phase {client_id}: {e}")
624
 
625
- try: # Message processing loop
626
  async for message in websocket:
627
  try:
628
  data = json.loads(message); msg_type = data.get("type"); payload = data.get("payload", {});
629
- sender_username = payload.get("username", username) # Username sent from client
630
-
631
- # --- Handle Different Message Types ---
632
- manager = get_world_state_manager() # Get state manager for world updates
633
 
634
  if msg_type == "chat_message":
635
- chat_text = payload.get('message', ''); voice = payload.get('voice', FUN_USERNAMES.get(sender_username, DEFAULT_TTS_VOICE));
636
- print(f"💬 WS Recv Chat from {sender_username}: {chat_text[:30]}...")
637
- run_async(save_chat_entry, sender_username, chat_text, voice) # Save locally async
638
- await broadcast_message(message, exclude_id=client_id) # Broadcast chat
639
 
640
  elif msg_type == "place_object":
641
  obj_data = payload.get("object_data");
642
  if obj_data and 'obj_id' in obj_data and 'type' in obj_data:
643
- print(f"➕ WS Recv Place from {sender_username}: {obj_data['type']} ({obj_data['obj_id']})")
644
- with manager["lock"]: manager["state"][obj_data['obj_id']] = obj_data # Update cached state
645
- # Broadcast placement to others
646
  broadcast_payload = json.dumps({"type": "object_placed", "payload": {"object_data": obj_data, "username": sender_username}});
647
  await broadcast_message(broadcast_payload, exclude_id=client_id)
648
- # Use run_async for session state update if needed from thread
649
- # run_async(lambda: add_action_log(f"Placed {obj_data['type']} ({obj_data['obj_id'][:6]}) by {sender_username}", TOOLS_MAP.get(obj_data['type'], '❓')))
650
- else: print(f"⚠️ WS Invalid place_object payload: {payload}")
651
 
652
  elif msg_type == "delete_object":
653
  obj_id = payload.get("obj_id"); removed = False
654
  if obj_id:
655
- print(f"➖ WS Recv Delete from {sender_username}: {obj_id}")
656
- with manager["lock"]:
657
- if obj_id in manager["state"]: del manager["state"][obj_id]; removed = True
658
  if removed:
659
  broadcast_payload = json.dumps({"type": "object_deleted", "payload": {"obj_id": obj_id, "username": sender_username}});
660
  await broadcast_message(broadcast_payload, exclude_id=client_id)
661
- # run_async(lambda: add_action_log(f"Deleted obj ({obj_id[:6]}) by {sender_username}", "🗑️"))
662
- else: print(f"⚠️ WS Invalid delete_object payload: {payload}")
663
 
664
  elif msg_type == "player_position":
665
  pos_data = payload.get("position"); rot_data = payload.get("rotation")
666
  if pos_data:
667
  broadcast_payload = json.dumps({"type": "player_moved", "payload": {"username": sender_username, "id": client_id, "position": pos_data, "rotation": rot_data}});
668
- await broadcast_message(broadcast_payload, exclude_id=client_id) # Broadcast movement
669
-
670
- elif msg_type == "ping": await websocket.send(json.dumps({"type": "pong"}))
671
- else: print(f"⚠️ WS Recv unknown type from {client_id}: {msg_type}")
672
 
673
- except json.JSONDecodeError: print(f"⚠️ WS Invalid JSON from {client_id}: {message[:100]}...")
674
- except Exception as e: print(f"WS Error processing msg from {client_id}: {e}")
675
- except websockets.ConnectionClosed: print(f"🔌 WS Client disconnected: {client_id} ({username})")
676
- except Exception as e: print(f"WS Unexpected handler error {client_id}: {e}")
677
  finally:
678
  await broadcast_message(json.dumps({"type": "user_leave", "payload": {"username": username, "id": client_id}}), exclude_id=client_id);
679
- await unregister_client(websocket) # Cleanup
680
 
681
 
682
  async def run_websocket_server():
683
- """Coroutine to run the WebSocket server."""
684
  if st.session_state.get('server_running_flag', False): return
685
- st.session_state['server_running_flag'] = True; print("⚙️ Attempting start WS server 0.0.0.0:8765...")
686
  stop_event = asyncio.Event(); st.session_state['websocket_stop_event'] = stop_event
687
  server = None
688
  try:
689
  server = await websockets.serve(websocket_handler, "0.0.0.0", 8765); st.session_state['server_instance'] = server
690
- print(f"WS server started: {server.sockets[0].getsockname()}. Waiting for stop signal...")
691
  await stop_event.wait()
692
- except OSError as e: print(f"### FAILED START WS SERVER: {e}"); st.session_state['server_running_flag'] = False;
693
- except Exception as e: print(f"### UNEXPECTED WS SERVER ERROR: {e}"); st.session_state['server_running_flag'] = False;
694
  finally:
695
- print("⚙️ WS server task finishing...");
696
- if server: server.close(); await server.wait_closed(); print("WS server closed.")
697
  st.session_state['server_running_flag'] = False; st.session_state['server_instance'] = None; st.session_state['websocket_stop_event'] = None
698
 
699
  def start_websocket_server_thread():
700
- """Starts the WebSocket server in a separate thread if not already running."""
701
  if st.session_state.get('server_task') and st.session_state.server_task.is_alive(): return
702
  if st.session_state.get('server_running_flag', False): return
703
- print("⚙️ Creating/starting new server thread.");
704
- def run_loop(): # Wrapper to manage event loop in thread
705
  loop = None
706
  try: loop = asyncio.get_running_loop()
707
  except RuntimeError: loop = asyncio.new_event_loop(); asyncio.set_event_loop(loop)
708
  try: loop.run_until_complete(run_websocket_server())
709
  finally:
710
  if loop and not loop.is_closed():
711
- tasks = asyncio.all_tasks(loop);
712
- if tasks:
713
- for task in tasks: task.cancel()
714
- try: loop.run_until_complete(asyncio.gather(*tasks, return_exceptions=True))
715
- except asyncio.CancelledError: pass
716
- loop.close(); print("⚙️ Server thread loop closed.")
717
  st.session_state.server_task = threading.Thread(target=run_loop, daemon=True); st.session_state.server_task.start(); time.sleep(1.5)
718
- if not st.session_state.server_task.is_alive(): print("### Server thread failed to stay alive!")
 
719
 
720
  # ==============================================================================
721
- # 9. 🎨 Streamlit UI Layout Functions
722
  # ==============================================================================
723
 
724
  def render_sidebar():
725
  """Renders the Streamlit sidebar contents."""
726
  with st.sidebar:
727
- # 1. World Management
728
- st.header("1. 💾 World Management")
729
- st.caption("💾 Save the current view or ✨ load a past creation.")
730
-
731
- # World Save Button
732
- current_file = st.session_state.get('current_world_file')
733
- current_world_name = "Live State"
734
- default_save_name = st.session_state.get('new_world_name', 'MyDreamscape')
735
- if current_file:
736
- try: parsed = parse_world_filename(os.path.join(SAVED_WORLDS_DIR, current_file)); current_world_name = parsed.get("name", current_file)
737
- except Exception: pass
738
- default_save_name = current_world_name
739
-
740
- world_save_name = st.text_input("World Name:", key="world_save_name_input", value=default_save_name, help="Enter name to save.")
741
-
742
- if st.button("💾 Save Current World View", key="sidebar_save_world"):
743
- if not world_save_name.strip():
744
- st.warning("⚠️ Please enter a World Name.")
745
- else:
746
- filename_to_save = ""; is_overwrite = False
747
- if current_file:
748
- try:
749
- parsed_current = parse_world_filename(os.path.join(SAVED_WORLDS_DIR, current_file))
750
- if world_save_name == parsed_current.get('name', ''): filename_to_save = current_file; is_overwrite = True
751
- except Exception: pass
752
- if not filename_to_save: filename_to_save = generate_world_save_filename(st.session_state.username, world_save_name)
753
-
754
- op_text = f"Overwriting {filename_to_save}..." if is_overwrite else f"Saving as {filename_to_save}..."
755
- with st.spinner(op_text):
756
- # Save uses the current state from the cached resource
757
- if save_world_state_to_md(filename_to_save):
758
- action = "Overwritten" if is_overwrite else "Saved new"
759
- st.success(f"World {action}: {filename_to_save}"); add_action_log(f"Saved world: {filename_to_save}", emoji="💾")
760
- st.session_state.current_world_file = filename_to_save # Track saved file
761
- st.rerun()
762
- else: st.error("❌ Failed to save world state.")
763
-
764
- # --- World Load ---
765
- st.markdown("---")
766
- st.header("2. 📂 Load World")
767
- st.caption("📜 Unfurl a previously woven dreamscape.")
768
- saved_worlds = get_saved_worlds()
769
 
770
- if not saved_worlds: st.caption("🌫️ The archives are empty.")
771
- else:
772
- cols_header = st.columns([4, 1, 1]);
773
- with cols_header[0]: st.write("**Name** (User, Time)")
774
- with cols_header[1]: st.write("**Load**")
775
- with cols_header[2]: st.write("**DL**")
776
-
777
- list_container = st.container(height=300 if len(saved_worlds) > 7 else None)
778
- with list_container:
779
- for world_info in saved_worlds:
780
- f_basename = os.path.basename(world_info['filename']); f_fullpath = os.path.join(SAVED_WORLDS_DIR, f_basename);
781
- display_name = world_info.get('name', f_basename); user = world_info.get('user', 'N/A'); timestamp = world_info.get('timestamp', 'N/A')
782
- display_text = f"{display_name} ({user}, {timestamp})"
783
- col1, col2, col3 = st.columns([4, 1, 1])
784
- with col1: st.write(f"<small>{display_text}</small>", unsafe_allow_html=True)
785
- with col2:
786
- is_current = (st.session_state.get('current_world_file') == f_basename)
787
- btn_load = st.button("✨", key=f"load_{f_basename}", help=f"Load {f_basename}", disabled=is_current)
788
- with col3: st.markdown(get_download_link(f_fullpath, "md"), unsafe_allow_html=True)
789
-
790
- if btn_load:
791
- print(f"🖱️ Load button clicked for: {f_basename}")
792
- with st.spinner(f"Loading {f_basename}..."):
793
- # load_world_state_from_md updates the cached resource
794
- if load_world_state_from_md(f_basename):
795
- run_async(broadcast_world_update) # Broadcast the newly loaded state
796
- add_action_log(f"Loading world: {f_basename}", emoji="📂")
797
- st.toast("World loaded!", icon="✅")
798
- st.rerun() # Rerun to update UI
799
- else: st.error(f" Failed to load world file: {f_basename}")
800
-
801
- # --- Build Tools ---
802
- st.markdown("---")
803
- st.header("3. 🛠️ Build Tools")
804
- st.caption("Select your creative instrument.")
805
- tool_options = list(TOOLS_MAP.keys())
806
- current_tool_name = st.session_state.get('selected_object', 'None')
807
- try: tool_index = tool_options.index(current_tool_name)
808
- except ValueError: tool_index = 0
809
-
810
- selected_tool = st.radio(
811
- "Select Tool:", options=tool_options, index=tool_index,
812
- format_func=lambda name: f"{TOOLS_MAP.get(name, '')} {name}",
813
- key="tool_selector_radio", horizontal=True, label_visibility="collapsed"
814
- )
815
- if selected_tool != current_tool_name:
816
- st.session_state.selected_object = selected_tool
817
- tool_emoji = TOOLS_MAP.get(selected_tool, '❓')
818
- add_action_log(f"Tool selected: {selected_tool}", emoji=tool_emoji)
819
- try: # Use streamlit_js_eval, not sync
820
- streamlit_js_eval(js_code=f"updateSelectedObjectType({json.dumps(selected_tool)});", key=f"update_tool_js_{selected_tool}")
821
- except Exception as e: print(f"❌ JS tool update error: {e}")
822
- st.rerun()
823
 
824
 
825
- # --- Action Log ---
826
  st.markdown("---")
827
- st.header("4. 📝 Action Log")
828
- st.caption("📜 A chronicle of your recent creative acts.")
829
- log_container = st.container(height=200)
830
- with log_container:
831
- log_entries = st.session_state.get('action_log', [])
832
- if log_entries: st.code('\n'.join(log_entries), language="log")
833
- else: st.caption("🌬️ The log awaits your first action...")
834
-
835
-
836
- # --- Voice/User ---
 
 
 
837
  st.markdown("---")
838
- st.header("5. 👤 Voice & User")
839
- st.caption("🎭 Choose your persona in this realm.")
840
- current_username = st.session_state.get('username', "DefaultUser")
841
- username_options = list(FUN_USERNAMES.keys()) if FUN_USERNAMES else [current_username]
842
- current_index = 0;
843
- # Corrected try-except for finding index
844
- try:
845
- if current_username in username_options: current_index = username_options.index(current_username)
846
- except ValueError: pass # Keep index 0
847
 
 
 
 
 
 
 
848
  new_username = st.selectbox("Change Name/Voice", options=username_options, index=current_index, key="username_select", format_func=lambda x: x.split(" ")[0])
849
- if new_username != st.session_state.get('username'):
850
  old_username = st.session_state.username
851
  change_msg = json.dumps({"type":"user_rename", "payload": {"old_username": old_username, "new_username": new_username}})
852
- run_async(broadcast_message, change_msg) # Broadcast name change
853
- st.session_state.username = new_username; st.session_state.tts_voice = FUN_USERNAMES.get(new_username, DEFAULT_TTS_VOICE); save_username(st.session_state.username)
854
- add_action_log(f"Persona changed to {new_username}", emoji="🎭")
855
  st.rerun()
856
- st.session_state['enable_audio'] = st.toggle("🔊 Enable TTS Audio", value=st.session_state.get('enable_audio', True), help="Generate audio for chat messages?")
857
 
858
 
859
  def render_main_content():
860
  """Renders the main content area with tabs."""
861
  st.title(f"{Site_Name} - User: {st.session_state.username}")
862
 
863
- # NOTE: World loading is handled by button click -> load_world_state_from_md -> broadcast -> rerun
864
- # Initial world state is sent by WS handler on connect
865
-
866
- # Define Tabs
867
  tab_world, tab_chat, tab_pdf, tab_files = st.tabs(["🏗️ World Builder", "🗣️ Chat", "📚 PDF Tools", "📂 Files & Settings"])
868
 
869
  # --- World Builder Tab ---
870
  with tab_world:
871
- st.header("🌌 Shared Dreamscape")
872
- st.caption(" Weave reality with sidebar tools. Changes shared live! Use sidebar to save/load.")
873
  current_file_basename = st.session_state.get('current_world_file', None)
874
  if current_file_basename:
875
- full_path = os.path.join(SAVED_WORLDS_DIR, current_file_basename)
876
- if os.path.exists(full_path): parsed = parse_world_filename(full_path); st.info(f"🌠 Viewing: **{parsed['name']}** (`{current_file_basename}`)")
877
- else: st.warning(f"⚠️ Loaded file '{current_file_basename}' missing."); st.session_state.current_world_file = None
878
- else: st.info("☁️ Live State Active (Save to persist)")
879
 
880
  # Embed HTML Component
881
  html_file_path = 'index.html'
@@ -885,174 +758,159 @@ def render_main_content():
885
  try: # Get WS URL (Best effort)
886
  from streamlit.web.server.server import Server
887
  session_info = Server.get_current()._get_session_info(st.runtime.scriptrunner.get_script_run_ctx().session_id)
888
- host_attr = getattr(session_info.ws.stream.request, 'host', getattr(getattr(session_info, 'client', None), 'request', None))
889
- if host_attr: server_host = host_attr.host.split(':')[0]; ws_url = f"ws://{server_host}:8765"
890
- else: raise AttributeError("Host attribute not found")
891
- except Exception as e: print(f"⚠️ WS URL detection failed ({e}), using localhost.")
892
 
893
- # Initial world state sent via WS, only need constants/UI state here
894
  js_injection_script = f"""<script>
895
  window.USERNAME = {json.dumps(st.session_state.username)};
896
- window.WEBSOCKET_URL = {json.dumps(ws_url)}; // Needed by JS to connect
897
  window.SELECTED_OBJECT_TYPE = {json.dumps(st.session_state.selected_object)};
898
  window.PLOT_WIDTH = {json.dumps(PLOT_WIDTH)};
899
  window.PLOT_DEPTH = {json.dumps(PLOT_DEPTH)};
900
- // window.INITIAL_WORLD_OBJECTS not needed here, comes via WebSocket
901
- console.log("🐍 Streamlit State Injected:", {{ username: window.USERNAME, websocketUrl: window.WEBSOCKET_URL, selectedObject: window.SELECTED_OBJECT_TYPE }});
902
  </script>"""
903
  html_content_with_state = html_template.replace('</head>', js_injection_script + '\n</head>', 1)
904
  components.html(html_content_with_state, height=700, scrolling=False)
905
- except FileNotFoundError: st.error(f"CRITICAL ERROR: Could not find '{html_file_path}'.")
906
- except Exception as e: st.error(f"Error loading 3D component: {e}"); st.exception(e)
907
 
908
  # --- Chat Tab ---
909
  with tab_chat:
910
- st.header(f"💬 Whispers in the Void")
911
- chat_history_list = st.session_state.get('chat_history', [])
912
- if not chat_history_list: chat_history_list = asyncio.run(load_chat_history())
913
  chat_container = st.container(height=500)
914
  with chat_container:
915
- if chat_history_list: st.markdown("----\n".join(reversed(chat_history_list[-50:])))
916
- else: st.caption("🌬️ Silence reigns...")
917
 
918
- def clear_chat_input_callback(): st.session_state.message_input = ""
919
  message_value = st.text_input("Your Message:", key="message_input", label_visibility="collapsed")
920
- send_button_clicked = st.button("✉️ Send Message", key="send_chat_button", on_click=clear_chat_input_callback)
 
921
 
922
- if send_button_clicked:
923
  message_to_send = message_value
924
  if message_to_send.strip() and message_to_send != st.session_state.get('last_message', ''):
925
  st.session_state.last_message = message_to_send
926
- voice = st.session_state.get('tts_voice', DEFAULT_TTS_VOICE)
927
  ws_message = json.dumps({"type": "chat_message", "payload": {"username": st.session_state.username, "message": message_to_send, "voice": voice}})
928
- run_async(broadcast_message, ws_message) # Broadcast Chat via WS
929
- run_async(save_chat_entry, st.session_state.username, message_to_send, voice) # Save async
930
- add_action_log(f"Sent chat: {message_to_send[:20]}...", emoji="💬")
931
- # Rerun is handled implicitly by button + on_click
932
  elif send_button_clicked: st.toast("Message empty or same as last.")
 
933
 
934
  # --- PDF Tab ---
935
  with tab_pdf:
936
- st.header("📚 Tome Translator")
937
- st.caption("🔊 Give voice to the silent knowledge within PDF scrolls.")
938
- pdf_file = st.file_uploader("Upload PDF Scroll", type="pdf", key="pdf_upload")
939
- max_pages = st.slider('Max Pages to Animate', 1, 50, 10, key="pdf_pages")
940
  if pdf_file:
941
- if st.button("🎙️ Animate PDF to Audio", key="process_pdf_button"):
942
- with st.spinner(" Transcribing ancient glyphs to sound..."):
943
- process_pdf_tab(pdf_file, max_pages, st.session_state.tts_voice)
944
 
945
  # --- Files & Settings Tab ---
946
  with tab_files:
947
- st.header("📂 Archives & Settings")
948
- st.caption("⚙️ Manage saved scrolls and application settings.")
949
-
950
- st.subheader("💾 World Scroll Management")
951
  current_file_basename = st.session_state.get('current_world_file', None)
952
 
953
- # Button to save changes to the currently loaded file
954
  if current_file_basename:
955
- full_path = os.path.join(SAVED_WORLDS_DIR, current_file_basename)
956
- save_label = f"Save Changes to '{current_file_basename}'"
957
- if os.path.exists(full_path): parsed = parse_world_filename(full_path); save_label = f"💾 Save Changes to '{parsed['name']}'"
958
- if st.button(save_label, key="save_current_world_files", help=f"Overwrite '{current_file_basename}'"):
959
- if not os.path.exists(full_path): st.error(f" Cannot save, file missing.")
960
- else:
961
- with st.spinner(f"Saving changes to {current_file_basename}..."):
962
- # Save the state currently held in the cached resource
963
- if save_world_state_to_md(current_file_basename):
964
- st.success(" Current world saved!"); add_action_log(f"Saved world: {current_file_basename}", emoji="💾")
965
- else: st.error(" Failed to save world state.")
966
- else: st.info("➡️ Load a world from the sidebar to enable 'Save Changes'.")
967
-
968
- st.subheader("✨ Save As New Scroll")
969
- new_name_files = st.text_input("New Scroll Name:", key="new_world_name_files_tab", value=st.session_state.get('new_world_name', 'MyDreamscape'))
970
- if st.button("💾 Save Current View as New Scroll", key="save_new_version_files"):
971
  if new_name_files.strip():
972
- with st.spinner(f"Saving new version '{new_name_files}'..."):
973
- new_filename_base = generate_world_save_filename(st.session_state.username, new_name_files)
974
- # Save the state currently held in the cached resource to a NEW file
975
- if save_world_state_to_md(new_filename_base):
976
- st.success(f"✅ Saved as {new_filename_base}")
977
- st.session_state.current_world_file = new_filename_base; st.session_state.new_world_name = "MyDreamscape";
978
- add_action_log(f"Saved new world: {new_filename_base}", emoji="✨")
979
- st.rerun() # Rerun to update sidebar list
980
- else: st.error("❌ Failed to save new version.")
981
- else: st.warning("⚠️ Please enter a name.")
982
-
983
- # Server Status
984
  st.subheader("⚙️ Server Status")
985
  col_ws, col_clients = st.columns(2)
986
  with col_ws:
987
  server_alive = st.session_state.get('server_task') and st.session_state.server_task.is_alive(); ws_status = "Running" if server_alive else "Stopped"; st.metric("WebSocket Server", ws_status)
988
- if not server_alive and st.button("🔄 Restart Server Thread", key="restart_ws"): start_websocket_server_thread(); st.rerun()
989
- with col_clients:
990
- with clients_lock: client_count = len(connected_clients)
991
- st.metric("🔗 Connected Clients", client_count)
992
-
993
- # File Deletion
994
- st.subheader("🗑️ Archive Maintenance")
995
- st.caption("🧹 Cleanse the old to make way for the new.")
996
  st.warning("Deletion is permanent!", icon="⚠️")
997
  col_del1, col_del2, col_del3, col_del4 = st.columns(4)
998
  with col_del1:
999
- if st.button("🗑️ Chats", key="del_chat_md"): delete_files([os.path.join(CHAT_DIR, "*.md")]); st.session_state.chat_history = []; add_action_log("Cleared Chats", emoji="🧹"); st.rerun()
1000
  with col_del2:
1001
- if st.button("🗑️ Audio", key="del_audio_mp3"): delete_files([os.path.join(AUDIO_DIR, "*.mp3"), os.path.join(AUDIO_CACHE_DIR, "*.mp3")]); st.session_state.audio_cache = {}; add_action_log("Cleared Audio", emoji="🧹"); st.rerun()
1002
  with col_del3:
1003
- if st.button("🗑️ Worlds", key="del_worlds_md"): delete_files([os.path.join(SAVED_WORLDS_DIR, f"{WORLD_STATE_FILE_MD_PREFIX}*.md")]); st.session_state.current_world_file = None; add_action_log("Cleared Worlds", emoji="🧹"); st.rerun()
1004
  with col_del4:
1005
- if st.button("🗑️ All Gen", key="del_all_gen"): delete_files([os.path.join(CHAT_DIR, "*.md"), os.path.join(AUDIO_DIR, "*.mp3"), os.path.join(AUDIO_CACHE_DIR, "*.mp3"), os.path.join(SAVED_WORLDS_DIR, "*.md"), os.path.join(MEDIA_DIR, "*.zip")]); st.session_state.chat_history = []; st.session_state.audio_cache = {}; st.session_state.current_world_file = None; add_action_log("Cleared All Generated", emoji="🔥"); st.rerun()
1006
 
1007
- # Download Archives
1008
  st.subheader("📦 Download Archives")
1009
- st.caption("Bundle your creations for safekeeping or sharing.")
1010
- col_zip1, col_zip2, col_zip3 = st.columns(3)
1011
- with col_zip1:
1012
- if st.button("Zip Worlds"): create_zip_of_files(glob.glob(os.path.join(SAVED_WORLDS_DIR, f"{WORLD_STATE_FILE_MD_PREFIX}*.md")), "Worlds")
1013
- with col_zip2:
1014
- if st.button("Zip Chats"): create_zip_of_files(glob.glob(os.path.join(CHAT_DIR, "*.md")), "Chats")
1015
- with col_zip3:
1016
- if st.button("Zip Audio"): create_zip_of_files(glob.glob(os.path.join(AUDIO_DIR, "*.mp3")) + glob.glob(os.path.join(AUDIO_CACHE_DIR, "*.mp3")), "Audio")
1017
  zip_files = sorted(glob.glob(os.path.join(MEDIA_DIR,"*.zip")), key=os.path.getmtime, reverse=True)
1018
  if zip_files:
1019
- st.caption("Existing Zip Files:")
1020
- for zip_file in zip_files: st.markdown(get_download_link(zip_file, "zip"), unsafe_allow_html=True)
 
 
 
 
 
 
 
 
1021
  else:
1022
- st.caption("🌬️ No archives found.")
 
 
1023
 
1024
  # ==============================================================================
1025
- # 10. 🚀 Main Execution Logic
1026
  # ==============================================================================
1027
 
1028
- def initialize_app():
1029
- """Handles session init, server start, and ensures world state resource is accessed."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1030
  init_session_state()
1031
- # Load/Assign username
1032
- if not st.session_state.username:
1033
- loaded_user = load_username()
1034
- if loaded_user and loaded_user in FUN_USERNAMES: st.session_state.username = loaded_user; st.session_state.tts_voice = FUN_USERNAMES[loaded_user]
1035
- else: st.session_state.username = random.choice(list(FUN_USERNAMES.keys())) if FUN_USERNAMES else "User"; st.session_state.tts_voice = FUN_USERNAMES.get(st.session_state.username, DEFAULT_TTS_VOICE); save_username(st.session_state.username)
1036
 
1037
- # Start WebSocket server thread if needed
1038
  server_thread = st.session_state.get('server_task'); server_alive = server_thread is not None and server_thread.is_alive()
1039
  if not st.session_state.get('server_running_flag', False) and not server_alive: start_websocket_server_thread()
1040
  elif server_alive and not st.session_state.get('server_running_flag', False): st.session_state.server_running_flag = True
1041
 
1042
- # Trigger the cached resource initialization/retrieval
1043
- # This ensures the initial state is loaded from file if the resource is created now
1044
- try:
1045
- get_world_state_manager()
1046
- # Set initial current_world_file state if needed (based on what cache loaded)
1047
- if st.session_state.get('current_world_file') is None and '_initial_world_file_loaded' in st.session_state:
1048
- st.session_state.current_world_file = st.session_state.pop('_initial_world_file_loaded') # Use and remove temp flag
1049
- print(f"🐍 Set initial session 'current_world_file' to: {st.session_state.current_world_file}")
1050
-
1051
- except Exception as e:
1052
- st.error(f"❌ Fatal error initializing world state manager: {e}"); st.exception(e); st.stop()
1053
 
1054
-
1055
- if __name__ == "__main__":
1056
- initialize_app()
1057
  render_sidebar()
1058
  render_main_content()
 
 
1
  import streamlit as st
2
  import asyncio
3
+ import websockets
4
  import uuid
5
  from datetime import datetime
6
  import os
 
21
  import json
22
  import zipfile
23
  from dotenv import load_dotenv
24
+ from streamlit_marquee import streamlit_marquee # Keep import if used
25
+ from collections import defaultdict, Counter
26
+ import pandas as pd # Keep for potential fallback logic if needed
27
+ from streamlit_js_eval import streamlit_js_eval
28
+ from PIL import Image # Needed for paste_image_component
29
 
30
  # ==============================================================================
31
+ # Configuration & Constants
32
  # ==============================================================================
33
 
34
  # 🛠️ Patch asyncio for nesting
 
36
 
37
  # 🎨 Page Config
38
  st.set_page_config(
39
+ page_title="🤖🏗️ Shared World Builder 🏆",
40
  page_icon="🏗️",
41
  layout="wide",
42
  initial_sidebar_state="expanded"
43
  )
44
 
45
  # General Constants
46
+ icons = '🤖🏗️🗣️💾'
47
+ Site_Name = '🤖🏗️ Shared World Builder 🗣️'
48
+ START_ROOM = "World Lobby 🌍"
49
+ MEDIA_DIR = "." # Base directory for general files
50
+ STATE_FILE = "user_state.txt" # For remembering username
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  # User/Chat Constants
53
  FUN_USERNAMES = {
54
  "BuilderBot 🤖": "en-US-AriaNeural", "WorldWeaver 🕸️": "en-US-JennyNeural",
55
  "Terraformer 🌱": "en-GB-SoniaNeural", "SkyArchitect ☁️": "en-AU-NatashaNeural",
56
  "PixelPainter 🎨": "en-CA-ClaraNeural", "VoxelVortex 🌪️": "en-US-GuyNeural",
57
+ "CosmicCrafter ✨": "en-GB-RyanNeural", "GeoGuru 🗺️": "en-AU-WilliamNeural",
58
+ "BlockBard 🧱": "en-CA-LiamNeural", "SoundSculptor 🔊": "en-US-AnaNeural",
59
+ }
60
  EDGE_TTS_VOICES = list(set(FUN_USERNAMES.values()))
61
+ CHAT_DIR = "chat_logs"
62
+
63
+ # Audio Constants
64
+ AUDIO_CACHE_DIR = "audio_cache"
65
+ AUDIO_DIR = "audio_logs"
66
+
67
+ # World Builder Constants
68
+ SAVED_WORLDS_DIR = "saved_worlds" # Directory for MD world files
69
+ PLOT_WIDTH = 50.0 # Needed for JS injection
70
+ PLOT_DEPTH = 50.0 # Needed for JS injection
71
+ WORLD_STATE_FILE_MD_PREFIX = "🌍_" # Prefix for world save files
72
 
73
  # File Emojis
74
+ FILE_EMOJIS = {"md": "📝", "mp3": "🎵", "png": "🖼️", "mp4": "🎥", "zip": "📦", "json": "📄"}
75
 
76
+ # --- Mapping Emojis to Primitive Types ---
77
+ # Ensure these types match the createPrimitiveMesh function keys in index.html
78
  PRIMITIVE_MAP = {
79
+ "🌳": "Tree", "🗿": "Rock", "🏛️": "Simple House", "🌲": "Pine Tree", "🧱": "Brick Wall",
80
+ "🔵": "Sphere", "📦": "Cube", "🧴": "Cylinder", "🍦": "Cone", "🍩": "Torus",
81
+ "🍄": "Mushroom", "🌵": "Cactus", "🔥": "Campfire", "": "Star", "💎": "Gem",
82
+ "🗼": "Tower", "🚧": "Barrier", "": "Fountain", "🏮": "Lantern", "": "Sign Post"
83
+ # Add more pairs up to ~20 if desired
84
  }
 
 
85
 
86
+ # --- Directories ---
87
  for d in [CHAT_DIR, AUDIO_DIR, AUDIO_CACHE_DIR, SAVED_WORLDS_DIR]:
88
  os.makedirs(d, exist_ok=True)
89
+
90
+ # --- API Keys (Placeholder) ---
91
  load_dotenv()
92
+ # ANTHROPIC_KEY = os.getenv('ANTHROPIC_API_KEY', st.secrets.get('ANTHROPIC_API_KEY', ""))
93
+ # OPENAI_KEY = os.getenv('OPENAI_API_KEY', st.secrets.get('OPENAI_API_KEY', ""))
94
 
95
+ # --- Global State & Locks ---
96
+ world_objects_lock = threading.Lock()
97
+ world_objects = defaultdict(dict) # In-memory world state {obj_id: data}
98
+ connected_clients = set() # Holds client_id strings
99
 
100
  # ==============================================================================
101
+ # Utility Functions
102
  # ==============================================================================
103
+
104
  def get_current_time_str(tz='UTC'):
105
  """Gets formatted timestamp string in specified timezone (default UTC)."""
106
  try:
 
109
  except pytz.UnknownTimeZoneError:
110
  now_aware = datetime.now(pytz.utc)
111
  except Exception as e:
112
+ print(f"Timezone error ({tz}), using UTC. Error: {e}")
113
+ now_aware = datetime.now(pytz.utc)
114
  return now_aware.strftime('%Y%m%d_%H%M%S')
115
 
116
+
117
+ def clean_filename_part(text, max_len=30):
118
  """Cleans a string part for use in a filename."""
119
  if not isinstance(text, str): text = "invalid_name"
120
+ text = re.sub(r'\s+', '_', text) # Replace spaces
121
+ text = re.sub(r'[^\w\-.]', '', text) # Keep word chars, hyphen, period
122
  return text[:max_len]
123
 
124
  def run_async(async_func, *args, **kwargs):
125
+ """Runs an async function safely from a sync context using create_task."""
126
  try:
127
  loop = asyncio.get_running_loop()
128
  return loop.create_task(async_func(*args, **kwargs))
129
+ except RuntimeError: # No running loop in this thread
130
+ print(f"Warning: Running async func {async_func.__name__} in new event loop.")
131
+ try:
132
+ return asyncio.run(async_func(*args, **kwargs))
133
+ except Exception as e:
134
+ print(f"Error running async func {async_func.__name__} in new loop: {e}")
135
+ return None
136
+ except Exception as e:
137
+ print(f"Error scheduling async task {async_func.__name__}: {e}")
138
+ return None
139
 
140
  def ensure_dir(dir_path):
141
+ """Creates directory if it doesn't exist."""
142
+ os.makedirs(dir_path, exist_ok=True)
143
+
144
+ # Ensure directories exist on startup
145
+ for d in [CHAT_DIR, AUDIO_DIR, AUDIO_CACHE_DIR, SAVED_WORLDS_DIR]:
146
+ ensure_dir(d)
147
 
148
  # ==============================================================================
149
+ # World State File Handling (Markdown + JSON)
150
  # ==============================================================================
151
 
152
+ def generate_world_save_filename(name="World"):
153
+ """Generates a filename for saving world state MD files."""
154
+ timestamp = get_current_time_str() # Use UTC for consistency
155
+ clean_name = clean_filename_part(name)
156
+ rand_hash = hashlib.md5(str(time.time()).encode() + name.encode()).hexdigest()[:6] # Seed hash
157
+ return f"{WORLD_STATE_FILE_MD_PREFIX}{clean_name}_{timestamp}_{rand_hash}.md"
 
 
 
158
 
159
+ def parse_world_filename(filename):
160
  """Extracts info from filename if possible, otherwise returns defaults."""
161
  basename = os.path.basename(filename)
162
+ # Check prefix and suffix
163
  if basename.startswith(WORLD_STATE_FILE_MD_PREFIX) and basename.endswith(".md"):
164
+ core_name = basename[len(WORLD_STATE_FILE_MD_PREFIX):-3]
165
+ parts = core_name.split('_')
166
+ if len(parts) >= 3: # Expecting Name_Timestamp_Hash
167
+ timestamp_str = parts[-2]
168
+ name_parts = parts[:-2]; name = "_".join(name_parts) if name_parts else "Untitled"
169
+ dt_obj = None
170
+ try: # Try parsing timestamp
171
+ dt_obj = datetime.strptime(timestamp_str, '%Y%m%d_%H%M%S')
172
+ dt_obj = pytz.utc.localize(dt_obj) # Assume UTC
173
+ except (ValueError, pytz.exceptions.AmbiguousTimeError, pytz.exceptions.NonExistentTimeError): dt_obj = None
174
+ return {"name": name.replace('_', ' '), "timestamp": timestamp_str, "dt": dt_obj, "filename": filename}
175
+
176
+ # Fallback for unknown format
177
+ dt_fallback = None
178
+ try: mtime = os.path.getmtime(filename); dt_fallback = datetime.fromtimestamp(mtime, tz=pytz.utc)
179
+ except Exception: pass
180
+ return {"name": basename.replace('.md',''), "timestamp": "Unknown", "dt": dt_fallback, "filename": filename}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
 
 
 
 
 
 
 
 
 
182
 
183
  def save_world_state_to_md(target_filename_base):
184
+ """Saves the current in-memory world state to a specific MD file (basename)."""
185
+ global world_objects
186
  save_path = os.path.join(SAVED_WORLDS_DIR, target_filename_base)
187
+ print(f"Acquiring lock to save world state to: {save_path}...")
188
  success = False
189
+ with world_objects_lock:
190
+ world_data_dict = dict(world_objects) # Convert defaultdict for saving
191
+ print(f"Saving {len(world_data_dict)} objects...")
192
+ parsed_info = parse_world_filename(save_path) # Parse the full path/intended name
193
  timestamp_save = get_current_time_str()
194
+ md_content = f"""# World State: {parsed_info['name']}
195
  * **File Saved:** {timestamp_save} (UTC)
196
  * **Source Timestamp:** {parsed_info['timestamp']}
197
  * **Objects:** {len(world_data_dict)}
 
200
  {json.dumps(world_data_dict, indent=2)}
201
  ```"""
202
  try:
203
+ ensure_dir(SAVED_WORLDS_DIR)
204
  with open(save_path, 'w', encoding='utf-8') as f: f.write(md_content)
205
+ print(f"World state saved successfully to {target_filename_base}")
206
  success = True
207
+ except Exception as e:
208
+ print(f"Error saving world state to {save_path}: {e}")
209
  return success
210
 
211
+
212
  def load_world_state_from_md(filename_base):
213
+ """Loads world state from an MD file (basename), updates global state, returns success bool."""
214
+ global world_objects
215
  load_path = os.path.join(SAVED_WORLDS_DIR, filename_base)
216
+ print(f"Loading world state from MD file: {load_path}...")
217
  if not os.path.exists(load_path): st.error(f"World file not found: {filename_base}"); return False
218
+
219
  try:
220
  with open(load_path, 'r', encoding='utf-8') as f: content = f.read()
221
+ json_match = re.search(r"```json\s*(\{[\s\S]*?\})\s*```", content, re.IGNORECASE) # More robust regex
222
+ if not json_match: st.error(f"Could not find valid JSON block in {filename_base}"); return False
223
+
224
  world_data_dict = json.loads(json_match.group(1))
225
 
226
+ print(f"Acquiring lock to update world state from {filename_base}...")
227
+ with world_objects_lock:
228
+ world_objects.clear()
229
+ for k, v in world_data_dict.items(): world_objects[str(k)] = v
230
+ loaded_count = len(world_objects)
231
+ print(f"Loaded {loaded_count} objects from {filename_base}. Lock released.")
232
+ st.session_state.current_world_file = filename_base # Track loaded file (basename)
233
  return True
234
 
235
+ except json.JSONDecodeError as e: st.error(f"Invalid JSON found in {filename_base}: {e}"); return False
236
  except Exception as e: st.error(f"Error loading world state from {filename_base}: {e}"); st.exception(e); return False
237
 
238
+ def get_saved_worlds():
239
+ """Scans the saved worlds directory for world MD files and parses them."""
240
+ try:
241
+ ensure_dir(SAVED_WORLDS_DIR)
242
+ world_files = glob.glob(os.path.join(SAVED_WORLDS_DIR, f"{WORLD_STATE_FILE_MD_PREFIX}*.md"))
243
+ parsed_worlds = [parse_world_filename(f) for f in world_files]
244
+ parsed_worlds.sort(key=lambda x: x['dt'] if x['dt'] else datetime.min.replace(tzinfo=pytz.utc), reverse=True)
245
+ return parsed_worlds
246
+ except Exception as e:
247
+ print(f"Error scanning saved worlds: {e}"); st.error(f"Could not scan saved worlds: {e}"); return []
248
+
249
  # ==============================================================================
250
+ # User State & Session Init
251
  # ==============================================================================
252
+
253
  def save_username(username):
254
  try:
255
  with open(STATE_FILE, 'w') as f: f.write(username)
256
+ except Exception as e: print(f"Failed save username: {e}")
257
 
258
  def load_username():
259
  if os.path.exists(STATE_FILE):
260
  try:
261
  with open(STATE_FILE, 'r') as f: return f.read().strip()
262
+ except Exception as e: print(f"Failed load username: {e}")
263
  return None
264
 
265
  def init_session_state():
266
  """Initializes Streamlit session state variables."""
267
  defaults = {
268
  'server_running_flag': False, 'server_instance': None, 'server_task': None,
269
+ 'active_connections': defaultdict(dict), 'last_chat_update': 0, 'message_input': "",
270
+ 'audio_cache': {}, 'tts_voice': "en-US-AriaNeural", 'chat_history': [],
271
+ 'marquee_settings': {"background": "#1E1E1E", "color": "#FFFFFF", "font-size": "14px", "animationDuration": "20s", "width": "100%", "lineHeight": "35px"},
272
+ 'enable_audio': True, 'download_link_cache': {}, 'username': None, 'autosend': False,
273
+ 'last_message': "", 'timer_start': time.time(), 'last_sent_transcript': "",
274
+ 'last_refresh': time.time(), 'auto_refresh': False, 'refresh_rate': 30,
275
+ 'selected_object': 'None', 'initial_world_state_loaded': False,
276
  'current_world_file': None, # Track loaded world filename (basename)
277
+ 'operation_timings': {}, 'performance_metrics': defaultdict(list),
278
+ 'paste_image_base64': "", 'new_world_name': "MyWorld"
 
279
  }
280
  for k, v in defaults.items():
281
+ if k not in st.session_state: st.session_state[k] = v
282
+ # Ensure complex types initialized correctly
 
 
283
  if not isinstance(st.session_state.active_connections, defaultdict): st.session_state.active_connections = defaultdict(dict)
284
  if not isinstance(st.session_state.chat_history, list): st.session_state.chat_history = []
285
+ if not isinstance(st.session_state.marquee_settings, dict): st.session_state.marquee_settings = defaults['marquee_settings']
286
  if not isinstance(st.session_state.audio_cache, dict): st.session_state.audio_cache = {}
287
  if not isinstance(st.session_state.download_link_cache, dict): st.session_state.download_link_cache = {}
 
288
 
289
  # ==============================================================================
290
+ # Audio / TTS / Chat / File Handling Helpers
291
  # ==============================================================================
 
 
 
 
 
 
292
 
293
+ # --- Text & File Helpers ---
 
 
294
  def clean_text_for_tts(text):
295
  if not isinstance(text, str): return "No text"
296
  text = re.sub(r'\[([^\]]+)\]\([^\)]+\)', r'\1', text); text = re.sub(r'[#*_`!]', '', text)
297
  text = ' '.join(text.split()); return text[:250] or "No text"
298
+
299
  def create_file(content, username, file_type="md", save_path=None):
300
  if not save_path: filename = generate_filename(content, username, file_type); save_path = os.path.join(MEDIA_DIR, filename)
301
  ensure_dir(os.path.dirname(save_path))
302
  try:
303
+ with open(save_path, 'w', encoding='utf-8') as f: f.write(content)
304
+ # print(f"Created file: {save_path}"); # Can be too verbose
305
+ return save_path
306
+ except Exception as e: print(f"Error creating file {save_path}: {e}"); return None
307
+
308
  def get_download_link(file_path, file_type="md"):
309
  if not file_path or not os.path.exists(file_path): basename = os.path.basename(file_path) if file_path else "N/A"; return f"<small>Not found: {basename}</small>"
310
  try: mtime = os.path.getmtime(file_path)
 
318
  basename = os.path.basename(file_path)
319
  link_html = f'<a href="data:{mime_types.get(file_type, "application/octet-stream")};base64,{b64}" download="{basename}" title="Download {basename}">{FILE_EMOJIS.get(file_type, "📄")}</a>'
320
  st.session_state.download_link_cache[cache_key] = link_html
321
+ except Exception as e: print(f"Error generating DL link for {file_path}: {e}"); return f"<small>Err</small>"
322
  return st.session_state.download_link_cache.get(cache_key, "<small>CacheErr</small>")
323
+
324
+ # --- Audio / TTS ---
325
  async def async_edge_tts_generate(text, voice, username):
326
  if not text: return None
327
  cache_key = hashlib.md5(f"{text[:150]}_{voice}".encode()).hexdigest();
 
335
  try:
336
  communicate = edge_tts.Communicate(text_cleaned, voice); await communicate.save(save_path);
337
  if os.path.exists(save_path) and os.path.getsize(save_path) > 0: st.session_state.audio_cache[cache_key] = save_path; return save_path
338
+ else: print(f"Audio file {save_path} failed generation."); return None
339
+ except Exception as e: print(f"Edge TTS Error: {e}"); return None
340
+
341
  def play_and_download_audio(file_path):
342
  if file_path and os.path.exists(file_path):
343
  try:
344
  st.audio(file_path)
345
  file_type = file_path.split('.')[-1]
346
  st.markdown(get_download_link(file_path, file_type), unsafe_allow_html=True)
347
+ except Exception as e: st.error(f"Audio display error for {os.path.basename(file_path)}: {e}")
348
+
349
+ # --- Chat ---
350
  async def save_chat_entry(username, message, voice, is_markdown=False):
351
  if not message.strip(): return None, None
352
  timestamp_str = get_current_time_str();
353
  entry = f"[{timestamp_str}] {username} ({voice}): {message}" if not is_markdown else f"[{timestamp_str}] {username} ({voice}):\n```markdown\n{message}\n```"
354
  md_filename_base = generate_filename(message, username, "md"); md_file_path = os.path.join(CHAT_DIR, md_filename_base);
355
+ md_file = create_file(entry, username, "md", save_path=md_file_path) # Save to file
356
  if 'chat_history' not in st.session_state: st.session_state.chat_history = [];
357
+ st.session_state.chat_history.append(entry) # Add to live history
358
  audio_file = None;
359
  if st.session_state.get('enable_audio', True):
360
+ tts_message = message # Use original message for TTS
361
  audio_file = await async_edge_tts_generate(tts_message, voice, username)
362
  return md_file, audio_file
363
+
364
  async def load_chat_history():
365
+ if 'chat_history' not in st.session_state: st.session_state.chat_history = []
366
+ if not st.session_state.chat_history: # Only load from files if session state is empty
367
+ ensure_dir(CHAT_DIR)
368
+ print("Loading chat history from files...")
369
+ chat_files = sorted(glob.glob(os.path.join(CHAT_DIR, "*.md")), key=os.path.getmtime); loaded_count = 0
370
+ temp_history = []
371
+ for f_path in chat_files:
372
+ try:
373
+ with open(f_path, 'r', encoding='utf-8') as file: temp_history.append(file.read().strip()); loaded_count += 1
374
+ except Exception as e: print(f"Err read chat {f_path}: {e}")
375
+ st.session_state.chat_history = temp_history # Assign loaded history
376
+ print(f"Loaded {loaded_count} chat entries from files.")
377
+ return st.session_state.chat_history
378
+
379
+ # --- File Management ---
380
  def create_zip_of_files(files_to_zip, prefix="Archive"):
381
+ if not files_to_zip: st.warning("No files provided to zip."); return None
382
  timestamp = format_timestamp_prefix(f"Zip_{prefix}"); zip_name = f"{prefix}_{timestamp}.zip"
383
  try:
384
+ print(f"Creating zip: {zip_name}...");
385
  with zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) as z:
386
  for f in files_to_zip:
387
+ if os.path.exists(f): z.write(f, os.path.basename(f)) # Use basename in archive
388
+ else: print(f"Skip zip missing: {f}")
389
+ print("Zip success."); st.success(f"Created {zip_name}"); return zip_name
390
+ except Exception as e: print(f"Zip failed: {e}"); st.error(f"Zip failed: {e}"); return None
391
+
392
  def delete_files(file_patterns, exclude_files=None):
393
+ """Deletes files matching patterns, excluding protected/specified files."""
394
  protected = [STATE_FILE, "app.py", "index.html", "requirements.txt", "README.md"]
395
+ # Don't automatically protect current world file during generic delete
 
396
  if exclude_files: protected.extend(exclude_files)
397
+
398
  deleted_count = 0; errors = 0
399
  for pattern in file_patterns:
400
+ pattern_path = pattern # Assume pattern includes path from os.path.join
401
+ print(f"Attempting to delete files matching: {pattern_path}")
402
  try:
403
  files_to_delete = glob.glob(pattern_path)
404
+ if not files_to_delete: print(f"No files found for pattern: {pattern}"); continue
405
  for f_path in files_to_delete:
406
  basename = os.path.basename(f_path)
407
  if os.path.isfile(f_path) and basename not in protected:
408
+ try: os.remove(f_path); print(f"Deleted: {f_path}"); deleted_count += 1
409
+ except Exception as e: print(f"Failed delete {f_path}: {e}"); errors += 1
410
+ elif os.path.isdir(f_path): print(f"Skipping directory: {f_path}")
411
+ except Exception as glob_e: print(f"Error matching pattern {pattern}: {glob_e}"); errors += 1
412
+ msg = f"Deleted {deleted_count} files.";
413
  if errors > 0: msg += f" Encountered {errors} errors."; st.warning(msg)
414
  elif deleted_count > 0: st.success(msg)
415
+ else: st.info("No matching files found to delete.")
416
  st.session_state['download_link_cache'] = {}; st.session_state['audio_cache'] = {}
417
+
418
+
419
+ # --- Image Handling ---
420
  async def save_pasted_image(image, username):
421
  if not image: return None
422
+ try:
423
+ img_hash = hashlib.md5(image.tobytes()).hexdigest()[:8]; timestamp = format_timestamp_prefix(username); filename = f"{timestamp}_pasted_{img_hash}.png"; filepath = os.path.join(MEDIA_DIR, filename)
424
+ image.save(filepath, "PNG"); print(f"Pasted image saved: {filepath}"); return filepath
425
+ except Exception as e: print(f"Failed image save: {e}"); return None
426
+
427
  def paste_image_component():
428
  pasted_img = None; img_type = None
429
+ # Added default value to text_area to ensure key exists even if empty
430
+ paste_input = st.text_area("Paste Image Data Here", key="paste_input_area", height=50, value="")
431
+ if st.button("Process Pasted Image 📋", key="paste_form_button"): # Simplified trigger
432
+ if paste_input and paste_input.startswith('data:image'):
433
  try:
434
+ mime_type = paste_input.split(';')[0].split(':')[1]; base64_str = paste_input.split(',')[1]; img_bytes = base64.b64decode(base64_str); pasted_img = Image.open(io.BytesIO(img_bytes)); img_type = mime_type.split('/')[1]
435
+ st.image(pasted_img, caption=f"Pasted ({img_type.upper()})", width=150); st.session_state.paste_image_base64 = base64_str
436
+ except ImportError: st.error("Pillow library needed for image pasting.")
437
+ except Exception as e: st.error(f"Img decode err: {e}"); st.session_state.paste_image_base64 = ""
438
+ else: st.warning("No valid image data pasted."); st.session_state.paste_image_base64 = ""
439
+ # Return image if processed successfully in this run
440
+ return pasted_img
441
+
442
+
 
 
 
443
  # --- PDF Processing ---
444
  class AudioProcessor:
445
  def __init__(self): self.cache_dir=AUDIO_CACHE_DIR; ensure_dir(self.cache_dir); self.metadata=json.load(open(f"{self.cache_dir}/metadata.json", 'r')) if os.path.exists(f"{self.cache_dir}/metadata.json") else {}
446
  def _save_metadata(self):
447
  try:
448
  with open(f"{self.cache_dir}/metadata.json", 'w') as f: json.dump(self.metadata, f, indent=2)
449
+ except Exception as e: print(f"Failed metadata save: {e}")
450
+ async def create_audio(self, text, voice='en-US-AriaNeural'):
451
  cache_key=hashlib.md5(f"{text[:150]}:{voice}".encode()).hexdigest(); cache_path=os.path.join(self.cache_dir, f"{cache_key}.mp3");
452
  if cache_key in self.metadata and os.path.exists(cache_path): return cache_path
453
  text_cleaned=clean_text_for_tts(text);
 
457
  communicate=edge_tts.Communicate(text_cleaned,voice); await communicate.save(cache_path)
458
  if os.path.exists(cache_path) and os.path.getsize(cache_path) > 0: self.metadata[cache_key]={'timestamp': datetime.now().isoformat(), 'text_length': len(text_cleaned), 'voice': voice}; self._save_metadata(); return cache_path
459
  else: return None
460
+ except Exception as e: print(f"TTS Create Audio Error: {e}"); return None
461
 
462
  def process_pdf_tab(pdf_file, max_pages, voice):
463
+ st.subheader("PDF Processing Results") # Change header
464
+ if pdf_file is None: st.info("Upload a PDF file and click 'Process PDF' to begin."); return
465
  audio_processor = AudioProcessor()
466
  try:
467
+ reader=PdfReader(pdf_file)
468
+ if reader.is_encrypted: st.warning("PDF is encrypted."); return
469
+ total_pages=min(len(reader.pages),max_pages);
470
+ st.write(f"Processing first {total_pages} pages of '{pdf_file.name}'...")
471
  texts, audios={}, {}; page_threads = []; results_lock = threading.Lock()
472
 
473
  def process_page_sync(page_num, page_text):
474
  async def run_async_audio(): return await audio_processor.create_audio(page_text, voice)
475
+ try:
476
+ # asyncio.run can cause issues if called repeatedly in threads, use run_async helper
477
+ audio_path_future = run_async(run_async_audio)
478
+ # This needs careful handling. run_async might return a task or result.
479
+ # For simplicity here, let's assume it blocks or we retrieve result later.
480
+ # A better pattern uses concurrent.futures or manages event loop properly.
481
+ # Sticking to asyncio.run for now as run_async tries it.
482
+ audio_path = asyncio.run(run_async_audio()) # Revert to simpler asyncio.run for thread context
483
  if audio_path:
484
+ with results_lock: audios[page_num] = audio_path
485
+ except Exception as page_e: print(f"Err process page {page_num+1}: {page_e}")
486
 
487
  # Start threads
488
+ for i in range(total_pages):
489
+ try:
490
+ page = reader.pages[i]; text = page.extract_text();
491
+ if text and text.strip(): texts[i]=text; thread = threading.Thread(target=process_page_sync, args=(i, text)); page_threads.append(thread); thread.start()
492
+ else: texts[i] = "[No text extracted]"
493
+ except Exception as extract_e: texts[i] = f"[Error extract: {extract_e}]"; print(f"Error page {i+1} extract: {extract_e}")
 
 
 
 
 
 
 
 
 
494
 
495
  # Wait for threads and display progress
496
+ progress_bar = st.progress(0.0, text="Processing pages...")
497
+ total_threads = len(page_threads)
498
+ start_join_time = time.time()
499
  while any(t.is_alive() for t in page_threads):
500
+ completed_threads = total_threads - sum(t.is_alive() for t in page_threads)
501
+ progress = completed_threads / total_threads if total_threads > 0 else 1.0
502
+ progress_bar.progress(min(progress, 1.0), text=f"Processed {completed_threads}/{total_threads} pages...")
503
+ if time.time() - start_join_time > 600: print("PDF processing timed out."); break # 10 min timeout
504
  time.sleep(0.5)
505
+ progress_bar.progress(1.0, text="Processing complete.")
506
 
507
  # Display results
508
+ for i in range(total_pages):
 
509
  with st.expander(f"Page {i+1}"):
510
+ st.markdown(texts.get(i, "[Error getting text]"))
511
  audio_file = audios.get(i)
512
  if audio_file: play_and_download_audio(audio_file)
513
+ else: st.caption("Audio generation failed or was skipped.")
 
 
 
 
 
514
 
515
+ except Exception as pdf_e: st.error(f"Err read PDF: {pdf_e}"); st.exception(pdf_e)
516
 
517
  # ==============================================================================
518
+ # WebSocket Server Logic
519
  # ==============================================================================
520
 
521
  async def register_client(websocket):
522
+ client_id = str(websocket.id); connected_clients.add(client_id);
523
+ if 'active_connections' not in st.session_state: st.session_state.active_connections = defaultdict(dict);
524
+ st.session_state.active_connections[client_id] = websocket; print(f"Client registered: {client_id}. Total: {len(connected_clients)}")
 
 
 
 
525
 
526
  async def unregister_client(websocket):
527
+ client_id = str(websocket.id); connected_clients.discard(client_id);
528
+ if 'active_connections' in st.session_state: st.session_state.active_connections.pop(client_id, None);
529
+ print(f"Client unregistered: {client_id}. Remaining: {len(connected_clients)}")
 
 
 
530
 
531
  async def send_safely(websocket, message, client_id):
532
+ try: await websocket.send(message)
533
+ except websockets.ConnectionClosed: print(f"WS Send failed (Closed) client {client_id}"); raise
534
+ except RuntimeError as e: print(f"WS Send failed (Runtime {e}) client {client_id}"); raise
535
+ except Exception as e: print(f"WS Send failed (Other {e}) client {client_id}"); raise
 
 
 
 
 
 
 
 
536
 
537
  async def broadcast_message(message, exclude_id=None):
538
+ if not connected_clients: return
539
+ tasks = []; current_client_ids = list(connected_clients); active_connections_copy = st.session_state.active_connections.copy()
 
 
 
 
 
 
 
 
 
 
 
540
  for client_id in current_client_ids:
541
+ if client_id == exclude_id: continue
542
+ websocket = active_connections_copy.get(client_id)
543
+ if websocket: tasks.append(asyncio.create_task(send_safely(websocket, message, client_id)))
544
+ if tasks: await asyncio.gather(*tasks, return_exceptions=True)
 
 
 
 
545
 
546
  async def broadcast_world_update():
547
+ with world_objects_lock: current_state_payload = dict(world_objects)
548
+ update_msg = json.dumps({"type": "initial_state", "payload": current_state_payload})
549
+ print(f"Broadcasting full world update ({len(current_state_payload)} objects)...")
550
+ await broadcast_message(update_msg)
 
551
 
552
  async def websocket_handler(websocket, path):
 
553
  await register_client(websocket); client_id = str(websocket.id);
 
554
  username = st.session_state.get('username', f"User_{client_id[:4]}")
 
555
  try: # Send initial state
556
+ with world_objects_lock: initial_state_payload = dict(world_objects)
557
+ initial_state_msg = json.dumps({"type": "initial_state", "payload": initial_state_payload}); await websocket.send(initial_state_msg)
558
+ print(f"Sent initial state ({len(initial_state_payload)} objs) to {client_id}")
 
 
559
  await broadcast_message(json.dumps({"type": "user_join", "payload": {"username": username, "id": client_id}}), exclude_id=client_id)
560
+ except Exception as e: print(f"Error initial phase {client_id}: {e}")
561
 
562
+ try: # Message loop
563
  async for message in websocket:
564
  try:
565
  data = json.loads(message); msg_type = data.get("type"); payload = data.get("payload", {});
566
+ sender_username = payload.get("username", username)
 
 
 
567
 
568
  if msg_type == "chat_message":
569
+ chat_text = payload.get('message', ''); voice = payload.get('voice', FUN_USERNAMES.get(sender_username, "en-US-AriaNeural"));
570
+ run_async(save_chat_entry, sender_username, chat_text, voice)
571
+ await broadcast_message(message, exclude_id=client_id)
 
572
 
573
  elif msg_type == "place_object":
574
  obj_data = payload.get("object_data");
575
  if obj_data and 'obj_id' in obj_data and 'type' in obj_data:
576
+ with world_objects_lock: world_objects[obj_data['obj_id']] = obj_data
 
 
577
  broadcast_payload = json.dumps({"type": "object_placed", "payload": {"object_data": obj_data, "username": sender_username}});
578
  await broadcast_message(broadcast_payload, exclude_id=client_id)
579
+ else: print(f"WS Invalid place_object payload: {payload}")
 
 
580
 
581
  elif msg_type == "delete_object":
582
  obj_id = payload.get("obj_id"); removed = False
583
  if obj_id:
584
+ with world_objects_lock:
585
+ if obj_id in world_objects: del world_objects[obj_id]; removed = True
 
586
  if removed:
587
  broadcast_payload = json.dumps({"type": "object_deleted", "payload": {"obj_id": obj_id, "username": sender_username}});
588
  await broadcast_message(broadcast_payload, exclude_id=client_id)
589
+ else: print(f"WS Invalid delete_object payload: {payload}")
 
590
 
591
  elif msg_type == "player_position":
592
  pos_data = payload.get("position"); rot_data = payload.get("rotation")
593
  if pos_data:
594
  broadcast_payload = json.dumps({"type": "player_moved", "payload": {"username": sender_username, "id": client_id, "position": pos_data, "rotation": rot_data}});
595
+ await broadcast_message(broadcast_payload, exclude_id=client_id)
 
 
 
596
 
597
+ except json.JSONDecodeError: print(f"WS Invalid JSON from {client_id}: {message[:100]}...")
598
+ except Exception as e: print(f"WS Error processing msg from {client_id}: {e}")
599
+ except websockets.ConnectionClosed: print(f"WS Client disconnected: {client_id} ({username})")
600
+ except Exception as e: print(f"WS Unexpected handler error {client_id}: {e}")
601
  finally:
602
  await broadcast_message(json.dumps({"type": "user_leave", "payload": {"username": username, "id": client_id}}), exclude_id=client_id);
603
+ await unregister_client(websocket)
604
 
605
 
606
  async def run_websocket_server():
 
607
  if st.session_state.get('server_running_flag', False): return
608
+ st.session_state['server_running_flag'] = True; print("Attempting start WS server 0.0.0.0:8765...")
609
  stop_event = asyncio.Event(); st.session_state['websocket_stop_event'] = stop_event
610
  server = None
611
  try:
612
  server = await websockets.serve(websocket_handler, "0.0.0.0", 8765); st.session_state['server_instance'] = server
613
+ print(f"WS server started: {server.sockets[0].getsockname()}. Waiting for stop signal...")
614
  await stop_event.wait()
615
+ except OSError as e: print(f"### FAILED START WS SERVER: {e}"); st.session_state['server_running_flag'] = False;
616
+ except Exception as e: print(f"### UNEXPECTED WS SERVER ERROR: {e}"); st.session_state['server_running_flag'] = False;
617
  finally:
618
+ print("WS server task finishing...");
619
+ if server: server.close(); await server.wait_closed(); print("WS server closed.")
620
  st.session_state['server_running_flag'] = False; st.session_state['server_instance'] = None; st.session_state['websocket_stop_event'] = None
621
 
622
  def start_websocket_server_thread():
 
623
  if st.session_state.get('server_task') and st.session_state.server_task.is_alive(): return
624
  if st.session_state.get('server_running_flag', False): return
625
+ print("Creating/starting new server thread.");
626
+ def run_loop():
627
  loop = None
628
  try: loop = asyncio.get_running_loop()
629
  except RuntimeError: loop = asyncio.new_event_loop(); asyncio.set_event_loop(loop)
630
  try: loop.run_until_complete(run_websocket_server())
631
  finally:
632
  if loop and not loop.is_closed():
633
+ tasks = asyncio.all_tasks(loop);
634
+ for task in tasks: task.cancel()
635
+ try: loop.run_until_complete(asyncio.gather(*tasks, return_exceptions=True))
636
+ except asyncio.CancelledError: pass
637
+ finally: loop.close(); print("Server thread loop closed.")
638
+ else: print("Server thread loop already closed or None.")
639
  st.session_state.server_task = threading.Thread(target=run_loop, daemon=True); st.session_state.server_task.start(); time.sleep(1.5)
640
+ if not st.session_state.server_task.is_alive(): print("### Server thread failed to stay alive!")
641
+
642
 
643
  # ==============================================================================
644
+ # Streamlit UI Layout Functions
645
  # ==============================================================================
646
 
647
  def render_sidebar():
648
  """Renders the Streamlit sidebar contents."""
649
  with st.sidebar:
650
+ st.header("💾 World Versions")
651
+ st.caption("Load or save named world states.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
652
 
653
+ saved_worlds = get_saved_worlds()
654
+ world_options_display = {os.path.basename(w['filename']): f"{w['name']} ({w['timestamp']})" for w in saved_worlds}
655
+ radio_options_basenames = [None] + [os.path.basename(w['filename']) for w in saved_worlds]
656
+ current_selection_basename = st.session_state.get('current_world_file', None)
657
+ current_radio_index = 0
658
+ if current_selection_basename and current_selection_basename in radio_options_basenames:
659
+ try: current_radio_index = radio_options_basenames.index(current_selection_basename)
660
+ except ValueError: current_radio_index = 0
661
+
662
+ selected_basename = st.radio(
663
+ "Load World:", options=radio_options_basenames, index=current_radio_index,
664
+ format_func=lambda x: "Live State (Unsaved)" if x is None else world_options_display.get(x, x),
665
+ key="world_selector_radio"
666
+ )
667
+
668
+ if selected_basename != current_selection_basename:
669
+ st.session_state.current_world_file = selected_basename
670
+ if selected_basename:
671
+ with st.spinner(f"Loading {selected_basename}..."):
672
+ if load_world_state_from_md(selected_basename):
673
+ run_async(broadcast_world_update)
674
+ st.toast("World loaded!", icon="✅")
675
+ else: st.error("Failed to load world."); st.session_state.current_world_file = None
676
+ else:
677
+ print("Switched to live state."); st.toast("Switched to Live State.")
678
+ st.rerun()
679
+
680
+ st.caption("Download:")
681
+ cols = st.columns([4, 1])
682
+ with cols[0]: st.write("**Name** (Timestamp)")
683
+ with cols[1]: st.write("**DL**")
684
+ display_limit = 10
685
+ for i, world_info in enumerate(saved_worlds):
686
+ if i >= display_limit and len(saved_worlds) > display_limit + 1: # Show expander after limit
687
+ with st.expander(f"Show {len(saved_worlds)-display_limit} more..."):
688
+ for world_info_more in saved_worlds[display_limit:]:
689
+ f_basename = os.path.basename(world_info_more['filename']); f_fullpath = os.path.join(SAVED_WORLDS_DIR, f_basename); display_name = world_info_more.get('name', f_basename); timestamp = world_info_more.get('timestamp', 'N/A')
690
+ colA, colB = st.columns([4, 1]);
691
+ with colA: st.write(f"<small>{display_name} ({timestamp})</small>", unsafe_allow_html=True)
692
+ with colB: st.markdown(get_download_link(f_fullpath, "md"), unsafe_allow_html=True)
693
+ break # Stop outer loop after showing expander
694
+
695
+ f_basename = os.path.basename(world_info['filename']); f_fullpath = os.path.join(SAVED_WORLDS_DIR, f_basename); display_name = world_info.get('name', f_basename); timestamp = world_info.get('timestamp', 'N/A')
696
+ col1, col2 = st.columns([4, 1]);
697
+ with col1: st.write(f"<small>{display_name} ({timestamp})</small>", unsafe_allow_html=True)
698
+ with col2: st.markdown(get_download_link(f_fullpath, "md"), unsafe_allow_html=True)
 
 
 
 
 
 
 
699
 
700
 
 
701
  st.markdown("---")
702
+ st.header("🏗️ Build Tools")
703
+ st.caption("Select an object to place.")
704
+ cols = st.columns(5)
705
+ col_idx = 0
706
+ current_tool = st.session_state.get('selected_object', 'None')
707
+ for emoji, name in PRIMITIVE_MAP.items():
708
+ button_key = f"primitive_{name}"; button_type = "primary" if current_tool == name else "secondary"
709
+ if cols[col_idx % 5].button(emoji, key=button_key, help=name, type=button_type, use_container_width=True):
710
+ if st.session_state.get('selected_object', 'None') != name:
711
+ st.session_state.selected_object = name
712
+ run_async(lambda name_arg=name: streamlit_js_eval(f"updateSelectedObjectType({json.dumps(name_arg)});", key=f"update_tool_js_{name_arg}"))
713
+ st.rerun()
714
+ col_idx += 1
715
  st.markdown("---")
716
+ if st.button("🚫 Clear Tool", key="clear_tool", use_container_width=True):
717
+ if st.session_state.get('selected_object', 'None') != 'None':
718
+ st.session_state.selected_object = 'None';
719
+ run_async(lambda: streamlit_js_eval("updateSelectedObjectType('None');", key="update_tool_js_none"))
720
+ st.rerun()
 
 
 
 
721
 
722
+ st.markdown("---")
723
+ st.header("🗣️ Voice & User")
724
+ current_username = st.session_state.get('username', list(FUN_USERNAMES.keys())[0])
725
+ username_options = list(FUN_USERNAMES.keys()); current_index = 0
726
+ try: current_index = username_options.index(current_username)
727
+ except ValueError: current_index = 0
728
  new_username = st.selectbox("Change Name/Voice", options=username_options, index=current_index, key="username_select", format_func=lambda x: x.split(" ")[0])
729
+ if new_username != st.session_state.username:
730
  old_username = st.session_state.username
731
  change_msg = json.dumps({"type":"user_rename", "payload": {"old_username": old_username, "new_username": new_username}})
732
+ run_async(broadcast_message, change_msg)
733
+ st.session_state.username = new_username; st.session_state.tts_voice = FUN_USERNAMES[new_username]; save_username(st.session_state.username)
 
734
  st.rerun()
735
+ st.session_state['enable_audio'] = st.toggle("Enable TTS Audio", value=st.session_state.get('enable_audio', True))
736
 
737
 
738
  def render_main_content():
739
  """Renders the main content area with tabs."""
740
  st.title(f"{Site_Name} - User: {st.session_state.username}")
741
 
 
 
 
 
742
  tab_world, tab_chat, tab_pdf, tab_files = st.tabs(["🏗️ World Builder", "🗣️ Chat", "📚 PDF Tools", "📂 Files & Settings"])
743
 
744
  # --- World Builder Tab ---
745
  with tab_world:
746
+ st.header("Shared 3D World")
747
+ st.caption("Place objects using the sidebar tools. Changes are shared live!")
748
  current_file_basename = st.session_state.get('current_world_file', None)
749
  if current_file_basename:
750
+ parsed = parse_world_filename(os.path.join(SAVED_WORLDS_DIR, current_file_basename)); st.info(f"Current World: **{parsed['name']}** (`{current_file_basename}`)")
751
+ else: st.info("Live State Active (Unsaved changes only persist if saved)")
 
 
752
 
753
  # Embed HTML Component
754
  html_file_path = 'index.html'
 
758
  try: # Get WS URL (Best effort)
759
  from streamlit.web.server.server import Server
760
  session_info = Server.get_current()._get_session_info(st.runtime.scriptrunner.get_script_run_ctx().session_id)
761
+ server_host = session_info.ws.stream.request.host.split(':')[0]
762
+ ws_url = f"ws://{server_host}:8765"
763
+ except Exception as e: print(f"WS URL detection failed ({e}), using localhost.")
 
764
 
 
765
  js_injection_script = f"""<script>
766
  window.USERNAME = {json.dumps(st.session_state.username)};
767
+ window.WEBSOCKET_URL = {json.dumps(ws_url)};
768
  window.SELECTED_OBJECT_TYPE = {json.dumps(st.session_state.selected_object)};
769
  window.PLOT_WIDTH = {json.dumps(PLOT_WIDTH)};
770
  window.PLOT_DEPTH = {json.dumps(PLOT_DEPTH)};
771
+ console.log("Streamlit State Injected:", {{ username: window.USERNAME, websocketUrl: window.WEBSOCKET_URL, selectedObject: window.SELECTED_OBJECT_TYPE }});
 
772
  </script>"""
773
  html_content_with_state = html_template.replace('</head>', js_injection_script + '\n</head>', 1)
774
  components.html(html_content_with_state, height=700, scrolling=False)
775
+ except FileNotFoundError: st.error(f"CRITICAL ERROR: Could not find '{html_file_path}'.")
776
+ except Exception as e: st.error(f"Error loading 3D component: {e}"); st.exception(e)
777
 
778
  # --- Chat Tab ---
779
  with tab_chat:
780
+ st.header(f"{START_ROOM} Chat")
781
+ chat_history_task = run_async(load_chat_history) # Schedule load
782
+ chat_history = chat_history_task.result() if chat_history_task else st.session_state.chat_history # Get result if task ran sync, else use current state
783
  chat_container = st.container(height=500)
784
  with chat_container:
785
+ if chat_history: st.markdown("----\n".join(reversed(chat_history[-50:])))
786
+ else: st.caption("No chat messages yet.")
787
 
 
788
  message_value = st.text_input("Your Message:", key="message_input", label_visibility="collapsed")
789
+ send_button_clicked = st.button("Send Chat 💬", key="send_chat_button")
790
+ should_autosend = st.session_state.get('autosend', False) and message_value
791
 
792
+ if send_button_clicked or should_autosend:
793
  message_to_send = message_value
794
  if message_to_send.strip() and message_to_send != st.session_state.get('last_message', ''):
795
  st.session_state.last_message = message_to_send
796
+ voice = FUN_USERNAMES.get(st.session_state.username, "en-US-AriaNeural")
797
  ws_message = json.dumps({"type": "chat_message", "payload": {"username": st.session_state.username, "message": message_to_send, "voice": voice}})
798
+ run_async(broadcast_message, ws_message)
799
+ run_async(save_chat_entry, st.session_state.username, message_to_send, voice)
800
+ st.session_state.message_input = "" # Clear state for next run
801
+ st.rerun()
802
  elif send_button_clicked: st.toast("Message empty or same as last.")
803
+ st.checkbox("Autosend Chat", key="autosend")
804
 
805
  # --- PDF Tab ---
806
  with tab_pdf:
807
+ st.header("📚 PDF Tools")
808
+ pdf_file = st.file_uploader("Upload PDF for Audio Conversion", type="pdf", key="pdf_upload")
809
+ max_pages = st.slider('Max Pages to Process', 1, 50, 10, key="pdf_pages")
 
810
  if pdf_file:
811
+ if st.button("Process PDF to Audio", key="process_pdf_button"):
812
+ with st.spinner("Processing PDF... This may take time."):
813
+ process_pdf_tab(pdf_file, max_pages, st.session_state.tts_voice)
814
 
815
  # --- Files & Settings Tab ---
816
  with tab_files:
817
+ st.header("📂 Files & Settings")
818
+ st.subheader("💾 World State Management")
 
 
819
  current_file_basename = st.session_state.get('current_world_file', None)
820
 
 
821
  if current_file_basename:
822
+ parsed = parse_world_filename(os.path.join(SAVED_WORLDS_DIR, current_file_basename))
823
+ save_label = f"Save Changes to '{parsed['name']}'"
824
+ if st.button(save_label, key="save_current_world", help=f"Overwrite '{current_file_basename}'"):
825
+ with st.spinner(f"Overwriting {current_file_basename}..."):
826
+ if save_world_state_to_md(current_file_basename): st.success("Current world saved!")
827
+ else: st.error("Failed to save world state.")
828
+ else:
829
+ st.info("Load a world from the sidebar to enable saving changes to it.")
830
+
831
+ st.subheader("Save As New Version")
832
+ new_name_files = st.text_input("New World Name:", key="new_world_name_files", value=st.session_state.get('new_world_name', 'MyWorld'))
833
+ if st.button("💾 Save Live State as New Version", key="save_new_version_files"):
 
 
 
 
834
  if new_name_files.strip():
835
+ new_filename_base = generate_world_save_filename(new_name_files)
836
+ with st.spinner(f"Saving new version '{new_name_files}'..."):
837
+ if save_world_state_to_md(new_filename_base):
838
+ st.success(f"Saved as {new_filename_base}")
839
+ st.session_state.current_world_file = new_filename_base; st.session_state.new_world_name = "MyWorld"; st.rerun()
840
+ else: st.error("Failed to save new version.")
841
+ else: st.warning("Please enter a name.")
842
+
 
 
 
 
843
  st.subheader("⚙️ Server Status")
844
  col_ws, col_clients = st.columns(2)
845
  with col_ws:
846
  server_alive = st.session_state.get('server_task') and st.session_state.server_task.is_alive(); ws_status = "Running" if server_alive else "Stopped"; st.metric("WebSocket Server", ws_status)
847
+ if not server_alive and st.button("Restart Server Thread", key="restart_ws"): start_websocket_server_thread(); st.rerun()
848
+ with col_clients: st.metric("Connected Clients", len(connected_clients))
849
+
850
+ st.subheader("🗑️ Delete Files")
 
 
 
 
851
  st.warning("Deletion is permanent!", icon="⚠️")
852
  col_del1, col_del2, col_del3, col_del4 = st.columns(4)
853
  with col_del1:
854
+ if st.button("🗑️ Chats", key="del_chat_md"): delete_files([os.path.join(CHAT_DIR, "*.md")]); st.session_state.chat_history = []; st.rerun()
855
  with col_del2:
856
+ if st.button("🗑️ Audio", key="del_audio_mp3"): delete_files([os.path.join(AUDIO_DIR, "*.mp3"), os.path.join(AUDIO_CACHE_DIR, "*.mp3")]); st.session_state.audio_cache = {}; st.rerun()
857
  with col_del3:
858
+ if st.button("🗑️ Worlds", key="del_worlds_md"): delete_files([os.path.join(SAVED_WORLDS_DIR, f"{WORLD_STATE_FILE_MD_PREFIX}*.md")]); st.session_state.current_world_file = None; st.rerun()
859
  with col_del4:
860
+ if st.button("🗑️ All Gen", key="del_all_gen"): delete_files([os.path.join(CHAT_DIR, "*.md"), os.path.join(AUDIO_DIR, "*.mp3"), os.path.join(AUDIO_CACHE_DIR, "*.mp3"), os.path.join(SAVED_WORLDS_DIR, "*.md"), "*.zip"]); st.session_state.chat_history = []; st.session_state.audio_cache = {}; st.session_state.current_world_file = None; st.rerun()
861
 
 
862
  st.subheader("📦 Download Archives")
 
 
 
 
 
 
 
 
863
  zip_files = sorted(glob.glob(os.path.join(MEDIA_DIR,"*.zip")), key=os.path.getmtime, reverse=True)
864
  if zip_files:
865
+ col_zip1, col_zip2, col_zip3 = st.columns(3)
866
+ with col_zip1:
867
+ if st.button("Zip Worlds"): create_zip_of_files([w['filename'] for w in get_saved_worlds()], "Worlds")
868
+ with col_zip2:
869
+ if st.button("Zip Chats"): create_zip_of_files(glob.glob(os.path.join(CHAT_DIR, "*.md")), "Chats")
870
+ with col_zip3:
871
+ if st.button("Zip Audio"): create_zip_of_files(glob.glob(os.path.join(AUDIO_DIR, "*.mp3")) + glob.glob(os.path.join(AUDIO_CACHE_DIR, "*.mp3")), "Audio")
872
+
873
+ st.caption("Existing Zip Files:")
874
+ for zip_file in zip_files: st.markdown(get_download_link(zip_file, "zip"), unsafe_allow_html=True)
875
  else:
876
+ # Ensure correct indentation here for the else block
877
+ st.caption("No zip archives found.")
878
+
879
 
880
  # ==============================================================================
881
+ # Main Execution Logic
882
  # ==============================================================================
883
 
884
+ def initialize_world():
885
+ """Loads initial world state (most recent) if not already done for this session."""
886
+ if not st.session_state.get('initial_world_state_loaded', False):
887
+ print("Performing initial world load for session...")
888
+ saved_worlds = get_saved_worlds()
889
+ loaded_successfully = False
890
+ if saved_worlds:
891
+ latest_world_file_basename = os.path.basename(saved_worlds[0]['filename'])
892
+ print(f"Loading most recent world on startup: {latest_world_file_basename}")
893
+ if load_world_state_from_md(latest_world_file_basename): loaded_successfully = True
894
+ else: print("Failed to load most recent world, starting empty.")
895
+ else: print("No saved worlds found, starting with empty state.")
896
+ if not loaded_successfully:
897
+ with world_objects_lock: world_objects.clear()
898
+ st.session_state.current_world_file = None
899
+ st.session_state.initial_world_state_loaded = True
900
+ print("Initial world load process complete.")
901
+
902
+ if __name__ == "__main__":
903
+ # 1. Initialize session state
904
  init_session_state()
 
 
 
 
 
905
 
906
+ # 2. Start WebSocket server thread if needed
907
  server_thread = st.session_state.get('server_task'); server_alive = server_thread is not None and server_thread.is_alive()
908
  if not st.session_state.get('server_running_flag', False) and not server_alive: start_websocket_server_thread()
909
  elif server_alive and not st.session_state.get('server_running_flag', False): st.session_state.server_running_flag = True
910
 
911
+ # 3. Load initial world state (once per session)
912
+ initialize_world()
 
 
 
 
 
 
 
 
 
913
 
914
+ # 4. Render UI
 
 
915
  render_sidebar()
916
  render_main_content()