Class: Ask::Agent::ToolExecutor
- Inherits:
-
Object
- Object
- Ask::Agent::ToolExecutor
- 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
-
#telemetry ⇒ Object
writeonly
Sets the attribute telemetry.
-
#total_executions ⇒ Object
readonly
Returns the value of attribute total_executions.
Instance Method Summary collapse
- #execute(tool_calls, tools, hooks:, event_emitter:, session_id: nil, result_callback: nil) ⇒ Object
- #execute_parallel(tool_calls, tools, hooks, event_emitter, sibling_abort, &result_callback) ⇒ Object
- #execute_sequential(tool_calls, tools, hooks, event_emitter, sibling_abort, &result_callback) ⇒ Object
-
#initialize(max_retries: 3, parallel: true) ⇒ ToolExecutor
constructor
A new instance of ToolExecutor.
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
20 21 22 |
# File 'lib/ask/agent/tool_executor.rb', line 20 def telemetry=(value) @telemetry = value end |
#total_executions ⇒ Object (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, result_callback: nil) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/ask/agent/tool_executor.rb', line 22 def execute(tool_calls, tools, hooks:, event_emitter:, session_id: nil, result_callback: 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, &result_callback) else execute_sequential(tool_calls, tools, hooks, event_emitter, sibling_abort) do |id, result| result_callback&.call(id, result) end end end |
#execute_parallel(tool_calls, tools, hooks, event_emitter, sibling_abort, &result_callback) ⇒ Object
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 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ask/agent/tool_executor.rb', line 38 def execute_parallel(tool_calls, tools, hooks, event_emitter, sibling_abort, &result_callback) threads = [] mutex = Mutex.new results = {} # Inherit the caller's thread-local state (Rails CurrentAttributes # and similar frameworks store per-request context in Thread.current) # so tools see the same context they would in a sequential run. inherited_locals = {} Thread.current.keys.each { |key| inherited_locals[key] = Thread.current[key] } tool_calls.each do |id, tool_call| threads << Thread.new do inherited_locals.each { |key, value| Thread.current[key] = value } 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 if result[:halted] sibling_abort.abort! end rescue => e mutex.synchronize do results[id] = { tool_name: tool_call.name, message: e., 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, &result_callback) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/ask/agent/tool_executor.rb', line 87 def execute_sequential(tool_calls, tools, hooks, event_emitter, sibling_abort, &result_callback) 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 result_callback&.call(id, result) break if result[:critical_failure] break if result[:halted] end results end |