"""AgentHub / AgentsRuntime error payload extractor."""

from typing import Any

from .._enriched_exception import ExtractedErrorInfo
from ._helpers import get_typed_field


def extract_agenthub(body: dict[str, Any]) -> ExtractedErrorInfo:
    message = get_typed_field(body, str, "message", "title")
    error_code = get_typed_field(body, str, "errorCode", "code")
    trace_id = get_typed_field(body, str, "traceId")

    problem = get_typed_field(body, dict, "problem")
    if problem is not None:
        message = message or get_typed_field(problem, str, "detail", "title")
        error_code = error_code or get_typed_field(problem, str, "errorCode", "type")
        trace_id = trace_id or get_typed_field(problem, str, "traceId")

    return ExtractedErrorInfo(
        message=message,
        error_code=error_code,
        trace_id=trace_id,
    )
