File size: 2,856 Bytes
c8e832f
 
 
1c8b7f1
c8e832f
1c8b7f1
c8e832f
 
 
1c8b7f1
 
 
c8e832f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Client for the Python code review environment."""

from __future__ import annotations

from typing import Dict

from compat import install_openenv_fastmcp_compat

install_openenv_fastmcp_compat()

from openenv.core import EnvClient
from openenv.core.client_types import StepResult

from models import (
    HistoryEntry,
    PythonCodeReviewAction,
    PythonCodeReviewObservation,
    PythonCodeReviewState,
    RewardDetails,
)


class PythonEnv(
    EnvClient[PythonCodeReviewAction, PythonCodeReviewObservation, PythonCodeReviewState]
):
    """OpenEnv HTTP client for the Python code review benchmark."""

    def _step_payload(self, action: PythonCodeReviewAction) -> Dict:
        return action.model_dump(exclude_none=True)

    def _parse_result(self, payload: Dict) -> StepResult[PythonCodeReviewObservation]:
        obs = payload.get("observation", {})
        observation = PythonCodeReviewObservation(
            task_id=obs["task_id"],
            title=obs["title"],
            difficulty=obs["difficulty"],
            task_kind=obs["task_kind"],
            task_description=obs["task_description"],
            current_code=obs.get("current_code", ""),
            errors=obs.get("errors", ""),
            test_results=obs.get("test_results", ""),
            history=[HistoryEntry(**entry) for entry in obs.get("history", [])],
            attempts_remaining=obs.get("attempts_remaining", 0),
            last_action_status=obs.get("last_action_status", ""),
            score=obs.get("score", 0.0),
            reward_details=RewardDetails(**obs.get("reward_details", {})),
            done=payload.get("done", obs.get("done", False)),
            reward=payload.get("reward", obs.get("reward")),
            metadata=obs.get("metadata", {}),
        )
        return StepResult(
            observation=observation,
            reward=payload.get("reward", obs.get("reward")),
            done=payload.get("done", obs.get("done", False)),
        )

    def _parse_state(self, payload: Dict) -> PythonCodeReviewState:
        return PythonCodeReviewState(
            episode_id=payload.get("episode_id"),
            step_count=payload.get("step_count", 0),
            task_id=payload.get("task_id"),
            difficulty=payload.get("difficulty"),
            task_kind=payload.get("task_kind"),
            attempts_remaining=payload.get("attempts_remaining", 0),
            current_code=payload.get("current_code", ""),
            errors=payload.get("errors", ""),
            test_results=payload.get("test_results", ""),
            history=[HistoryEntry(**entry) for entry in payload.get("history", [])],
            score=payload.get("score", 0.0),
            done=payload.get("done", False),
        )


CodeReviewEnv = PythonEnv
MyEnv = PythonEnv