health.mojo (2082B)
1 from hyf_assist.contract import max_local_query_rewrite_route 2 from hyf_provider.client import get_max_local_health 3 from hyf_provider.config import MaxLocalProviderConfig 4 from hyf_provider.result import MaxLocalProviderStatus 5 6 7 @fieldwise_init 8 struct MaxLocalHealthFailure(Copyable, Movable): 9 var kind: String 10 var reason: String 11 12 13 def _health_failure(kind: String, reason: String) -> MaxLocalHealthFailure: 14 return MaxLocalHealthFailure(kind=String(kind), reason=String(reason)) 15 16 17 def _provider_status( 18 config: MaxLocalProviderConfig, 19 reachable: Bool, 20 state: String, 21 reason: String, 22 ) -> MaxLocalProviderStatus: 23 return MaxLocalProviderStatus( 24 backend_kind="max_local", 25 provider="max_local", 26 route=max_local_query_rewrite_route(), 27 model=String(config.model), 28 reachable=reachable, 29 state=String(state), 30 reason=String(reason), 31 ) 32 33 34 def max_local_health_failure_from_reason( 35 reason: String, 36 ) -> MaxLocalHealthFailure: 37 if reason == "invalid_url": 38 return _health_failure("transport", "invalid_url") 39 if reason == "timeout": 40 return _health_failure("transport", "timeout") 41 if reason == "connection_failed": 42 return _health_failure("transport", "connection_failed") 43 if reason == "non_2xx": 44 return _health_failure("http_status", "non_2xx") 45 return _health_failure("transport", "connection_failed") 46 47 48 def resolve_max_local_provider_status( 49 config: MaxLocalProviderConfig, 50 ) -> MaxLocalProviderStatus: 51 var transport = get_max_local_health(config) 52 if transport.response: 53 return _provider_status(config, True, "ready", "ready") 54 55 if transport.failure: 56 var failure = max_local_health_failure_from_reason( 57 transport.failure.value().reason 58 ) 59 return _provider_status( 60 config, 61 False, 62 "unavailable", 63 String(failure.reason), 64 ) 65 66 return _provider_status( 67 config, 68 False, 69 "unavailable", 70 "connection_failed", 71 )