Class: SwarmSDK::Hooks::Context
- Inherits:
-
Object
- Object
- SwarmSDK::Hooks::Context
- Defined in:
- lib/swarm_sdk/hooks/context.rb
Overview
Rich context object passed to hook callbacks
Provides hooks with comprehensive access to:
- Agent information (name, definition)
- Tool call details (for tool-related events)
- Delegation details (for delegation events)
- Swarm context (access to other agents, configuration)
- Metadata (arbitrary additional data)
The context is read-write, allowing hooks to modify data (e.g., add metadata, modify tool parameters).
Instance Attribute Summary collapse
-
#agent_definition ⇒ Object
readonly
Returns the value of attribute agent_definition.
-
#agent_name ⇒ Object
readonly
Returns the value of attribute agent_name.
-
#delegation_result ⇒ Object
Returns the value of attribute delegation_result.
-
#delegation_target ⇒ Object
Returns the value of attribute delegation_target.
-
#event ⇒ Object
readonly
Returns the value of attribute event.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#swarm ⇒ Object
readonly
Returns the value of attribute swarm.
-
#tool_call ⇒ Object
Returns the value of attribute tool_call.
-
#tool_result ⇒ Object
Returns the value of attribute tool_result.
Instance Method Summary collapse
-
#breakpoint ⇒ void
Enter an interactive debugging breakpoint.
-
#delegation_event? ⇒ Boolean
Check if this is a delegation-related event.
-
#finish_agent(message) ⇒ Result
Finish the current agent's execution with a final message.
-
#finish_swarm(message) ⇒ Result
Finish the entire swarm execution with a final message.
-
#halt(message) ⇒ Result
Halt the current operation and return a message.
-
#initialize(event:, agent_name:, agent_definition: nil, swarm: nil, tool_call: nil, tool_result: nil, delegation_target: nil, delegation_result: nil, metadata: {}) ⇒ Context
constructor
A new instance of Context.
-
#replace(value) ⇒ Result
Replace the current result with a custom value.
-
#reprompt(prompt) ⇒ Result
Reprompt the agent with a new prompt.
-
#to_h ⇒ Hash
Convert to hash for logging and debugging.
-
#tool_event? ⇒ Boolean
Check if this is a tool-related event.
-
#tool_name ⇒ String?
Get tool name (convenience method).
-
#with(**attributes) ⇒ Context
Create a copy of this context with modified attributes.
Constructor Details
#initialize(event:, agent_name:, agent_definition: nil, swarm: nil, tool_call: nil, tool_result: nil, delegation_target: nil, delegation_result: nil, metadata: {}) ⇒ Context
Returns a new instance of Context.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/swarm_sdk/hooks/context.rb', line 42 def initialize( event:, agent_name:, agent_definition: nil, swarm: nil, tool_call: nil, tool_result: nil, delegation_target: nil, delegation_result: nil, metadata: {} ) @event = event @agent_name = agent_name @agent_definition = agent_definition @swarm = swarm @tool_call = tool_call @tool_result = tool_result @delegation_target = delegation_target @delegation_result = delegation_result = end |
Instance Attribute Details
#agent_definition ⇒ Object (readonly)
Returns the value of attribute agent_definition.
30 31 32 |
# File 'lib/swarm_sdk/hooks/context.rb', line 30 def agent_definition @agent_definition end |
#agent_name ⇒ Object (readonly)
Returns the value of attribute agent_name.
30 31 32 |
# File 'lib/swarm_sdk/hooks/context.rb', line 30 def agent_name @agent_name end |
#delegation_result ⇒ Object
Returns the value of attribute delegation_result.
31 32 33 |
# File 'lib/swarm_sdk/hooks/context.rb', line 31 def delegation_result @delegation_result end |
#delegation_target ⇒ Object
Returns the value of attribute delegation_target.
31 32 33 |
# File 'lib/swarm_sdk/hooks/context.rb', line 31 def delegation_target @delegation_target end |
#event ⇒ Object (readonly)
Returns the value of attribute event.
30 31 32 |
# File 'lib/swarm_sdk/hooks/context.rb', line 30 def event @event end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
30 31 32 |
# File 'lib/swarm_sdk/hooks/context.rb', line 30 def end |
#swarm ⇒ Object (readonly)
Returns the value of attribute swarm.
30 31 32 |
# File 'lib/swarm_sdk/hooks/context.rb', line 30 def swarm @swarm end |
#tool_call ⇒ Object
Returns the value of attribute tool_call.
31 32 33 |
# File 'lib/swarm_sdk/hooks/context.rb', line 31 def tool_call @tool_call end |
#tool_result ⇒ Object
Returns the value of attribute tool_result.
31 32 33 |
# File 'lib/swarm_sdk/hooks/context.rb', line 31 def tool_result @tool_result end |
Instance Method Details
#breakpoint ⇒ void
This method returns an undefined value.
Enter an interactive debugging breakpoint
This method:
- Emits a breakpoint_enter event (formatters can pause spinners)
- Opens binding.irb for interactive debugging
- Emits a breakpoint_exit event (formatters can resume spinners)
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/swarm_sdk/hooks/context.rb', line 175 def breakpoint # Emit breakpoint_enter event LogStream.emit( type: "breakpoint_enter", agent: @agent_name, event: @event, timestamp: Time.now.utc.iso8601, ) # Enter interactive debugging binding.irb # rubocop:disable Lint/Debugger # Emit breakpoint_exit event LogStream.emit( type: "breakpoint_exit", agent: @agent_name, event: @event, timestamp: Time.now.utc.iso8601, ) end |
#delegation_event? ⇒ Boolean
Check if this is a delegation-related event
74 75 76 |
# File 'lib/swarm_sdk/hooks/context.rb', line 74 def delegation_event? [:pre_delegation, :post_delegation].include?(@event) end |
#finish_agent(message) ⇒ Result
Finish the current agent's execution with a final message
150 151 152 |
# File 'lib/swarm_sdk/hooks/context.rb', line 150 def finish_agent() Result.finish_agent() end |
#finish_swarm(message) ⇒ Result
Finish the entire swarm execution with a final message
158 159 160 |
# File 'lib/swarm_sdk/hooks/context.rb', line 158 def finish_swarm() Result.finish_swarm() end |
#halt(message) ⇒ Result
Halt the current operation and return a message
126 127 128 |
# File 'lib/swarm_sdk/hooks/context.rb', line 126 def halt() Result.halt() end |
#replace(value) ⇒ Result
Replace the current result with a custom value
134 135 136 |
# File 'lib/swarm_sdk/hooks/context.rb', line 134 def replace(value) Result.replace(value) end |
#reprompt(prompt) ⇒ Result
Reprompt the agent with a new prompt
142 143 144 |
# File 'lib/swarm_sdk/hooks/context.rb', line 142 def reprompt(prompt) Result.reprompt(prompt) end |
#to_h ⇒ Hash
Convert to hash for logging and debugging
109 110 111 112 113 114 115 116 117 |
# File 'lib/swarm_sdk/hooks/context.rb', line 109 def to_h { event: @event, agent_name: @agent_name, tool_name: tool_name, delegation_target: @delegation_target, metadata: , } end |
#tool_event? ⇒ Boolean
Check if this is a tool-related event
67 68 69 |
# File 'lib/swarm_sdk/hooks/context.rb', line 67 def tool_event? [:pre_tool_use, :post_tool_use].include?(@event) end |
#tool_name ⇒ String?
Get tool name (convenience method)
81 82 83 |
# File 'lib/swarm_sdk/hooks/context.rb', line 81 def tool_name tool_call&.name end |
#with(**attributes) ⇒ Context
Create a copy of this context with modified attributes
Useful for chaining hooks that need to pass modified context to subsequent hooks.
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/swarm_sdk/hooks/context.rb', line 92 def with(**attributes) Context.new( event: attributes[:event] || @event, agent_name: attributes[:agent_name] || @agent_name, agent_definition: attributes[:agent_definition] || @agent_definition, swarm: attributes[:swarm] || @swarm, tool_call: attributes[:tool_call] || @tool_call, tool_result: attributes[:tool_result] || @tool_result, delegation_target: attributes[:delegation_target] || @delegation_target, delegation_result: attributes[:delegation_result] || @delegation_result, metadata: attributes[:metadata] || .dup, ) end |