Class: DeepagentsRails::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/deepagents_rails/service.rb

Overview

Service class for interacting with DeepAgents in a Rails application

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent_name = nil, options = {}) ⇒ Service

Initialize a new DeepAgents service

Parameters:

  • agent_name (Symbol, String) (defaults to: nil)

    The name of the agent to use

  • options (Hash) (defaults to: {})

    Additional options for the agent



11
12
13
14
15
16
17
18
# File 'lib/deepagents_rails/service.rb', line 11

def initialize(agent_name = nil, options = {})
  @config = DeepagentsRails::Engine.config.deepagents
  @agent_name = agent_name || :default
  @options = options
  
  # Load the agent if specified
  load_agent if @agent_name
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



6
7
8
# File 'lib/deepagents_rails/service.rb', line 6

def agent
  @agent
end

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/deepagents_rails/service.rb', line 6

def config
  @config
end

Instance Method Details

#available_agentsArray<String>

Get a list of available agents

Returns:

  • (Array<String>)

    The available agent names



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/deepagents_rails/service.rb', line 58

def available_agents
  agents = []
  
  # Load agents from the Rails app
  Dir[Rails.root.join('app/deepagents/agents/**/*_agent.rb')].each do |file|
    # Extract the agent name from the file path
    agent_name = File.basename(file, '.rb').gsub('_agent', '')
    agents << agent_name
  end
  
  agents
end

#available_toolsArray<DeepAgents::Tool>

Get a list of available tools

Returns:

  • (Array<DeepAgents::Tool>)

    The available tools



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/deepagents_rails/service.rb', line 33

def available_tools
  tools = []
  
  # Load tools from the Rails app
  Dir[Rails.root.join('app/deepagents/tools/**/*_tool.rb')].each do |file|
    require file
    
    # Extract the tool class name from the file path
    tool_name = File.basename(file, '.rb').gsub('_tool', '')
    tool_class_name = tool_name.camelize
    
    # Try to load the tool class
    begin
      tool_class = "Deepagents::Tools::#{tool_class_name}Tool".constantize
      tools << tool_class.build if tool_class.respond_to?(:build)
    rescue NameError => e
      Rails.logger.warn("Could not load tool class for #{tool_name}: #{e.message}")
    end
  end
  
  tools
end

#run(input, context = {}) ⇒ Hash

Run the agent with the given input

Parameters:

  • input (String)

    The input to send to the agent

  • context (Hash) (defaults to: {})

    Additional context for the agent

Returns:

  • (Hash)

    The agent’s response



24
25
26
27
28
29
# File 'lib/deepagents_rails/service.rb', line 24

def run(input, context = {})
  ensure_agent_loaded
  
  # Run the agent
  @agent.run(input, context)
end