import asyncio
from math import isfinite
from typesafe_sdk import AsyncTypeSafeClient, Choice, Noul, Score, RetryPolicy


async def suggest_team(client):
    # Overall deadline bounds requests, retries and backoff together.
    async with asyncio.timeout(8):
        response = await client.system_one(
            model="jev-1.13.0",
            state={"message": "Checkout fails for every customer.", "scope": "all customers"},
            questions={
                "route": Choice(instructions="Which team owns this incident?", criteria={
                    "technical": "Bugs or integrations", "billing": "Invoices", "human": "Unclear ownership"}),
                "severity": Score(instructions="How blocked are customers?", criteria=[
                    "Unaffected", "Workaround available", "Core workflow blocked"]),
                "urgent": Noul(instructions="Does the message describe an ongoing service disruption?"),
            },
        )
    route = response.choices["route"]
    # The SDK validates the response shape. This branch is application policy.
    valid = route.choice in {"technical", "billing", "human"} and isfinite(route.confidence) and 0 <= route.confidence <= 1
    # Teaching threshold only; calibrate on independently labeled outcomes.
    return {"action": "review" if not valid or route.choice == "human" or route.confidence < 0.75 else "suggest",
            "team": route.choice, "severity": response.scores["severity"].score,
            "urgent_probability": response.nouls["urgent"].noul}


async def main():
    try:
        async with AsyncTypeSafeClient(timeout=2, retry=RetryPolicy(max_retries=1)) as client:
            print(await suggest_team(client))
    except Exception:
        print({"action": "review", "reason": "provider_or_configuration_failure"})


if __name__ == "__main__":
    asyncio.run(main())
