File size: 9,046 Bytes
2929135
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# src/agent.py
from typing import Dict, Optional, List
import uuid
from datetime import datetime
from langchain_core.messages import HumanMessage, SystemMessage, AnyMessage
from langgraph.graph import StateGraph, END
from langchain_openai import ChatOpenAI

# Remove this line as it's causing the error
# from langgraph.checkpoint import BaseCheckpointSaver

from .config.settings import Settings
from .models.state import (
    HospitalState,
    create_initial_state,
    validate_state
)
from .nodes import (
    InputAnalyzerNode,
    TaskRouterNode,
    PatientFlowNode,
    ResourceManagerNode,
    QualityMonitorNode,
    StaffSchedulerNode,
    OutputSynthesizerNode
)
from .tools import (
    PatientTools,
    ResourceTools,
    QualityTools,
    SchedulingTools
)

from .utils.logger import setup_logger
from .utils.error_handlers import (
    ErrorHandler, 
    HealthcareError, 
    ValidationError,  # Add this import
    ProcessingError   # Add this import
)


logger = setup_logger(__name__)

class HealthcareAgent:
    def __init__(self, api_key: Optional[str] = None):
        try:
            # Initialize settings and validate
            self.settings = Settings()
            if api_key:
                self.settings.OPENAI_API_KEY = api_key
            self.settings.validate_settings()
            
            # Initialize LLM
            self.llm = ChatOpenAI(
                model=self.settings.MODEL_NAME,
                temperature=self.settings.MODEL_TEMPERATURE,
                api_key=self.settings.OPENAI_API_KEY
            )
            
            # Initialize tools
            self.tools = self._initialize_tools()
            
            # Initialize nodes
            self.nodes = self._initialize_nodes()
            
            # Initialize conversation states (replacing checkpointer)
            self.conversation_states = {}
            
            # Build graph
            self.graph = self._build_graph()
            
            logger.info("Healthcare Agent initialized successfully")
            
        except Exception as e:
            logger.error(f"Error initializing Healthcare Agent: {str(e)}")
            raise HealthcareError(
                message="Failed to initialize Healthcare Agent",
                error_code="INIT_ERROR",
                details={"error": str(e)}
            )

    def _initialize_tools(self) -> Dict:
        """Initialize all tools used by the agent"""
        return {
            "patient": PatientTools(),
            "resource": ResourceTools(),
            "quality": QualityTools(),
            "scheduling": SchedulingTools()
        }

    def _initialize_nodes(self) -> Dict:
        """Initialize all nodes in the agent workflow"""
        return {
            "input_analyzer": InputAnalyzerNode(self.llm),
            "task_router": TaskRouterNode(),
            "patient_flow": PatientFlowNode(self.llm),
            "resource_manager": ResourceManagerNode(self.llm),
            "quality_monitor": QualityMonitorNode(self.llm),
            "staff_scheduler": StaffSchedulerNode(self.llm),
            "output_synthesizer": OutputSynthesizerNode(self.llm)
        }

    def _build_graph(self) -> StateGraph:
        """Build the workflow graph with all nodes and edges"""
        try:
            # Initialize graph
            builder = StateGraph(HospitalState)
            
            # Add all nodes
            for name, node in self.nodes.items():
                builder.add_node(name, node)
            
            # Set entry point
            builder.set_entry_point("input_analyzer")
            
            # Add edge from input analyzer to task router
            builder.add_edge("input_analyzer", "task_router")
            
            # Define conditional routing based on task router output
            def route_next(state: Dict):
                return state["context"]["next_node"]
            
            # Add conditional edges from task router
            builder.add_conditional_edges(
                "task_router",
                route_next,
                {
                    "patient_flow": "patient_flow",
                    "resource_management": "resource_manager",
                    "quality_monitoring": "quality_monitor",
                    "staff_scheduling": "staff_scheduler",
                    "output_synthesis": "output_synthesizer"
                }
            )
            
            # Add edges from functional nodes to output synthesizer
            functional_nodes = [
                "patient_flow",
                "resource_manager",
                "quality_monitor",
                "staff_scheduler"
            ]
            
            for node in functional_nodes:
                builder.add_edge(node, "output_synthesizer")
            
            # Add end condition
            builder.add_edge("output_synthesizer", END)
            
            # Compile graph
            return builder.compile()
            
        except Exception as e:
            logger.error(f"Error building graph: {str(e)}")
            raise HealthcareError(
                message="Failed to build agent workflow graph",
                error_code="GRAPH_BUILD_ERROR",
                details={"error": str(e)}
            )

    @ErrorHandler.error_decorator
    def process(
        self,
        input_text: str,
        thread_id: Optional[str] = None,
        context: Optional[Dict] = None
    ) -> Dict:
        """Process input through the healthcare operations workflow"""
        try:
            # Validate input
            ErrorHandler.validate_input(input_text)
            
            # Create or use thread ID
            thread_id = thread_id or str(uuid.uuid4())
            
            # Initialize state
            initial_state = create_initial_state(thread_id)
            
            # Add input message as HumanMessage object
            initial_state["messages"].append(
                HumanMessage(content=input_text)
            )
            
            # Add context if provided
            if context:
                initial_state["context"].update(context)
            
            # Validate state
            validate_state(initial_state)
            
            # Store state in conversation states
            self.conversation_states[thread_id] = initial_state
            
            # Process through graph
            result = self.graph.invoke(initial_state)
            
            return self._format_response(result)
            
        except ValidationError as ve:
            logger.error(f"Validation error: {str(ve)}")
            raise
        except Exception as e:
            logger.error(f"Error processing input: {str(e)}")
            raise HealthcareError(
                message="Failed to process input",
                error_code="PROCESSING_ERROR",
                details={"error": str(e)}
            )

    def _format_response(self, result: Dict) -> Dict:
        """Format the final response from the graph execution"""
        try:
            if not result or "messages" not in result:
                raise ProcessingError(
                    message="Invalid result format",
                    error_code="INVALID_RESULT",
                    details={"result": str(result)}
                )
                
            return {
                "response": result["messages"][-1].content if result["messages"] else "",
                "analysis": result.get("analysis", {}),
                "metrics": result.get("metrics", {}),
                "timestamp": datetime.now()
            }
        except Exception as e:
            logger.error(f"Error formatting response: {str(e)}")
            raise HealthcareError(
                message="Failed to format response",
                error_code="FORMAT_ERROR",
                details={"error": str(e)}
            )

    def get_conversation_history(
        self,
        thread_id: str
    ) -> List[Dict]:
        """Retrieve conversation history for a specific thread"""
        try:
            return self.conversation_states.get(thread_id, {}).get("messages", [])
        except Exception as e:
            logger.error(f"Error retrieving conversation history: {str(e)}")
            raise HealthcareError(
                message="Failed to retrieve conversation history",
                error_code="HISTORY_ERROR",
                details={"error": str(e)}
            )

    def reset_conversation(
        self,
        thread_id: str
    ) -> bool:
        """Reset conversation state for a specific thread"""
        try:
            self.conversation_states[thread_id] = create_initial_state(thread_id)
            return True
        except Exception as e:
            logger.error(f"Error resetting conversation: {str(e)}")
            raise HealthcareError(
                message="Failed to reset conversation",
                error_code="RESET_ERROR",
                details={"error": str(e)}
            )