File size: 3,448 Bytes
433f3f1
dc3dc12
433f3f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc3dc12
 
 
433f3f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc3dc12
433f3f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from Agent_Chat_Classification_Helper import call_classification_llm
from Classification_parameters import TRACKED_FIELDS, ALLOWED_STORY_TOPICS
from supabase_ie import load_user_info, save_user_info, update_country
from db_5_process_session import _load_history


RECENT_HISTORY_LIMIT = 3

def analyze_message(user_id: str, query: str):
    # --- Load current user_info for this user ---
    user_info = load_user_info(user_id)

    profile = user_info.get("user_profile", {})
    missing_fields = [f for f in ["name", "living_country", "origin_country"] if not profile.get(f)]

    # --- Trim history to the most recent turns ---
    history = _load_history("chat_history_short", user_id)
    all_messages = []
    for session in history.get("sessions", []):
        all_messages.extend(session.get("messages", []))

    recent_history = all_messages[-RECENT_HISTORY_LIMIT:] if all_messages else []
    
    # --- Call LLM for classification + extraction ---
    analysis = call_classification_llm(query, recent_history, user_info, missing_fields)

    extracted_info = analysis.get("extracted_user_info", {})
    relevant_missing = analysis.get("relevant_missing_fields", [])
    topic = analysis.get("topic", "personal")
    response_mode = analysis.get("response_mode", "dialogic")
    topic_for_story = analysis.get("topic_for_story", "none")
    if topic_for_story not in ALLOWED_STORY_TOPICS:
        topic_for_story = "none"
    
    print(f"[DEBUG][CLASSIFICATION_RAW] analysis={analysis}")
    # --- Update user_info in Supabase if new info found ---
    if extracted_info:
        
        print(f"[DEBUG][USER_INFO_BEFORE] {user_info.get('user_profile', {})}")
        profile.update(extracted_info)
        print(f"[DEBUG][USER_INFO_AFTER] {user_info.get('user_profile', {})}")

        user_info["user_profile"] = profile
    
        save_user_info(user_info, user_id)
        print(f"[DEBUG] Updated user_info in Supabase for {user_id}:", user_info)

        # --- Update countries in supabase  ---
        living_country = profile.get("living_country")
        origin_country = profile.get("origin_country")
        if living_country:
            update_country("living", living_country, user_id)
        if origin_country:
            update_country("origin", origin_country, user_id)

    # --- Build return object ---
    result = {
        "topic": topic,
        "response_mode": response_mode,
        "user_info": user_info,
        "relevant_missing": relevant_missing,
        "topic_for_story": topic_for_story, 
        "chunks": [],  # retrieval results can be appended later
        "needs_news_fetch": analysis.get("needs_news_fetch", False),
        "news_topic": analysis.get("news_topic", ""),
        "news_question": analysis.get("news_question", "")
    }
    
    # --- Debug logging ---
    print(f"[DEBUG][CLASSIFICATION] Topic: {topic}")
    print(f"[DEBUG][CLASSIFICATION] Response mode: {response_mode}")
    print(f"[DEBUG][MISSING_INFO] Relevant missing fields: {relevant_missing if relevant_missing else 'None'}")

    profile = user_info.get("user_profile", {})
    living_country = profile.get("living_country")
    origin_country = profile.get("origin_country")
    if living_country or origin_country:
        print(f"[DEBUG][COUNTRY] living_country={living_country}, origin_country={origin_country}")
    else:
        print("[DEBUG][COUNTRY] No country info available")
        
    return result