Class: RCrewAI::Tools::Base

Inherits:
Object
  • Object
show all
Extended by:
RCrewAI::ToolSchema
Defined in:
lib/rcrewai/tools/base.rb

Constant Summary

Constants included from RCrewAI::ToolSchema

RCrewAI::ToolSchema::TYPE_MAP

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RCrewAI::ToolSchema

extended, param, params, tool_name

Constructor Details

#initializeBase

Returns a new instance of Base.



10
11
12
13
14
# File 'lib/rcrewai/tools/base.rb', line 10

def initialize
  # @name and @description are no longer set here.
  # Instance #name and #description delegate to the class-level DSL
  # (tool_name / description) via the fallback in the reader methods below.
end

Class Method Details

.available_toolsObject



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rcrewai/tools/base.rb', line 72

def self.available_tools
  [
    WebSearch,
    FileReader,
    FileWriter,
    SqlDatabase,
    EmailSender,
    CodeExecutor,
    PdfProcessor
  ]
end

.create_tool(tool_name, **options) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rcrewai/tools/base.rb', line 84

def self.create_tool(tool_name, **options)
  tool_class = case tool_name.to_s.downcase
               when 'websearch', 'web_search'
                 WebSearch
               when 'filereader', 'file_reader'
                 FileReader
               when 'filewriter', 'file_writer'
                 FileWriter
               when 'sqldatabase', 'sql_database', 'database'
                 SqlDatabase
               when 'emailsender', 'email_sender', 'email'
                 EmailSender
               when 'codeexecutor', 'code_executor', 'code'
                 CodeExecutor
               when 'pdfprocessor', 'pdf_processor', 'pdf'
                 PdfProcessor
               else
                 raise ToolError, "Unknown tool: #{tool_name}"
               end

  tool_class.new(**options)
end

.list_available_toolsObject



107
108
109
110
111
# File 'lib/rcrewai/tools/base.rb', line 107

def self.list_available_tools
  available_tools.each_with_object({}) do |klass, h|
    h[klass.tool_name] = klass.description
  end
end

Instance Method Details

#descriptionObject



20
21
22
# File 'lib/rcrewai/tools/base.rb', line 20

def description
  @description || self.class.description
end

#execute(**params) ⇒ Object

Raises:

  • (NotImplementedError)


55
56
57
# File 'lib/rcrewai/tools/base.rb', line 55

def execute(**params)
  raise NotImplementedError, 'Subclasses must implement #execute method'
end

#execute_with_validation(args_hash) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rcrewai/tools/base.rb', line 28

def execute_with_validation(args_hash)
  coerced = {}
  schema_params = self.class.params

  if schema_params.empty?
    coerced = args_hash.transform_keys(&:to_sym)
    return execute(**coerced)
  end

  schema_params.each do |p|
    key_str = p[:name].to_s
    key_sym = p[:name].to_sym
    if args_hash.key?(key_str)
      raw = args_hash[key_str]
    elsif args_hash.key?(key_sym)
      raw = args_hash[key_sym]
    else
      raise ToolError, "missing required param: #{p[:name]}" if p[:required]

      next
    end
    coerced[key_sym] = coerce(raw, p[:type], p[:name])
  end

  execute(**coerced)
end

#json_schemaObject



24
25
26
# File 'lib/rcrewai/tools/base.rb', line 24

def json_schema
  self.class.json_schema
end

#nameObject



16
17
18
# File 'lib/rcrewai/tools/base.rb', line 16

def name
  @name || self.class.tool_name
end

#validate_params!(params, required: [], optional: []) ⇒ Object

Raises:



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rcrewai/tools/base.rb', line 59

def validate_params!(params, required: [], optional: [])
  # Check required parameters
  missing = required - params.keys
  raise ToolError, "Missing required parameters: #{missing.join(', ')}" unless missing.empty?

  # Check for unexpected parameters
  allowed = required + optional
  unexpected = params.keys - allowed
  return if unexpected.empty?

  raise ToolError, "Unexpected parameters: #{unexpected.join(', ')}"
end