openenv/coding_env
21
1"""2CodingEnv3---------4Client-side wrapper for the Coding environment server.5Talks HTTP to a single base_url exposing: /reset and /step.6 7- users instantiate CodingEnv with a base_url provided by the higher-level8 vector/orchestration layer.9- Environment authors ship the Docker image that serves the HTTP API.10 11(Seeds, episode IDs, request IDs, capabilities can be added later in the payloads.)12"""13 14from __future__ import annotations15 16from typing import TYPE_CHECKING, Optional17 18from core.http_env_client import HTTPEnvClient19from core.types import StepResult20 21from .models import CodeAction, CodeObservation, CodeState22 23if TYPE_CHECKING:24 from core.containers.runtime import ContainerProvider25 26 27class CodingEnv(HTTPEnvClient[CodeAction, CodeObservation]):28 # --- HTTPEnvClient abstract hooks ---29 30 def _step_payload(self, action: CodeAction) -> dict:31 # Shape expected by the server's /step endpoint under "action"32 return {33 "code": action.code,34 }35 36 def _parse_result(self, payload: dict) -> StepResult[CodeObservation]:37 # Expecting: { "observation": {...}, "reward": <float|null>, "done": <bool>, "info": {...} }38 obs = CodeObservation(**payload["observation"])39 return StepResult(40 observation=obs,41 reward=payload.get("reward"),42 done=bool(payload.get("done", False)),43 )44 45 def _parse_state(self, payload: dict) -> CodeState:46 """47 Parse server response into CodeState object.48 49 Args:50 payload: JSON response from /state endpoint51 52 Returns:53 CodeState object with episode_id, step_count, and last_exit_code54 """55 return CodeState(56 episode_id=payload.get("episode_id"),57 step_count=payload.get("step_count", 0),58 last_exit_code=payload.get("last_exit_code", 0),59 )60 