selector.mojo (1335B)
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( 27 backend_name=heuristic_backend_name(), available=True 28 ) 29 30 return BackendSelection( 31 backend_name=heuristic_backend_name(), available=True 32 ) 33 34 35 def execute_capability( 36 capability_id: String, input: Value, context: RequestContext 37 ) raises -> CapabilityResult: 38 var selection = resolve_backend(context) 39 if not selection.available: 40 return failed_capability( 41 backend_unavailable_error(selection.backend_name) 42 ) 43 44 if selection.backend_name == heuristic_backend_name(): 45 return execute_heuristic_capability(capability_id, input, context) 46 47 return failed_capability(backend_unavailable_error(selection.backend_name))