Class: RcrewAI::Rails::Crew

Inherits:
ApplicationRecord show all
Defined in:
app/models/rcrewai/rails/crew.rb

Instance Method Summary collapse

Instance Method Details

#batch_executions(batch_id) ⇒ Object



79
80
81
# File 'app/models/rcrewai/rails/crew.rb', line 79

def batch_executions(batch_id)
  executions.where(batch_id: batch_id).order(:created_at, :id)
end

#crew_planning_optionsObject

rcrewai 0.5.0 planning options. Emit a key only when meaningfully set, so an all-default crew constructs exactly as it did before.



42
43
44
45
46
47
48
49
# File 'app/models/rcrewai/rails/crew.rb', line 42

def crew_planning_options
  opts = {}
  opts[:planning] = planning if planning
  opts[:planning_llm] = planning_llm.to_sym if planning_llm.present?
  sources = rcrew_knowledge_sources
  opts[:knowledge_sources] = sources if sources.any?
  opts
end

#execute_async(inputs = {}) ⇒ Object



55
56
57
# File 'app/models/rcrewai/rails/crew.rb', line 55

def execute_async(inputs = {})
  CrewExecutionJob.perform_later(self, inputs)
end

#execute_batch_async(inputs_list) ⇒ Object



63
64
65
66
67
68
69
# File 'app/models/rcrewai/rails/crew.rb', line 63

def execute_batch_async(inputs_list)
  batch_id = SecureRandom.uuid
  normalize_batch_inputs(inputs_list).each do |inputs|
    CrewExecutionJob.perform_later(self, inputs, batch_id: batch_id)
  end
  batch_id
end

#execute_batch_sync(inputs_list) ⇒ Object



71
72
73
74
75
76
77
# File 'app/models/rcrewai/rails/crew.rb', line 71

def execute_batch_sync(inputs_list)
  batch_id = SecureRandom.uuid
  normalize_batch_inputs(inputs_list).each do |inputs|
    CrewExecutionJob.perform_now(self, inputs, batch_id: batch_id)
  end
  { batch_id: batch_id, executions: batch_executions(batch_id).to_a }
end

#execute_sync(inputs = {}) ⇒ Object



59
60
61
# File 'app/models/rcrewai/rails/crew.rb', line 59

def execute_sync(inputs = {})
  CrewExecutionJob.perform_now(self, inputs)
end

#execution_statsObject



87
88
89
90
91
92
93
94
95
# File 'app/models/rcrewai/rails/crew.rb', line 87

def execution_stats
  {
    total: executions.count,
    successful: executions.successful.count,
    failed: executions.failed.count,
    pending: executions.pending.count,
    average_duration: executions.successful.average(:duration_seconds)
  }
end

#last_executionObject



83
84
85
# File 'app/models/rcrewai/rails/crew.rb', line 83

def last_execution
  executions.order(created_at: :desc).first
end

#rcrew_knowledge_sourcesObject



51
52
53
# File 'app/models/rcrewai/rails/crew.rb', line 51

def rcrew_knowledge_sources
  knowledge_sources.active.map(&:to_rcrew_source)
end

#to_rcrewObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/rcrewai/rails/crew.rb', line 20

def to_rcrew
  crew = RCrewAI::Crew.new(
    name,
    process: process_type.to_sym,
    verbose: verbose,
    **crew_planning_options
  )

  agents.each do |agent|
    crew.add_agent(agent.to_rcrew_agent)
  end

  tasks.each do |task|
    crew.add_task(task.to_rcrew_task)
  end

  register_kickoff_hooks(crew)
  crew
end