Class: PromptObjects::HumanRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/prompt_objects/human_queue.rb

Overview

Represents a pending request for human input

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = options
  @created_at = Time.now
  @response = nil
  @mutex = Mutex.new
  @condition = ConditionVariable.new
end

Instance Attribute Details

#capabilityObject (readonly)

Returns the value of attribute capability.



8
9
10
# File 'lib/prompt_objects/human_queue.rb', line 8

def capability
  @capability
end

#created_atObject (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

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/prompt_objects/human_queue.rb', line 8

def id
  @id
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/prompt_objects/human_queue.rb', line 8

def options
  @options
end

#questionObject (readonly)

Returns the value of attribute question.



8
9
10
# File 'lib/prompt_objects/human_queue.rb', line 8

def question
  @question
end

#responseObject

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

#ageObject



26
27
28
# File 'lib/prompt_objects/human_queue.rb', line 26

def age
  Time.now - @created_at
end

#age_stringObject



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

Returns:

  • (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_responseObject

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