Update formatting.py
Browse files- formatting.py +27 -21
formatting.py
CHANGED
|
@@ -113,6 +113,19 @@ def _extract_topic_from_text(text: str, fallback: Optional[str] = None) -> str:
|
|
| 113 |
return "general"
|
| 114 |
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
def _format_answer_mode(
|
| 117 |
lines: List[str],
|
| 118 |
topic: str,
|
|
@@ -126,17 +139,13 @@ def _format_answer_mode(
|
|
| 126 |
output.append(prefix)
|
| 127 |
output.append("")
|
| 128 |
|
| 129 |
-
|
|
|
|
|
|
|
| 130 |
if limited:
|
| 131 |
output.append("Answer path:")
|
| 132 |
-
|
| 133 |
-
output.append(f"-
|
| 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("")
|
|
@@ -165,11 +174,12 @@ def format_reply(
|
|
| 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(
|
| 171 |
|
| 172 |
-
shown = _limit_steps(
|
| 173 |
output: List[str] = []
|
| 174 |
|
| 175 |
if prefix:
|
|
@@ -177,7 +187,7 @@ def format_reply(
|
|
| 177 |
output.append("")
|
| 178 |
|
| 179 |
if help_mode == "hint":
|
| 180 |
-
index = max(0, min(int(hint_stage or 0), len(shown) - 1))
|
| 181 |
output.append("Hint:")
|
| 182 |
output.append(f"- {shown[index]}")
|
| 183 |
if transparency >= 0.8:
|
|
@@ -188,8 +198,7 @@ def format_reply(
|
|
| 188 |
if help_mode == "walkthrough":
|
| 189 |
output.append("Walkthrough:")
|
| 190 |
for line in shown:
|
| 191 |
-
|
| 192 |
-
output.append(f"- {clean_line}")
|
| 193 |
if transparency >= 0.8:
|
| 194 |
output.append("")
|
| 195 |
output.append(_why_line(resolved_topic))
|
|
@@ -198,8 +207,7 @@ def format_reply(
|
|
| 198 |
if help_mode in {"instruction", "step_by_step"}:
|
| 199 |
output.append("First step:")
|
| 200 |
for line in shown:
|
| 201 |
-
|
| 202 |
-
output.append(f"- {clean_line}")
|
| 203 |
if transparency >= 0.8:
|
| 204 |
output.append("")
|
| 205 |
output.append(_why_line(resolved_topic))
|
|
@@ -214,16 +222,14 @@ def format_reply(
|
|
| 214 |
}[help_mode]
|
| 215 |
output.append(label)
|
| 216 |
for line in shown:
|
| 217 |
-
|
| 218 |
-
output.append(f"- {clean_line}")
|
| 219 |
if transparency >= 0.75:
|
| 220 |
output.append("")
|
| 221 |
output.append(_why_line(resolved_topic))
|
| 222 |
return "\n".join(output).strip()
|
| 223 |
|
| 224 |
for line in shown:
|
| 225 |
-
|
| 226 |
-
output.append(f"- {clean_line}")
|
| 227 |
|
| 228 |
if transparency >= 0.85:
|
| 229 |
output.append("")
|
|
@@ -231,6 +237,7 @@ def format_reply(
|
|
| 231 |
|
| 232 |
return "\n".join(output).strip()
|
| 233 |
|
|
|
|
| 234 |
def _get_scaffold(result: Any):
|
| 235 |
return getattr(result, "scaffold", None)
|
| 236 |
|
|
@@ -261,7 +268,6 @@ def _staged_scaffold_lines(
|
|
| 261 |
hint_ladder = _coerce_list(getattr(scaffold, "hint_ladder", []))
|
| 262 |
key_operations = _coerce_list(getattr(scaffold, "key_operations", []))
|
| 263 |
|
| 264 |
-
# Hint mode should use the hint ladder directly, not dump the whole scaffold again.
|
| 265 |
if help_mode == "hint":
|
| 266 |
if hint_ladder:
|
| 267 |
if stage <= 0:
|
|
|
|
| 113 |
return "general"
|
| 114 |
|
| 115 |
|
| 116 |
+
def _strip_bullet_prefix(text: str) -> str:
|
| 117 |
+
return re.sub(r"^\s*-\s*", "", (text or "").strip())
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
def _normalize_display_lines(lines: List[str]) -> List[str]:
|
| 121 |
+
cleaned: List[str] = []
|
| 122 |
+
for line in lines:
|
| 123 |
+
item = _strip_bullet_prefix(line)
|
| 124 |
+
if item:
|
| 125 |
+
cleaned.append(item)
|
| 126 |
+
return cleaned
|
| 127 |
+
|
| 128 |
+
|
| 129 |
def _format_answer_mode(
|
| 130 |
lines: List[str],
|
| 131 |
topic: str,
|
|
|
|
| 139 |
output.append(prefix)
|
| 140 |
output.append("")
|
| 141 |
|
| 142 |
+
normalized = _normalize_display_lines(lines)
|
| 143 |
+
limited = _limit_steps(normalized, verbosity, minimum=2)
|
| 144 |
+
|
| 145 |
if limited:
|
| 146 |
output.append("Answer path:")
|
| 147 |
+
for step in limited:
|
| 148 |
+
output.append(f"- {step}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
if transparency >= 0.8:
|
| 151 |
output.append("")
|
|
|
|
| 174 |
return prefix or "Start with the structure of the problem."
|
| 175 |
|
| 176 |
resolved_topic = _extract_topic_from_text(core, topic)
|
| 177 |
+
normalized_lines = _normalize_display_lines(lines)
|
| 178 |
|
| 179 |
if help_mode == "answer":
|
| 180 |
+
return _format_answer_mode(normalized_lines, resolved_topic, tone, verbosity, transparency)
|
| 181 |
|
| 182 |
+
shown = _limit_steps(normalized_lines, verbosity, minimum=1)
|
| 183 |
output: List[str] = []
|
| 184 |
|
| 185 |
if prefix:
|
|
|
|
| 187 |
output.append("")
|
| 188 |
|
| 189 |
if help_mode == "hint":
|
| 190 |
+
index = max(0, min(int(hint_stage or 0) - 1, len(shown) - 1))
|
| 191 |
output.append("Hint:")
|
| 192 |
output.append(f"- {shown[index]}")
|
| 193 |
if transparency >= 0.8:
|
|
|
|
| 198 |
if help_mode == "walkthrough":
|
| 199 |
output.append("Walkthrough:")
|
| 200 |
for line in shown:
|
| 201 |
+
output.append(f"- {line}")
|
|
|
|
| 202 |
if transparency >= 0.8:
|
| 203 |
output.append("")
|
| 204 |
output.append(_why_line(resolved_topic))
|
|
|
|
| 207 |
if help_mode in {"instruction", "step_by_step"}:
|
| 208 |
output.append("First step:")
|
| 209 |
for line in shown:
|
| 210 |
+
output.append(f"- {line}")
|
|
|
|
| 211 |
if transparency >= 0.8:
|
| 212 |
output.append("")
|
| 213 |
output.append(_why_line(resolved_topic))
|
|
|
|
| 222 |
}[help_mode]
|
| 223 |
output.append(label)
|
| 224 |
for line in shown:
|
| 225 |
+
output.append(f"- {line}")
|
|
|
|
| 226 |
if transparency >= 0.75:
|
| 227 |
output.append("")
|
| 228 |
output.append(_why_line(resolved_topic))
|
| 229 |
return "\n".join(output).strip()
|
| 230 |
|
| 231 |
for line in shown:
|
| 232 |
+
output.append(f"- {line}")
|
|
|
|
| 233 |
|
| 234 |
if transparency >= 0.85:
|
| 235 |
output.append("")
|
|
|
|
| 237 |
|
| 238 |
return "\n".join(output).strip()
|
| 239 |
|
| 240 |
+
|
| 241 |
def _get_scaffold(result: Any):
|
| 242 |
return getattr(result, "scaffold", None)
|
| 243 |
|
|
|
|
| 268 |
hint_ladder = _coerce_list(getattr(scaffold, "hint_ladder", []))
|
| 269 |
key_operations = _coerce_list(getattr(scaffold, "key_operations", []))
|
| 270 |
|
|
|
|
| 271 |
if help_mode == "hint":
|
| 272 |
if hint_ladder:
|
| 273 |
if stage <= 0:
|