Spaces:
Sleeping
Sleeping
| import unicodedata | |
| import logging | |
| from .fixed_output import run_fixed_output | |
| from .ai_output import run_ai_output | |
| TRIGGER_MAP = {} | |
| def set_trigger_map(mapping): | |
| global TRIGGER_MAP | |
| TRIGGER_MAP = mapping | |
| logging.warning(f"[AGENT] TRIGGER_MAP loaded: {TRIGGER_MAP}") | |
| def normalize(s): | |
| return unicodedata.normalize("NFKC", s) | |
| def clean(s): | |
| s = unicodedata.normalize("NFKC", s) | |
| return "".join(ch for ch in s if ch.isprintable()) | |
| def run_agent(user_input: str, patient_id: int, db): | |
| logging.warning(f"[AGENT] user_input = {user_input}") | |
| token = clean(user_input.split()[-1]) | |
| logging.warning(f"[AGENT] token = {token} (repr={repr(token)})") | |
| # ----------------------------- | |
| # 1. prefix 補全(支援多選) | |
| # ----------------------------- | |
| prefix_candidates = [] | |
| for skill_name, triggers in TRIGGER_MAP.items(): | |
| for t in triggers: | |
| t_clean = clean(t) | |
| if t_clean.startswith(token) and token != t_clean: | |
| prefix_candidates.append(t_clean) | |
| if len(prefix_candidates) > 1: | |
| return { | |
| "type": "trigger-multi-prefix", | |
| "prefix": token, | |
| "candidates": prefix_candidates | |
| } | |
| if len(prefix_candidates) == 1: | |
| return { | |
| "type": "trigger-prefix", | |
| "prefix": token, | |
| "full": prefix_candidates[0] | |
| } | |
| # ----------------------------- | |
| # 2. 完整 trigger | |
| # ----------------------------- | |
| for skill_name, triggers in TRIGGER_MAP.items(): | |
| for t in triggers: | |
| if clean(t) == token: | |
| logging.warning(f"[AGENT] → full trigger match: {t}") | |
| return run_fixed_output(skill_name) | |
| # ----------------------------- | |
| # 3. fallback → AI(帶 patient_id + db) | |
| # ----------------------------- | |
| logging.warning("[AGENT] → fallback to AI") | |
| return run_ai_output( | |
| input_text=user_input, | |
| patient_id=patient_id, | |
| db=db | |
| ) |