Spaces:
Running
Running
| """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), | |
| ) | |