Class: SwarmSDK::V3::AgentBuilder
- Inherits:
-
Object
- Object
- SwarmSDK::V3::AgentBuilder
- Defined in:
- lib/swarm_sdk/v3/agent_builder.rb
Overview
DSL builder for creating a single V3 Agent
Provides a clean, block-based interface for defining an agent without needing to construct AgentDefinition kwargs directly. Unlike the V2 builder which manages multi-agent swarms, this creates a single agent.
DSL Methods collapse
-
#after_ask {|ctx| ... } ⇒ void
Register an after_ask hook.
-
#after_tool(match: nil) {|ctx| ... } ⇒ void
Register an after_tool hook.
-
#api_version(value) ⇒ void
Set API version for OpenAI provider.
-
#base_url(value) ⇒ void
Set custom API endpoint.
-
#before_ask {|ctx| ... } ⇒ void
Register a before_ask hook.
-
#before_tool(match: nil) {|ctx| ... } ⇒ void
Register a before_tool hook.
-
#description(value) ⇒ void
Set agent description.
-
#directory(value) ⇒ void
Set working directory.
-
#headers(value) ⇒ void
Set raw HTTP headers.
-
#max_concurrent_tools(value) ⇒ void
Set maximum concurrent tool executions.
-
#mcp_server(name, **options) ⇒ void
Add an MCP server connection.
-
#memory { ... } ⇒ void
Configure memory via a sub-block.
-
#model(value) ⇒ void
Set LLM model.
-
#name(value) ⇒ void
Set agent name.
-
#on_stop {|ctx| ... } ⇒ void
Register an on_stop hook.
-
#output_schema(value) ⇒ void
Set output schema for structured output.
-
#parameters(value) ⇒ void
Set raw API body parameters.
-
#provider(value) ⇒ void
Set LLM provider.
-
#skills(*paths) ⇒ void
Add skill directories to the agent.
-
#system_prompt(value) ⇒ void
Set system prompt instructions.
-
#tools(*names) ⇒ void
Add tools to the agent.
Class Method Summary collapse
-
.build { ... } ⇒ Agent
Build an Agent from a DSL block.
Instance Method Summary collapse
-
#initialize ⇒ AgentBuilder
constructor
A new instance of AgentBuilder.
-
#to_agent ⇒ Agent
Build the Agent from collected configuration.
Constructor Details
#initialize ⇒ AgentBuilder
Returns a new instance of AgentBuilder.
55 56 57 58 59 60 61 62 |
# File 'lib/swarm_sdk/v3/agent_builder.rb', line 55 def initialize @config = {} @tools = [] @skills = [] @mcp_servers = [] @hooks = [] @memory_config = nil end |
Class Method Details
.build { ... } ⇒ Agent
Build an Agent from a DSL block
48 49 50 51 52 |
# File 'lib/swarm_sdk/v3/agent_builder.rb', line 48 def build(&block) builder = new builder.instance_eval(&block) builder.to_agent end |
Instance Method Details
#after_ask {|ctx| ... } ⇒ void
This method returns an undefined value.
Register an after_ask hook
Fires after SwarmSDK::V3::Agent#execute_turn completes. Observation only —halt and replace have no effect.
286 287 288 |
# File 'lib/swarm_sdk/v3/agent_builder.rb', line 286 def after_ask(&block) @hooks << { event: :after_ask, block: block } end |
#after_tool(match: nil) {|ctx| ... } ⇒ void
This method returns an undefined value.
Register an after_tool hook
Fires after tool execution. Can replace the tool result. Supports match filtering.
319 320 321 |
# File 'lib/swarm_sdk/v3/agent_builder.rb', line 319 def after_tool(match: nil, &block) @hooks << { event: :after_tool, match: compile_matcher(match), block: block } end |
#api_version(value) ⇒ void
This method returns an undefined value.
Set API version for OpenAI provider
Controls which OpenAI API endpoint to use. Only applicable when provider is “openai”. Defaults to “v1/responses” for OpenAI agents.
240 241 242 |
# File 'lib/swarm_sdk/v3/agent_builder.rb', line 240 def api_version(value) @config[:api_version] = value end |
#base_url(value) ⇒ void
This method returns an undefined value.
Set custom API endpoint
176 177 178 |
# File 'lib/swarm_sdk/v3/agent_builder.rb', line 176 def base_url(value) @config[:base_url] = value end |
#before_ask {|ctx| ... } ⇒ void
This method returns an undefined value.
Register a before_ask hook
Fires before SwarmSDK::V3::Agent#execute_turn. Can halt (returns nil from ask) or replace the prompt.
272 273 274 |
# File 'lib/swarm_sdk/v3/agent_builder.rb', line 272 def before_ask(&block) @hooks << { event: :before_ask, block: block } end |
#before_tool(match: nil) {|ctx| ... } ⇒ void
This method returns an undefined value.
Register a before_tool hook
Fires before tool execution. Can halt (returns error string to LLM without executing the tool). Supports match filtering.
304 305 306 |
# File 'lib/swarm_sdk/v3/agent_builder.rb', line 304 def before_tool(match: nil, &block) @hooks << { event: :before_tool, match: compile_matcher(match), block: block } end |
#description(value) ⇒ void
This method returns an undefined value.
Set agent description
84 85 86 |
# File 'lib/swarm_sdk/v3/agent_builder.rb', line 84 def description(value) @config[:description] = value end |
#directory(value) ⇒ void
This method returns an undefined value.
Set working directory
165 166 167 |
# File 'lib/swarm_sdk/v3/agent_builder.rb', line 165 def directory(value) @config[:directory] = value end |
#headers(value) ⇒ void
This method returns an undefined value.
Set raw HTTP headers
209 210 211 |
# File 'lib/swarm_sdk/v3/agent_builder.rb', line 209 def headers(value) @config[:headers] = value end |
#max_concurrent_tools(value) ⇒ void
This method returns an undefined value.
Set maximum concurrent tool executions
187 188 189 |
# File 'lib/swarm_sdk/v3/agent_builder.rb', line 187 def max_concurrent_tools(value) @config[:max_concurrent_tools] = value end |
#mcp_server(name, **options) ⇒ void
This method returns an undefined value.
Add an MCP server connection
Can be called multiple times; servers accumulate across calls. Each server provides tools that the agent can invoke via MCP protocol.
366 367 368 |
# File 'lib/swarm_sdk/v3/agent_builder.rb', line 366 def mcp_server(name, **) @mcp_servers << .merge(name: name) end |
#memory { ... } ⇒ void
This method returns an undefined value.
Configure memory via a sub-block
255 256 257 |
# File 'lib/swarm_sdk/v3/agent_builder.rb', line 255 def memory(&block) @memory_config = MemoryBuilder.build(&block) end |
#model(value) ⇒ void
This method returns an undefined value.
Set LLM model
95 96 97 |
# File 'lib/swarm_sdk/v3/agent_builder.rb', line 95 def model(value) @config[:model] = value end |
#name(value) ⇒ void
This method returns an undefined value.
Set agent name
73 74 75 |
# File 'lib/swarm_sdk/v3/agent_builder.rb', line 73 def name(value) @config[:name] = value end |
#on_stop {|ctx| ... } ⇒ void
This method returns an undefined value.
Register an on_stop hook
Fires after ask() completes successfully. Observation only —the response has already been produced. Useful for logging, metrics, or cleanup.
334 335 336 |
# File 'lib/swarm_sdk/v3/agent_builder.rb', line 334 def on_stop(&block) @hooks << { event: :on_stop, block: block } end |
#output_schema(value) ⇒ void
This method returns an undefined value.
Set output schema for structured output
The schema is passed through to RubyLLM’s Chat#with_schema. Can be a Hash (raw JSON Schema) or any object responding to #to_json_schema.
223 224 225 |
# File 'lib/swarm_sdk/v3/agent_builder.rb', line 223 def output_schema(value) @config[:output_schema] = value end |
#parameters(value) ⇒ void
This method returns an undefined value.
Set raw API body parameters
198 199 200 |
# File 'lib/swarm_sdk/v3/agent_builder.rb', line 198 def parameters(value) @config[:parameters] = value end |
#provider(value) ⇒ void
This method returns an undefined value.
Set LLM provider
106 107 108 |
# File 'lib/swarm_sdk/v3/agent_builder.rb', line 106 def provider(value) @config[:provider] = value end |
#skills(*paths) ⇒ void
This method returns an undefined value.
Add skill directories to the agent
Can be called multiple times; paths accumulate across calls. Each path should be a directory containing a SKILL.md file, or a parent directory whose children contain SKILL.md files.
154 155 156 |
# File 'lib/swarm_sdk/v3/agent_builder.rb', line 154 def skills(*paths) @skills.concat(paths.flatten) end |
#system_prompt(value) ⇒ void
This method returns an undefined value.
Set system prompt instructions
117 118 119 |
# File 'lib/swarm_sdk/v3/agent_builder.rb', line 117 def system_prompt(value) @config[:system_prompt] = value end |
#to_agent ⇒ Agent
Build the Agent from collected configuration
376 377 378 379 |
# File 'lib/swarm_sdk/v3/agent_builder.rb', line 376 def to_agent definition = AgentDefinition.new(**definition_kwargs) Agent.new(definition) end |
#tools(*names) ⇒ void
This method returns an undefined value.
Add tools to the agent
Can be called multiple times; tools accumulate across calls.
135 136 137 |
# File 'lib/swarm_sdk/v3/agent_builder.rb', line 135 def tools(*names) @tools.concat(names.flatten) end |