Class: DeepAgents::SubAgent

Inherits:
Object
  • Object
show all
Defined in:
lib/deepagents/sub_agent.rb

Overview

SubAgent class for creating and managing sub-agents

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, instructions, tools = [], model = nil) ⇒ SubAgent

Returns a new instance of SubAgent.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
# File 'lib/deepagents/sub_agent.rb', line 9

def initialize(name, instructions, tools = [], model = nil)
  raise ArgumentError, "Sub-agent name cannot be nil or empty" if name.nil? || name.empty?
  raise ArgumentError, "Sub-agent instructions cannot be nil or empty" if instructions.nil? || instructions.empty?
  
  @name = name
  @instructions = instructions
  @tools = tools
  @model = model
  @state = DeepAgentState.new
end

Instance Attribute Details

#instructionsObject (readonly)

Returns the value of attribute instructions.



7
8
9
# File 'lib/deepagents/sub_agent.rb', line 7

def instructions
  @instructions
end

#modelObject (readonly)

Returns the value of attribute model.



7
8
9
# File 'lib/deepagents/sub_agent.rb', line 7

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/deepagents/sub_agent.rb', line 7

def name
  @name
end

#stateObject (readonly)

Returns the value of attribute state.



7
8
9
# File 'lib/deepagents/sub_agent.rb', line 7

def state
  @state
end

#toolsObject (readonly)

Returns the value of attribute tools.



7
8
9
# File 'lib/deepagents/sub_agent.rb', line 7

def tools
  @tools
end

Instance Method Details

#run(input, max_iterations = 10) ⇒ Object

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/deepagents/sub_agent.rb', line 20

def run(input, max_iterations = 10)
  raise ArgumentError, "Model must be provided to run the sub-agent" unless @model
  
  prompt = build_prompt(input)
  iterations = 0
  messages = []
  
  while iterations < max_iterations
    iterations += 1
    
    response = @model.generate(prompt, @tools)
    
    if response[:tool_calls] && !response[:tool_calls].empty?
      tool_call = response[:tool_calls].first
      tool_name = tool_call[:name]
      tool_args = tool_call[:arguments]
      
      tool = find_tool(tool_name)
      result = execute_tool(tool, tool_args)
      
      messages << { role: "assistant", content: "I'll use the #{tool_name} tool." }
      messages << { role: "system", content: "Tool result: #{result}" }
      
      prompt = build_prompt(input, messages)
    else
      messages << { role: "assistant", content: response[:content] }
      break
    end
  end
  
  if iterations >= max_iterations
    raise MaxIterationsError.new(max_iterations)
  end
  
  # Return the final response
  messages.last[:content]
end