Spaces:
Sleeping
Sleeping
| # Copyright (c) Meta Platforms, Inc. and affiliates. | |
| # All rights reserved. | |
| # | |
| # This source code is licensed under the BSD-style license found in the | |
| # LICENSE file in the root directory of this source tree. | |
| """FixOS 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 FixOSAction, FixOSObservation | |
| class FixOSEnv( | |
| EnvClient[FixOSAction, FixOSObservation, State] | |
| ): | |
| """ | |
| Client for the FixOS environment. | |
| This client maintains a persistent WebSocket connection to the environment server, | |
| enabling efficient multi-step interactions with lower latency. | |
| Each client instance has its own dedicated environment session on the server. | |
| Example: | |
| >>> with FixOSEnv(base_url="http://localhost:8000") as client: | |
| ... result = client.reset() | |
| ... action = FixOSAction(command="status", args={}) | |
| ... result = client.step(action) | |
| """ | |
| def _step_payload(self, action: FixOSAction) -> Dict: | |
| """ | |
| Convert FixOSAction to JSON payload for step message. | |
| Args: | |
| action: FixOSAction instance | |
| Returns: | |
| Dictionary representation suitable for JSON encoding | |
| """ | |
| return {"command": action.command, "args": action.args} | |
| def _parse_result(self, payload: Dict) -> StepResult[FixOSObservation]: | |
| """ | |
| Parse server response into StepResult[FixOSObservation]. | |
| Args: | |
| payload: JSON response data from server | |
| Returns: | |
| StepResult with FixOSObservation | |
| """ | |
| obs_data = payload.get("observation", {}) | |
| observation = FixOSObservation( | |
| command_output=obs_data.get("command_output", ""), | |
| processes=obs_data.get("processes", []), | |
| services=obs_data.get("services", []), | |
| filesystem=obs_data.get("filesystem", []), | |
| resources=obs_data.get("resources", {}), | |
| logs=obs_data.get("logs", []), | |
| history=obs_data.get("history", []), | |
| task_id=obs_data.get("task_id", ""), | |
| task_difficulty=obs_data.get("task_difficulty", ""), | |
| task_score=obs_data.get("task_score", 0.0), | |
| is_success_step=obs_data.get("is_success_step", False), | |
| remaining_steps=obs_data.get("remaining_steps", 0), | |
| done=payload.get("done", False), | |
| reward=payload.get("reward"), | |
| ) | |
| return StepResult( | |
| observation=observation, | |
| reward=payload.get("reward"), | |
| done=payload.get("done", False), | |
| ) | |
| def _parse_state(self, payload: Dict) -> State: | |
| """ | |
| Parse server response into State object. | |
| Args: | |
| payload: JSON response from state request | |
| Returns: | |
| State object with episode_id and step_count | |
| """ | |
| return State( | |
| episode_id=payload.get("episode_id"), | |
| step_count=payload.get("step_count", 0), | |
| ) | |