File size: 1,681 Bytes
50bf486
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
"""WebSocket client for talking to the SQL Debug Environment server."""

from __future__ import annotations

from typing import Any, Dict

from openenv.core.env_client import EnvClient, StepResult

from sql_debug_env.models import SQLAction, SQLObservation, SQLState


class SQLDebugEnv(EnvClient[SQLAction, SQLObservation, SQLState]):
    """Client for the SQL Debug Environment.

    Wraps the OpenEnv WebSocket protocol — handles serialization,
    connection management, and type parsing so you don't have to.

    Quick start:
        async with SQLDebugEnv(base_url="http://localhost:7860") as env:
            obs = await env.reset(task_id="fix_syntax")
            obs = await env.step(SQLAction(query="SELECT ..."))

    Or synchronously:
        with SQLDebugEnv(base_url="http://localhost:7860").sync() as env:
            obs = env.reset(task_id="fix_syntax")
            obs = env.step(SQLAction(query="SELECT ..."))
    """

    def _parse_result(self, payload: Dict[str, Any]) -> StepResult[SQLObservation]:
        """Turn the raw JSON payload into a typed StepResult."""
        obs_data = payload.get("observation", payload)
        observation = SQLObservation(**obs_data)
        return StepResult(
            observation=observation,
            reward=payload.get("reward", observation.reward),
            done=payload.get("done", observation.done),
        )

    def _parse_state(self, payload: Dict[str, Any]) -> SQLState:
        """Deserialize a state payload."""
        return SQLState(**payload)

    def _step_payload(self, action: SQLAction) -> Dict[str, Any]:
        """Serialize the action for the wire."""
        return action.model_dump()