Class: RCrewAI::Crew

Inherits:
Object
  • Object
show all
Includes:
AsyncExtensions
Defined in:
lib/rcrewai/crew.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AsyncExtensions

#async_execution?, included, #thread_id

Constructor Details

#initialize(name, **options) ⇒ Crew

Returns a new instance of Crew.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rcrewai/crew.rb', line 15

def initialize(name, **options)
  @name = name
  @agents = []
  @tasks = []
  @process_type = options.fetch(:process, :sequential)
  @verbose = options.fetch(:verbose, false)
  @max_iterations = options.fetch(:max_iterations, 10)
  @planning = options.fetch(:planning, false)
  @planning_llm = options[:planning_llm]
  @planned = false
  @knowledge = build_knowledge(options[:knowledge], options[:knowledge_sources])
  @before_kickoff_hooks = []
  @after_kickoff_hooks = []
  @last_inputs = {}
  @process_instance = nil
  validate_process_type!
end

Instance Attribute Details

#agentsObject (readonly)

Returns the value of attribute agents.



12
13
14
# File 'lib/rcrewai/crew.rb', line 12

def agents
  @agents
end

#knowledgeObject (readonly)

Returns the value of attribute knowledge.



33
34
35
# File 'lib/rcrewai/crew.rb', line 33

def knowledge
  @knowledge
end

#last_inputsObject (readonly)

Returns the value of attribute last_inputs.



33
34
35
# File 'lib/rcrewai/crew.rb', line 33

def last_inputs
  @last_inputs
end

#max_iterationsObject

Returns the value of attribute max_iterations.



13
14
15
# File 'lib/rcrewai/crew.rb', line 13

def max_iterations
  @max_iterations
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/rcrewai/crew.rb', line 12

def name
  @name
end

#process_typeObject (readonly)

Returns the value of attribute process_type.



12
13
14
# File 'lib/rcrewai/crew.rb', line 12

def process_type
  @process_type
end

#stream_sinkObject (readonly)

Returns the value of attribute stream_sink.



33
34
35
# File 'lib/rcrewai/crew.rb', line 33

def stream_sink
  @stream_sink
end

#tasksObject (readonly)

Returns the value of attribute tasks.



12
13
14
# File 'lib/rcrewai/crew.rb', line 12

def tasks
  @tasks
end

#verboseObject

Returns the value of attribute verbose.



13
14
15
# File 'lib/rcrewai/crew.rb', line 13

def verbose
  @verbose
end

Class Method Details

.create(name) ⇒ Object



145
146
147
148
149
150
# File 'lib/rcrewai/crew.rb', line 145

def self.create(name)
  crew = new(name)
  crew.save
  puts "Crew '#{name}' created successfully!"
  crew
end

.listObject



157
158
159
160
# File 'lib/rcrewai/crew.rb', line 157

def self.list
  # List all available crews
  %w[example_crew research_crew development_crew]
end

.load(name) ⇒ Object



152
153
154
155
# File 'lib/rcrewai/crew.rb', line 152

def self.load(name)
  # Load crew configuration from file
  new(name)
end

Instance Method Details

#add_agent(agent) ⇒ Object



53
54
55
# File 'lib/rcrewai/crew.rb', line 53

def add_agent(agent)
  @agents << agent
end

#add_task(task) ⇒ Object



57
58
59
# File 'lib/rcrewai/crew.rb', line 57

def add_task(task)
  @tasks << task
end

#after_kickoff(&block) ⇒ Object

Register a callback run after execution. Receives the result and may return a transformed result. Multiple hooks run in registration order.



48
49
50
51
# File 'lib/rcrewai/crew.rb', line 48

def after_kickoff(&block)
  @after_kickoff_hooks << block
  self
end

#before_kickoff(&block) ⇒ Object

Register a callback run before execution. Receives the inputs hash and may return a transformed hash. Multiple hooks run in registration order.



41
42
43
44
# File 'lib/rcrewai/crew.rb', line 41

def before_kickoff(&block)
  @before_kickoff_hooks << block
  self
end

#execute(async: false, stream: nil, inputs: {}, **async_options, &block) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rcrewai/crew.rb', line 61

def execute(async: false, stream: nil, inputs: {}, **async_options, &block)
  sinks = []
  sinks << block if block_given?
  Array(stream).each { |s| sinks << s } if stream
  @stream_sink = sinks.empty? ? nil : RCrewAI::Events.fan_out(sinks)

  run_before_hooks(inputs)

  distribute_knowledge if @knowledge
  run_planning_pass if planning?

  result = async ? execute_async(**async_options) : execute_sync
  run_after_hooks(result)
end

#execute_async(**options) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rcrewai/crew.rb', line 111

def execute_async(**options)
  puts "Executing crew: #{name} (async #{process_type} process)"

  case process_type
  when :sequential
    execute_sequential_async(**options)
  when :hierarchical
    execute_hierarchical_async(**options)
  when :consensual
    execute_consensual_async(**options)
  else
    raise ConfigurationError, "Async execution not implemented for #{process_type} process"
  end
end

#execute_syncObject



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rcrewai/crew.rb', line 126

def execute_sync
  puts "Executing crew: #{name} (#{process_type} process)"

  # Create appropriate process instance
  @process_instance = create_process_instance

  # Execute using the process
  results = @process_instance.execute

  # Return formatted results
  format_execution_results(results)
end

#kickoff_for_each(inputs:) ⇒ Object

Runs the crew once per input set, returning one result per input in order. Runs are isolated: each execution starts from only its own inputs.



78
79
80
# File 'lib/rcrewai/crew.rb', line 78

def kickoff_for_each(inputs:)
  Array(inputs).map { |input| execute(inputs: input) }
end

#planning?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/rcrewai/crew.rb', line 35

def planning?
  @planning
end

#process=(new_process) ⇒ Object



139
140
141
142
143
# File 'lib/rcrewai/crew.rb', line 139

def process=(new_process)
  @process_type = new_process.to_sym
  validate_process_type!
  @process_instance = nil # Reset process instance
end

#saveObject



162
163
164
165
# File 'lib/rcrewai/crew.rb', line 162

def save
  # Save crew configuration to file
  true
end

#test(n_iterations:, scorer: nil, model: nil) ⇒ Object

Runs the crew repeatedly and scores each run. scorer is a callable ->(result) { Float }; it defaults to the run's success_rate. Mirrors CrewAI's crew.test.



103
104
105
106
107
108
109
# File 'lib/rcrewai/crew.rb', line 103

def test(n_iterations:, scorer: nil, model: nil) # rubocop:disable Lint/UnusedMethodArgument
  scorer ||= ->(result) { result[:success_rate].to_f }
  scores = (1..n_iterations).map { scorer.call(execute) }
  average = scores.empty? ? 0.0 : (scores.sum / scores.length).round(2)

  { iterations: n_iterations, scores: scores, average_score: average }
end

#train(n_iterations:, filename:, feedback: nil) ⇒ Object

Runs the crew repeatedly, collecting feedback after each iteration and persisting it to filename as JSON. feedback is a callable ->(iteration, result) { "..." }; it defaults to prompting a human. Mirrors CrewAI's crew.train.



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rcrewai/crew.rb', line 86

def train(n_iterations:, filename:, feedback: nil)
  feedback ||= method(:default_training_feedback)
  entries = []

  (1..n_iterations).each do |iteration|
    result = execute
    note = feedback.call(iteration, result)
    entries << { iteration: iteration, feedback: note }
  end

  write_training_file(filename, entries)
  { iterations: n_iterations, filename: filename, entries: entries }
end