Class: RCrewAI::Tools::Base
Constant Summary
RCrewAI::ToolSchema::TYPE_MAP
Class Method Summary
collapse
Instance Method Summary
collapse
extended, param, params, tool_name
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
10
11
12
13
14
|
# File 'lib/rcrewai/tools/base.rb', line 10
def initialize
end
|
Class Method Details
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
|
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
#description ⇒ Object
20
21
22
|
# File 'lib/rcrewai/tools/base.rb', line 20
def description
@description || self.class.description
end
|
#execute(**params) ⇒ Object
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_schema ⇒ Object
24
25
26
|
# File 'lib/rcrewai/tools/base.rb', line 24
def json_schema
self.class.json_schema
end
|
#name ⇒ Object
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
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: [])
missing = required - params.keys
raise ToolError, "Missing required parameters: #{missing.join(', ')}" unless missing.empty?
allowed = required + optional
unexpected = params.keys - allowed
return if unexpected.empty?
raise ToolError, "Unexpected parameters: #{unexpected.join(', ')}"
end
|