File size: 12,034 Bytes
a3d65ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93f0ae5
55ff252
a3d65ce
 
 
 
 
 
 
 
 
9302f63
 
 
 
 
 
 
 
 
 
a3d65ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93f0ae5
55ff252
a3d65ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55ff252
93f0ae5
 
a3d65ce
 
 
 
 
 
55ff252
a3d65ce
 
 
55ff252
a3d65ce
 
 
 
55ff252
 
 
 
a3d65ce
55ff252
a3d65ce
55ff252
a3d65ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93f0ae5
a3d65ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93f0ae5
a3d65ce
 
 
3d8844e
 
a3d65ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
295
296
297
298
299
300
301
302
303
304
305
"""
Customer Support Ticket Resolution β€” OpenEnv Environment (server side).

Implements the three tasks:
  Task 1 (easy)   – Classify a single ticket
  Task 2 (medium) – Choose the correct action for a classified ticket
  Task 3 (hard)   – Fully resolve a queue of tickets with minimal steps
"""

from __future__ import annotations

import random
from typing import Optional

from openenv.core.env_server.interfaces import Environment
from openenv.core.env_server.types import State

from support_ticket_env.models import SupportAction, SupportObservation, SupportState
from support_ticket_env.tickets import TICKETS, TICKET_LOOKUP
from support_ticket_env.graders import (
    grade_task1,
    grade_task2,
    grade_task3,
    loop_penalty,
)


