Class: DeepAgentsRb::DeepAgent

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

Overview

Deep Agent class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, prompt, tools, state_class) ⇒ DeepAgent

Returns a new instance of DeepAgent.



73
74
75
76
77
78
# File 'lib/deepagents/deepagentsrb/graph.rb', line 73

def initialize(model, prompt, tools, state_class)
  @model = model
  @prompt = prompt
  @tools = tools
  @state_class = state_class
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



71
72
73
# File 'lib/deepagents/deepagentsrb/graph.rb', line 71

def model
  @model
end

#promptObject (readonly)

Returns the value of attribute prompt.



71
72
73
# File 'lib/deepagents/deepagentsrb/graph.rb', line 71

def prompt
  @prompt
end

#state_classObject (readonly)

Returns the value of attribute state_class.



71
72
73
# File 'lib/deepagents/deepagentsrb/graph.rb', line 71

def state_class
  @state_class
end

#toolsObject (readonly)

Returns the value of attribute tools.



71
72
73
# File 'lib/deepagents/deepagentsrb/graph.rb', line 71

def tools
  @tools
end

Instance Method Details

#invoke(input) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/deepagents/deepagentsrb/graph.rb', line 80

def invoke(input)
  # Create initial state
  state = @state_class.new
  
  # Add messages to state if provided
  if input[:messages]
    state.messages = input[:messages]
  end
  
  # Add files to state if provided
  if input[:files]
    state.files = input[:files]
  end
  
  # Start the agent loop
  run_agent_loop(state)
end

#run_agent_loop(state) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
# File 'lib/deepagents/deepagentsrb/graph.rb', line 98

def run_agent_loop(state)
  # Maximum number of iterations to prevent infinite loops
  max_iterations = 10
  iterations = 0
  
  while iterations < max_iterations
    iterations += 1
    
    # Get the current messages
    messages = state.messages
    
    # Generate a response from the model
    response = @model.generate(@prompt, messages)
    
    # Parse the response for tool calls
    tool_calls = parse_tool_calls(response)
    
    if tool_calls.empty?
      # No tool calls, just add the response as an assistant message
      state.update(messages: [{ role: "assistant", content: response }])
      break
    else
      # Add the response with tool calls as an assistant message
      state.update(messages: [{ role: "assistant", content: response }])
      
      # Execute each tool call
      tool_results = []
      tool_calls.each do |tool_call|
        result = execute_tool_call(tool_call, state)
        tool_results << result
        
        # Update state with tool result if it's a Command
        if result.is_a?(Tools::Command)
          state.update(result.update)
        else
          # Add tool result as a tool message
          state.update(messages: [{ 
            role: "tool", 
            content: result.to_s,
            tool_call_id: tool_call[:id]
          }])
        end
      end
      
      # Check if we should continue the loop
      if should_continue?(response, tool_results)
        next
      else
        break
      end
    end
  end
  
  state
end