Class: RcrewAI::Rails::Agent

Inherits:
ApplicationRecord show all
Defined in:
app/models/rcrewai/rails/agent.rb

Instance Method Summary collapse

Instance Method Details

#add_tool(tool_class, params = {}) ⇒ Object



76
77
78
79
80
81
82
83
# File 'app/models/rcrewai/rails/agent.rb', line 76

def add_tool(tool_class, params = {})
  self.tools ||= []
  self.tools << {
    "class" => tool_class.to_s,
    "params" => params
  }
  save
end

#agent_optionsObject

rcrewai 0.5.0 agent options. Only emit a key when it is meaningfully set, so an all-default record constructs exactly as it did pre-0.5.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/rcrewai/rails/agent.rb', line 35

def agent_options
  opts = {}
  opts[:max_rpm] = max_rpm if max_rpm.present? && max_rpm.positive?
  opts[:reasoning] = reasoning if reasoning
  opts[:max_reasoning_attempts] = max_reasoning_attempts if reasoning && max_reasoning_attempts
  opts[:respect_context_window] = respect_context_window if respect_context_window
  opts[:llm] = llm_config.symbolize_keys if llm_config.present?
  sources = rcrew_knowledge_sources
  opts[:knowledge_sources] = sources if sources.any?
  opts[:memory] = memory_options if memory_enabled
  opts
end

#instantiated_toolsObject



66
67
68
69
70
71
72
73
74
# File 'app/models/rcrewai/rails/agent.rb', line 66

def instantiated_tools
  return [] if tools.blank?

  tools.map do |tool_config|
    tool_class = tool_config["class"].constantize
    tool_params = tool_config["params"] || {}
    tool_class.new(**tool_params.symbolize_keys)
  end
end

#memory_optionsObject

Agent memory config (rcrewai 0.6+). Scalars come from columns; embedder and store come from the engine configuration (set in a host initializer). May return {} — an empty hash still enables memory with core defaults.



51
52
53
54
55
56
57
58
59
60
# File 'app/models/rcrewai/rails/agent.rb', line 51

def memory_options
  m = {}
  m[:scope] = memory_scope if memory_scope.present?
  m[:short_term_limit] = memory_short_term_limit if memory_short_term_limit.present?
  embedder = RcrewAI::Rails.config.default_memory_embedder
  store = RcrewAI::Rails.config.default_memory_store
  m[:embedder] = embedder if embedder
  m[:store] = store if store
  m
end

#rcrew_knowledge_sourcesObject



62
63
64
# File 'app/models/rcrewai/rails/agent.rb', line 62

def rcrew_knowledge_sources
  knowledge_sources.active.map(&:to_rcrew_source)
end

#remove_tool(tool_class) ⇒ Object



85
86
87
88
# File 'app/models/rcrewai/rails/agent.rb', line 85

def remove_tool(tool_class)
  self.tools = tools.reject { |t| t["class"] == tool_class.to_s }
  save
end

#to_rcrew_agentObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/rcrewai/rails/agent.rb', line 19

def to_rcrew_agent
  RCrewAI::Agent.new(
    name: name,
    role: role,
    goal: goal,
    backstory: backstory,
    verbose: verbose,
    allow_delegation: allow_delegation,
    tools: instantiated_tools,
    max_iterations: max_iterations,
    **agent_options
  )
end