Spaces:
Sleeping
Sleeping
Mohitcr1
Complete state mutation refactor: all nodes now return partial dicts (LangGraph reducer pattern)
f9e8eed | from src.state import AgentState | |
| COMPENSATION_TIERS = { | |
| "low": {"days": (1, 7), "code": "LATE10", "discount": "10%"}, | |
| "mid": {"days": (8, 14), "code": "LATE15", "discount": "15% + free shipping"}, | |
| "high": {"days": (15, 9999), "code": "LATE20", "discount": "20% + priority support"}, | |
| } | |
| def check_proactive_logic(state: AgentState) -> dict: | |
| """Detect late deliveries and assign compensation tiers""" | |
| sql = state.get("sql_result", {}) | |
| rows = sql.get("rows", []) | |
| if not rows: | |
| return {} | |
| row = rows[0] | |
| result = {} | |
| # Late delivery detection | |
| is_late = bool(row.get("is_late", 0)) | |
| days_overdue = float(row.get("days_overdue") or 0) | |
| order_status = row.get("order_status", "").lower() | |
| if is_late or days_overdue > 0: | |
| result["is_late_delivery"] = True | |
| result["days_overdue"] = days_overdue | |
| # Don't offer compensation for cancelled/refunded orders (but DO offer for delivered late orders) | |
| if order_status in ["canceled", "cancelled", "refunded"]: | |
| result["is_late_delivery"] = False | |
| return result | |
| # Assign compensation tier (only if not already offered) | |
| if not state.get("compensation_offered"): | |
| for tier, cfg in COMPENSATION_TIERS.items(): | |
| lo, hi = cfg["days"] | |
| if lo <= days_overdue <= hi: | |
| result["compensation_tier"] = tier | |
| break | |
| if "compensation_tier" not in result: | |
| result["compensation_tier"] = "high" | |
| else: | |
| result["is_late_delivery"] = False | |
| # Enhanced escalation logic with multiple triggers | |
| frustration = state.get("frustration_score") or 0.0 | |
| # Check prior frustration from session context (for transactional-only paths) | |
| prior_frustration = state.get("session_context", {}).get("prior_frustration_score", 0.0) | |
| # If no current frustration but prior frustration was high, use prior value | |
| if frustration == 0.0 and prior_frustration > 0.0: | |
| frustration = prior_frustration | |
| print(f"[proactive_logic] Using prior frustration score: {prior_frustration}") | |
| # Tier 1: Auto-compensate for very late orders (15+ days) regardless of frustration | |
| if result.get("is_late_delivery") and days_overdue >= 15: | |
| if not state.get("compensation_offered"): | |
| result["compensation_tier"] = "high" | |
| # Tier 2: High frustration alone triggers escalation | |
| if frustration >= 0.75: | |
| result["escalation_required"] = True | |
| result["escalation_reason"] = "High customer frustration detected" | |
| # Tier 3: Moderate frustration + late delivery triggers escalation | |
| elif frustration >= 0.5 and result.get("is_late_delivery"): | |
| result["escalation_required"] = True | |
| result["escalation_reason"] = "Customer frustration combined with late delivery" | |
| # Tier 4: Very late delivery + any frustration | |
| elif frustration > 0.3 and days_overdue >= 10: | |
| result["escalation_required"] = True | |
| result["escalation_reason"] = "Significant delay with customer concern" | |
| return result | |