Class: Kward::RPC::PromptBridge
- Inherits:
-
Object
- Object
- Kward::RPC::PromptBridge
- Defined in:
- lib/kward/rpc/prompt_bridge.rb
Constant Summary collapse
- MIN_QUESTIONS =
1- MAX_QUESTIONS =
4- MIN_OPTIONS =
2- MAX_OPTIONS =
4
Instance Method Summary collapse
- #answer(request_id, answers) ⇒ Object
- #ask_user_question(questions, cancellation: nil) ⇒ Object
- #cancel_request(request_id) ⇒ Object
-
#initialize(server:, session_id:) ⇒ PromptBridge
constructor
A new instance of PromptBridge.
Constructor Details
#initialize(server:, session_id:) ⇒ PromptBridge
Returns a new instance of PromptBridge.
12 13 14 15 16 17 18 19 |
# File 'lib/kward/rpc/prompt_bridge.rb', line 12 def initialize(server:, session_id:) @server = server @session_id = session_id @mutex = Mutex.new @condition = ConditionVariable.new @answers = {} @pending_requests = {} end |
Instance Method Details
#answer(request_id, answers) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/kward/rpc/prompt_bridge.rb', line 44 def answer(request_id, answers) @mutex.synchronize do request_id = request_id.to_s return unless @pending_requests.key?(request_id) return if @answers.key?(request_id) @answers[request_id] = normalize_answers(answers) @condition.broadcast end end |
#ask_user_question(questions, cancellation: nil) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/kward/rpc/prompt_bridge.rb', line 21 def ask_user_question(questions, cancellation: nil) questions = validate_questions(questions) request_id = SecureRandom.uuid @mutex.synchronize { @pending_requests[request_id] = true } cancellation&.on_cancel { cancel_request(request_id) } unless cancellation&.cancelled? @server.notify("ui/question", { sessionId: @session_id, questionRequestId: request_id, questions: questions }) end @mutex.synchronize do @condition.wait(@mutex) until @answers.key?(request_id) answer = @answers.delete(request_id) @pending_requests.delete(request_id) return nil if answer.nil? answer end end |
#cancel_request(request_id) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/kward/rpc/prompt_bridge.rb', line 55 def cancel_request(request_id) @mutex.synchronize do request_id = request_id.to_s return unless @pending_requests.key?(request_id) return if @answers.key?(request_id) @answers[request_id] = nil @condition.broadcast end end |