Module: RobotLab::Hooks
- Defined in:
- lib/robot_lab/hooks.rb
Class Method Summary collapse
- .around(hook_name, context, registries:, per_run_hooks: nil) ⇒ Object
- .call(hook_name, context, registries:, per_run_hooks: nil) ⇒ Object
- .call_all(registrations, hook_name, context) ⇒ Object
- .call_around(registrations, hook_name, context, &block) ⇒ Object
- .call_registration(registration, hook_name, context) ⇒ Object
- .error_registrations(family, registries, per_run_hooks) ⇒ Object
-
.per_run_entries(hook_name, hooks) ⇒ Object
per_run_hooks is now a single handler class or an array of handler classes.
- .registrations(hook_name, registries, per_run_hooks) ⇒ Object
- .run(family, context, registries:, per_run_hooks: nil) ⇒ Object
- .set_result(context, family, result) ⇒ Object
Class Method Details
.around(hook_name, context, registries:, per_run_hooks: nil) ⇒ Object
28 29 30 |
# File 'lib/robot_lab/hooks.rb', line 28 def around(hook_name, context, registries:, per_run_hooks: nil, &) call_around(registrations(hook_name, registries, per_run_hooks), hook_name, context, &) end |
.call(hook_name, context, registries:, per_run_hooks: nil) ⇒ Object
24 25 26 |
# File 'lib/robot_lab/hooks.rb', line 24 def call(hook_name, context, registries:, per_run_hooks: nil) call_all(registrations(hook_name, registries, per_run_hooks), hook_name, context) end |
.call_all(registrations, hook_name, context) ⇒ Object
43 44 45 |
# File 'lib/robot_lab/hooks.rb', line 43 def call_all(registrations, hook_name, context) registrations.each { |registration| call_registration(registration, hook_name, context) } end |
.call_around(registrations, hook_name, context, &block) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/robot_lab/hooks.rb', line 47 def call_around(registrations, hook_name, context, &block) chain = registrations.reverse.inject(block) do |next_link, registration| proc { call_registration(registration, hook_name, context, &next_link) } end chain.call end |
.call_registration(registration, hook_name, context) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/robot_lab/hooks.rb', line 55 def call_registration(registration, hook_name, context, &) context.with_namespace(registration.namespace) do if registration.context && registration.namespace context.ext(registration.namespace).merge_defaults(registration.context) end registration.handler_class.call(hook_name, context, &) end end |
.error_registrations(family, registries, per_run_hooks) ⇒ Object
37 38 39 40 41 |
# File 'lib/robot_lab/hooks.rb', line 37 def error_registrations(family, registries, per_run_hooks) return [] unless %i[run network_run task].include?(family.to_sym) registrations(:on_error, registries, per_run_hooks) end |
.per_run_entries(hook_name, hooks) ⇒ Object
per_run_hooks is now a single handler class or an array of handler classes.
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/robot_lab/hooks.rb', line 65 def per_run_entries(hook_name, hooks) return [] unless hooks hook_sym = hook_name.to_sym Array(hooks).filter_map do |handler_class| next unless handler_class.singleton_class.public_method_defined?(hook_sym) HookRegistry::Registration.new(handler_class: handler_class) end end |
.registrations(hook_name, registries, per_run_hooks) ⇒ Object
32 33 34 35 |
# File 'lib/robot_lab/hooks.rb', line 32 def registrations(hook_name, registries, per_run_hooks) registry_entries = registries.compact.flat_map { |registry| registry.registrations_for(hook_name) } registry_entries + per_run_entries(hook_name, per_run_hooks) end |
.run(family, context, registries:, per_run_hooks: nil) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/robot_lab/hooks.rb', line 7 def run(family, context, registries:, per_run_hooks: nil, &) before = registrations(:"before_#{family}", registries, per_run_hooks) around = registrations(:"around_#{family}", registries, per_run_hooks) after = registrations(:"after_#{family}", registries, per_run_hooks) errors = error_registrations(family, registries, per_run_hooks) call_all(before, :"before_#{family}", context) result = call_around(around, :"around_#{family}", context, &) set_result(context, family, result) call_all(after, :"after_#{family}", context) result rescue Exception => e # rubocop:disable Lint/RescueException context.error = e if context.respond_to?(:error=) call_all(errors, :on_error, context) raise end |
.set_result(context, family, result) ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/robot_lab/hooks.rb', line 76 def set_result(context, family, result) case family.to_sym when :run context.response = result if context.respond_to?(:response=) when :network_run, :task context.result = result if context.respond_to?(:result=) when :llm_generation context.generation_response = result if context.respond_to?(:generation_response=) end end |