Class: Ask::Agent::ToolExecutor

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

Constant Summary collapse

CRITICAL_ERROR_CLASSES =
%w[
  Ask::Unauthorized
  Ask::Forbidden
  Ask::PaymentRequired
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_retries: 3, parallel: true) ⇒ ToolExecutor

Returns a new instance of ToolExecutor.



14
15
16
17
18
# File 'lib/ask/agent/tool_executor.rb', line 14

def initialize(max_retries: 3, parallel: true)
  @max_retries = max_retries
  @parallel = parallel
  @total_executions = 0
end

Instance Attribute Details

#telemetry=(value) ⇒ Object (writeonly)

Sets the attribute telemetry

Parameters:

  • value

    the value to set the attribute telemetry to.



20
21
22
# File 'lib/ask/agent/tool_executor.rb', line 20

def telemetry=(value)
  @telemetry = value
end

#total_executionsObject (readonly)

Returns the value of attribute total_executions.



12
13
14
# File 'lib/ask/agent/tool_executor.rb', line 12

def total_executions
  @total_executions
end

Instance Method Details

#execute(tool_calls, tools, hooks:, event_emitter:, session_id: nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ask/agent/tool_executor.rb', line 22

def execute(tool_calls, tools, hooks:, event_emitter:, session_id: nil)
  return [] if tool_calls.empty?

  @total_executions = 0
  @session_id = session_id
  sibling_abort = ToolAbortController.new

  if @parallel
    execute_parallel(tool_calls, tools, hooks, event_emitter, sibling_abort)
  else
    execute_sequential(tool_calls, tools, hooks, event_emitter, sibling_abort)
  end
end

#execute_parallel(tool_calls, tools, hooks, event_emitter, sibling_abort, &result_callback) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ask/agent/tool_executor.rb', line 36

def execute_parallel(tool_calls, tools, hooks, event_emitter, sibling_abort, &result_callback)
  threads = []
  mutex = Mutex.new
  results = {}

  tool_calls.each do |id, tool_call|
    threads << Thread.new do
      begin
        if sibling_abort.aborted?
          mutex.synchronize { results[id] = aborted_result(tool_call) }
          next
        end

        result = execute_single_tool(tool_call, tools, hooks, event_emitter, sibling_abort)
        mutex.synchronize { results[id] = result }

        # Stream result back as it completes
        result_callback&.call(tool_call.id, result)

        if result[:critical_failure]
          sibling_abort.abort!
        end
      rescue => e
        mutex.synchronize do
          results[id] = {
            tool_name: tool_call.name, message: e.message,
            status: "error", is_error: true, critical_failure: false
          }
        end
        result_callback&.call(tool_call.id, results[id])
      end
    end
  end

  threads.each(&:join)
  tool_calls.keys.map { |id| results[id] }.compact
end

#execute_sequential(tool_calls, tools, hooks, event_emitter, sibling_abort) ⇒ Object



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

def execute_sequential(tool_calls, tools, hooks, event_emitter, sibling_abort)
  results = []
  tool_calls.each do |id, tool_call|
    break if sibling_abort.aborted?

    result = execute_single_tool(tool_call, tools, hooks, event_emitter, sibling_abort)
    results << result
    break if result[:critical_failure]
  end
  results
end