File size: 2,129 Bytes
9a3b69b
 
 
 
 
 
 
 
43f41de
 
 
 
9a3b69b
 
 
 
 
 
 
 
 
eb1ebe6
 
 
 
43f41de
 
 
 
eb1ebe6
 
 
 
 
9a3b69b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Explainer Env Environment Client."""

from typing import Dict

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

try:
    from .models import ExplainerAction, ExplainerObservation
except ImportError:  # pragma: no cover - supports direct test execution
    from models import ExplainerAction, ExplainerObservation


class ExplainerEnv(
    EnvClient[ExplainerAction, ExplainerObservation, State]
):
    """
    Client for the Research → Interactive Explainer environment.

    Example:
        >>> with ExplainerEnv(base_url="http://localhost:8000").sync() as sc:
        ...     result = sc.reset()
        ...     # Explore phase
        ...     result = sc.step(ExplainerAction(
        ...         action_type="explore",
        ...         tool="search_arxiv",
        ...         query="attention mechanism transformers",
        ...         intent="visual intuition and equations",
        ...     ))
        ...     # Generate phase
        ...     result = sc.step(ExplainerAction(
        ...         action_type="generate", format="marimo", code="import marimo..."
        ...     ))
    """

    def _step_payload(self, action: ExplainerAction) -> Dict:
        return action.model_dump()

    def _parse_result(self, payload: Dict) -> StepResult[ExplainerObservation]:
        obs_data = payload.get("observation", {})
        observation = ExplainerObservation(
            **{
                k: obs_data.get(k)
                for k in ExplainerObservation.model_fields
                if k in obs_data
            },
            done=payload.get("done", False),
            reward=payload.get("reward", 0.0),
            metadata=obs_data.get("metadata", {}),
        )
        return StepResult(
            observation=observation,
            reward=payload.get("reward", 0.0),
            done=payload.get("done", False),
        )

    def _parse_state(self, payload: Dict) -> State:
        return State(
            episode_id=payload.get("episode_id"),
            step_count=payload.get("step_count", 0),
        )