File size: 6,155 Bytes
e9462cd
 
 
1460fe5
778d360
 
 
 
 
1460fe5
 
778d360
1460fe5
778d360
 
 
 
 
1460fe5
 
 
 
778d360
1460fe5
 
 
778d360
 
 
 
1460fe5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
778d360
 
1460fe5
 
778d360
 
 
1460fe5
 
 
 
 
 
 
 
 
778d360
1460fe5
 
778d360
1460fe5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
778d360
 
1460fe5
778d360
1460fe5
778d360
 
1460fe5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
778d360
 
1460fe5
e9462cd
 
1460fe5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e7b568
e9462cd
0e7b568
 
e9462cd
0e7b568
 
 
 
 
 
 
e9462cd
0e7b568
 
e9462cd
0e7b568
 
 
 
 
4e50c8f
0e7b568
 
fa70564
0e7b568
 
 
 
 
 
 
fa70564
0e7b568
 
fa70564
0e7b568
 
 
 
 
4e50c8f
0e7b568
fa70564
0e7b568
4e50c8f
 
 
 
 
 
0e7b568
 
 
 
 
 
 
 
 
 
 
e9462cd
0e7b568
fa70564
e9462cd
 
fa70564
0e7b568
 
fa70564
 
0e7b568
 
 
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
from __future__ import annotations

import re
from typing import Any, Dict, List, Optional, Tuple


def split_unity_message(text: str) -> Tuple[str, str]:
    """
    Splits a Unity-style message into:
    - hidden/system/game context prefix
    - actual user-facing message

    If no obvious split is found, returns ("", original_text).
    """
    raw = (text or "").strip()
    if not raw:
        return "", ""

    # Pattern like:
    # CONTEXT: ...
    # USER: ...
    m = re.search(r"(?is)^(.*?)(?:\buser\b|\bprompt\b|\bmessage\b)\s*:\s*(.+)$", raw)
    if m:
        hidden = (m.group(1) or "").strip()
        user = (m.group(2) or "").strip()
        return hidden, user

    return "", raw


def _extract_options(text: str) -> List[str]:
    if not text:
        return []

    lines = [line.strip() for line in text.splitlines() if line.strip()]
    options: List[str] = []

    for line in lines:
        if re.match(r"^[A-E][\)\.\:]\s*", line, flags=re.I):
            options.append(re.sub(r"^[A-E][\)\.\:]\s*", "", line, flags=re.I).strip())

    if options:
        return options

    # fallback: inline A) ... B) ...
    matches = re.findall(r"(?:^|\s)([A-E])[\)\.\:]\s*(.*?)(?=(?:\s+[A-E][\)\.\:])|$)", text, flags=re.I | re.S)
    if matches:
        return [m[1].strip() for m in matches if m[1].strip()]

    return []


