Class: SwarmSDK::Hooks::Adapter
- Inherits:
-
Object
- Object
- SwarmSDK::Hooks::Adapter
- Defined in:
- lib/swarm_sdk/hooks/adapter.rb
Overview
Translates YAML hooks configuration to Ruby hooks
Adapter bridges the gap between declarative YAML hooks (shell commands) and SwarmSDK's internal hook system. It creates hooks that execute shell commands and translate exit codes to Result objects.
YAML Hooks are YAML-Only
Hooks are a YAML-only feature designed for users who want Claude Code-style shell command hooks. Users of the Ruby API should use hooks directly.
Swarm-Level vs Agent-Level
- Swarm-level: Only
swarm_startandswarm_stop(lifecycle hooks) - Agent-level: All other events (per-agent or all_agents)
- all_agents: Hooks applied as swarm defaults to all agents
Event Naming
Uses snake_case to match internal hook events directly (no translation):
pre_tool_use→ :pre_tool_useswarm_start→ :swarm_start- etc.
Constant Summary collapse
- SWARM_LEVEL_EVENTS =
Swarm-level events (only these allowed at swarm.hooks level)
[:swarm_start, :swarm_stop].freeze
- AGENT_LEVEL_EVENTS =
Agent-level events (allowed in all_agents.hooks and agent.hooks)
[ :pre_tool_use, :post_tool_use, :user_prompt, :agent_step, :agent_stop, :first_message, :pre_delegation, :post_delegation, :context_warning, ].freeze
Class Method Summary collapse
-
.apply_agent_hooks(agent, agent_name, hooks_config, swarm_name) ⇒ void
Apply agent-specific hooks to an already-initialized agent.
-
.apply_hooks(swarm, config) ⇒ void
Apply hooks from YAML configuration to swarm.
Class Method Details
.apply_agent_hooks(agent, agent_name, hooks_config, swarm_name) ⇒ void
This method returns an undefined value.
Apply agent-specific hooks to an already-initialized agent
This is called during agent initialization for each agent that has hooks configured.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/swarm_sdk/hooks/adapter.rb', line 99 def apply_agent_hooks(agent, agent_name, hooks_config, swarm_name) return unless hooks_config&.any? hooks_config.each do |event_name, hook_defs| event_symbol = event_name.to_sym validate_agent_event!(event_symbol) # Each hook def can have optional matcher Array(hook_defs).each do |hook_def| matcher = hook_def[:matcher] || hook_def["matcher"] hook = create_hook_callback(hook_def, event_symbol, agent_name, swarm_name) agent.add_hook(event_symbol, matcher: matcher, &hook) end end end |
.apply_hooks(swarm, config) ⇒ void
This method returns an undefined value.
Apply hooks from YAML configuration to swarm
This is called automatically by Swarm.load after creating the swarm instance. It translates YAML hooks into hooks that execute shell commands.
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/swarm_sdk/hooks/adapter.rb', line 79 def apply_hooks(swarm, config) # 1. Apply swarm-level hooks (from swarm.hooks) apply_swarm_hooks(swarm, config.swarm_hooks) if config.swarm_hooks&.any? # 2. Apply all_agents hooks (as swarm defaults) apply_all_agents_hooks(swarm, config.all_agents_hooks) if config.all_agents_hooks&.any? # 3. Store agent hooks for later application (after agents are initialized) store_agent_hooks(config) end |