Class: RCrewAI::Tools::CodeExecutor

Inherits:
Base
  • Object
show all
Defined in:
lib/rcrewai/tools/code_executor.rb

Constant Summary

Constants included from RCrewAI::ToolSchema

RCrewAI::ToolSchema::TYPE_MAP

Instance Method Summary collapse

Methods inherited from Base

available_tools, create_tool, #description, #execute_with_validation, #json_schema, list_available_tools, #name, #validate_params!

Methods included from RCrewAI::ToolSchema

#description, extended, #json_schema, #param, #params, #tool_name

Constructor Details

#initialize(**options) ⇒ CodeExecutor

Returns a new instance of CodeExecutor.



22
23
24
25
26
27
28
29
30
# File 'lib/rcrewai/tools/code_executor.rb', line 22

def initialize(**options)
  super()
  @timeout = options.fetch(:timeout, 30)
  @max_output_size = options.fetch(:max_output_size, 100_000) # 100KB
  @allowed_languages = options.fetch(:allowed_languages, %w[python ruby javascript bash])
  @working_directory = options[:working_directory] || Dir.mktmpdir('rcrewai_code_')
  @enable_file_operations = options.fetch(:enable_file_operations, false)
  setup_security_restrictions
end

Instance Method Details

#execute(**params) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rcrewai/tools/code_executor.rb', line 32

def execute(**params)
  validate_params!(params, required: %i[code language], optional: %i[args stdin])

  language = params[:language].to_s.downcase
  code = params[:code]
  args = params[:args] || []
  stdin_input = params[:stdin]

  begin
    validate_execution_params!(language, code)
    result = execute_code(language, code, args, stdin_input)
    format_execution_result(language, code, result)
  rescue StandardError => e
    "Code execution failed: #{e.message}"
  end
end