File size: 7,925 Bytes
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# salespath_env/server/salespath_environment.py

import uuid

from openenv.core.env_server import Environment

from ..models import (
    SalesPathAction,
    SalesPathObservation,
    SalesPathState,
)
from .task_bank import sample_profile
from .rules import check_rules
from .reward import compute_reward
from .prospect_simulator import ProspectSimulator


DIFFICULTY_WORKFLOW = {
    1: [
        "QUALIFY",
        "PRESENT",
        "CLOSE",
    ],
    2: [
        "QUALIFY",
        "PRESENT",
        "HANDLE_OBJECTION",
        "OFFER_DEMO",
        "CLOSE",
    ],
    3: [
        "QUALIFY",
        "PRESENT",
        "HANDLE_OBJECTION",
        "OFFER_DEMO",
        "HANDLE_OBJECTION",
        "NEGOTIATE",
        "CLOSE",
    ],
    4: [],  # Agent must determine; DISQUALIFY may be correct
}


MAX_VIOLATIONS_BEFORE_TERMINATE = 3
MAX_TURNS = 20


class SalesPathEnvironment(Environment):
    """
    Core OpenEnv environment.
    All business logic routes through:
    - rules.py
    - reward.py
    - prospect_simulator.py
    """

    def __init__(self):
        super().__init__()
        self._state = SalesPathState()
        self._simulator = ProspectSimulator()

    def reset(self, difficulty: int = 1) -> SalesPathObservation:
        """
        Start a new episode.
        """

        profile = sample_profile(difficulty)

        hidden_state = {
            "true_budget": profile.true_budget,
            "close_threshold": profile.close_threshold,
            "stall_probability": profile.stall_probability,
            "num_objections": {
                1: 0,
                2: 1,
                3: 2,
                4: 2,
            }[difficulty],
            "revealed_budget": (
                "high"
                if profile.true_budget >= 0.7
                else "medium"
                if profile.true_budget >= 0.4
                else "low"
            ),
        }

        public_profile = {
            "company_name": profile.company_name,
            "company_size": profile.company_size,
            "industry": profile.industry,
            "budget_signal": profile.budget_signal,
            "pain_points": profile.pain_points,
            "decision_maker": profile.decision_maker,
        }

        self._state = SalesPathState(
            episode_id=str(uuid.uuid4()),
            prospect_profile=public_profile,
            conversation_history=[],
            workflow_stage="START",
            required_workflow=DIFFICULTY_WORKFLOW[difficulty],
            steps_completed=[],
            constraints_violated=[],
            objections_handled=0,
            turn_number=0,
            difficulty=difficulty,
            done=False,
            hidden_state=hidden_state,
        )

        intro_message = (
            f"You are engaging {profile.company_name}, "
            f"a {profile.company_size} {profile.industry} company. "
            f"Pain points: {', '.join(profile.pain_points)}. "
            f"Begin the sales conversation."
        )

        return SalesPathObservation(
            prospect_response=intro_message,
            workflow_stage="START",
            constraints_violated=[],
            steps_completed=[],
            turn_number=0,
            reward=0.0,
            reward_components={},
            done=False,
            info={
                "difficulty": difficulty,
                "episode_id": self._state.episode_id,
            },
        )

    def step(
        self,
        action: SalesPathAction,
    ) -> SalesPathObservation:
        """
        One environment transition.
        """

        state = self._state

        # -----------------------------------
        # Advance turn
        # -----------------------------------

        state.turn_number += 1

        # -----------------------------------
        # Strict action validation
        # Must return observation, never crash
        # -----------------------------------

        if not action.is_valid():
            return SalesPathObservation(
                prospect_response="Invalid action type.",
                workflow_stage=state.workflow_stage,
                constraints_violated=list(state.constraints_violated),
                steps_completed=list(state.steps_completed),
                turn_number=state.turn_number,
                reward=-0.2,
                reward_components={
                    "r_format": -0.1,
                },
                done=False,
                info={
                    "error": (
                        f"Invalid action_type: "
                        f"{action.action_type}"
                    )
                },
            )

        # -----------------------------------
        # Rule checks
        # -----------------------------------

        new_violations = check_rules(
            state,
            action,
        )

        state.constraints_violated.extend(
            new_violations
        )

        # -----------------------------------
        # Record agent action
        # -----------------------------------

        state.conversation_history.append(
            {
                "turn": state.turn_number,
                "speaker": "agent",
                "action_type": action.action_type,
                "content": action.content,
            }
        )

        # -----------------------------------
        # Update workflow state
        # -----------------------------------

        if action.action_type not in state.steps_completed:
            state.steps_completed.append(
                action.action_type
            )

        state.workflow_stage = action.action_type

        # -----------------------------------
        # Prospect response
        # -----------------------------------

        response_token, response_text = (
            self._simulator.respond(
                action,
                state,
            )
        )

        state.conversation_history.append(
            {
                "turn": state.turn_number,
                "speaker": "prospect",
                "response_token": response_token,
                "text": response_text,
            }
        )

        # -----------------------------------
        # Episode termination
        # -----------------------------------

        terminal_actions = {
            "CLOSE",
            "DISQUALIFY",
        }

        too_many_violations = (
            len(state.constraints_violated)
            >= MAX_VIOLATIONS_BEFORE_TERMINATE
        )

        turn_limit_reached = (
            state.turn_number >= MAX_TURNS
        )

        done = (
            action.action_type in terminal_actions
            or too_many_violations
            or turn_limit_reached
        )

        state.done = done

        # -----------------------------------
        # Reward
        # -----------------------------------

        total_reward, components = (
            compute_reward(
                state=state,
                action=action,
                response_token=response_token,
                new_violations=new_violations,
                episode_done=done,
            )
        )

        return SalesPathObservation(
            prospect_response=response_text,
            workflow_stage=state.workflow_stage,
            constraints_violated=list(
                state.constraints_violated
            ),
            steps_completed=list(
                state.steps_completed
            ),
            turn_number=state.turn_number,
            reward=total_reward,
            reward_components=components,
            done=done,
            info={
                "response_token": response_token,
                "new_violations": new_violations,
                "episode_id": state.episode_id,
            },
        )

    @property
    def state(self) -> SalesPathState:
        return self._state