Class: RSmolagent::Tools::CustomClassExecutorTool
- Inherits:
-
RSmolagent::Tool
- Object
- RSmolagent::Tool
- RSmolagent::Tools::CustomClassExecutorTool
- Defined in:
- lib/rsmolagent/tools/ruby_executor.rb
Overview
A more specific custom tool executor that defines a class template
Instance Attribute Summary
Attributes inherited from RSmolagent::Tool
#description, #input_schema, #name
Instance Method Summary collapse
- #execute(args) ⇒ Object
-
#initialize(name: "custom_executor", description: nil, base_class: nil) ⇒ CustomClassExecutorTool
constructor
A new instance of CustomClassExecutorTool.
Methods inherited from RSmolagent::Tool
Constructor Details
#initialize(name: "custom_executor", description: nil, base_class: nil) ⇒ CustomClassExecutorTool
Returns a new instance of CustomClassExecutorTool.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/rsmolagent/tools/ruby_executor.rb', line 96 def initialize(name: "custom_executor", description: nil, base_class: nil) description ||= "Executes a custom Ruby class with predefined methods. " + "The class must implement a 'run' method that will be called." @base_class = base_class super( name: name, description: description, input_schema: { code: { type: "string", description: "The Ruby class definition, must include a 'run' method" }, args: { type: "object", description: "Arguments to pass to the run method (optional)", required: false } } ) end |
Instance Method Details
#execute(args) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/rsmolagent/tools/ruby_executor.rb', line 119 def execute(args) code = args[:code] run_args = args[:args] || {} return "Error: No code provided" if code.nil? || code.strip.empty? # Capture stdout original_stdout = $stdout captured_stdout = StringIO.new $stdout = captured_stdout begin # Create a clean context context = Object.new.instance_eval { binding } # Inject base class if provided if @base_class context.eval("BaseClass = #{@base_class.name}") end # Evaluate the code to define the class context.eval(code) # Find the class we just defined class_name = extract_class_name(code) if class_name.nil? return "Error: Could not find a class definition in the provided code" end # Instantiate the class runner = context.eval("#{class_name}.new") # Ensure it has a run method unless runner.respond_to?(:run) return "Error: The class must implement a 'run' method" end # Run the class's run method with provided arguments if run_args.is_a?(Hash) result = runner.run(**run_args) else result = runner.run(run_args) end # Format the output stdout_content = captured_stdout.string.strip if stdout_content.empty? "Result: #{result.inspect}" else "Output:\n#{stdout_content}\n\nResult: #{result.inspect}" end rescue => e "Error: #{e.}\n#{e.backtrace.first(3).join("\n")}" ensure # Restore stdout $stdout = original_stdout end end |