Module: DeepAgents::Graph

Defined in:
lib/deepagents/graph.rb

Overview

Graph module for creating deep agents

Defined Under Namespace

Classes: ReactAgentWrapper

Class Method Summary collapse

Class Method Details

.create_deep_agent(tools, instructions, model: nil, subagents: nil, state_schema: nil) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/deepagents/graph.rb', line 123

def self.create_deep_agent(tools, instructions, model: nil, subagents: nil, state_schema: nil)
  # Create a default model if none is provided
  if model.nil?
    begin
      model = Models::Claude.new
    rescue => e
      begin
        model = Models::OpenAI.new
      rescue => e2
        # Fall back to mock model if no API keys are available
        model = Models::MockModel.new
      end
    end
  end
  
  # Add standard tools
  file_system = FileSystem.new
  state = state_schema ? state_schema.new : DeepAgentState.new
  
  # Create standard tools
  standard_tools = StandardTools.file_system_tools(file_system) + 
                  StandardTools.todo_tools(state) + [StandardTools.planning_tool]
  
  # Combine with user-provided tools
  all_tools = tools + standard_tools
  
  # Create LangGraph tools
  langgraph_tools = all_tools.map do |tool|
    LangGraphRb::Tool.new(
      name: tool.name,
      description: tool.description,
      parameters: tool.parameters,
      function: ->(args) { tool.call(**args) }
    )
  end
  
  # Create sub-agent tools if provided
  if subagents && !subagents.empty?
    subagent_registry = SubAgentRegistry.new
    
    subagents.each do |subagent|
      subagent_registry.register(subagent)
      
      # Create a tool for each subagent
      subagent_tool = LangGraphRb::Tool.new(
        name: "task_#{subagent.name}",
        description: "Use the #{subagent.name} subagent for tasks related to: #{subagent.description}",
        parameters: { task: "The task to perform" },
        function: ->(args) { subagent.invoke(args[:task]) }
      )
      
      langgraph_tools << subagent_tool
    end
  end
  
  # Create the LangGraph agent
  agent = LangGraphRb::Agent.new(
    model: model.to_langchain_model,
    tools: langgraph_tools,
    system_prompt: instructions,
    state: state
  )
  
  # Create a wrapper that maintains compatibility with our ReactAgent interface
  ReactAgentWrapper.new(agent, all_tools, instructions, model, state)
end