File size: 2,066 Bytes
3867d94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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"