Class: RCrewAI::LegacyReactRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/rcrewai/legacy_react_runner.rb

Overview

Behavior-preserving extraction of the prompt-parsed USE_TOOL[] / FINAL_ANSWER[] loop that lived in Agent. Used as a fallback when an agent's tools have no DSL schemas declared OR the configured LLM does not support native function calling.

Constant Summary collapse

DEFAULT_MAX_ITERATIONS =
10

Instance Method Summary collapse

Constructor Details

#initialize(agent:, llm:, tools:, max_iterations: DEFAULT_MAX_ITERATIONS, event_sink: nil) ⇒ LegacyReactRunner

Returns a new instance of LegacyReactRunner.



13
14
15
16
17
18
19
# File 'lib/rcrewai/legacy_react_runner.rb', line 13

def initialize(agent:, llm:, tools:, max_iterations: DEFAULT_MAX_ITERATIONS, event_sink: nil)
  @agent = agent
  @llm = llm
  @tools = tools
  @max_iterations = max_iterations
  @sink = event_sink || ->(_) {}
end

Instance Method Details

#run(messages:) ⇒ Object



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
# File 'lib/rcrewai/legacy_react_runner.rb', line 21

def run(messages:)
  msgs = messages.dup
  history = []
  iter = 0
  total_usage = { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 }
  last_reasoning = nil
  last_action_result = nil

  while iter < @max_iterations
    iter += 1
    emit(Events::IterationStart, iteration: iter, iteration_index: iter)

    response = @llm.chat(messages: fit_context(msgs))
    accumulate_usage(total_usage, response[:usage])
    reasoning = response[:content] || ''
    last_reasoning = reasoning

    action_result, iteration_history = parse_and_execute_actions(reasoning, iter)
    history.concat(iteration_history)
    last_action_result = action_result

    msgs << { role: 'assistant', content: reasoning }
    msgs << { role: 'user', content: action_result } if action_result && !action_result.empty?

    finish_reason = response[:finish_reason]
    emit(Events::IterationEnd, iteration: iter, finish_reason: finish_reason)

    next unless task_complete?(reasoning, action_result) || finish_reason == :stop

    final = extract_final_result(reasoning, action_result)
    return finalize(content: final, history: history, iter: iter,
                    finish_reason: finish_reason || :stop, usage: total_usage)
  end

  final = extract_final_result(last_reasoning || '', last_action_result) ||
          'Task execution reached limits without clear completion'
  finalize(content: final, history: history, iter: iter,
           finish_reason: :max_iterations, usage: total_usage)
end