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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def _staged_scaffold_lines(
|
| 2 |
result: Any,
|
| 3 |
hint_stage: int,
|
|
@@ -23,7 +248,6 @@ def _staged_scaffold_lines(
|
|
| 23 |
hint_ladder = _coerce_list(getattr(scaffold, "hint_ladder", []))
|
| 24 |
key_operations = _coerce_list(getattr(scaffold, "key_operations", []))
|
| 25 |
|
| 26 |
-
# STAGE 0 = full scaffold / walkthrough
|
| 27 |
if stage == 0:
|
| 28 |
if concept and transparency >= 0.75:
|
| 29 |
output.append("Core idea:")
|
|
@@ -82,7 +306,6 @@ def _staged_scaffold_lines(
|
|
| 82 |
|
| 83 |
return output
|
| 84 |
|
| 85 |
-
# STAGE 1 = short hint only
|
| 86 |
if stage == 1:
|
| 87 |
if ask:
|
| 88 |
output.append("What to identify first:")
|
|
@@ -100,7 +323,6 @@ def _staged_scaffold_lines(
|
|
| 100 |
|
| 101 |
return output
|
| 102 |
|
| 103 |
-
# STAGE 2 = deeper structure only
|
| 104 |
if stage == 2:
|
| 105 |
if setup_actions:
|
| 106 |
output.append("Set-up path:")
|
|
@@ -127,7 +349,6 @@ def _staged_scaffold_lines(
|
|
| 127 |
|
| 128 |
return output
|
| 129 |
|
| 130 |
-
# STAGE 3+ = fuller help, still no answer reveal
|
| 131 |
if setup_actions:
|
| 132 |
output.append("Set-up path:")
|
| 133 |
for item in _limit_steps(setup_actions, verbosity, minimum=2):
|
|
@@ -168,4 +389,67 @@ def _staged_scaffold_lines(
|
|
| 168 |
for item in common_traps[:3]:
|
| 169 |
output.append(f"- {item}")
|
| 170 |
|
| 171 |
-
return output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import re
|
| 4 |
+
from typing import Any, List, Optional
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def style_prefix(tone: float) -> str:
|
| 8 |
+
if tone < 0.2:
|
| 9 |
+
return ""
|
| 10 |
+
if tone < 0.45:
|
| 11 |
+
return "ok.."
|
| 12 |
+
if tone < 0.75:
|
| 13 |
+
return "Let’s work through it."
|
| 14 |
+
return "You’ve got this — let’s solve it cleanly."
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def _normalize_key(text: str) -> str:
|
| 18 |
+
text = (text or "").strip().lower()
|
| 19 |
+
text = text.replace("’", "'")
|
| 20 |
+
text = re.sub(r"\s+", " ", text)
|
| 21 |
+
return text
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def _clean_lines(core: str) -> list[str]:
|
| 25 |
+
lines = []
|
| 26 |
+
for line in (core or "").splitlines():
|
| 27 |
+
cleaned = line.strip()
|
| 28 |
+
if cleaned:
|
| 29 |
+
lines.append(cleaned)
|
| 30 |
+
return lines
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def _dedupe_lines(lines: list[str]) -> list[str]:
|
| 34 |
+
seen = set()
|
| 35 |
+
output = []
|
| 36 |
+
for line in lines:
|
| 37 |
+
key = _normalize_key(line)
|
| 38 |
+
if key and key not in seen:
|
| 39 |
+
seen.add(key)
|
| 40 |
+
output.append(line.strip())
|
| 41 |
+
return output
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def _coerce_string(value: Any) -> str:
|
| 45 |
+
return (value or "").strip() if isinstance(value, str) else ""
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def _coerce_list(value: Any) -> List[str]:
|
| 49 |
+
if not value:
|
| 50 |
+
return []
|
| 51 |
+
if isinstance(value, list):
|
| 52 |
+
return [str(v).strip() for v in value if str(v).strip()]
|
| 53 |
+
if isinstance(value, tuple):
|
| 54 |
+
return [str(v).strip() for v in value if str(v).strip()]
|
| 55 |
+
if isinstance(value, str):
|
| 56 |
+
text = value.strip()
|
| 57 |
+
return [text] if text else []
|
| 58 |
+
return []
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def _limit_steps(steps: List[str], verbosity: float, minimum: int = 1) -> List[str]:
|
| 62 |
+
if not steps:
|
| 63 |
+
return []
|
| 64 |
+
if verbosity < 0.25:
|
| 65 |
+
limit = minimum
|
| 66 |
+
elif verbosity < 0.5:
|
| 67 |
+
limit = max(minimum, 2)
|
| 68 |
+
elif verbosity < 0.75:
|
| 69 |
+
limit = max(minimum, 3)
|
| 70 |
+
else:
|
| 71 |
+
limit = max(minimum, 5)
|
| 72 |
+
return steps[:limit]
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def _why_line(topic: str) -> str:
|
| 76 |
+
topic = (topic or "").lower()
|
| 77 |
+
|
| 78 |
+
if topic == "algebra":
|
| 79 |
+
return "Why: algebra works by keeping the relationship balanced while undoing the operations attached to the variable."
|
| 80 |
+
if topic == "percent":
|
| 81 |
+
return "Why: percent questions depend on choosing the correct base before doing any calculation."
|
| 82 |
+
if topic == "ratio":
|
| 83 |
+
return "Why: ratio questions depend on preserving the comparison and using one shared scale factor."
|
| 84 |
+
if topic == "probability":
|
| 85 |
+
return "Why: probability compares successful outcomes to all possible outcomes."
|
| 86 |
+
if topic == "statistics":
|
| 87 |
+
return "Why: the right method depends on which summary measure the question actually asks for."
|
| 88 |
+
if topic == "geometry":
|
| 89 |
+
return "Why: geometry depends on the relationships between the parts of the figure."
|
| 90 |
+
if topic == "number_theory":
|
| 91 |
+
return "Why: number properties follow fixed rules about divisibility, factors, and remainders."
|
| 92 |
+
return "Why: start with the structure of the problem before calculating."
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def _extract_topic_from_text(text: str, fallback: Optional[str] = None) -> str:
|
| 96 |
+
low = (text or "").lower()
|
| 97 |
+
if fallback:
|
| 98 |
+
return fallback
|
| 99 |
+
if any(word in low for word in ["equation", "variable", "isolate", "algebra"]):
|
| 100 |
+
return "algebra"
|
| 101 |
+
if any(word in low for word in ["percent", "percentage", "%"]):
|
| 102 |
+
return "percent"
|
| 103 |
+
if any(word in low for word in ["ratio", "proportion"]):
|
| 104 |
+
return "ratio"
|
| 105 |
+
if any(word in low for word in ["probability", "outcome", "chance", "odds"]):
|
| 106 |
+
return "probability"
|
| 107 |
+
if any(word in low for word in ["mean", "median", "average"]):
|
| 108 |
+
return "statistics"
|
| 109 |
+
if any(word in low for word in ["triangle", "circle", "angle", "area", "perimeter"]):
|
| 110 |
+
return "geometry"
|
| 111 |
+
if any(word in low for word in ["integer", "factor", "multiple", "prime", "remainder"]):
|
| 112 |
+
return "number_theory"
|
| 113 |
+
return "general"
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def _format_answer_mode(
|
| 117 |
+
lines: List[str],
|
| 118 |
+
topic: str,
|
| 119 |
+
tone: float,
|
| 120 |
+
verbosity: float,
|
| 121 |
+
transparency: float,
|
| 122 |
+
) -> str:
|
| 123 |
+
output: List[str] = []
|
| 124 |
+
prefix = style_prefix(tone)
|
| 125 |
+
if prefix:
|
| 126 |
+
output.append(prefix)
|
| 127 |
+
output.append("")
|
| 128 |
+
|
| 129 |
+
limited = _limit_steps(lines, verbosity, minimum=2)
|
| 130 |
+
if limited:
|
| 131 |
+
output.append("Answer path:")
|
| 132 |
+
if len(limited) >= 1:
|
| 133 |
+
output.append(f"- What to identify: {limited[0]}")
|
| 134 |
+
if len(limited) >= 2:
|
| 135 |
+
output.append(f"- First move: {limited[1]}")
|
| 136 |
+
if len(limited) >= 3:
|
| 137 |
+
output.append(f"- Next step: {limited[2]}")
|
| 138 |
+
for extra in limited[3:]:
|
| 139 |
+
output.append(f"- Keep in mind: {extra}")
|
| 140 |
+
|
| 141 |
+
if transparency >= 0.8:
|
| 142 |
+
output.append("")
|
| 143 |
+
output.append(_why_line(topic))
|
| 144 |
+
|
| 145 |
+
return "\n".join(output).strip()
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
def format_reply(
|
| 149 |
+
core: str,
|
| 150 |
+
tone: float,
|
| 151 |
+
verbosity: float,
|
| 152 |
+
transparency: float,
|
| 153 |
+
help_mode: str,
|
| 154 |
+
hint_stage: int = 0,
|
| 155 |
+
topic: Optional[str] = None,
|
| 156 |
+
) -> str:
|
| 157 |
+
prefix = style_prefix(tone)
|
| 158 |
+
core = (core or "").strip()
|
| 159 |
+
|
| 160 |
+
if not core:
|
| 161 |
+
return prefix or "Start with the structure of the problem."
|
| 162 |
+
|
| 163 |
+
lines = _dedupe_lines(_clean_lines(core))
|
| 164 |
+
if not lines:
|
| 165 |
+
return prefix or "Start with the structure of the problem."
|
| 166 |
+
|
| 167 |
+
resolved_topic = _extract_topic_from_text(core, topic)
|
| 168 |
+
|
| 169 |
+
if help_mode == "answer":
|
| 170 |
+
return _format_answer_mode(lines, resolved_topic, tone, verbosity, transparency)
|
| 171 |
+
|
| 172 |
+
shown = _limit_steps(lines, verbosity, minimum=1)
|
| 173 |
+
output: List[str] = []
|
| 174 |
+
|
| 175 |
+
if prefix:
|
| 176 |
+
output.append(prefix)
|
| 177 |
+
output.append("")
|
| 178 |
+
|
| 179 |
+
if help_mode == "hint":
|
| 180 |
+
output.append("Hint:")
|
| 181 |
+
output.append(f"- {shown[0]}")
|
| 182 |
+
if transparency >= 0.8:
|
| 183 |
+
output.append("")
|
| 184 |
+
output.append(_why_line(resolved_topic))
|
| 185 |
+
return "\n".join(output).strip()
|
| 186 |
+
|
| 187 |
+
if help_mode in {"instruction", "step_by_step", "walkthrough"}:
|
| 188 |
+
label = "First step:" if help_mode == "instruction" else "Walkthrough:"
|
| 189 |
+
output.append(label)
|
| 190 |
+
for line in shown:
|
| 191 |
+
output.append(f"- {line}")
|
| 192 |
+
if transparency >= 0.8:
|
| 193 |
+
output.append("")
|
| 194 |
+
output.append(_why_line(resolved_topic))
|
| 195 |
+
return "\n".join(output).strip()
|
| 196 |
+
|
| 197 |
+
if help_mode in {"method", "explain", "concept", "definition"}:
|
| 198 |
+
label = {
|
| 199 |
+
"method": "Method:",
|
| 200 |
+
"explain": "Explanation:",
|
| 201 |
+
"concept": "Key idea:",
|
| 202 |
+
"definition": "Key idea:",
|
| 203 |
+
}[help_mode]
|
| 204 |
+
output.append(label)
|
| 205 |
+
for line in shown:
|
| 206 |
+
output.append(f"- {line}")
|
| 207 |
+
if transparency >= 0.75:
|
| 208 |
+
output.append("")
|
| 209 |
+
output.append(_why_line(resolved_topic))
|
| 210 |
+
return "\n".join(output).strip()
|
| 211 |
+
|
| 212 |
+
for line in shown:
|
| 213 |
+
output.append(f"- {line}")
|
| 214 |
+
|
| 215 |
+
if transparency >= 0.85:
|
| 216 |
+
output.append("")
|
| 217 |
+
output.append(_why_line(resolved_topic))
|
| 218 |
+
|
| 219 |
+
return "\n".join(output).strip()
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
def _get_scaffold(result: Any):
|
| 223 |
+
return getattr(result, "scaffold", None)
|
| 224 |
+
|
| 225 |
+
|
| 226 |
def _staged_scaffold_lines(
|
| 227 |
result: Any,
|
| 228 |
hint_stage: int,
|
|
|
|
| 248 |
hint_ladder = _coerce_list(getattr(scaffold, "hint_ladder", []))
|
| 249 |
key_operations = _coerce_list(getattr(scaffold, "key_operations", []))
|
| 250 |
|
|
|
|
| 251 |
if stage == 0:
|
| 252 |
if concept and transparency >= 0.75:
|
| 253 |
output.append("Core idea:")
|
|
|
|
| 306 |
|
| 307 |
return output
|
| 308 |
|
|
|
|
| 309 |
if stage == 1:
|
| 310 |
if ask:
|
| 311 |
output.append("What to identify first:")
|
|
|
|
| 323 |
|
| 324 |
return output
|
| 325 |
|
|
|
|
| 326 |
if stage == 2:
|
| 327 |
if setup_actions:
|
| 328 |
output.append("Set-up path:")
|
|
|
|
| 349 |
|
| 350 |
return output
|
| 351 |
|
|
|
|
| 352 |
if setup_actions:
|
| 353 |
output.append("Set-up path:")
|
| 354 |
for item in _limit_steps(setup_actions, verbosity, minimum=2):
|
|
|
|
| 389 |
for item in common_traps[:3]:
|
| 390 |
output.append(f"- {item}")
|
| 391 |
|
| 392 |
+
return output
|
| 393 |
+
|
| 394 |
+
|
| 395 |
+
def format_explainer_response(
|
| 396 |
+
result: Any,
|
| 397 |
+
tone: float,
|
| 398 |
+
verbosity: float,
|
| 399 |
+
transparency: float,
|
| 400 |
+
hint_stage: int = 0,
|
| 401 |
+
) -> str:
|
| 402 |
+
if not result or not getattr(result, "understood", False):
|
| 403 |
+
return "I can help explain what the question is asking, but I need the full wording of the question."
|
| 404 |
+
|
| 405 |
+
output: List[str] = []
|
| 406 |
+
prefix = style_prefix(tone)
|
| 407 |
+
if prefix:
|
| 408 |
+
output.append(prefix)
|
| 409 |
+
|
| 410 |
+
stage = max(0, int(getattr(result, "hint_stage", hint_stage) or 0))
|
| 411 |
+
|
| 412 |
+
summary = _coerce_string(getattr(result, "summary", ""))
|
| 413 |
+
teaching_points = _coerce_list(getattr(result, "teaching_points", []))
|
| 414 |
+
|
| 415 |
+
if stage == 0 and summary:
|
| 416 |
+
if output:
|
| 417 |
+
output.append("")
|
| 418 |
+
output.append(summary)
|
| 419 |
+
|
| 420 |
+
scaffold_lines = _staged_scaffold_lines(
|
| 421 |
+
result=result,
|
| 422 |
+
hint_stage=stage,
|
| 423 |
+
verbosity=verbosity,
|
| 424 |
+
transparency=transparency,
|
| 425 |
+
)
|
| 426 |
+
if scaffold_lines:
|
| 427 |
+
if output:
|
| 428 |
+
output.append("")
|
| 429 |
+
output.extend(scaffold_lines)
|
| 430 |
+
|
| 431 |
+
if teaching_points and (stage == 0 or stage >= 3) and verbosity >= 0.55:
|
| 432 |
+
output.append("")
|
| 433 |
+
output.append("Key teaching points:")
|
| 434 |
+
for item in _limit_steps(teaching_points, verbosity, minimum=2):
|
| 435 |
+
output.append(f"- {item}")
|
| 436 |
+
|
| 437 |
+
topic = _extract_topic_from_text(
|
| 438 |
+
f"{summary} {' '.join(teaching_points)}",
|
| 439 |
+
getattr(result, "topic", None),
|
| 440 |
+
)
|
| 441 |
+
|
| 442 |
+
if transparency >= 0.8 and stage not in {1, 2}:
|
| 443 |
+
output.append("")
|
| 444 |
+
output.append(_why_line(topic))
|
| 445 |
+
|
| 446 |
+
cleaned: List[str] = []
|
| 447 |
+
previous_blank = False
|
| 448 |
+
for line in output:
|
| 449 |
+
is_blank = not line.strip()
|
| 450 |
+
if is_blank and previous_blank:
|
| 451 |
+
continue
|
| 452 |
+
cleaned.append(line)
|
| 453 |
+
previous_blank = is_blank
|
| 454 |
+
|
| 455 |
+
return "\n".join(cleaned).strip()
|