Class: SkillBench::Agent::ReactAgent::ToolExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/skill_bench/agent/react_agent/tool_executor.rb

Overview

Service object responsible for executing a list of tool calls and returning the results formatted as messages to be appended to the conversation history.

Class Method Summary collapse

Class Method Details

.call(tool_calls, working_dir, container_id = nil) ⇒ Array<Hash>

Executes the provided tool calls.

Parameters:

  • tool_calls (Array<Hash>)

    The tool calls requested by the LLM.

  • working_dir (String)

    The directory where tools should operate.

  • container_id (String, nil) (defaults to: nil)

    The Docker container ID for isolated execution.

Returns:

  • (Array<Hash>)

    An array of message hashes containing tool results.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/skill_bench/agent/react_agent/tool_executor.rb', line 17

def self.call(tool_calls, working_dir, container_id = nil)
  tool_calls.map do |tool_call|
    function_name = tool_call.dig('function', 'name')
    next tool_error_message(tool_call, 'Missing function name') unless function_name

    warn "=== Calling Tool: #{function_name} ===" unless defined?(Minitest)

    result = execute_tool(tool_call, working_dir, container_id)
    if result.is_a?(Hash) && result[:role] == 'tool'
      result
    else
      error_msg = result.dig(:response, :error, :message) || 'Unknown tool error'
      tool_error_message(tool_call, error_msg)
    end
  end
end

.execute_tool(tool_call, working_dir, container_id) ⇒ Hash

Executes a single tool call and returns the result message.

Parameters:

  • tool_call (Hash)

    The tool call hash.

  • working_dir (String)

    The directory where tools should operate.

  • container_id (String, nil)

    The Docker container ID.

Returns:

  • (Hash)

    Tool result message or error hash.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/skill_bench/agent/react_agent/tool_executor.rb', line 40

def self.execute_tool(tool_call, working_dir, container_id)
  function_name = tool_call.dig('function', 'name')
  arguments = tool_call.dig('function', 'arguments')

  result = Tools.execute(function_name, arguments, working_dir, container_id)

  {
    role: 'tool',
    tool_call_id: tool_call['id'],
    content: result
  }
rescue StandardError => e
  SkillBench::ErrorLogger.log_error(e, "Tool execution failed: #{function_name}")
  tool_error_result(tool_call, e.message)
end

.tool_error_message(tool_call, message) ⇒ Hash

Builds a tool error message for the conversation history.

Parameters:

  • tool_call (Hash)

    The tool call hash.

  • message (String)

    The error message.

Returns:

  • (Hash)

    Tool message with error content.



61
62
63
64
65
66
67
# File 'lib/skill_bench/agent/react_agent/tool_executor.rb', line 61

def self.tool_error_message(tool_call, message)
  {
    role: 'tool',
    tool_call_id: tool_call['id'],
    content: "Error: #{message}"
  }
end

.tool_error_result(tool_call, message) ⇒ Hash

Builds an error result for a failed tool call.

Parameters:

  • tool_call (Hash)

    The tool call hash.

  • message (String)

    The error message.

Returns:

  • (Hash)

    Error result hash.



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/skill_bench/agent/react_agent/tool_executor.rb', line 74

def self.tool_error_result(tool_call, message)
  {
    success: false,
    response: {
      error: {
        message: "Tool call failed: #{message}",
        tool_call: tool_call
      }
    }
  }
end