Class: Ask::Agent::Hooks

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

Instance Method Summary collapse

Constructor Details

#initialize(hooks = {}) ⇒ Hooks

Returns a new instance of Hooks.



6
7
8
9
# File 'lib/ask/agent/hooks.rb', line 6

def initialize(hooks = {})
  @before_tool = Array(hooks[:before_tool])
  @after_tool = Array(hooks[:after_tool])
end

Instance Method Details

#run_after_tool(tool_call, result, context) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/ask/agent/hooks.rb', line 20

def run_after_tool(tool_call, result, context)
  final = nil
  @after_tool.each do |hook|
    final = hook.call(tool_call, result, context)
    break if final.is_a?(Hash) && final[:action] == :block
  end
  final
end

#run_before_tool(tool_call, context) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/ask/agent/hooks.rb', line 11

def run_before_tool(tool_call, context)
  result = nil
  @before_tool.each do |hook|
    result = hook.call(tool_call, context)
    break if result.is_a?(Hash) && result[:action] != :proceed
  end
  result
end