Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

1.7 KB
Raw Blame
"""
Facade for ml_inference.

The ONLY module other products are allowed to import. Accepts and returns the frozen contracts.
"""

from __future__ import annotations

from ..logic import decisions
from . import contracts


def decisions_available_here() -> bool:
    """Whether this region serves decisions at all. A batch caller checks this once before it
    enumerates work, so a region without the service spends nothing per team."""
    return decisions.decisions_available_here()


def decisions_enabled(team_id: int) -> bool:
    return decisions.decisions_enabled(team_id)


def decide(request: contracts.DecisionRequest) -> contracts.DecisionResult:
    """Ask the model for an enrolled team; raises DecisionsDisabledError otherwise."""
    if not decisions.decisions_enabled(request.team_id):
        raise contracts.DecisionsDisabledError(request.team_id)
    return decisions.decide(request)


def decide_when_available(
    request: contracts.DecisionRequest, *, timeout_seconds: float | None = None
) -> contracts.DecisionResult:
    """Ask the model where the decision service is available; the caller owns its rollout gate."""
    if not decisions.decisions_available_here():
        raise contracts.DecisionsDisabledError(request.team_id)
    if timeout_seconds is None:
        return decisions.decide(request)
    return decisions.decide(request, timeout_seconds=timeout_seconds)


def decide_unchecked(
    request: contracts.DecisionRequest, *, timeout_seconds: float | None = None
) -> contracts.DecisionResult:
    """Ask the model when the caller owns its rollout gate."""
    if timeout_seconds is None:
        return decisions.decide(request)
    return decisions.decide(request, timeout_seconds=timeout_seconds)