Spaces:
Sleeping
Sleeping
| """DocEdit 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 | |
| from .models import DocEditAction, DocEditObservation | |
| class DocEditEnv(EnvClient[DocEditAction, DocEditObservation, State]): | |
| """ | |
| Client for the DocEdit Environment. | |
| Maintains a persistent WebSocket connection to the environment server. | |
| Example: | |
| >>> with DocEditEnv(base_url="http://localhost:8000") as client: | |
| ... result = client.reset() | |
| ... result = client.step(DocEditAction(operation="replace", target="revnue", content="revenue")) | |
| """ | |
| def _step_payload(self, action: DocEditAction) -> Dict: | |
| return { | |
| "operation": action.operation, | |
| "target": action.target, | |
| "content": action.content, | |
| "position": action.position, | |
| } | |
| def _parse_result(self, payload: Dict) -> StepResult[DocEditObservation]: | |
| obs_data = payload.get("observation", {}) | |
| observation = DocEditObservation( | |
| document=obs_data.get("document", ""), | |
| target_description=obs_data.get("target_description", ""), | |
| similarity=obs_data.get("similarity", 0.0), | |
| task_name=obs_data.get("task_name", ""), | |
| steps_remaining=obs_data.get("steps_remaining", 0), | |
| done=payload.get("done", False), | |
| reward=payload.get("reward"), | |
| metadata=obs_data.get("metadata", {}), | |
| ) | |
| return StepResult( | |
| observation=observation, | |
| reward=payload.get("reward"), | |
| 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), | |
| ) | |