""" Monkey-patch for sender attribution bug fix in modules/api.py This module patches the akira_endpoint to properly validate and reconstruct sender names """ import sys from functools import wraps def patch_akira_api(): """Apply the sender attribution fix by monkey-patching the modules.api module""" try: from modules import api # Store original endpoint method original_get_blueprint = api.get_blueprint def patched_get_blueprint(): """Wrapper that patches the blueprint routes""" bp = original_get_blueprint() # Get the akira_endpoint from the blueprint for rule in bp.defsurl_map.iter_rules(): if rule.endpoint == 'akira_endpoint': original_endpoint = bp.view_functions.get('akira_endpoint') if original_endpoint: # Wrap the endpoint @wraps(original_endpoint) def patched_akira_endpoint(*args, **kwargs): # Call original result = original_endpoint(*args, **kwargs) return result bp.view_functions['akira_endpoint'] = patched_akira_endpoint break return bp # Replace the function api.get_blueprint = patched_get_blueprint print("✅ Sender attribution fix monkey-patch applied to modules.api") return True except Exception as e: print(f"⚠️ Failed to apply monkey-patch: {e}") return False # Auto-apply when imported try: patch_akira_api() except Exception as e: print(f"Error during auto-patch: {e}")