Class: SwarmSDK::Hooks::Definition

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

Overview

Represents a single hook configuration

A hook definition includes:

  • Event type (when to trigger)
  • Optional matcher (regex for tool names)
  • Priority (execution order)
  • Proc to execute

Examples:

Create a hook definition

definition = SwarmSDK::Hooks::Definition.new(
  event: :pre_tool_use,
  matcher: "Write|Edit",
  priority: 10,
  proc: ->(ctx) { validate_code(ctx.tool_call) }
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event:, matcher: nil, priority: 0, proc:) ⇒ Definition

Returns a new instance of Definition.

Parameters:

  • Event type (e.g., :pre_tool_use)

  • (defaults to: nil)

    Optional regex pattern for tool names

  • (defaults to: 0)

    Execution priority (higher = earlier)

  • Hook proc or named hook symbol



27
28
29
30
31
32
# File 'lib/swarm_sdk/hooks/definition.rb', line 27

def initialize(event:, matcher: nil, priority: 0, proc:)
  @event = event
  @matcher = compile_matcher(matcher)
  @priority = priority
  @proc = proc
end

Instance Attribute Details

#eventObject (readonly)

Returns the value of attribute event.



21
22
23
# File 'lib/swarm_sdk/hooks/definition.rb', line 21

def event
  @event
end

#matcherObject (readonly)

Returns the value of attribute matcher.



21
22
23
# File 'lib/swarm_sdk/hooks/definition.rb', line 21

def matcher
  @matcher
end

#priorityObject (readonly)

Returns the value of attribute priority.



21
22
23
# File 'lib/swarm_sdk/hooks/definition.rb', line 21

def priority
  @priority
end

#procObject (readonly)

Returns the value of attribute proc.



21
22
23
# File 'lib/swarm_sdk/hooks/definition.rb', line 21

def proc
  @proc
end

Instance Method Details

#matches?(tool_name) ⇒ Boolean

Check if this hook should execute for a given tool name

Parameters:

  • Name of the tool being called

Returns:

  • true if hook should execute



38
39
40
41
42
# File 'lib/swarm_sdk/hooks/definition.rb', line 38

def matches?(tool_name)
  return true if @matcher.nil? # No matcher = matches everything

  @matcher.match?(tool_name)
end

#named_hook?Boolean

Check if this hook uses a named reference

Returns:

  • true if proc is a symbol (named hook)



47
48
49
# File 'lib/swarm_sdk/hooks/definition.rb', line 47

def named_hook?
  @proc.is_a?(Symbol)
end

#resolve_proc(registry) ⇒ Proc

Resolve the actual proc, looking up named hooks if needed

Parameters:

  • Registry to lookup named hooks

Returns:

  • The actual proc to execute

Raises:

  • if named hook not found



56
57
58
59
60
61
62
63
# File 'lib/swarm_sdk/hooks/definition.rb', line 56

def resolve_proc(registry)
  return @proc unless named_hook?

  resolved = registry.get(@proc)
  raise ArgumentError, "Named hook :#{@proc} not found in registry" unless resolved

  resolved
end