Class: PromptObjects::HumanQueue

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

Overview

Queue for managing pending human requests across all POs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHumanQueue

Returns a new instance of HumanQueue.



62
63
64
65
66
# File 'lib/prompt_objects/human_queue.rb', line 62

def initialize
  @pending = []
  @subscribers = []
  @mutex = Mutex.new
end

Instance Attribute Details

#pendingObject (readonly)

Returns the value of attribute pending.



60
61
62
# File 'lib/prompt_objects/human_queue.rb', line 60

def pending
  @pending
end

Instance Method Details

#all_pendingObject

Get all pending requests (thread-safe copy)



129
130
131
# File 'lib/prompt_objects/human_queue.rb', line 129

def all_pending
  @mutex.synchronize { @pending.dup }
end

#countObject

Total pending count



114
115
116
# File 'lib/prompt_objects/human_queue.rb', line 114

def count
  @mutex.synchronize { @pending.length }
end

#enqueue(capability:, question:, options: nil) ⇒ Object

Add a request to the queue Returns the HumanRequest object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/prompt_objects/human_queue.rb', line 70

def enqueue(capability:, question:, options: nil)
  request = HumanRequest.new(
    capability: capability,
    question: question,
    options: options
  )

  @mutex.synchronize do
    @pending << request
  end

  notify_subscribers(:added, request)
  request
end

#pending_countsObject

Get count of pending requests per capability



107
108
109
110
111
# File 'lib/prompt_objects/human_queue.rb', line 107

def pending_counts
  @mutex.synchronize do
    @pending.group_by(&:capability).transform_values(&:count)
  end
end

#pending_for(capability_name) ⇒ Object

Get pending requests for a specific capability



100
101
102
103
104
# File 'lib/prompt_objects/human_queue.rb', line 100

def pending_for(capability_name)
  @mutex.synchronize do
    @pending.select { |r| r.capability == capability_name }
  end
end

#respond(request_id, value) ⇒ Object

Respond to a pending request by ID



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/prompt_objects/human_queue.rb', line 86

def respond(request_id, value)
  request = nil
  @mutex.synchronize do
    request = @pending.find { |r| r.id == request_id }
    return unless request

    @pending.delete(request)
  end

  notify_subscribers(:resolved, request)
  request.respond!(value)
end

#subscribe(&block) ⇒ Object

Subscribe to queue events Callback receives (event, request) where event is :added or :resolved



120
121
122
# File 'lib/prompt_objects/human_queue.rb', line 120

def subscribe(&block)
  @subscribers << block
end

#unsubscribe(block) ⇒ Object



124
125
126
# File 'lib/prompt_objects/human_queue.rb', line 124

def unsubscribe(block)
  @subscribers.delete(block)
end