Class: OxAiWorkers::Tool::Eval

Inherits:
Object
  • Object
show all
Includes:
DependencyHelper, LoadI18n, OxAiWorkers::ToolDefinition
Defined in:
lib/oxaiworkers/tool/eval.rb

Instance Attribute Summary collapse

Attributes included from LoadI18n

#locale

Attributes included from OxAiWorkers::ToolDefinition

#white_list

Instance Method Summary collapse

Methods included from LoadI18n

#store_locale, #with_locale

Methods included from DependencyHelper

#depends_on

Methods included from OxAiWorkers::ToolDefinition

#define_function, #full_function_name, #function_schemas, #init_white_list_with, #tool_name

Constructor Details

#initialize(only: nil, current_dir: nil) ⇒ Eval



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/oxaiworkers/tool/eval.rb', line 14

def initialize(only: nil, current_dir: nil)
  store_locale

  init_white_list_with only
  @current_dir = current_dir

  define_function :ruby, description: I18n.t('oxaiworkers.tool.eval.ruby.description') do
    property :input, type: 'string', description: I18n.t('oxaiworkers.tool.eval.ruby.input'), required: true
  end

  define_function :sh, description: I18n.t('oxaiworkers.tool.eval.sh.description') do
    property :input, type: 'string', description: I18n.t('oxaiworkers.tool.eval.sh.input'), required: true
  end
end

Instance Attribute Details

#current_dirObject

Returns the value of attribute current_dir.



12
13
14
# File 'lib/oxaiworkers/tool/eval.rb', line 12

def current_dir
  @current_dir
end

Instance Method Details

#ruby(input:) ⇒ Object



29
30
31
32
# File 'lib/oxaiworkers/tool/eval.rb', line 29

def ruby(input:)
  OxAiWorkers.logger.info("Executing ruby: \"#{input}\"", for: self.class)
  execute_in_directory { eval(input) }
end

#sh(input:) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/oxaiworkers/tool/eval.rb', line 34

def sh(input:)
  OxAiWorkers.logger.info("Executing sh: \"#{input}\"", for: self.class)
  execute_in_directory do
    stdout_and_stderr_s, status = Open3.capture2e(input)
    stdout_and_stderr_s.present? ? stdout_and_stderr_s : status.to_s
  end
end