Class: RailsAgents::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_agents/agent.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.description_textObject (readonly)

Returns the value of attribute description_text.



8
9
10
# File 'lib/rails_agents/agent.rb', line 8

def description_text
  @description_text
end

.discover_tools_configObject (readonly)

Returns the value of attribute discover_tools_config.



8
9
10
# File 'lib/rails_agents/agent.rb', line 8

def discover_tools_config
  @discover_tools_config
end

.max_turns_configObject (readonly)

Returns the value of attribute max_turns_config.



8
9
10
# File 'lib/rails_agents/agent.rb', line 8

def max_turns_config
  @max_turns_config
end

.model_nameObject (readonly)

Returns the value of attribute model_name.



8
9
10
# File 'lib/rails_agents/agent.rb', line 8

def model_name
  @model_name
end

.provider_nameObject (readonly)

Returns the value of attribute provider_name.



8
9
10
# File 'lib/rails_agents/agent.rb', line 8

def provider_name
  @provider_name
end

.tool_classesObject (readonly)

Returns the value of attribute tool_classes.



8
9
10
# File 'lib/rails_agents/agent.rb', line 8

def tool_classes
  @tool_classes
end

Class Method Details

.description(value = nil, &block) ⇒ Object



28
29
30
31
# File 'lib/rails_agents/agent.rb', line 28

def description(value = nil, &block)
  return @description_text if value.nil? && !block
  @description_text = block || value
end

.discover_tools(value = nil) ⇒ Object

When false, only declared tools and portable skill tools are available. Useful when app/agents/tools contains tools for multiple agents.



63
64
65
66
# File 'lib/rails_agents/agent.rb', line 63

def discover_tools(value = nil)
  return @discover_tools_config.nil? ? true : @discover_tools_config if value.nil?
  @discover_tools_config = value
end

.inherited(subclass) ⇒ Object



11
12
13
14
15
16
# File 'lib/rails_agents/agent.rb', line 11

def inherited(subclass)
  super
  subclass.instance_variable_set(:@tool_classes, Array(tool_classes).dup)
  subclass.instance_variable_set(:@skill_declarations, Array(skill_declarations).dup)
  subclass.instance_variable_set(:@discover_tools_config, discover_tools_config)
end

.max_turns(value = nil) ⇒ Object



56
57
58
59
# File 'lib/rails_agents/agent.rb', line 56

def max_turns(value = nil)
  return @max_turns_config || Runner::DEFAULT_TURNS if value.nil?
  @max_turns_config = value
end

.model(value = nil) ⇒ Object



23
24
25
26
# File 'lib/rails_agents/agent.rb', line 23

def model(value = nil)
  return @model_name if value.nil?
  @model_name = value
end

.provider(value = nil) ⇒ Object



18
19
20
21
# File 'lib/rails_agents/agent.rb', line 18

def provider(value = nil)
  return @provider_name if value.nil?
  @provider_name = value.to_sym
end

.provider_clientObject



76
77
78
# File 'lib/rails_agents/agent.rb', line 76

def provider_client
  Providers.build(resolved_provider)
end

.render_instructions(context = {}) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/rails_agents/agent.rb', line 91

def render_instructions(context = {})
  text = description_text
  case text
  when Proc then text.call(context)
  when nil then raise ConfigurationError, "Add a description to #{name}. Example: description \"What this agent does\""
  else text.to_s
  end
end

.resolved_modelObject



72
73
74
# File 'lib/rails_agents/agent.rb', line 72

def resolved_model
  model_name || raise(ConfigurationError, "Add a model to #{name}. Example: model \"gpt-4o-mini\"")
end

.resolved_providerObject



68
69
70
# File 'lib/rails_agents/agent.rb', line 68

def resolved_provider
  provider_name || RailsAgents.config.default_provider
end

.run(input = nil, save_files_to: nil, callbacks: {}, parse_json: false, **context) ⇒ Object Also known as: ask, call



100
101
102
103
104
105
106
107
108
# File 'lib/rails_agents/agent.rb', line 100

def run(input = nil, save_files_to: nil, callbacks: {}, parse_json: false, **context)
  Runner.new(
    self,
    input: input.nil? ? context : input,
    context: context.merge(save_files_to:),
    callbacks: callbacks,
    parse_json: parse_json
  ).call
end

.skill_declarationsObject



52
53
54
# File 'lib/rails_agents/agent.rb', line 52

def skill_declarations
  @skill_declarations || []
end

.skill_setObject



80
81
82
# File 'lib/rails_agents/agent.rb', line 80

def skill_set
  @skill_set ||= SkillSet.new(declarations: skill_declarations, provider: resolved_provider).tap(&:validate!)
end

.skills(*args, **kwargs) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rails_agents/agent.rb', line 38

def skills(*args, **kwargs)
  return skill_declarations if args.empty? && kwargs.empty?

  @skill_declarations ||= []
  args.each do |arg|
    if arg.is_a?(Hash)
      arg.each { |name, options| merge_skill(name, options) }
    else
      merge_skill(arg, kwargs)
    end
  end
  @skill_declarations
end

.tool_setObject



84
85
86
87
88
89
# File 'lib/rails_agents/agent.rb', line 84

def tool_set
  discovered = discover_tools && defined?(Rails) ? ToolSet.from_directory : ToolSet.new
  portable = ToolSet.new(*skill_set.portable_tool_classes)
  declared = ToolSet.new(*tool_classes)
  declared.+(portable).+(discovered)
end

.tools(*values) ⇒ Object



33
34
35
36
# File 'lib/rails_agents/agent.rb', line 33

def tools(*values)
  return @tool_classes || [] if values.empty?
  @tool_classes = values.flatten
end