Spaces:
Runtime error
Runtime error
File size: 5,650 Bytes
b77d3c5 1af4cba b77d3c5 1af4cba b77d3c5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | # salespath_env/server/rules.py
from dataclasses import dataclass
from typing import Callable
from ..models import SalesPathAction, SalesPathState
@dataclass
class BusinessRule:
"""
Returns True when the rule is VIOLATED.
"""
rule_id: str
name: str
description: str
check: Callable[[SalesPathState, SalesPathAction], bool]
def _qualify_before_present(
state: SalesPathState,
action: SalesPathAction,
) -> bool:
"""
R01:
PRESENT before QUALIFY is invalid.
"""
if action.action_type == "PRESENT":
return "QUALIFY" not in state.steps_completed
return False
def _demo_before_negotiate(
state: SalesPathState,
action: SalesPathAction,
) -> bool:
"""
R02:
NEGOTIATE before OFFER_DEMO is invalid.
"""
if action.action_type == "NEGOTIATE":
return "OFFER_DEMO" not in state.steps_completed
return False
def _budget_known_to_negotiate(
state: SalesPathState,
action: SalesPathAction,
) -> bool:
"""
R03:
Cannot NEGOTIATE while budget is unknown.
"""
if action.action_type == "NEGOTIATE":
return state.prospect_profile.get("budget_signal") == "unknown"
return False
def _discount_after_objections(
state: SalesPathState,
action: SalesPathAction,
) -> bool:
"""
R04:
Discount only after 2 objections handled.
"""
if action.action_type == "NEGOTIATE":
if "discount" in action.content.lower():
return state.objections_handled < 2
return False
def _no_repeat_action(
state: SalesPathState,
action: SalesPathAction,
) -> bool:
"""
R05:
Same action twice in a row is invalid.
"""
if state.conversation_history:
last_action = state.conversation_history[-1].get("action_type", "")
return last_action == action.action_type
return False
def _prospect_first(
state: SalesPathState,
action: SalesPathAction,
) -> bool:
"""
R06:
First action must be PROSPECT.
"""
if state.turn_number == 1:
return action.action_type != "PROSPECT"
return False
def _followup_timing(
state: SalesPathState,
action: SalesPathAction,
) -> bool:
"""
R07:
FOLLOW_UP only valid after prospect silence.
Violation if the prospect's last response had actual content
(i.e., the prospect is still engaged and waiting for a reply).
"""
if action.action_type == "FOLLOW_UP":
if state.conversation_history:
# Walk backwards to find the last prospect message
for entry in reversed(state.conversation_history):
if entry.get("speaker") == "prospect":
response_token = entry.get("response_token", "")
# FOLLOW_UP is only valid if the prospect went silent
return response_token != "silence"
# No prospect message found — first turn, so violation
return True
# No history at all — first turn, can't FOLLOW_UP yet
return True
return False
def _disqualify_logic(
state: SalesPathState,
action: SalesPathAction,
) -> bool:
"""
R08:
DISQUALIFY only when prospect is genuinely not closeable.
Violation if prospect is actually closeable.
"""
if action.action_type == "DISQUALIFY":
true_budget = state.hidden_state.get("true_budget", 0.5)
close_threshold = state.hidden_state.get("close_threshold", 0.5)
decision_maker = state.prospect_profile.get("decision_maker", True)
return (true_budget >= close_threshold) and decision_maker
return False
def _close_requires_demo(
state: SalesPathState,
action: SalesPathAction,
) -> bool:
"""
R09:
Difficulty 2+ requires OFFER_DEMO before CLOSE.
"""
if action.action_type == "CLOSE":
if state.difficulty >= 2:
return "OFFER_DEMO" not in state.steps_completed
return False
BUSINESS_RULES = [
BusinessRule(
"R01",
"qualify_before_present",
"Must QUALIFY before PRESENT",
_qualify_before_present,
),
BusinessRule(
"R02",
"demo_before_negotiate",
"Must OFFER_DEMO before NEGOTIATE",
_demo_before_negotiate,
),
BusinessRule(
"R03",
"budget_known_to_negotiate",
"Budget must be known before NEGOTIATE",
_budget_known_to_negotiate,
),
BusinessRule(
"R04",
"discount_after_objections",
"Discount only after 2 objections handled",
_discount_after_objections,
),
BusinessRule(
"R05",
"no_repeat_action",
"Cannot repeat same action consecutively",
_no_repeat_action,
),
BusinessRule(
"R06",
"prospect_first",
"First action must be PROSPECT",
_prospect_first,
),
BusinessRule(
"R07",
"followup_timing",
"FOLLOW_UP only after prospect silence",
_followup_timing,
),
BusinessRule(
"R08",
"disqualify_logic",
"DISQUALIFY only when prospect is genuinely unqualified",
_disqualify_logic,
),
BusinessRule(
"R09",
"close_requires_demo",
"Must OFFER_DEMO before CLOSE (difficulty 2+)",
_close_requires_demo,
),
]
def check_rules(
state: SalesPathState,
action: SalesPathAction,
) -> list[str]:
"""
Returns list of violated rule IDs.
"""
violated = []
for rule in BUSINESS_RULES:
if rule.check(state, action):
violated.append(rule.rule_id)
return violated |