MCPcopy Create free account
hub / github.com/CommonstackAI/UncommonRoute / handle_route_preview

Function handle_route_preview

uncommon_route/proxy.py:3840–3908  ·  view source on GitHub ↗

POST /v1/route-preview — preview routing decision without sending request.

(request: Request)

Source from the content-addressed store, hash-verified

3838 return JSONResponse(metrics)
3839
3840 async def handle_route_preview(request: Request) -> JSONResponse:
3841 """POST /v1/route-preview — preview routing decision without sending request."""
3842 try:
3843 body = await request.json()
3844 except Exception:
3845 return JSONResponse({"error": "invalid JSON"}, status_code=400)
3846 preview_body = dict(body)
3847 if not preview_body.get("model") and not preview_body.get("mode"):
3848 try:
3849 risk_tolerance = float(preview_body.get("risk_tolerance", 0.5))
3850 except (TypeError, ValueError):
3851 risk_tolerance = 0.5
3852 if risk_tolerance <= 0.25:
3853 preview_body["mode"] = RoutingMode.BEST.value
3854 elif risk_tolerance >= 0.75:
3855 preview_body["mode"] = RoutingMode.FAST.value
3856
3857 normalized_body, error = _normalize_selector_body(
3858 preview_body,
3859 default_mode=_routing_store.default_mode(),
3860 )
3861 if normalized_body is None:
3862 return JSONResponse({"error": error or "Invalid route preview payload"}, status_code=400)
3863 try:
3864 preview = _build_selector_preview(normalized_body, request)
3865 except RoutingInfeasibleError as exc:
3866 payload = _routing_infeasible_payload(exc)
3867 payload["selector"] = _selector_state()
3868 return JSONResponse(payload, status_code=400)
3869
3870 tier_name = str(preview.get("served_tier") or "MEDIUM").upper()
3871 tier_index = {
3872 "SIMPLE": 0,
3873 "MEDIUM": 1,
3874 "COMPLEX": 3,
3875 }.get(tier_name, 1)
3876 legacy_tier_name = {
3877 "SIMPLE": "low",
3878 "MEDIUM": "mid",
3879 "COMPLEX": "high",
3880 }.get(tier_name, "mid")
3881 routing_features = preview.get("routing_features")
3882 step_risk = ""
3883 if isinstance(routing_features, dict):
3884 step_risk = str(routing_features.get("step_risk") or "")
3885 risk_tier = {"low": 0, "normal": 1, "high": 3}.get(step_risk, tier_index)
3886
3887 result = {
3888 **preview,
3889 # Compatibility fields consumed by the existing dashboard Playground.
3890 "tier": tier_index,
3891 "tier_name": legacy_tier_name,
3892 "cost_estimate": preview.get("estimated_cost", 0.0),
3893 "cost_baseline": preview.get("baseline_cost", preview.get("estimated_cost", 0.0)),
3894 "signals": [
3895 {
3896 "name": "router",
3897 "tier": tier_index,

Callers

nothing calls this directly

Calls 6

_normalize_selector_bodyFunction · 0.85
_build_selector_previewFunction · 0.85
_selector_stateFunction · 0.85
default_modeMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected