Update conversation_logic.py
Browse files- conversation_logic.py +76 -3
conversation_logic.py
CHANGED
|
@@ -85,6 +85,74 @@ def detect_help_mode(text: str) -> str:
|
|
| 85 |
return "explain"
|
| 86 |
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
def _normalize_classified_topic(topic: Optional[str], category: Optional[str], question_text: str) -> str:
|
| 89 |
t = (topic or "").strip().lower()
|
| 90 |
q = (question_text or "").lower()
|
|
@@ -308,7 +376,6 @@ def _compose_reply(
|
|
| 308 |
category: Optional[str] = None,
|
| 309 |
) -> str:
|
| 310 |
steps = _safe_steps(result.steps or [])
|
| 311 |
-
topic = (result.topic or "").lower().strip()
|
| 312 |
meta = result.meta or {}
|
| 313 |
|
| 314 |
scaffold_reply = _build_scaffold_reply(
|
|
@@ -468,8 +535,13 @@ class ConversationEngine:
|
|
| 468 |
options_text: Optional[List[str]] = None,
|
| 469 |
**kwargs,
|
| 470 |
) -> SolverResult:
|
| 471 |
-
solver_input = (question_text or raw_user_text or "").strip()
|
| 472 |
user_text = (raw_user_text or "").strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 473 |
|
| 474 |
category = normalize_category(kwargs.get("category"))
|
| 475 |
classification = classify_question(question_text=solver_input, category=category)
|
|
@@ -540,6 +612,7 @@ class ConversationEngine:
|
|
| 540 |
result.meta["can_reveal_answer"] = bool(
|
| 541 |
result.solved and _is_direct_solve_request(user_text or solver_input, resolved_intent) and hint_stage >= 3
|
| 542 |
)
|
|
|
|
| 543 |
|
| 544 |
if explainer_understood:
|
| 545 |
reply = format_explainer_response(
|
|
@@ -574,7 +647,7 @@ class ConversationEngine:
|
|
| 574 |
result.reply = reply
|
| 575 |
result.help_mode = resolved_help_mode
|
| 576 |
result.meta["intent"] = resolved_intent
|
| 577 |
-
result.meta["question_text"] =
|
| 578 |
result.meta["options_count"] = len(options_text or [])
|
| 579 |
result.meta["category"] = inferred_category
|
| 580 |
result.meta["classified_topic"] = question_topic
|
|
|
|
| 85 |
return "explain"
|
| 86 |
|
| 87 |
|
| 88 |
+
def _extract_text_from_history_item(item: Dict[str, Any]) -> str:
|
| 89 |
+
if not isinstance(item, dict):
|
| 90 |
+
return ""
|
| 91 |
+
|
| 92 |
+
for key in ("content", "text", "message", "question_text", "raw_user_text"):
|
| 93 |
+
value = item.get(key)
|
| 94 |
+
if isinstance(value, str) and value.strip():
|
| 95 |
+
return value.strip()
|
| 96 |
+
|
| 97 |
+
meta = item.get("meta")
|
| 98 |
+
if isinstance(meta, dict):
|
| 99 |
+
for key in ("question_text",):
|
| 100 |
+
value = meta.get(key)
|
| 101 |
+
if isinstance(value, str) and value.strip():
|
| 102 |
+
return value.strip()
|
| 103 |
+
|
| 104 |
+
return ""
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
def _is_followup_hint_only(text: str) -> bool:
|
| 108 |
+
low = (text or "").strip().lower()
|
| 109 |
+
return low in {
|
| 110 |
+
"hint",
|
| 111 |
+
"another hint",
|
| 112 |
+
"next hint",
|
| 113 |
+
"next step",
|
| 114 |
+
"first step",
|
| 115 |
+
"continue",
|
| 116 |
+
"go on",
|
| 117 |
+
"walk me through it",
|
| 118 |
+
"step by step",
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
def _recover_question_text_from_history(
|
| 123 |
+
raw_user_text: str,
|
| 124 |
+
question_text: Optional[str],
|
| 125 |
+
chat_history: Optional[List[Dict[str, Any]]],
|
| 126 |
+
) -> str:
|
| 127 |
+
explicit = (question_text or "").strip()
|
| 128 |
+
if explicit:
|
| 129 |
+
return explicit
|
| 130 |
+
|
| 131 |
+
if not _is_followup_hint_only(raw_user_text):
|
| 132 |
+
return (raw_user_text or "").strip()
|
| 133 |
+
|
| 134 |
+
if not chat_history:
|
| 135 |
+
return (raw_user_text or "").strip()
|
| 136 |
+
|
| 137 |
+
for item in reversed(chat_history):
|
| 138 |
+
role = str(item.get("role", "")).lower()
|
| 139 |
+
text = _extract_text_from_history_item(item)
|
| 140 |
+
|
| 141 |
+
if not text:
|
| 142 |
+
continue
|
| 143 |
+
|
| 144 |
+
if role == "assistant":
|
| 145 |
+
continue
|
| 146 |
+
|
| 147 |
+
low = text.lower().strip()
|
| 148 |
+
if _is_followup_hint_only(low):
|
| 149 |
+
continue
|
| 150 |
+
|
| 151 |
+
return text
|
| 152 |
+
|
| 153 |
+
return (raw_user_text or "").strip()
|
| 154 |
+
|
| 155 |
+
|
| 156 |
def _normalize_classified_topic(topic: Optional[str], category: Optional[str], question_text: str) -> str:
|
| 157 |
t = (topic or "").strip().lower()
|
| 158 |
q = (question_text or "").lower()
|
|
|
|
| 376 |
category: Optional[str] = None,
|
| 377 |
) -> str:
|
| 378 |
steps = _safe_steps(result.steps or [])
|
|
|
|
| 379 |
meta = result.meta or {}
|
| 380 |
|
| 381 |
scaffold_reply = _build_scaffold_reply(
|
|
|
|
| 535 |
options_text: Optional[List[str]] = None,
|
| 536 |
**kwargs,
|
| 537 |
) -> SolverResult:
|
|
|
|
| 538 |
user_text = (raw_user_text or "").strip()
|
| 539 |
+
recovered_question_text = _recover_question_text_from_history(
|
| 540 |
+
raw_user_text=user_text,
|
| 541 |
+
question_text=question_text,
|
| 542 |
+
chat_history=chat_history,
|
| 543 |
+
)
|
| 544 |
+
solver_input = recovered_question_text
|
| 545 |
|
| 546 |
category = normalize_category(kwargs.get("category"))
|
| 547 |
classification = classify_question(question_text=solver_input, category=category)
|
|
|
|
| 612 |
result.meta["can_reveal_answer"] = bool(
|
| 613 |
result.solved and _is_direct_solve_request(user_text or solver_input, resolved_intent) and hint_stage >= 3
|
| 614 |
)
|
| 615 |
+
result.meta["recovered_question_text"] = recovered_question_text
|
| 616 |
|
| 617 |
if explainer_understood:
|
| 618 |
reply = format_explainer_response(
|
|
|
|
| 647 |
result.reply = reply
|
| 648 |
result.help_mode = resolved_help_mode
|
| 649 |
result.meta["intent"] = resolved_intent
|
| 650 |
+
result.meta["question_text"] = recovered_question_text or ""
|
| 651 |
result.meta["options_count"] = len(options_text or [])
|
| 652 |
result.meta["category"] = inferred_category
|
| 653 |
result.meta["classified_topic"] = question_topic
|