File size: 1,963 Bytes
f352609
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""PayOps Environment Client."""

from typing import Dict, Any

from openenv.core import EnvClient
from openenv.core.client_types import StepResult
from openenv.core.env_server.types import State

from payops_env.models import PayOpsAction, PayOpsObservation


class PayOpsEnv(EnvClient[PayOpsAction, PayOpsObservation]):
    """
    Client for the PayOps Payment Operations Incident Response Environment.

    Maintains a persistent WebSocket connection to the environment server,
    enabling efficient multi-step interactions.

    Example::

        with PayOpsEnv(base_url="http://localhost:7860") as client:
            result = client.reset()
            while not result.done:
                action = PayOpsAction(
                    action_type="approve",
                    transaction_id=result.observation.transaction_id,
                )
                result = client.step(action)
            print("Score:", result.observation.cumulative_reward)
    """

    def _step_payload(self, action: PayOpsAction) -> Dict[str, Any]:
        """Convert PayOpsAction to the official openenv wire format."""
        return {
            "action": action.model_dump(exclude_none=True),
        }

    def _parse_result(self, payload: Dict[str, Any]) -> StepResult[PayOpsObservation]:
        """Parse server response into StepResult[PayOpsObservation]."""
        obs_data = payload.get("observation", payload)  # support wrapped or flat
        observation = PayOpsObservation(**obs_data)
        return StepResult(
            observation=observation,
            reward=payload.get("reward", obs_data.get("reward")),
            done=payload.get("done", obs_data.get("done", False)),
        )

    def _parse_state(self, payload: Dict[str, Any]) -> State:
        """Parse server state response into State object."""
        return State(
            episode_id=payload.get("episode_id"),
            step_count=payload.get("step_count", 0),
        )