j-js commited on
Commit
f517e49
·
verified ·
1 Parent(s): 99c40d6

Update formatting.py

Browse files
Files changed (1) hide show
  1. formatting.py +166 -5
formatting.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from __future__ import annotations
2
 
3
  import re
@@ -55,6 +56,15 @@ def _is_wrapper_line(line: str) -> bool:
55
  "lets break down what the question is asking",
56
  "you’ve got this — here’s what the question is really asking",
57
  "you've got this — here's what the question is really asking",
 
 
 
 
 
 
 
 
 
58
  }
59
  return key in wrapper_lines
60
 
@@ -274,13 +284,118 @@ def _append_section(lines: List[str], title: str, items: List[str], limit: int)
274
  lines.append(f"- {item}")
275
 
276
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
277
  def format_explainer_response(
278
  result: ExplainerResult,
279
  tone: float,
280
  verbosity: float,
281
  transparency: float,
282
  ) -> str:
283
- if not result or not result.understood:
284
  return "I can help explain what the question is asking, but I need the full wording of the question."
285
 
286
  output: List[str] = []
@@ -290,11 +405,34 @@ def format_explainer_response(
290
  output.append(header)
291
  output.append("")
292
 
293
- plain = (getattr(result, "plain_english", "") or "").strip()
294
- if plain and not _is_wrapper_line(plain):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
295
  output.append(plain)
296
 
297
- asks_for = (getattr(result, "asks_for", "") or "").strip()
298
  if asks_for and transparency >= 0.3:
299
  output.append("")
300
  output.append(f"The question is asking for: {asks_for}")
@@ -339,11 +477,34 @@ def format_explainer_response(
339
  4,
340
  )
341
 
342
- strategy_hint = (getattr(result, "strategy_hint", "") or "").strip()
343
  if strategy_hint and verbosity >= 0.35 and not _is_wrapper_line(strategy_hint):
344
  output.append("")
345
  output.append(f"Best starting move: {strategy_hint}")
346
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347
  final_lines = []
348
  previous_key = None
349
  for line in output:
 
1
+ # formatting.py
2
  from __future__ import annotations
3
 
4
  import re
 
56
  "lets break down what the question is asking",
57
  "you’ve got this — here’s what the question is really asking",
58
  "you've got this — here's what the question is really asking",
59
+ "what to identify first",
60
+ "set-up path",
61
+ "setup path",
62
+ "first move",
63
+ "next hint",
64
+ "watch out for",
65
+ "variables to define",
66
+ "equations to form",
67
+ "key teaching points",
68
  }
69
  return key in wrapper_lines
70
 
 
284
  lines.append(f"- {item}")
285
 
286
 
287
+ def _coerce_string(value) -> str:
288
+ return (value or "").strip() if isinstance(value, str) else ""
289
+
290
+
291
+ def _coerce_list(value) -> List[str]:
292
+ if not value:
293
+ return []
294
+ if isinstance(value, list):
295
+ return [str(v).strip() for v in value if str(v).strip()]
296
+ if isinstance(value, tuple):
297
+ return [str(v).strip() for v in value if str(v).strip()]
298
+ if isinstance(value, str):
299
+ text = value.strip()
300
+ return [text] if text else []
301
+ return []
302
+
303
+
304
+ def _get_scaffold(result: ExplainerResult):
305
+ return getattr(result, "scaffold", None)
306
+
307
+
308
+ def _explainer_topic(result: ExplainerResult) -> str:
309
+ topic = _coerce_string(getattr(result, "topic", ""))
310
+ if topic:
311
+ return topic
312
+
313
+ joined = " ".join(
314
+ _coerce_list(getattr(result, "teaching_points", []))
315
+ + _coerce_list(getattr(result, "givens", []))
316
+ + _coerce_list(getattr(result, "relationships", []))
317
+ )
318
+ return _extract_topic_from_lines([joined]) if joined else "general"
319
+
320
+
321
+ def _build_scaffold_sections(
322
+ result: ExplainerResult,
323
+ verbosity: float,
324
+ transparency: float,
325
+ ) -> List[str]:
326
+ output: List[str] = []
327
+ scaffold = _get_scaffold(result)
328
+
329
+ if scaffold is None:
330
+ return output
331
+
332
+ ask = _coerce_string(getattr(scaffold, "ask", ""))
333
+ setup_actions = _coerce_list(getattr(scaffold, "setup_actions", []))
334
+ intermediate_steps = _coerce_list(getattr(scaffold, "intermediate_steps", []))
335
+ first_move = _coerce_string(getattr(scaffold, "first_move", ""))
336
+ next_hint = _coerce_string(getattr(scaffold, "next_hint", ""))
337
+ variables_to_define = _coerce_list(getattr(scaffold, "variables_to_define", []))
338
+ equations_to_form = _coerce_list(getattr(scaffold, "equations_to_form", []))
339
+ common_traps = _coerce_list(getattr(scaffold, "common_traps", []))
340
+
341
+ if ask:
342
+ output.append("")
343
+ output.append("What to identify first:")
344
+ output.append(f"- {ask}")
345
+
346
+ if setup_actions:
347
+ output.append("")
348
+ output.append("Set-up path:")
349
+ limit = 2 if verbosity < 0.4 else 3 if verbosity < 0.75 else 5
350
+ for item in setup_actions[:limit]:
351
+ output.append(f"- {item}")
352
+
353
+ if verbosity >= 0.55 and intermediate_steps:
354
+ output.append("")
355
+ output.append("How to build it:")
356
+ limit = 2 if verbosity < 0.75 else 4
357
+ for item in intermediate_steps[:limit]:
358
+ output.append(f"- {item}")
359
+
360
+ if first_move:
361
+ output.append("")
362
+ output.append("First move:")
363
+ output.append(f"- {first_move}")
364
+
365
+ if next_hint and (transparency >= 0.35 or verbosity >= 0.45):
366
+ output.append("")
367
+ output.append("Next hint:")
368
+ output.append(f"- {next_hint}")
369
+
370
+ if verbosity >= 0.65 and variables_to_define:
371
+ output.append("")
372
+ output.append("Variables to define:")
373
+ for item in variables_to_define[:3]:
374
+ output.append(f"- {item}")
375
+
376
+ if transparency >= 0.55 and equations_to_form:
377
+ output.append("")
378
+ output.append("Equations to form:")
379
+ for item in equations_to_form[:3]:
380
+ output.append(f"- {item}")
381
+
382
+ if transparency >= 0.6 or verbosity >= 0.75:
383
+ if common_traps:
384
+ output.append("")
385
+ output.append("Watch out for:")
386
+ for item in common_traps[:4]:
387
+ output.append(f"- {item}")
388
+
389
+ return output
390
+
391
+
392
  def format_explainer_response(
393
  result: ExplainerResult,
394
  tone: float,
395
  verbosity: float,
396
  transparency: float,
397
  ) -> str:
398
+ if not result or not getattr(result, "understood", False):
399
  return "I can help explain what the question is asking, but I need the full wording of the question."
400
 
401
  output: List[str] = []
 
405
  output.append(header)
406
  output.append("")
407
 
408
+ # -------------------------------------------------
409
+ # New publish-ready scaffold-aware path
410
+ # -------------------------------------------------
411
+ summary = _coerce_string(getattr(result, "summary", ""))
412
+ teaching_points = _coerce_list(getattr(result, "teaching_points", []))
413
+
414
+ if summary and not _is_wrapper_line(summary):
415
+ output.append(summary)
416
+
417
+ scaffold_sections = _build_scaffold_sections(result, verbosity, transparency)
418
+ if scaffold_sections:
419
+ output.extend(scaffold_sections)
420
+
421
+ if teaching_points and verbosity >= 0.5:
422
+ output.append("")
423
+ output.append("Key teaching points:")
424
+ limit = 2 if verbosity < 0.75 else 4
425
+ for item in teaching_points[:limit]:
426
+ output.append(f"- {item}")
427
+
428
+ # -------------------------------------------------
429
+ # Backward-compatible support for older explainer fields
430
+ # -------------------------------------------------
431
+ plain = _coerce_string(getattr(result, "plain_english", ""))
432
+ if plain and not summary and not _is_wrapper_line(plain):
433
  output.append(plain)
434
 
435
+ asks_for = _coerce_string(getattr(result, "asks_for", ""))
436
  if asks_for and transparency >= 0.3:
437
  output.append("")
438
  output.append(f"The question is asking for: {asks_for}")
 
477
  4,
478
  )
479
 
480
+ strategy_hint = _coerce_string(getattr(result, "strategy_hint", ""))
481
  if strategy_hint and verbosity >= 0.35 and not _is_wrapper_line(strategy_hint):
482
  output.append("")
483
  output.append(f"Best starting move: {strategy_hint}")
484
 
485
+ if transparency >= 0.8:
486
+ topic = _explainer_topic(result)
487
+ why_seed_lines: List[str] = []
488
+
489
+ if summary:
490
+ why_seed_lines.append(summary)
491
+
492
+ scaffold = _get_scaffold(result)
493
+ if scaffold is not None:
494
+ ask = _coerce_string(getattr(scaffold, "ask", ""))
495
+ first_move = _coerce_string(getattr(scaffold, "first_move", ""))
496
+ if ask:
497
+ why_seed_lines.append(ask)
498
+ if first_move:
499
+ why_seed_lines.append(first_move)
500
+
501
+ if not why_seed_lines:
502
+ why_seed_lines.extend(teaching_points[:2])
503
+
504
+ if why_seed_lines:
505
+ output.append("")
506
+ output.append(_why_line(topic, why_seed_lines))
507
+
508
  final_lines = []
509
  previous_key = None
510
  for line in output: