Class: PromptObjects::HumanRequest
- Inherits:
-
Object
- Object
- PromptObjects::HumanRequest
- Defined in:
- lib/prompt_objects/human_queue.rb
Overview
Represents a pending request for human input
Instance Attribute Summary collapse
-
#capability ⇒ Object
readonly
Returns the value of attribute capability.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#question ⇒ Object
readonly
Returns the value of attribute question.
-
#response ⇒ Object
Returns the value of attribute response.
Instance Method Summary collapse
- #age ⇒ Object
- #age_string ⇒ Object
-
#initialize(capability:, question:, options: nil) ⇒ HumanRequest
constructor
A new instance of HumanRequest.
- #pending? ⇒ Boolean
-
#respond!(value) ⇒ Object
Called by the UI thread when human responds.
-
#wait_for_response ⇒ Object
Called by the background thread to wait for response.
Constructor Details
#initialize(capability:, question:, options: nil) ⇒ HumanRequest
Returns a new instance of HumanRequest.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/prompt_objects/human_queue.rb', line 11 def initialize(capability:, question:, options: nil) @id = SecureRandom.uuid @capability = capability @question = question @options = @created_at = Time.now @response = nil @mutex = Mutex.new @condition = ConditionVariable.new end |
Instance Attribute Details
#capability ⇒ Object (readonly)
Returns the value of attribute capability.
8 9 10 |
# File 'lib/prompt_objects/human_queue.rb', line 8 def capability @capability end |
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
8 9 10 |
# File 'lib/prompt_objects/human_queue.rb', line 8 def created_at @created_at end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
8 9 10 |
# File 'lib/prompt_objects/human_queue.rb', line 8 def id @id end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/prompt_objects/human_queue.rb', line 8 def @options end |
#question ⇒ Object (readonly)
Returns the value of attribute question.
8 9 10 |
# File 'lib/prompt_objects/human_queue.rb', line 8 def question @question end |
#response ⇒ Object
Returns the value of attribute response.
9 10 11 |
# File 'lib/prompt_objects/human_queue.rb', line 9 def response @response end |
Instance Method Details
#age ⇒ Object
26 27 28 |
# File 'lib/prompt_objects/human_queue.rb', line 26 def age Time.now - @created_at end |
#age_string ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/prompt_objects/human_queue.rb', line 30 def age_string seconds = age.to_i if seconds < 60 "#{seconds}s" elsif seconds < 3600 "#{seconds / 60}m" else "#{seconds / 3600}h" end end |
#pending? ⇒ Boolean
22 23 24 |
# File 'lib/prompt_objects/human_queue.rb', line 22 def pending? @response.nil? end |
#respond!(value) ⇒ Object
Called by the UI thread when human responds
50 51 52 53 54 55 |
# File 'lib/prompt_objects/human_queue.rb', line 50 def respond!(value) @mutex.synchronize do @response = value @condition.broadcast end end |
#wait_for_response ⇒ Object
Called by the background thread to wait for response
42 43 44 45 46 47 |
# File 'lib/prompt_objects/human_queue.rb', line 42 def wait_for_response @mutex.synchronize do @condition.wait(@mutex) while @response.nil? @response end end |