Class: Yorishiro::Hooks

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

Overview

Registry for lifecycle hooks declared in .yorishirorc via the on DSL. before_tool_use / user_prompt_submit blocks can veto the action by returning :deny or a Denial (built with the deny("reason") helper). Any other return value (including nil) lets the action proceed, so logging-only hooks are safe by default.

Defined Under Namespace

Classes: Denial

Constant Summary collapse

EVENTS =
%i[before_tool_use after_tool_use user_prompt_submit].freeze

Instance Method Summary collapse

Constructor Details

#initializeHooks

Returns a new instance of Hooks.



14
15
16
# File 'lib/yorishiro/hooks.rb', line 14

def initialize
  @blocks = Hash.new { |hash, key| hash[key] = [] }
end

Instance Method Details

#any?(event) ⇒ Boolean

Returns:

  • (Boolean)


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

def any?(event)
  @blocks[event].any?
end

#on(event, &block) ⇒ Object

Raises:



18
19
20
21
22
# File 'lib/yorishiro/hooks.rb', line 18

def on(event, &block)
  raise ConfigurationError, "Unknown hook event: #{event}. Available events: #{EVENTS.join(", ")}" unless EVENTS.include?(event)

  @blocks[event] << block
end

#run_after_tool_use(tool_name, arguments, result) ⇒ Object

Exceptions propagate to the caller: after hooks are observational, so the CLI just warns and keeps the tool result.



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

def run_after_tool_use(tool_name, arguments, result)
  @blocks[:after_tool_use].each { |block| block.call(tool_name, arguments, result) }
  nil
end

#run_before_tool_use(tool_name, arguments) ⇒ Object

Returns nil to proceed, or a Denial. A hook that raises denies the call (fail closed) so a broken guard cannot silently let tools run.



30
31
32
# File 'lib/yorishiro/hooks.rb', line 30

def run_before_tool_use(tool_name, arguments)
  first_denial(:before_tool_use, tool_name, arguments)
end

#run_user_prompt_submit(input) ⇒ Object



34
35
36
# File 'lib/yorishiro/hooks.rb', line 34

def run_user_prompt_submit(input)
  first_denial(:user_prompt_submit, input)
end