def extract_game_context_fields(text: str) -> Dict[str, Any]:
    """
    Extracts lightweight structured fields from hidden Unity/game context.
    Always returns stable keys expected by app.py.
    """
    raw = (text or "").strip()

    result: Dict[str, Any] = {
        "question": "",
        "options": [],
        "difficulty": None,
        "category": None,
        "money": None,
        "has_choices": False,
        "looks_like_quant": False,
    }

    if not raw:
        return result

    # question
    q_match = re.search(r"\bquestion\s*[:=]\s*(.+?)(?=\n[A-Za-z_ ]+\s*[:=]|\Z)", raw, flags=re.I | re.S)
    if q_match:
        result["question"] = q_match.group(1).strip()

    # options block
    opt_match = re.search(r"\b(?:options|choices|answers)\s*[:=]\s*(.+?)(?=\n[A-Za-z_ ]+\s*[:=]|\Z)", raw, flags=re.I | re.S)
    if opt_match:
        result["options"] = _extract_options(opt_match.group(1))

    # if no explicit options block, scan whole context
    if not result["options"]:
        result["options"] = _extract_options(raw)

    result["has_choices"] = len(result["options"]) > 0

    # difficulty
    difficulty_match = re.search(r"\bdifficulty\s*[:=]\s*([A-Za-z0-9_\- ]+)", raw, flags=re.I)
    if difficulty_match:
        result["difficulty"] = difficulty_match.group(1).strip()

    # category/topic
    category_match = re.search(r"\b(?:category|topic)\s*[:=]\s*([A-Za-z0-9_\- /]+)", raw, flags=re.I)
    if category_match:
        result["category"] = category_match.group(1).strip()

    # money/balance
    money_match = re.search(r"\b(?:money|balance|bank)\s*[:=]\s*([\-]?\d+(?:\.\d+)?)", raw, flags=re.I)
    if money_match:
        try:
            result["money"] = float(money_match.group(1))
        except Exception:
            pass

    lower = raw.lower()
    result["looks_like_quant"] = any(
        token in lower
        for token in [
            "solve",
            "equation",
            "percent",
            "%",
            "ratio",
            "probability",
            "mean",
            "median",
            "algebra",
            "integer",
            "triangle",
            "circle",
        ]
    )

    return result


def detect_intent(text: str, incoming_help_mode: Optional[str] = None) -> str:
    """
    Returns one of:
    answer, hint, instruction, walkthrough, explain, method, definition, concept
    """
    forced = (incoming_help_mode or "").strip().lower()
    if forced in {
        "answer",
        "hint",
        "instruction",
        "walkthrough",
        "step_by_step",
        "explain",
        "method",
        "definition",
        "concept",
    }:
        return forced

    t = (text or "").strip().lower()

    if not t:
        return "answer"

    if (
        re.search(r"\bdefine\b", t)
        or re.search(r"\bdefinition\b", t)
        or re.search(r"\bwhat does\b", t)
        or re.search(r"\bwhat is meant by\b", t)
    ):
        return "definition"

    if re.search(r"\bhint\b", t) or re.search(r"\bclue\b", t) or re.search(r"\bnudge\b", t):
        return "hint"

    if (
        re.search(r"\bfirst step\b", t)
        or re.search(r"\bnext step\b", t)
        or re.search(r"\bwhat should i do first\b", t)
        or re.search(r"\bgive me the first step\b", t)
        or re.search(r"\bspecific step\b", t)
    ):
        return "instruction"

    if (
        re.search(r"\bwalk ?through\b", t)
        or re.search(r"\bstep by step\b", t)
        or re.search(r"\bfull working\b", t)
        or re.search(r"\bwork through\b", t)
    ):
        return "walkthrough"

    if re.search(r"\bexplain\b", t) or re.search(r"\bwhy\b", t):
        return "explain"

    if (
        re.search(r"\bmethod\b", t)
        or re.search(r"\bapproach\b", t)
        or re.search(r"\bhow do i solve\b", t)
        or re.search(r"\bhow to solve\b", t)
        or re.search(r"\bhow do i do this\b", t)
    ):
        return "method"

    if (
        re.search(r"\bconcept\b", t)
        or re.search(r"\bprinciple\b", t)
        or re.search(r"\brule\b", t)
        or re.search(r"\bwhat is the idea\b", t)
    ):
        return "concept"

    if (
        re.search(r"\bsolve\b", t)
        or re.search(r"\bwhat is\b", t)
        or re.search(r"\bfind\b", t)
        or re.search(r"\bgive (?:me )?the answer\b", t)
        or re.search(r"\bjust the answer\b", t)
        or re.search(r"\banswer only\b", t)
        or re.search(r"\bcalculate\b", t)
    ):
        return "answer"

    return "answer"


def intent_to_help_mode(intent: str) -> str:
    if intent in {"walkthrough", "step_by_step", "explain", "method", "concept"}:
        return "walkthrough"
    if intent == "hint":
        return "hint"
    if intent in {"definition", "instruction"}:
        return intent
    return "answer"