Class: RCrewAI::Task
- Inherits:
-
Object
- Object
- RCrewAI::Task
- Includes:
- AsyncExtensions, HumanInteractionExtensions
- Defined in:
- lib/rcrewai/task.rb
Defined Under Namespace
Classes: CLI
Instance Attribute Summary collapse
-
#agent ⇒ Object
readonly
Returns the value of attribute agent.
-
#async ⇒ Object
readonly
Returns the value of attribute async.
-
#attachments ⇒ Object
readonly
Returns the value of attribute attachments.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#end_time ⇒ Object
Returns the value of attribute end_time.
-
#execution_time ⇒ Object
Returns the value of attribute execution_time.
-
#expected_output ⇒ Object
readonly
Returns the value of attribute expected_output.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#raw_result ⇒ Object
readonly
Returns the value of attribute raw_result.
-
#result ⇒ Object
Returns the value of attribute result.
-
#start_time ⇒ Object
Returns the value of attribute start_time.
-
#status ⇒ Object
Returns the value of attribute status.
-
#structured_output ⇒ Object
readonly
Returns the value of attribute structured_output.
-
#tools ⇒ Object
readonly
Returns the value of attribute tools.
Instance Method Summary collapse
- #add_context_task(task) ⇒ Object
- #add_tool(tool) ⇒ Object
- #completed? ⇒ Boolean
- #context_data ⇒ Object
- #dependencies_met? ⇒ Boolean
- #disable_human_input ⇒ Object
- #enable_human_input(**options) ⇒ Object
-
#enrich_description(text) ⇒ Object
Appends supplementary guidance (e.g. a planning step) to the task's description without discarding the original instructions.
- #execute ⇒ Object
- #failed? ⇒ Boolean
- #human_input_enabled? ⇒ Boolean
-
#initialize(name:, description:, agent: nil, **options) ⇒ Task
constructor
A new instance of Task.
- #pending? ⇒ Boolean
- #running? ⇒ Boolean
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 |
# File 'lib/rcrewai/task.rb', line 15 def initialize(name:, description:, agent: nil, **) @name = name @description = description @agent = agent @expected_output = [:expected_output] @context = [:context] || [] # Tasks this task depends on @tools = [:tools] || [] # Additional tools for this specific task @async = [:async] || false # Whether task can run asynchronously @callback = [:callback] # Callback function after completion @attachments = [:attachments] || [] # Multimodal inputs (images) # Output processing (0.4.0) @output_schema = [:output_schema] # JSON-schema for structured output @guardrail = [:guardrail] # ->(output) { [ok, value_or_error] } @guardrail_max_retries = .fetch(:guardrail_max_retries, 3) @output_file = [:output_file] # Path to write result to @create_directory = .fetch(:create_directory, true) @markdown = .fetch(:markdown, false) @raw_result = nil # Unprocessed string content @structured_output = nil # Parsed object when output_schema set # Human interaction options @human_input_enabled = [:human_input] || false @require_human_confirmation = [:require_confirmation] || false @allow_human_guidance = [:allow_guidance] || false @human_review_points = [:human_review_points] || [] @result = nil @status = :pending @start_time = nil @end_time = nil @execution_time = nil @retry_count = 0 @max_retries = .fetch(:max_retries, 2) end |
Instance Attribute Details
#agent ⇒ Object (readonly)
Returns the value of attribute agent.
11 12 13 |
# File 'lib/rcrewai/task.rb', line 11 def agent @agent end |
#async ⇒ Object (readonly)
Returns the value of attribute async.
11 12 13 |
# File 'lib/rcrewai/task.rb', line 11 def async @async end |
#attachments ⇒ Object (readonly)
Returns the value of attribute attachments.
11 12 13 |
# File 'lib/rcrewai/task.rb', line 11 def @attachments end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
11 12 13 |
# File 'lib/rcrewai/task.rb', line 11 def context @context end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
11 12 13 |
# File 'lib/rcrewai/task.rb', line 11 def description @description end |
#end_time ⇒ Object
Returns the value of attribute end_time.
13 14 15 |
# File 'lib/rcrewai/task.rb', line 13 def end_time @end_time end |
#execution_time ⇒ Object
Returns the value of attribute execution_time.
13 14 15 |
# File 'lib/rcrewai/task.rb', line 13 def execution_time @execution_time end |
#expected_output ⇒ Object (readonly)
Returns the value of attribute expected_output.
11 12 13 |
# File 'lib/rcrewai/task.rb', line 11 def expected_output @expected_output end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/rcrewai/task.rb', line 11 def name @name end |
#raw_result ⇒ Object (readonly)
Returns the value of attribute raw_result.
11 12 13 |
# File 'lib/rcrewai/task.rb', line 11 def raw_result @raw_result end |
#result ⇒ Object
Returns the value of attribute result.
13 14 15 |
# File 'lib/rcrewai/task.rb', line 13 def result @result end |
#start_time ⇒ Object
Returns the value of attribute start_time.
13 14 15 |
# File 'lib/rcrewai/task.rb', line 13 def start_time @start_time end |
#status ⇒ Object
Returns the value of attribute status.
13 14 15 |
# File 'lib/rcrewai/task.rb', line 13 def status @status end |
#structured_output ⇒ Object (readonly)
Returns the value of attribute structured_output.
11 12 13 |
# File 'lib/rcrewai/task.rb', line 11 def structured_output @structured_output end |
#tools ⇒ Object (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
178 179 180 |
# File 'lib/rcrewai/task.rb', line 178 def add_context_task(task) @context << task unless @context.include?(task) end |
#add_tool(tool) ⇒ Object
188 189 190 |
# File 'lib/rcrewai/task.rb', line 188 def add_tool(tool) @tools << tool unless @tools.include?(tool) end |
#completed? ⇒ Boolean
158 159 160 |
# File 'lib/rcrewai/task.rb', line 158 def completed? @status == :completed end |
#context_data ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/rcrewai/task.rb', line 144 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
174 175 176 |
# File 'lib/rcrewai/task.rb', line 174 def dependencies_met? context.all?(&:completed?) end |
#disable_human_input ⇒ Object
199 200 201 202 203 204 |
# File 'lib/rcrewai/task.rb', line 199 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
192 193 194 195 196 197 |
# File 'lib/rcrewai/task.rb', line 192 def enable_human_input(**) @human_input_enabled = true @require_human_confirmation = .fetch(:require_confirmation, false) @allow_human_guidance = .fetch(:allow_guidance, false) @human_review_points = .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.
184 185 186 |
# File 'lib/rcrewai/task.rb', line 184 def enrich_description(text) @description = "#{@description}\n\n#{text}" end |
#execute ⇒ Object
51 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 |
# File 'lib/rcrewai/task.rb', line 51 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.}" 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.}" 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.}" raise TaskExecutionError, "Task '#{name}' failed: #{e.}" 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
162 163 164 |
# File 'lib/rcrewai/task.rb', line 162 def failed? @status == :failed end |
#human_input_enabled? ⇒ Boolean
206 207 208 |
# File 'lib/rcrewai/task.rb', line 206 def human_input_enabled? @human_input_enabled end |
#pending? ⇒ Boolean
170 171 172 |
# File 'lib/rcrewai/task.rb', line 170 def pending? @status == :pending end |
#running? ⇒ Boolean
166 167 168 |
# File 'lib/rcrewai/task.rb', line 166 def running? @status == :running end |