"""Host entrypoint for embedded Python execution (PyO3/XPC)."""

from __future__ import annotations

import json

from rlm_runner import run as run_runner


def run_for_host(request_json: str) -> str:
    """Run an RLM request from a host process and return JSON."""
    try:
        payload = json.loads(request_json)
    except json.JSONDecodeError as exc:
        return json.dumps({"error": {"type": "JSONDecodeError", "message": str(exc)}}, ensure_ascii=True)
    try:
        payload["adapter_module"] = "rcl_rlm.runner_adapter"
        response = run_runner(payload)
        return json.dumps(response, ensure_ascii=True)
    except Exception as exc:  # noqa: BLE001 - keep host boundary resilient
        return json.dumps(
            {"error": {"type": exc.__class__.__name__, "message": str(exc)}},
            ensure_ascii=True,
        )
