Class: RcrewAI::Rails::TaskExecutionJob

Inherits:
ActiveJob::Base
  • Object
show all
Defined in:
app/jobs/rcrewai/rails/task_execution_job.rb

Instance Method Summary collapse

Instance Method Details

#perform(task, agent, inputs = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
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
# File 'app/jobs/rcrewai/rails/task_execution_job.rb', line 8

def perform(task, agent, inputs = {})
  execution_log = {
    task_id: task.id,
    agent_id: agent.id,
    started_at: Time.current
  }

  begin
    # Convert to RcrewAI objects
    rcrew_task = task.to_rcrew_task
    rcrew_agent = agent.to_rcrew_agent

    # Execute the task. Agent#execute_task returns a hash:
    #   { content:, tool_calls_history:, usage:, iterations:, finish_reason: }
    # `inputs` is recorded by the caller and made available as task context
    # via the Task#context column; the gem does not accept it as a kwarg.
    result = rcrew_agent.execute_task(rcrew_task)
    content = result.is_a?(Hash) ? result[:content].to_s : result.to_s

    execution_log[:completed_at] = Time.current
    execution_log[:status] = "completed"
    execution_log[:result] = content
    if result.is_a?(Hash)
      execution_log[:usage] = result[:usage]
      execution_log[:tool_calls] = result[:tool_calls_history]
      execution_log[:iterations] = result[:iterations]
      execution_log[:finish_reason] = result[:finish_reason]
    end

    # Save result if configured
    save_output_to_file(task.output_file, content) if task.output_file.present?

    # Log success
    ::Rails.logger.info "Task #{task.id} completed successfully by agent #{agent.id}"

    result
  rescue => e
    execution_log[:completed_at] = Time.current
    execution_log[:status] = "failed"
    execution_log[:error] = e.message

    ::Rails.logger.error "Task #{task.id} failed: #{e.message}"
    
    raise
  ensure
    # Could save execution log to database if needed
    log_task_execution(execution_log)
  end
end