Class: RCrewAI::Task

Inherits:
Object
  • Object
show all
Includes:
AsyncExtensions, HumanInteractionExtensions
Defined in:
lib/rcrewai/task.rb

Defined Under Namespace

Classes: CLI

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HumanInteractionExtensions

#confirm_with_human, #get_human_feedback, #request_human_approval, #request_human_choice, #request_human_input, #request_human_review

Methods included from AsyncExtensions

#async_execution?, included, #thread_id

Constructor Details

#initialize(name:, description:, agent: nil, **options) ⇒ Task

Returns a new instance of Task.



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

def initialize(name:, description:, agent: nil, **options)
  @name = name
  @description = description
  @agent = agent
  @expected_output = options[:expected_output]
  @context = options[:context] || []  # Tasks this task depends on
  @tools = options[:tools] || []      # Additional tools for this specific task
  @async = options[:async] || false   # Whether task can run asynchronously
  @callback = options[:callback]      # Callback function after completion
  @attachments = options[:attachments] || [] # Multimodal inputs (images)

  # Output processing (0.4.0)
  @output_schema = options[:output_schema]            # JSON-schema for structured output
  @guardrail = options[:guardrail]                    # ->(output) { [ok, value_or_error] }
  @guardrail_max_retries = options.fetch(:guardrail_max_retries, 3)
  @output_file = options[:output_file]                # Path to write result to
  @create_directory = options.fetch(:create_directory, true)
  @markdown = options.fetch(:markdown, false)
  @raw_result = nil                                   # Unprocessed string content
  @structured_output = nil                            # Parsed object when output_schema set

  # Human interaction options
  @human_input_enabled = options[:human_input] || false
  @require_human_confirmation = options[:require_confirmation] || false
  @allow_human_guidance = options[:allow_guidance] || false
  @human_review_points = options[:human_review_points] || []

  @result = nil
  @status = :pending
  @start_time = nil
  @end_time = nil
  @execution_time = nil
  @stream_sink = nil
  @retry_count = 0
  @max_retries = options.fetch(:max_retries, 2)
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



11
12
13
# File 'lib/rcrewai/task.rb', line 11

def agent
  @agent
end

#asyncObject (readonly)

Returns the value of attribute async.



11
12
13
# File 'lib/rcrewai/task.rb', line 11

def async
  @async
end

#attachmentsObject (readonly)

Returns the value of attribute attachments.



11
12
13
# File 'lib/rcrewai/task.rb', line 11

def attachments
  @attachments
end

#contextObject (readonly)

Returns the value of attribute context.



11
12
13
# File 'lib/rcrewai/task.rb', line 11

def context
  @context
end

#descriptionObject (readonly)

Returns the value of attribute description.



11
12
13
# File 'lib/rcrewai/task.rb', line 11

def description
  @description
end

#end_timeObject

Returns the value of attribute end_time.



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

def end_time
  @end_time
end

#execution_timeObject

Returns the value of attribute execution_time.



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

def execution_time
  @execution_time
end

#expected_outputObject (readonly)

Returns the value of attribute expected_output.



11
12
13
# File 'lib/rcrewai/task.rb', line 11

def expected_output
  @expected_output
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/rcrewai/task.rb', line 11

def name
  @name
end

#raw_resultObject (readonly)

Returns the value of attribute raw_result.



11
12
13
# File 'lib/rcrewai/task.rb', line 11

def raw_result
  @raw_result
end

#resultObject

Returns the value of attribute result.



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

def result
  @result
end

#start_timeObject

Returns the value of attribute start_time.



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

def start_time
  @start_time
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

#stream_sinkObject

Returns the value of attribute stream_sink.



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

def stream_sink
  @stream_sink
end

#structured_outputObject (readonly)

Returns the value of attribute structured_output.



11
12
13
# File 'lib/rcrewai/task.rb', line 11

def structured_output
  @structured_output
end

#toolsObject (readonly)

Returns the value of attribute tools.



11
12
13
# File 'lib/rcrewai/task.rb', line 11

def tools
  @tools
end

Instance Method Details

#add_context_task(task) ⇒ Object



179
180
181
# File 'lib/rcrewai/task.rb', line 179

def add_context_task(task)
  @context << task unless @context.include?(task)
end

#add_tool(tool) ⇒ Object



189
190
191
# File 'lib/rcrewai/task.rb', line 189

def add_tool(tool)
  @tools << tool unless @tools.include?(tool)
end

#completed?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/rcrewai/task.rb', line 159

def completed?
  @status == :completed
end

#context_dataObject



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/rcrewai/task.rb', line 145

