hyf

Context-aware query service for Radroots
git clone https://radroots.dev/git/hyf.git
Log | Files | Refs | README | LICENSE

selector.mojo (1313B)


      1 from json import Value
      2 
      3 from hyf_core.backends.heuristic_backend import (
      4     backend_name as heuristic_backend_name,
      5     execute_capability as execute_heuristic_capability,
      6 )
      7 from hyf_core.errors import (
      8     CapabilityResult,
      9     backend_unavailable_error,
     10     failed_capability,
     11 )
     12 from hyf_core.request_context import (
     13     RequestContext,
     14     assisted_execution_requested,
     15 )
     16 
     17 
     18 @fieldwise_init
     19 struct BackendSelection(Copyable, Movable):
     20     var backend_name: String
     21     var available: Bool
     22 
     23 
     24 def resolve_backend(context: RequestContext) -> BackendSelection:
     25     if assisted_execution_requested(context):
     26         return BackendSelection(backend_name=heuristic_backend_name(), available=True)
     27 
     28     return BackendSelection(
     29         backend_name=heuristic_backend_name(), available=True
     30     )
     31 
     32 
     33 def execute_capability(
     34     capability_id: String, input: Value, context: RequestContext
     35 ) raises -> CapabilityResult:
     36     var selection = resolve_backend(context)
     37     if not selection.available:
     38         return failed_capability(
     39             backend_unavailable_error(selection.backend_name)
     40         )
     41 
     42     if selection.backend_name == heuristic_backend_name():
     43         return execute_heuristic_capability(capability_id, input, context)
     44 
     45     return failed_capability(backend_unavailable_error(selection.backend_name))