Module: Brainiac

Defined in:
lib/brainiac.rb,
lib/brainiac/hooks.rb,
lib/brainiac/version.rb

Overview

Brainiac hook system.

Provides a lightweight pub/sub mechanism for plugins to extend core behavior without core knowing about specific plugins.

Core emits events at lifecycle points. Plugins register handlers via Brainiac.on.

Events:

:agent_completed    

Usage (in plugin .register):

Brainiac.on(:agent_completed) do |ctx|
move_card_to_column(ctx[:card_number], "needs_review", ...)
end

Usage (in core):

Brainiac.emit(:agent_completed, card_number: 42, agent_name: "Sherlock", ...)

Defined Under Namespace

Modules: Plugins

Constant Summary collapse

VERSION =
"0.0.29"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.channel_pre_post_checksHash<Symbol, String> (readonly)

Get registered pre-post checks (used by render_prompt).



81
82
83
# File 'lib/brainiac/hooks.rb', line 81

def channel_pre_post_checks
  @channel_pre_post_checks
end

.channel_promptsHash<Symbol, String> (readonly)

Get registered channel prompts (used by render_prompt).



76
77
78
# File 'lib/brainiac/hooks.rb', line 76

def channel_prompts
  @channel_prompts
end

Class Method Details

.emit(event, **context) ⇒ Array

Emit an event, calling all registered handlers. Returns an array of results from each handler (nil results filtered out).



51
52
53
54
55
56
57
58
59
60
# File 'lib/brainiac/hooks.rb', line 51

def emit(event, **context)
  results = @hooks[event].map do |handler|
    handler.call(context)
  rescue StandardError => e
    LOG.error "[Hooks] Error in #{event} handler: #{e.message}" if defined?(LOG)
    LOG.error "[Hooks]   #{e.backtrace.first(3).join("\n  ")}" if defined?(LOG)
    nil
  end
  results.compact
end

.on(event, &block) ⇒ Object

Register a hook for an event.



41
42
43
# File 'lib/brainiac/hooks.rb', line 41

def on(event, &block)
  @hooks[event] << block
end

.register_channel_prompt(channel, prompt, pre_post_check: nil) ⇒ Object

Register a channel prompt template for use in render_prompt. Plugins call this to add their channel-specific prompt block.



68
69
70
71
# File 'lib/brainiac/hooks.rb', line 68

def register_channel_prompt(channel, prompt, pre_post_check: nil)
  @channel_prompts[channel] = prompt
  @channel_pre_post_checks[channel] = pre_post_check if pre_post_check
end

.reset_hooks!Object

Clear all hooks (useful for testing).



84
85
86
87
88
# File 'lib/brainiac/hooks.rb', line 84

def reset_hooks!
  @hooks = Hash.new { |h, k| h[k] = [] }
  @channel_prompts = {}
  @channel_pre_post_checks = {}
end