class SupportTicketEnvironment(Environment):
    """
    OpenEnv environment that simulates a customer-support triage desk.

    The task_id (1, 2, or 3) is set when the environment is reset.
    """

    SUPPORTS_CONCURRENT_SESSIONS = True

    def __init__(self) -> None:
        super().__init__()
        self._task_id: int = 1
        self._ticket: dict = {}
        self._classified: bool = False
        self._classified_correctly: bool = False  # tracks actual correctness, not just attempt
        self._task2_cls_score: float = 0.0        # accumulated classification partial credit for Task 2
        self._resolved: bool = False
        self._step_count: int = 0
        self._total_reward: float = 0.0
        self._episode_id: Optional[str] = None

        # Task 3: queue of tickets
        self._queue: list[dict] = []
        self._tickets_resolved: int = 0
        self._tickets_total: int = 1
	
    def get_metadata(self):
        from openenv.core.env_server.types import EnvironmentMetadata
        return EnvironmentMetadata(
            name="support_ticket_env",
            description="A real-world customer support ticket triage environment where an AI agent classifies tickets, selects actions, and resolves queues.",
            version="1.0.0",
            author="AlgoCore",
            documentation_url="https://github.com/TryingHardToBeDeveloper/support-ticket-env",
        )

    # ──────────────────────── reset ────────────────────────────

    def reset(
        self,
        seed: Optional[int] = None,
        episode_id: Optional[str] = None,
        task_id: int = 1,
        **kwargs,
    ) -> SupportObservation:
        rng = random.Random(seed)
        self._episode_id = episode_id
        self._task_id = int(task_id)
        self._step_count = 0
        self._total_reward = 0.0
        self._classified = False
        self._classified_correctly = False
        self._task2_cls_score = 0.0
        self._resolved = False

        if self._task_id == 3:
            # Give the agent a queue of 3 tickets
            self._queue = rng.sample(TICKETS, k=3)
            self._tickets_total = len(self._queue)
            self._tickets_resolved = 0
            self._ticket = self._queue[0]
        else:
            self._ticket = rng.choice(TICKETS)
            self._tickets_total = 1
            self._tickets_resolved = 0

        return self._make_obs(
            feedback="New episode started. Read the ticket and take action.",
            score=0.0,
        )

    # ──────────────────────── step ─────────────────────────────

    def step(self, action: SupportAction, **kwargs) -> SupportObservation:  # type: ignore[override]
        self._step_count += 1
        penalty = loop_penalty(self._step_count)

        if self._task_id == 1:
            obs = self._step_task1(action)
        elif self._task_id == 2:
            obs = self._step_task2(action)
        else:
            obs = self._step_task3(action)

        # Apply loop penalty on top of step reward
        obs.reward = (obs.reward or 0.0) + penalty
        obs.reward = round(max(-1.0, min(1.0, obs.reward)), 4)
        self._total_reward += obs.reward
        obs.step_count = self._step_count
        return obs

    # ──────────────────────── Task 1 ───────────────────────────

    def _step_task1(self, action: SupportAction) -> SupportObservation:
        if action.action_type != "classify":
            return self._make_obs(
                feedback="Task 1 requires a 'classify' action.",
                score=0.0,
                done=False,
            )

        score = grade_task1(
            predicted_category=action.category or "",
            correct_category=self._ticket["category"],
        )
        self._classified = score == 1.0
        correct = self._ticket["category"]

        if score == 1.0:
            feedback = f"βœ… Correct! Category: '{correct}'."
            done = True
        else:
            feedback = (
                f"❌ Wrong. You said '{action.category}', correct is '{correct}'."
            )
            done = True  # Task 1 is one-shot β€” agent gets one attempt

        obs = self._make_obs(feedback=feedback, score=score, done=done)
        if done:
            self._resolved = True
        return obs

    # ──────────────────────── Task 2 ───────────────────────────

    def _step_task2(self, action: SupportAction) -> SupportObservation:
        # First step must be classification
        if not self._classified:
            if action.action_type != "classify":
                return self._make_obs(
                    feedback="Please classify the ticket first.",
                    score=0.0,
                )
            cat_score = grade_task1(
                action.category or "", self._ticket["category"]
            )
            self._classified = True
            self._task2_cls_score = cat_score * 0.3  # store β€” combined with action score at step 2
            # TODO: store self._classified_correctly here too if grade_task2
            # is ever extended to factor in classification correctness
            return self._make_obs(
                feedback=(
                    f"Classified as '{action.category}'. "
                    f"{'Correct βœ…' if cat_score == 1.0 else 'Incorrect ❌'} "
                    "Now choose an action."
                ),
                score=self._task2_cls_score,
            )

        # Second step: choose action
        action_score = grade_task2(
            action_type=action.action_type,
            correct_action=self._ticket["correct_action"],
            category=self._ticket["category"],
        )
        # Scale action score to 0.7 max so classification credit (0.0-0.3) has real room.
        # Total max = 0.7 (perfect action) + 0.3 (correct classify) = 1.0
        # Clamp AFTER addition β€” pre-clamping would silently discard classification credit.
        score = round(min(1.0, action_score * 0.7 + self._task2_cls_score), 4)
        correct = self._ticket["correct_action"]
        if action_score == 1.0:
            feedback = f"βœ… Correct action: '{correct}'."
        elif action_score == 0.5:
            feedback = (
                f"⚠️ Partial credit. '{action.action_type}' is defensible "
                f"but '{correct}' is preferred."
            )
        else:
            feedback = f"❌ Wrong action. Correct: '{correct}'."

        self._resolved = True
        return self._make_obs(feedback=feedback, score=score, done=True)

    # ──────────────────────── Task 3 ───────────────────────────

    def _step_task3(self, action: SupportAction) -> SupportObservation:
        MAX_STEPS = 15

        if not self._classified:
            # Must classify first
            if action.action_type != "classify":
                return self._make_obs(
                    feedback="Classify the ticket before taking action.",
                    score=0.0,
                )
            cat_score = grade_task1(
                action.category or "", self._ticket["category"]
            )
            self._classified = True
            self._classified_correctly = (cat_score == 1.0)  # real correctness tracked
            return self._make_obs(
                feedback=(
                    f"Classified '{self._ticket['id']}' as '{action.category}'. "
                    f"{'Correct βœ…' if cat_score == 1.0 else 'Incorrect ❌'} "
                    "Now resolve it."
                ),
                score=cat_score * 0.1,
            )

        # Resolve current ticket
        action_correct = action.action_type == self._ticket["correct_action"]
        pair = frozenset({action.action_type, self._ticket["correct_action"]})
        action_partial = (not action_correct) and pair in {
            frozenset({"reply", "escalate"})
        }

        score = grade_task3(
            classified_correctly=self._classified_correctly,  # real score, not just attempt flag
            action_correct=action_correct,
            action_partial=action_partial,
            reply_text=action.reply_text,
            category=self._ticket["category"],       # ground truth category
            resolution_hint=self._ticket.get("resolution_hint", ""),  # per-ticket hint keywords
            resolved=True,
            steps_taken=self._step_count,
            max_steps=MAX_STEPS,
        )

        self._tickets_resolved += 1
        correct_action = self._ticket["correct_action"]

        # Advance to next ticket in queue
        if self._tickets_resolved < self._tickets_total:
            self._ticket = self._queue[self._tickets_resolved]
            self._classified = False
            feedback = (
                f"Ticket resolved (score {score:.2f}). "
                f"Moving to next ticket ({self._tickets_resolved + 1}/{self._tickets_total})."
            )
            done = False
        else:
            feedback = (
                f"All {self._tickets_total} tickets resolved! "
                f"Episode score: {self._total_reward + score:.2f}"
            )
            done = True
            self._resolved = True

        return self._make_obs(feedback=feedback, score=score, done=done)

    # ──────────────────────── helpers ──────────────────────────

    def _make_obs(
        self,
        feedback: str,
        score: float,
        done: bool = False,
    ) -> SupportObservation:
        return SupportObservation(
            ticket_id=self._ticket.get("id", ""),
            ticket_text=self._ticket.get("text", ""),
            task_id=self._task_id,
            current_category=self._ticket.get("category") if self._classified else None,
            resolved=self._resolved,
            step_count=self._step_count,
            feedback=feedback,
            score=score,
            reward=score,
            done=done,
        )

    # ──────────────────────── state ────────────────────────────

    @property
    def state(self) -> SupportState:
        return SupportState(
            episode_id=self._episode_id,
            step_count=self._step_count,
            task_id=self._task_id,
            ticket_id=self._ticket.get("id", ""),
            correct_category=self._ticket.get("category", ""),
            correct_action=self._ticket.get("correct_action", ""),
            classified=self._classified,
            resolved=self._resolved,
            total_reward=self._total_reward,
            tickets_resolved=self._tickets_resolved,
            tickets_total=self._tickets_total,
        )