Class: Antigravity::Hooks

Inherits:
Object
  • Object
show all
Defined in:
lib/antigravity/hooks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHooks

Returns a new instance of Hooks.



7
8
9
10
11
12
13
# File 'lib/antigravity/hooks.rb', line 7

def initialize
  @pre_prompt_hooks = []
  @post_response_hooks = []
  @pre_tool_hooks = []
  @post_tool_hooks = []
  @listeners = Hash.new { |h, k| h[k] = [] }
end

Instance Attribute Details

#post_response_hooksObject (readonly)

Returns the value of attribute post_response_hooks.



5
6
7
# File 'lib/antigravity/hooks.rb', line 5

def post_response_hooks
  @post_response_hooks
end

#post_tool_hooksObject (readonly)

Returns the value of attribute post_tool_hooks.



5
6
7
# File 'lib/antigravity/hooks.rb', line 5

def post_tool_hooks
  @post_tool_hooks
end

#pre_prompt_hooksObject (readonly)

Returns the value of attribute pre_prompt_hooks.



5
6
7
# File 'lib/antigravity/hooks.rb', line 5

def pre_prompt_hooks
  @pre_prompt_hooks
end

#pre_tool_hooksObject (readonly)

Returns the value of attribute pre_tool_hooks.



5
6
7
# File 'lib/antigravity/hooks.rb', line 5

def pre_tool_hooks
  @pre_tool_hooks
end

Instance Method Details

#after_response(&block) ⇒ Object



19
20
21
# File 'lib/antigravity/hooks.rb', line 19

def after_response(&block)
  @post_response_hooks << block if block_given?
end

#after_tool_call(&block) ⇒ Object

Filter / sanitize result after a tool runs before model receives it. Block can return modified result.



29
30
31
# File 'lib/antigravity/hooks.rb', line 29

def after_tool_call(&block)
  @post_tool_hooks << block if block_given?
end

#before_prompt(&block) ⇒ Object



15
16
17
# File 'lib/antigravity/hooks.rb', line 15

def before_prompt(&block)
  @pre_prompt_hooks << block if block_given?
end

#before_tool_call(&block) ⇒ Object

Guard execution before a tool runs. Block can return :allow, :deny, or a hash { status: :deny, reason: "..." }



24
25
26
# File 'lib/antigravity/hooks.rb', line 24

def before_tool_call(&block)
  @pre_tool_hooks << block if block_given?
end

#emit(event, *args) ⇒ Object

Emit a named event to all subscribers.



40
41
42
# File 'lib/antigravity/hooks.rb', line 40

def emit(event, *args)
  @listeners[event.to_sym].each { |cb| cb.call(*args) }
end

#on(event, &block) ⇒ Object

Generic event system — subscribe to any named event. Usage: hooks.on(:ws_message) { |msg| puts msg.keys }



35
36
37
# File 'lib/antigravity/hooks.rb', line 35

def on(event, &block)
  @listeners[event.to_sym] << block if block_given?
end

#run_post_response(response) ⇒ Object



48
49
50
# File 'lib/antigravity/hooks.rb', line 48

def run_post_response(response)
  @post_response_hooks.each { |hook| hook.call(response) }
end

#run_post_tool(tool_name, params, result) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/antigravity/hooks.rb', line 63

def run_post_tool(tool_name, params, result)
  current_result = result
  @post_tool_hooks.each do |hook|
    modified = hook.call(tool_name, params, current_result)
    current_result = modified unless modified.nil?
  end
  current_result
end

#run_pre_prompt(prompt_text) ⇒ Object



44
45
46
# File 'lib/antigravity/hooks.rb', line 44

def run_pre_prompt(prompt_text)
  @pre_prompt_hooks.each { |hook| hook.call(prompt_text) }
end

#run_pre_tool(tool_name, params) ⇒ Object



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

def run_pre_tool(tool_name, params)
  @pre_tool_hooks.each do |hook|
    result = hook.call(tool_name, params)
    if result == :deny || (result.is_a?(Hash) && result[:status] == :deny)
      reason = result.is_a?(Hash) ? result[:reason] : "Blocked by safety pre-hook policy"
      return { allowed: false, reason: reason }
    end
  end
  { allowed: true }
end