""" Priority-based Question Engine. Selects the single highest priority missing question. """ from typing import List, Optional, Dict, Any def pick_best_question( missing_list: List[str], lang: str, is_psych: bool = False, current_state: Optional[Dict[str, Any]] = None ) -> str: """ Picks the single highest priority missing question for onboarding. Enforces Rule 51 & 52: Short, natural, 1-2 sentences, max 15-20 words. Enforces Rule 67: Asks short clarification if conflict detected in state. """ if isinstance(current_state, dict) and current_state.get("conflicts"): conflict = current_state["conflicts"][-1] item = conflict.get("item", "this condition") if isinstance(item, dict): item = item.get("condition") or item.get("name") or "this condition" lang_clean = lang.lower() if lang else "en" if lang_clean.startswith("ta"): return f"உறுதிப்படுத்த கேட்கிறேன், உங்களுக்கு {item} இருக்கா?" elif lang_clean.startswith("hi"): return f"पुष्टि के लिए, क्या आपको {item} की समस्या है?" else: return f"Just to confirm, do you currently have {item}?" if not missing_list: return _build_completion_reply(lang) field = missing_list[0] has_meds = "medications" in missing_list has_algs = "allergies" in missing_list has_history = "medical_history" in missing_list combine_meds_algs = (field == "medications" and has_algs) or (field == "allergies" and has_meds) combine_history_meds = (field == "medical_history" and has_meds and not has_algs) symptom_name = None if isinstance(current_state, dict): rfv = current_state.get("reason_for_visit") syms = current_state.get("symptoms", []) if rfv and isinstance(rfv, str) and rfv.strip(): symptom_name = rfv.strip() elif syms and isinstance(syms, list) and len(syms) > 0 and isinstance(syms[0], dict) and syms[0].get("name"): symptom_name = syms[0].get("name").strip() lang_clean = lang.lower() if lang else "en" # Tamil if lang_clean.startswith("ta"): if field in ["reason_for_visit", "symptoms"]: return "வணக்கம், உங்களுக்கு என்ன பிரச்சனை இருக்கு?" elif field == "duration": if is_psych: return "இந்த மன அழுத்தம் எப்போலிருந்து இருக்கு?" if symptom_name: return f"இந்த {symptom_name} எத்தனை நாளா இருக்கு?" return "இந்த பிரச்சனை எத்தனை நாளா இருக்கு?" elif field == "medical_history": if is_psych: return "முன்னாடி இதுக்கு சிகிச்சை எடுத்திருக்கீங்களா?" if symptom_name: return f"{symptom_name} தவிர பிபி, சுகர் போன்ற பிரச்சனை இருக்கா?" return "உங்களுக்கு பிபி, சுகர் போன்ற வேறு உடம்பு பிரச்சனை இருக்கா?" elif field == "medications": return "வழக்கமா ஏதேனும் மாத்திரை சாப்பிடுறீங்களா?" elif field == "allergies": return "உங்களுக்கு ஏதேனும் அலர்ஜி இருக்கா?" # Hindi elif lang_clean.startswith("hi"): if field in ["reason_for_visit", "symptoms"]: return "नमस्ते, आपको क्या तकलीफ हो रही है?" elif field == "duration": if symptom_name: return f"यह {symptom_name} कितने दिनों से है?" return "यह तकलीफ कितने दिनों से हो रही है?" elif field == "medical_history": if symptom_name: return f"{symptom_name} के अलावा क्या बीपी या शुगर जैसी बीमारी है?" return "क्या आपको बीपी या शुगर जैसी कोई बीमारी है?" elif field == "medications": return "क्या आप नियमित रूप से कोई दवाई लेते हैं?" elif field == "allergies": return "क्या आपको किसी दवाई से एलर्जी है?" # Telugu elif lang_clean.startswith("te"): if field in ["reason_for_visit", "symptoms"]: return "నమస్కారం, మీ ఆరోగ్య సమస్య ఏమిటి?" elif field == "duration": if symptom_name: return f"ఈ {symptom_name} ఎన్ని రోజుల నుండి ఉంది?" return "ఈ ఇబ్బంది ఎన్ని రోజుల నుండి ఉంది?" elif field == "medical_history": if symptom_name: return f"అర్థమైంది, {symptom_name} కాకుండా బీపీ లేదా షుగర్ ఉందా?" return "మీకు బీపీ లేదా షుగర్ వంటి సమస్యలు ఉన్నాయా?" elif field == "medications": return "మీరు రోజూ ఏమైనా మందులు వాడుతున్నారా?" elif field == "allergies": return "మీకు ఏదైనా మందుల అలర్జీ ఉందా?" # Kannada elif lang_clean.startswith("kn"): if field in ["reason_for_visit", "symptoms"]: return "ನಮಸ್ಕಾರ, ನಿಮ್ಮ સમસ્યા ಏನು?" elif field == "duration": if symptom_name: return f"ಈ {symptom_name} ಎಷ್ಟು ದಿನದಿಂದ ಇದೆ?" return "ಈ ತೊಂದರೆ ಎಷ್ಟು ದಿನದಿಂದ ಇದೆ?" elif field == "medical_history": if symptom_name: return f"{symptom_name} ಹೊರತುಪಡಿಸಿ ಬಿಪಿ ಅಥವಾ ಶುಗರ್ ಇದೆಯಾ?" return "ನಿಮಗೆ ಬಿಪಿ ಅಥವಾ ಶುಗರ್ ಇದೆಯಾ?" elif field == "medications": return "ನೀವು ಯಾವುದೇ ಮಾತ್ರೆಗಳನ್ನು ತೆಗೆದುಕೊಳ್ಳುತ್ತಿದ್ದೀರಾ?" elif field == "allergies": return "ನಿಮಗೆ ಯಾವುದೇ ಅಲರ್ಜಿ ಇದೆಯಾ?" # English Default (Rule 51 & 52: Short, natural, <15-20 words) if field in ["reason_for_visit", "symptoms"]: return "Hello, what symptoms are you experiencing today?" elif field == "duration": if symptom_name: return f"Got it. How long have you had this {symptom_name}?" return "Got it. How long have you had these symptoms?" elif combine_history_meds: if symptom_name: return f"Apart from {symptom_name}, do you have BP, diabetes, or take regular medicines?" return "Do you have any past health conditions or take regular medicines?" elif combine_meds_algs: if symptom_name: return f"Are you taking regular medications for {symptom_name}, or do you have any allergies?" return "Are you currently taking any regular medications, or do you have any allergies?" elif field == "medical_history": if is_psych: return "Have you received treatment or counseling for this before?" if symptom_name: return f"Apart from {symptom_name}, do you have BP, diabetes, or other chronic conditions?" return "Do you have a history of BP, diabetes, or other health conditions?" elif field == "medications": if symptom_name: return f"Are you taking any daily medicines for {symptom_name} or other conditions?" return "Are you currently taking any regular daily medications?" elif field == "allergies": return "Do you have any medicine or food allergies?" return "Could you share any other details about your symptoms?" def _build_completion_reply(lang: str) -> str: lang_clean = lang.lower() if lang else "en" if lang_clean.startswith("ta"): return "நன்றி. உங்கள் விவரங்கள் பதிவு செய்யப்பட்டன." elif lang_clean.startswith("hi"): return "ठीक है, आपकी जानकारी दर्ज हो गई है।" elif lang_clean.startswith("te"): return "ధన్యవాదాలు, మీ వివరాలు నమోదు చేయబడ్డాయి." elif lang_clean.startswith("kn"): return "ಧನ್ಯವಾದಗಳು, ನಿಮ್ಮ ವಿವರಗಳನ್ನು ದಾಖಲಿಸಲಾಗಿದೆ." else: return "Thanks. Your onboarding is complete."