def context_data
  return '' if context.empty?

  context_results = context.map do |task|
    if task.completed?
      "Task: #{task.name}\nResult: #{task.result}\n---"
    else
      "Task: #{task.name}\nStatus: #{task.status}\n---"
    end
  end

  "Context from previous tasks:\n#{context_results.join("\n")}"
end

#dependencies_met?Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/rcrewai/task.rb', line 175

def dependencies_met?
  context.all?(&:completed?)
end

#disable_human_inputObject



200
201
202
203
204
205
# File 'lib/rcrewai/task.rb', line 200

def disable_human_input
  @human_input_enabled = false
  @require_human_confirmation = false
  @allow_human_guidance = false
  @human_review_points = []
end

#enable_human_input(**options) ⇒ Object



193
194
195
196
197
198
# File 'lib/rcrewai/task.rb', line 193

def enable_human_input(**options)
  @human_input_enabled = true
  @require_human_confirmation = options.fetch(:require_confirmation, false)
  @allow_human_guidance = options.fetch(:allow_guidance, false)
  @human_review_points = options.fetch(:review_points, [])
end

#enrich_description(text) ⇒ Object

Appends supplementary guidance (e.g. a planning step) to the task's description without discarding the original instructions.



185
186
187
# File 'lib/rcrewai/task.rb', line 185

def enrich_description(text)
  @description = "#{@description}\n\n#{text}"
end

#executeObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rcrewai/task.rb', line 52

def execute
  @start_time = Time.now
  @status = :running

  begin
    # Human confirmation before starting if required
    if @require_human_confirmation
      confirmation_result = confirm_task_execution
      unless confirmation_result[:approved]
        @status = :cancelled
        @result = "Task execution cancelled by human: #{confirmation_result[:reason]}"
        return @result
      end
    end

    if agent
      validate_dependencies!
      provide_context_to_agent

      # Enable human input for agent if task allows it
      if @human_input_enabled && agent.respond_to?(:enable_human_input)
        agent.enable_human_input(
          require_approval_for_tools: false,
          require_approval_for_final_answer: @allow_human_guidance
        )
      end

      @result = run_agent_with_output_processing

      # Post-execution human review if configured
      if @human_input_enabled && @human_review_points.include?(:completion)
        review_result = request_task_completion_review
        @result = handle_completion_review_feedback(review_result) if review_result && !review_result[:approved]
      end

      @status = :completed
    else
      @result = 'Task requires an agent'
      @status = :failed
    end

    @end_time = Time.now
    @execution_time = @end_time - @start_time

    # Execute callback if provided
    @callback&.call(self, @result)

    @result
  rescue TaskDependencyError => e
    # Don't retry dependency errors - they need dependencies to be completed first
    @status = :failed
    @end_time = Time.now
    @execution_time = @end_time - @start_time if @start_time
    @result = "Task dependencies not met: #{e.message}"
    raise e
  rescue StandardError => e
    @status = :failed
    @end_time = Time.now
    @execution_time = @end_time - @start_time if @start_time

    # Retry logic with human intervention
    if @retry_count < @max_retries
      @retry_count += 1
      puts "Task #{name} failed, retrying (#{@retry_count}/#{@max_retries})"

      # Ask human for retry guidance if enabled
      if @human_input_enabled
        retry_decision = request_retry_guidance(e)
        case retry_decision[:choice]
        when 'abort'
          @result = "Task execution aborted by human after failure: #{e.message}"
          raise TaskExecutionError, "Task '#{name}' aborted by human"
        when 'modify'
          # Allow human to modify task parameters
          modification_result = request_task_modification
          apply_task_modifications(modification_result[:changes]) if modification_result[:modified]
          # 'retry' is the default - continue with retry logic
        end
      end

      @status = :pending
      sleep(2**@retry_count) # Exponential backoff
      return execute
    end

    @result = "Task failed after #{@max_retries} retries: #{e.message}"
    raise TaskExecutionError, "Task '#{name}' failed: #{e.message}"
  ensure
    # Disable human input for agent after task completion
    agent.disable_human_input if @human_input_enabled && agent.respond_to?(:disable_human_input)
  end
end

#failed?Boolean

Returns:

  • (Boolean)


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

def failed?
  @status == :failed
end

#human_input_enabled?Boolean

Returns:

  • (Boolean)


207
208
209
# File 'lib/rcrewai/task.rb', line 207

def human_input_enabled?
  @human_input_enabled
end

#pending?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/rcrewai/task.rb', line 171

def pending?
  @status == :pending
end

#running?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/rcrewai/task.rb', line 167

def running?
  @status == :running
end