Echo / agents /tool_wrappers.py
moein99's picture
Initial Echo Space
8f51ef2
"""
Tool wrappers for the intelligent agent.
This module provides simple wrappers around the real echo analysis tools.
"""
import sys
from pathlib import Path
from typing import Dict, Any
# Ensure the project root is available on sys.path so local tool modules can be imported
PROJECT_ROOT = Path(__file__).resolve().parents[1]
if str(PROJECT_ROOT) not in sys.path:
sys.path.insert(0, str(PROJECT_ROOT))
try:
from tools.echo.echo_tool_managers import (
EchoViewClassificationTool,
EchoDiseasePredictionTool,
EchoMeasurementPredictionTool,
EchoSegmentationTool,
EchoReportGenerationTool,
EchoImageVideoGenerationTool,
)
class ViewClassificationWrapper:
def __init__(self):
self.name = "view_classification"
self.tool = EchoViewClassificationTool()
def run(self, video_path: str, **kwargs) -> Dict[str, Any]:
try:
result = self.tool.run(video_path)
return {
"success": True,
"view": result.get("view", "Unknown"),
"confidence": result.get("confidence", 0.0),
"reasoning": result.get("reasoning", "Real analysis completed"),
}
except Exception as exc: # pragma: no cover - fail gracefully when upstream tool errors
return {"success": False, "error": f"Real tool failed: {exc}"}
class DiseasePredictionWrapper:
def __init__(self):
self.name = "disease_prediction"
self.tool = EchoDiseasePredictionTool()
def run(self, video_path: str, **kwargs) -> Dict[str, Any]:
try:
result = self.tool.run(video_path)
return {
"success": True,
"diseases": result.get("diseases", ["Unknown"]),
"probabilities": result.get("probabilities", [0.0]),
"reasoning": result.get("reasoning", "Real analysis completed"),
}
except Exception as exc: # pragma: no cover
return {"success": False, "error": f"Real tool failed: {exc}"}
class MeasurementsWrapper:
def __init__(self):
self.name = "measurements"
self.tool = EchoMeasurementPredictionTool()
def run(self, video_path: str, **kwargs) -> Dict[str, Any]:
try:
result = self.tool.run(video_path)
return {
"success": True,
"ejection_fraction": result.get("ejection_fraction", 0.0),
"lv_dimensions": result.get("lv_dimensions", {}),
"reasoning": result.get("reasoning", "Real analysis completed"),
}
except Exception as exc: # pragma: no cover
return {"success": False, "error": f"Real tool failed: {exc}"}
class SegmentationWrapper:
def __init__(self):
self.name = "segmentation"
self.tool = EchoSegmentationTool()
def run(self, video_path: str, **kwargs) -> Dict[str, Any]:
try:
result = self.tool.run(video_path)
return {
"success": True,
"segments": result.get("segments", ["LV", "RV", "LA", "RA"]),
"masks": result.get("masks", ["lv_mask.npy", "rv_mask.npy"]),
"reasoning": result.get("reasoning", "Real analysis completed"),
}
except Exception as exc: # pragma: no cover
return {"success": False, "error": f"Real tool failed: {exc}"}
class ReportGenerationWrapper:
def __init__(self):
self.name = "report_generation"
self.tool = EchoReportGenerationTool()
def run(self, analysis_results: Dict[str, Any], **kwargs) -> Dict[str, Any]:
try:
result = self.tool.run(analysis_results)
return {
"success": True,
"report": result.get("report", "Comprehensive echo analysis report generated"),
"summary": result.get("summary", "Normal cardiac function with standard measurements"),
"recommendations": result.get("recommendations", ["Continue regular monitoring"]),
}
except Exception as exc: # pragma: no cover
return {"success": False, "error": f"Real tool failed: {exc}"}
class ImageVideoGenerationWrapper:
def __init__(self):
self.name = "echo_image_video_generation"
self.tool = EchoImageVideoGenerationTool()
def run(self, payload: Dict[str, Any], **kwargs) -> Dict[str, Any]:
try:
return self.tool.run(payload)
except Exception as exc: # pragma: no cover
return {"success": False, "error": f"Real tool failed: {exc}"}
except Exception as import_error:
print(f"⚠️ Failed to import real tools: {import_error}")
class ViewClassificationWrapper:
def __init__(self):
self.name = "view_classification"
def run(self, video_path: str, **kwargs) -> Dict[str, Any]:
return {
"success": True,
"view": "A4C",
"confidence": 0.85,
"reasoning": "Mock analysis - real tool not available",
}
class DiseasePredictionWrapper:
def __init__(self):
self.name = "disease_prediction"
def run(self, video_path: str, **kwargs) -> Dict[str, Any]:
return {
"success": True,
"diseases": ["Normal"],
"probabilities": [0.75],
"reasoning": "Mock analysis - real tool not available",
}
class MeasurementsWrapper:
def __init__(self):
self.name = "measurements"
def run(self, video_path: str, **kwargs) -> Dict[str, Any]:
return {
"success": True,
"ejection_fraction": 0.65,
"lv_dimensions": {"lvidd": 4.2, "lvids": 2.8},
"reasoning": "Mock analysis - real tool not available",
}
class SegmentationWrapper:
def __init__(self):
self.name = "segmentation"
def run(self, video_path: str, **kwargs) -> Dict[str, Any]:
return {
"success": True,
"segments": ["LV", "RV", "LA", "RA"],
"masks": ["lv_mask.npy", "rv_mask.npy"],
"reasoning": "Mock analysis - real tool not available",
}
class ReportGenerationWrapper:
def __init__(self):
self.name = "report_generation"
def run(self, analysis_results: Dict[str, Any], **kwargs) -> Dict[str, Any]:
return {
"success": True,
"report": "Comprehensive echo analysis report generated",
"summary": "Normal cardiac function with standard measurements",
"recommendations": ["Continue regular monitoring"],
}
class ImageVideoGenerationWrapper:
def __init__(self):
self.name = "echo_image_video_generation"
def run(self, payload: Dict[str, Any], **kwargs) -> Dict[str, Any]:
return {"success": True, "reasoning": "Mock generation - real tool not available"}