Module: Protege::HookMixin

Included in:
Hook
Defined in:
lib/protege/extensions/hook_mixin.rb

Overview

Contract mixin for hooks — the surface host apps use to declare reactions to engine lifecycle events. A concrete hook subclasses Protege::Hook (which includes this module) and declares handlers with the class-level on DSL, naming the event classes it cares about; each handler block receives an instance of that Protege::Event subclass.

This module is declaration only. on merely records the block in the class's subscriptions data, so a Zeitwerk reload just rebuilds that data. Activation — subscribing to the event bus and running handlers — is a separate concern owned by Protege::Subscribers::HookDispatcher, which reads this contract (+subscriptions+, stateful?) off each hook class when an event fires.

Handler blocks run (by the dispatcher) via instance_exec on a hook instance, so instance helper methods — and, for stateful hooks, instance variables across a run — are available inside them.

Examples:

class EventLoggerHook < Protege::Hook
  on Protege::InferenceStartedEvent do |event|
    log "START #{event.persona&.name}"
  end

  on Protege::ToolCallStartedEvent, Protege::ToolCallCompletedEvent do |event|
    log "TOOL #{event.tool_call&.name}"
  end

  private

  def log(line) = Protege.configuration.logger.info(line)
end

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.included(base) ⇒ void

This method returns an undefined value.

Extend the includer with ClassMethods so the +on+/+stateful+ DSL becomes available.

Parameters:

  • base (Class)

    the class including Hook



36
37
38
# File 'lib/protege/extensions/hook_mixin.rb', line 36

def self.included(base)
  base.extend(ClassMethods)
end