"""Benchmarking Widget Resource."""
import logging
from pathlib import Path
from app.service.mcp_server import mcp
logger = logging.getLogger(__name__)
VIEW_URI = "ui://benchmark/view.html"
# Widget paths - prefer built React version, fallback to vanilla HTML
# app/web/dist/ is at app level, not inside service
WEB_DIST_DIR = Path(__file__).parent.parent.parent / "web" / "dist"
WIDGETS_DIR = Path(__file__).parent.parent / "widgets"
def load_widget_html() -> str:
"""Load widget HTML - prefer built React version, fallback to vanilla."""
# Try built React widget first
react_widget = WEB_DIST_DIR / "benchmarking-widget.html"
if react_widget.exists():
logger.info("π¦ Using built React widget")
return react_widget.read_text()
# Fallback to vanilla HTML widget
vanilla_widget = WIDGETS_DIR / "benchmarking_widget.html"
if vanilla_widget.exists():
logger.info("π Using vanilla HTML widget (React build not found)")
return vanilla_widget.read_text()
logger.error("β No widget found!")
return "<html><body><h1>Widget not found</h1></body></html>"
@mcp.resource(
VIEW_URI,
name="benchmark-widget",
description="Interactive benchmark comparison visualization",
mime_type="text/html;profile=mcp-app",
meta={"ui": {"csp": {"resourceDomains": ["https://unpkg.com"]}}},
)
def benchmark_widget_html() -> str:
"""Serve benchmarking widget HTML."""
logger.info(f"π Serving widget: {VIEW_URI}")
return load_widget_html()
logger.info("β Benchmarking widget resource registered")