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