File size: 1,512 Bytes
822ca51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Typed HTTP client for the DB-ER environment."""

from __future__ import annotations

from typing import Any, Dict, Optional

from openenv.core import EnvClient
from openenv.core.client_types import StepResult

from db_er.models import DBERAction, DBERObservation, DBERState


class DBERClient(EnvClient[DBERAction, DBERObservation, DBERState]):
    """Async client that connects to a running DB-ER server.

    Usage (async)::

        async with DBERClient(base_url="http://localhost:8000") as client:
            result = await client.reset()
            print(result.observation.incident_ticket)

    Usage (sync)::

        with DBERClient(base_url="http://localhost:8000").sync() as client:
            result = client.reset()
            obs = result.observation

    The ``reset()`` method accepts an optional ``task_id`` keyword argument
    (1=Easy, 2=Medium, 3=Hard).  If omitted the server cycles tasks based on
    the provided ``seed``.
    """

    def _step_payload(self, action: DBERAction) -> Dict[str, Any]:
        return action.model_dump(exclude_none=True)

    def _parse_result(self, payload: Dict[str, Any]) -> StepResult[DBERObservation]:
        obs_data = payload.get("observation", {})
        reward = payload.get("reward")
        done = payload.get("done", False)
        obs = DBERObservation(**obs_data)
        return StepResult(observation=obs, reward=reward, done=done)

    def _parse_state(self, payload: Dict[str, Any]) -> DBERState:
        return DBERState(**payload)