Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -185,6 +185,46 @@ def _normalize_lines(text: str) -> List[str]:
|
|
| 185 |
return [raw.strip()] if raw.strip() else []
|
| 186 |
|
| 187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
def _ensure_numbering(text: str) -> str:
|
| 189 |
"""
|
| 190 |
Normalize raw SOP steps into a clean numbered list using circled digits.
|
|
@@ -320,6 +360,8 @@ def _detect_next_intent(user_query: str) -> bool:
|
|
| 320 |
]
|
| 321 |
return any(k in q for k in keys)
|
| 322 |
|
|
|
|
|
|
|
| 323 |
def _resolve_next_steps(user_query: str, numbered_text: str, max_next: int = 8, min_score: float = 0.25):
|
| 324 |
"""
|
| 325 |
Robust next-step resolver:
|
|
@@ -938,6 +980,25 @@ async def chat_with_ai(input_data: ChatInput):
|
|
| 938 |
|
| 939 |
if full_steps:
|
| 940 |
numbered_full = _ensure_numbering(full_steps)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 941 |
next_only = _resolve_next_steps(
|
| 942 |
input_data.user_message,
|
| 943 |
numbered_full,
|
|
|
|
| 185 |
return [raw.strip()] if raw.strip() else []
|
| 186 |
|
| 187 |
|
| 188 |
+
# --- NEW: action-aware filter for numbered steps (creation/update/delete) ---
|
| 189 |
+
def _filter_numbered_steps_by_actions(numbered_text: str,
|
| 190 |
+
wanted: set[str],
|
| 191 |
+
exclude: set[str]) -> str:
|
| 192 |
+
"""
|
| 193 |
+
Keep only lines that match the 'wanted' actions and drop lines that match 'exclude'.
|
| 194 |
+
Works on already-numbered/bulleted text (one step per line).
|
| 195 |
+
"""
|
| 196 |
+
# Keep synonyms aligned with kb_creation.ACTION_SYNONYMS
|
| 197 |
+
ACTION_SYNONYMS = {
|
| 198 |
+
"create": ("create", "creation", "add", "new", "generate"),
|
| 199 |
+
"update": ("update", "modify", "change", "edit"),
|
| 200 |
+
"delete": ("delete", "remove"),
|
| 201 |
+
"navigate": ("navigate", "go to", "open"),
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
def _has_any(line: str, keys: set[str]) -> bool:
|
| 205 |
+
low = (line or "").lower()
|
| 206 |
+
for k in keys:
|
| 207 |
+
for syn in ACTION_SYNONYMS.get(k, (k,)):
|
| 208 |
+
if syn in low:
|
| 209 |
+
return True
|
| 210 |
+
return False
|
| 211 |
+
|
| 212 |
+
out_lines = []
|
| 213 |
+
for ln in (numbered_text or "").splitlines():
|
| 214 |
+
# If an exclude action appears in the line, drop it
|
| 215 |
+
if _has_any(ln, exclude):
|
| 216 |
+
continue
|
| 217 |
+
# If caller asked for specific actions, keep only those
|
| 218 |
+
if wanted:
|
| 219 |
+
if _has_any(ln, wanted):
|
| 220 |
+
out_lines.append(ln)
|
| 221 |
+
else:
|
| 222 |
+
# no explicit wanted actions → keep as-is
|
| 223 |
+
out_lines.append(ln)
|
| 224 |
+
|
| 225 |
+
# If over-filtering made it empty, fall back to original text
|
| 226 |
+
return "\n".join(out_lines).strip() or (numbered_text or "").strip()
|
| 227 |
+
|
| 228 |
def _ensure_numbering(text: str) -> str:
|
| 229 |
"""
|
| 230 |
Normalize raw SOP steps into a clean numbered list using circled digits.
|
|
|
|
| 360 |
]
|
| 361 |
return any(k in q for k in keys)
|
| 362 |
|
| 363 |
+
|
| 364 |
+
|
| 365 |
def _resolve_next_steps(user_query: str, numbered_text: str, max_next: int = 8, min_score: float = 0.25):
|
| 366 |
"""
|
| 367 |
Robust next-step resolver:
|
|
|
|
| 980 |
|
| 981 |
if full_steps:
|
| 982 |
numbered_full = _ensure_numbering(full_steps)
|
| 983 |
+
wanted_actions = set((kb_results.get("actions") or []))
|
| 984 |
+
# If user specifically asked for creation, prefer only creation and exclude update/delete
|
| 985 |
+
if "create" in wanted_actions and not ({"update", "delete"} & wanted_actions):
|
| 986 |
+
numbered_full = _filter_numbered_steps_by_actions(
|
| 987 |
+
numbered_full,
|
| 988 |
+
wanted={"create"},
|
| 989 |
+
exclude={"update", "delete"}
|
| 990 |
+
)
|
| 991 |
+
elif "update" in wanted_actions and "delete" not in wanted_actions:
|
| 992 |
+
numbered_full = _filter_numbered_steps_by_actions(
|
| 993 |
+
numbered_full,
|
| 994 |
+
wanted={"update"},
|
| 995 |
+
exclude={"create", "delete"}
|
| 996 |
+
)
|
| 997 |
+
elif "delete" in wanted_actions and "update" not in wanted_actions:
|
| 998 |
+
numbered_full = _filter_numbered_steps_by_actions(
|
| 999 |
+
numbered_full, wanted={"delete"}, exclude={"create", "update"}
|
| 1000 |
+
)
|
| 1001 |
+
|
| 1002 |
next_only = _resolve_next_steps(
|
| 1003 |
input_data.user_message,
|
| 1004 |
numbered_full,
|