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
rcrew_task = task.to_rcrew_task
rcrew_agent = agent.to_rcrew_agent
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_output_to_file(task.output_file, content) if task.output_file.present?
::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
log_task_execution(execution_log)
end
end
|