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.

Returns:

  • (String)

    the current gem version

"0.0.13"

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).

Returns:

  • (Hash<Symbol, String>)


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

def channel_pre_post_checks
  @channel_pre_post_checks
end

.channel_promptsHash<Symbol, String> (readonly)

Get registered channel prompts (used by render_prompt).

Returns:

  • (Hash<Symbol, String>)


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).

Parameters:

  • event (Symbol)

    Event name

  • context (Hash)

    Context passed to each handler

Returns:

  • (Array)

    Results from handlers



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.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.

Parameters:

  • event (Symbol)

    Event name

  • block (Proc)

    Handler block, receives a context hash



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.

Parameters:

  • channel (Symbol)

    Channel name (e.g., :fizzy, :discord)

  • prompt (String)

    Channel prompt text

  • pre_post_check (String, nil) (defaults to: nil)

    Optional pre-post comment check instructions



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