File size: 8,723 Bytes
e9462cd
 
 
ffa674c
778d360
 
ffa674c
 
 
 
 
 
778d360
ffa674c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
778d360
 
ffa674c
 
 
 
 
 
 
 
 
 
 
 
 
 
778d360
ffa674c
778d360
ffa674c
 
 
 
 
 
 
 
778d360
ffa674c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
ffa674c
0e7b568
ffa674c
 
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
from __future__ import annotations

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


def _to_bool(value: str) -> bool:
    return str(value or "").strip().lower() in {"true", "1", "yes", "y"}


def _looks_like_int(value: str) -> bool:
    return bool(re.fullmatch(r"-?\d+", str(value or "").strip()))


def split_unity_message(text: str) -> Dict[str, Any]:
    """
    Parses several Unity-style message formats and always returns a stable dict.

    Supported cases:
    1. Plain user text
    2. Hidden-context prefix + USER:/PROMPT:/MESSAGE:
    3. Structured multiline payload like:
         hint
         x/5 = 12
         0
         solve
         False
         answer
         answer
         algebra
         Quantitative
    """
    raw = (text or "").strip()

    result: Dict[str, Any] = {
        "hidden_context": "",
        "user_text": raw,
        "question_text": "",
        "hint_stage": 0,
        "user_last_input_type": "",
        "built_on_previous_turn": False,
        "help_mode": "",
        "intent": "",
        "topic": "",
        "category": "",
    }

    if not raw:
        return result

    # Case 1: hidden/system context followed by USER:/PROMPT:/MESSAGE:
    tagged_match = re.search(r"(?is)^(.*?)(?:\buser\b|\bprompt\b|\bmessage\b)\s*:\s*(.+)$", raw)
    if tagged_match:
        hidden = (tagged_match.group(1) or "").strip()
        user = (tagged_match.group(2) or "").strip()
        result["hidden_context"] = hidden
        result["user_text"] = user
        return result

    # Case 2: exact structured Unity payload block
    lines = [line.strip() for line in raw.splitlines() if line.strip()]
    if len(lines) >= 9 and _looks_like_int(lines[2]) and lines[4].lower() in {"true", "false"}:
        result["user_text"] = lines[0]
        result["question_text"] = lines[1]
        result["hint_stage"] = int(lines[2])
        result["user_last_input_type"] = lines[3]
        result["built_on_previous_turn"] = _to_bool(lines[4])
        result["help_mode"] = lines[5]
        result["intent"] = lines[6]
        result["topic"] = lines[7]
        result["category"] = lines[8]
        return result

    # Case 3: field-based payload
    def _extract_field(name: str) -> str:
        match = re.search(rf"(?im)^\s*{re.escape(name)}\s*[:=]\s*(.+?)\s*$", raw)
        return (match.group(1) or "").strip() if match else ""

    question_text = _extract_field("question") or _extract_field("question_text")
    user_text = _extract_field("user") or _extract_field("message") or _extract_field("prompt")
    hint_stage_text = _extract_field("hint_stage")
    user_last_input_type = _extract_field("user_last_input_type")
    built_on_previous_turn = _extract_field("built_on_previous_turn")
    help_mode = _extract_field("help_mode")
    intent = _extract_field("intent")
    topic = _extract_field("topic")
    category = _extract_field("category")

    if any([
        question_text,
        user_text,
        hint_stage_text,
        user_last_input_type,
        built_on_previous_turn,
        help_mode,
        intent,
        topic,
        category,
    ]):
        result["question_text"] = question_text
        result["user_text"] = user_text or raw
        result["hint_stage"] = int(hint_stage_text) if _looks_like_int(hint_stage_text) else 0
        result["user_last_input_type"] = user_last_input_type
        result["built_on_previous_turn"] = _to_bool(built_on_previous_turn)
        result["help_mode"] = help_mode
        result["intent"] = intent
        result["topic"] = topic
        result["category"] = category
        return result

    # Fallback: plain message
    return result


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

    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]:
    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

    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()

    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 not result["options"]:
        result["options"] = _extract_options(raw)

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

    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_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_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:
    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"}:
        return "walkthrough"
    if intent in {"explain", "method", "concept"}:
        return "explain"
    if intent == "hint":
        return "hint"
    if intent in {"definition", "instruction"}:
        return intent
    return "answer"