# app/ai/utils/intent_extractor.py - Helper to extract intent from responses import json import re from typing import Optional, Dict def extract_intent_from_state(state: Dict) -> Optional[str]: """ Extract intent from state data. Checks multiple sources for intent value. Returns: str: "list", "search", or None """ # Priority 1: Check ai_reply for JSON with intent ai_reply = state.get("ai_reply", "") try: # Try to parse if it's JSON if ai_reply.strip().startswith("{"): data = json.loads(ai_reply) if "intent" in data: return data["intent"] except: pass # Priority 2: Check if intent is in the search_query or listing_type listing_type = state.get("listing_type") if listing_type: return "list" search_query = state.get("search_query") if search_query: return "search" # Priority 3: Check last message for keywords messages = state.get("messages", []) if messages: last_msg = messages[-1].get("content", "").lower() search_keywords = ["search", "find", "look for", "show me", "see", "apartments", "houses", "flats"] list_keywords = ["list", "publish", "sell", "rent out", "want to list"] if any(kw in last_msg for kw in search_keywords): return "search" elif any(kw in last_msg for kw in list_keywords): return "list" return None def should_check_permissions(state: Dict) -> bool: """ Determine if we should route to permission check. Returns: bool: True if intent is search or list """ intent = extract_intent_from_state(state) return intent in ["search", "list"] def should_search(state: Dict) -> bool: """Check if user wants to search""" return extract_intent_from_state(state) == "search" def should_create_listing(state: Dict) -> bool: """Check if user wants to create listing""" return extract_intent_from_state(state) == "list"