"""UI state management for the document intelligence demo. Per-session state via factory function + shared caches. """ import hashlib def create_initial_state() -> dict: """Create a fresh per-session state dictionary. Returns: Initial state with all fields set to defaults. """ return { "uploaded_file_hash": None, "uploaded_file_bytes": None, "parsed_result": {}, "page_images": [], "figures_info": [], "selected_figure": None, "last_csv": None, "current_figure_index": 0, "conversation_history": [], "current_image_path": None, } # Module-level shared caches (keyed by content hash, safe to share across sessions) parse_cache: dict[str, dict] = {} page_cache: dict[str, list] = {} def hash_bytes(data: bytes) -> str: """Generate SHA256 hash of bytes. Args: data: Bytes to hash. Returns: Hex string of SHA256 hash. """ return hashlib.sha256(data).hexdigest()