File size: 4,117 Bytes
749ed59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# 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.

"""MedCodeRL 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 MedAction, MedObservation


class MedCodeEnv(
    EnvClient[MedAction, MedObservation, State]
):
    """
    Client for the MedCodeRL Environment.

    This client maintains a persistent WebSocket connection to the environment server,
    enabling efficient multi-step interactions with lower latency.

    Example:
        >>> with MedCodeEnv(base_url="http://localhost:7680") as client:
        ...     result = client.reset()
        ...     print(result.observation.clinical_note)
        ...
        ...     action = MedAction(
        ...         diagnosis_codes=["J02.9"],
        ...         procedure_codes=["99213"],
        ...         decision="approve",
        ...         confidence=0.9,
        ...         reasoning="Acute pharyngitis with appropriate E&M coding.",
        ...         risk_flags=[]
        ...     )
        ...     result = client.step(action)
        ...     print(f"Score: {result.reward}")
    """

    def _step_payload(self, action: MedAction) -> Dict:
        """
        Convert MedAction to JSON payload for step message.

        Args:
            action: MedAction instance

        Returns:
            Dictionary representation suitable for JSON encoding
        """
        return {
            "diagnosis_codes": action.diagnosis_codes,
            "procedure_codes": action.procedure_codes,
            "decision": action.decision,
            "confidence": action.confidence,
            "reasoning": action.reasoning,
            "modifier_codes": action.modifier_codes,
            "risk_flags": action.risk_flags,
        }

    def _parse_result(self, payload: Dict) -> StepResult[MedObservation]:
        """
        Parse server response into StepResult[MedObservation].

        Args:
            payload: JSON response data from server

        Returns:
            StepResult with MedObservation
        """
        obs_data = payload.get("observation", {})
        observation = MedObservation(
            case_id=obs_data.get("case_id", ""),
            difficulty=obs_data.get("difficulty", "easy"),
            clinical_note=obs_data.get("clinical_note", ""),
            symptoms=obs_data.get("symptoms", []),
            treatments=obs_data.get("treatments", []),
            insurance_type=obs_data.get("insurance_type", "Private"),
            prior_auth_required=obs_data.get("prior_auth_required", False),
            treatment_cost=obs_data.get("treatment_cost", "low"),
            patient_age=obs_data.get("patient_age", 0),
            patient_sex=obs_data.get("patient_sex", "M"),
            provider_specialty=obs_data.get("provider_specialty", ""),
            visit_type=obs_data.get("visit_type", "outpatient"),
            comorbidities=obs_data.get("comorbidities", []),
            lab_results=obs_data.get("lab_results"),
            medications=obs_data.get("medications", []),
            reward_breakdown=obs_data.get("reward_breakdown"),
            feedback=obs_data.get("feedback", ""),
            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:
        """
        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),
        )