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 — After an agent session finishes (success or failure)
:pr_merged — After a GitHub PR is merged
:pr_opened — After a GitHub PR is opened
:pr_reviewed — After a PR review is submitted
:build_brain_context — When building brain context (plugins add source-specific queries)
:pre_dispatch — Before dispatching an agent (plugins can inject config)
:post_comment — After an agent posts a comment/response
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 =
Returns the current gem version.
"0.0.12"
Class Attribute Summary collapse
-
.channel_pre_post_checks ⇒ Hash<Symbol, String>
readonly
Get registered pre-post checks (used by render_prompt).
-
.channel_prompts ⇒ Hash<Symbol, String>
readonly
Get registered channel prompts (used by render_prompt).
Class Method Summary collapse
-
.emit(event, **context) ⇒ Array
Emit an event, calling all registered handlers.
-
.on(event, &block) ⇒ Object
Register a hook for an event.
-
.register_channel_prompt(channel, prompt, pre_post_check: nil) ⇒ Object
Register a channel prompt template for use in render_prompt.
-
.reset_hooks! ⇒ Object
Clear all hooks (useful for testing).
Class Attribute Details
.channel_pre_post_checks ⇒ Hash<Symbol, String> (readonly)
Get registered pre-post checks (used by render_prompt).
77 78 79 |
# File 'lib/brainiac/hooks.rb', line 77 def channel_pre_post_checks @channel_pre_post_checks end |
.channel_prompts ⇒ Hash<Symbol, String> (readonly)
Get registered channel prompts (used by render_prompt).
72 73 74 |
# File 'lib/brainiac/hooks.rb', line 72 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).
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/brainiac/hooks.rb', line 47 def emit(event, **context) results = @hooks[event].map do |handler| handler.call(context) rescue StandardError => e LOG.error "[Hooks] Error in #{event} handler: #{e.}" 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.
37 38 39 |
# File 'lib/brainiac/hooks.rb', line 37 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.
64 65 66 67 |
# File 'lib/brainiac/hooks.rb', line 64 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).
80 81 82 83 84 |
# File 'lib/brainiac/hooks.rb', line 80 def reset_hooks! @hooks = Hash.new { |h, k| h[k] = [] } @channel_prompts = {} @channel_pre_post_checks = {} end |