Spaces:
Running
Running
File size: 5,776 Bytes
08229e6 8644ad0 08229e6 8644ad0 08229e6 8644ad0 08229e6 8644ad0 08229e6 8644ad0 08229e6 8644ad0 08229e6 8644ad0 08229e6 8644ad0 08229e6 | 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 | from __future__ import annotations
import re
from typing import List, Optional
from models import ParsedContext
def extract_section(text: str, start_label: str, end_labels: List[str]) -> str:
start_idx = text.find(start_label)
if start_idx == -1:
return ""
start_idx += len(start_label)
remaining = text[start_idx:]
end_positions = []
for label in end_labels:
idx = remaining.find(label)
if idx != -1:
end_positions.append(idx)
if end_positions:
end_idx = min(end_positions)
return remaining[:end_idx].strip()
return remaining.strip()
def parse_hidden_context(message: str) -> ParsedContext:
raw = message or ""
visible_user_text = ""
question_text = ""
options_text = ""
recent_conversation = ""
if "USER_MESSAGE:" in raw:
visible_user_text = extract_section(raw, "USER_MESSAGE:", [])
elif "Latest player message:" in raw:
visible_user_text = extract_section(raw, "Latest player message:", [])
elif "Player message:" in raw:
visible_user_text = extract_section(raw, "Player message:", [])
else:
visible_user_text = raw.strip()
if "Question:" in raw:
question_text = extract_section(
raw,
"Question:",
[
"\nOptions:",
"\nPlayer balance:",
"\nLast outcome:",
"\nRecent conversation:",
"\nLatest player message:",
"\nPlayer message:",
"\nUSER_MESSAGE:",
],
)
if "Options:" in raw:
options_text = extract_section(
raw,
"Options:",
[
"\nPlayer balance:",
"\nLast outcome:",
"\nRecent conversation:",
"\nLatest player message:",
"\nPlayer message:",
"\nUSER_MESSAGE:",
],
)
if "Recent conversation:" in raw:
recent_conversation = extract_section(
raw,
"Recent conversation:",
[
"\nLatest player message:",
"\nPlayer message:",
"\nUSER_MESSAGE:",
],
)
combined_parts = []
if question_text:
combined_parts.append(question_text.strip())
if options_text:
combined_parts.append(options_text.strip())
combined_question_block = "\n".join([p for p in combined_parts if p]).strip()
if not combined_question_block:
combined_question_block = raw.strip()
return ParsedContext(
raw_message=raw,
visible_user_text=visible_user_text.strip(),
full_context_text=raw.strip(),
question_text=question_text.strip(),
options_text=options_text.strip(),
combined_question_block=combined_question_block.strip(),
recent_conversation=recent_conversation.strip(),
)
def detect_help_mode(user_text: str, supplied: Optional[str]) -> str:
lower = (user_text or "").lower().strip()
if supplied in {"hint", "walkthrough", "answer"}:
if "hint" in lower:
return "hint"
if (
"walkthrough" in lower
or "step by step" in lower
or "work through" in lower
or "explain step by step" in lower
):
return "walkthrough"
if "why" in lower or "explain" in lower:
return "walkthrough"
return supplied
if lower in {"hint", "just a hint", "hint please", "small hint"}:
return "hint"
if "hint" in lower:
return "hint"
if (
"walkthrough" in lower
or "step by step" in lower
or "work through" in lower
or "explain step by step" in lower
):
return "walkthrough"
if "why" in lower or "explain" in lower:
return "walkthrough"
return "answer"
def user_is_referring_to_existing_question(user_text: str, question_text: str) -> bool:
lower = (user_text or "").lower().strip()
if not question_text:
return False
short_refs = {
"help",
"hint",
"why",
"why?",
"explain",
"explain it",
"explain this",
"answer",
"what's the answer",
"what is the answer",
"is it a",
"is it b",
"is it c",
"is it d",
"is it e",
"which one",
"which option",
"what do i do first",
"what do i do",
"how do i start",
"are you sure",
"can you help",
"help me",
"i don't get it",
"i dont get it",
"why is it c",
"why is it b",
"why is it a",
"why is it d",
"why is it e",
"what is the question asking me to do",
"what is this asking",
"what do you need",
}
if lower in short_refs:
return True
if any(
phrase in lower
for phrase in [
"is it ",
"why",
"hint",
"explain",
"answer",
"help",
"first step",
"what is the question asking",
"what is this asking",
"what do you need",
]
):
return True
return False
def mentions_choice_letter(user_text: str) -> Optional[str]:
lower = (user_text or "").lower().strip()
m = re.search(r"\b(?:is it|answer is|it's|its|option|choice)\s*([a-e])\b", lower)
if m:
return m.group(1).upper()
m = re.search(r"\bwhy is it\s*([a-e])\b", lower)
if m:
return m.group(1).upper()
if lower in {"a", "b", "c", "d", "e"}:
return lower.upper()
return None |