File size: 2,011 Bytes
9f6318c
f3612a8
b0828d2
 
 
 
 
 
 
 
f3612a8
44b22d1
fe5abc7
 
 
b1f06e2
 
 
 
69aaa24
1e57c9c
f3612a8
 
b1f06e2
f3612a8
69aaa24
b0828d2
a2e0f7d
b0828d2
56c42a3
b0828d2
69aaa24
 
73b8b37
f3612a8
73b8b37
 
b0828d2
a2e0f7d
 
 
 
 
 
f3612a8
a2e0f7d
b0828d2
56c42a3
b0828d2
a2e0f7d
b0828d2
d7d509b
b0828d2
56c42a3
b0828d2
d7d509b
9f6318c
73b8b37
f3612a8
9f6318c
810a8a7
b0828d2
1e57c9c
b0828d2
f3612a8
1e57c9c
 
 
 
 
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
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
    )