Module: DeepAgentsRb::SubAgentSystem

Defined in:
lib/deepagents/deepagentsrb/sub_agent.rb

Constant Summary collapse

TASK_DESCRIPTION_PREFIX =

Task description templates

"Use this tool to delegate a task to a sub-agent. Available sub-agents:\n- general-purpose: A general purpose agent with the same capabilities as the main agent\n{other_agents}"
TASK_DESCRIPTION_SUFFIX =
"\n\nThe sub-agent will have access to the same tools as you do, and will be able to use them to complete the task."

Class Method Summary collapse

Class Method Details

.create_task_tool(tools, instructions, subagents, model, state_class) ⇒ Object

Create a task tool for delegating to sub-agents



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/deepagents/deepagentsrb/sub_agent.rb', line 39

def self.create_task_tool(tools, instructions, subagents, model, state_class)
  # Create agents hash with general-purpose agent
  agents = {
    "general-purpose" => ReactAgent.new(model, instructions, tools, state_class)
  }
  
  # Create tools by name hash
  tools_by_name = {}
  tools.each do |tool|
    tools_by_name[tool.name] = tool
  end
  
  # Create sub-agents
  subagents.each do |agent|
    if agent.tools
      agent_tools = agent.tools.map { |t| tools_by_name[t] }
    else
      agent_tools = tools
    end
    
    agents[agent.name] = ReactAgent.new(model, agent.prompt, agent_tools, state_class)
  end
  
  # Create other agents string for task description
  other_agents_string = subagents.map do |agent|
    "- #{agent.name}: #{agent.description}"
  end
  
  # Create task tool
  Tools::Tool.new(
    "task",
    TASK_DESCRIPTION_PREFIX.gsub("{other_agents}", other_agents_string.join("\n")) + TASK_DESCRIPTION_SUFFIX
  ) do |description, subagent_type, state, tool_call_id:|
    if !agents.key?(subagent_type)
      return "Error: invoked agent of type #{subagent_type}, the only allowed types are #{agents.keys.map { |k| "`#{k}`" }}"
    end
    
    sub_agent = agents[subagent_type]
    sub_state = state.dup
    sub_state.messages = [{ role: "user", content: description }]
    
    result = sub_agent.invoke(sub_state)
    
    Tools::Command.new(
      update: {
        files: result.get("files", {}),
        messages: [
          Tools::ToolMessage.new(
            result.messages.last[:content],
            tool_call_id: tool_call_id
          )
        ]
      }
    )
  end
end