Class: RCrewAI::ToolRunner
- Inherits:
-
Object
- Object
- RCrewAI::ToolRunner
- Defined in:
- lib/rcrewai/tool_runner.rb
Constant Summary collapse
- DEFAULT_MAX_ITERATIONS =
10
Instance Method Summary collapse
-
#initialize(agent:, llm:, tools:, max_iterations: DEFAULT_MAX_ITERATIONS, event_sink: nil) ⇒ ToolRunner
constructor
A new instance of ToolRunner.
-
#run(messages:) ⇒ Object
rubocop:disable Metrics/AbcSize.
Constructor Details
#initialize(agent:, llm:, tools:, max_iterations: DEFAULT_MAX_ITERATIONS, event_sink: nil) ⇒ ToolRunner
Returns a new instance of ToolRunner.
10 11 12 13 14 15 16 17 |
# File 'lib/rcrewai/tool_runner.rb', line 10 def initialize(agent:, llm:, tools:, max_iterations: DEFAULT_MAX_ITERATIONS, event_sink: nil) @agent = agent @llm = llm @tools = tools @tools_by_name = tools.each_with_object({}) { |t, h| h[t.name] = t } @max_iterations = max_iterations @sink = event_sink || ->(_) {} end |
Instance Method Details
#run(messages:) ⇒ Object
rubocop:disable Metrics/AbcSize
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 73 74 75 76 77 78 79 |
# File 'lib/rcrewai/tool_runner.rb', line 19 def run(messages:) # rubocop:disable Metrics/AbcSize msgs = .dup history = [] iter = 0 total_usage = { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 } while iter < @max_iterations iter += 1 emit(Events::IterationStart, iteration: iter, iteration_index: iter) response = @llm.chat( messages: fit_context(msgs), tools: @tools.map(&:json_schema), stream: ->(e) { @sink.call(retag(e, iter)) } ) accumulate_usage(total_usage, response[:usage]) if response[:tool_calls].nil? || response[:tool_calls].empty? emit(Events::IterationEnd, iteration: iter, finish_reason: response[:finish_reason]) return finalize(content: response[:content], history: history, iter: iter, finish_reason: response[:finish_reason], usage: total_usage) end msgs << { role: 'assistant', content: response[:content], tool_calls: response[:tool_calls] } response[:tool_calls].each do |tc| tool = @tools_by_name[tc[:name]] emit(Events::ToolCallStart, iteration: iter, tool: tc[:name], args: tc[:arguments], call_id: tc[:id]) if tool.nil? err = "tool not found: #{tc[:name]}" emit(Events::ToolCallError, iteration: iter, tool: tc[:name], call_id: tc[:id], error: err) msgs << (tc[:id], "ERROR: #{err}") next end started = monotonic_ms begin result = tool.execute_with_validation(tc[:arguments] || {}) duration = monotonic_ms - started @agent.memory.add_tool_usage(tc[:name], tc[:arguments], result) if @agent.respond_to?(:memory) && @agent.memory emit(Events::ToolCallResult, iteration: iter, tool: tc[:name], call_id: tc[:id], result: result, duration_ms: duration) history << { tool: tc[:name], args: tc[:arguments], result: result, duration_ms: duration } msgs << (tc[:id], result.to_s) rescue StandardError => e emit(Events::ToolCallError, iteration: iter, tool: tc[:name], call_id: tc[:id], error: e.) msgs << (tc[:id], "ERROR: #{e.}") end end emit(Events::IterationEnd, iteration: iter, finish_reason: :tool_calls) end finalize(content: nil, history: history, iter: iter, finish_reason: :max_iterations, usage: total_usage) end |