Class: Kward::RPC::PromptBridge
- Inherits:
-
Object
- Object
- Kward::RPC::PromptBridge
- Defined in:
- lib/kward/rpc/prompt_bridge.rb
Overview
RPC prompt bridge for structured user questions.
Instance Method Summary collapse
- #answer(request_id, answers) ⇒ Object
- #answer_tool_approval(request_id, approved:) ⇒ Object
- #ask_user_question(questions, cancellation: nil) ⇒ Object
- #cancel_request(request_id) ⇒ Object
-
#initialize(server:, session_id:) ⇒ PromptBridge
constructor
A new instance of PromptBridge.
- #request_tool_approval(tool_call_id:, tool_name:, args:, cancellation: nil) ⇒ Object
Constructor Details
#initialize(server:, session_id:) ⇒ PromptBridge
Returns a new instance of PromptBridge.
11 12 13 14 15 16 17 18 |
# File 'lib/kward/rpc/prompt_bridge.rb', line 11 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
69 70 71 |
# File 'lib/kward/rpc/prompt_bridge.rb', line 69 def answer(request_id, answers) answer_raw(request_id, normalize_answers(answers)) end |
#answer_tool_approval(request_id, approved:) ⇒ Object
42 43 44 |
# File 'lib/kward/rpc/prompt_bridge.rb', line 42 def answer_tool_approval(request_id, approved:) answer_raw(request_id, !!approved) end |
#ask_user_question(questions, cancellation: nil) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/kward/rpc/prompt_bridge.rb', line 46 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
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/kward/rpc/prompt_bridge.rb', line 73 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 |
#request_tool_approval(tool_call_id:, tool_name:, args:, cancellation: nil) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/kward/rpc/prompt_bridge.rb', line 20 def request_tool_approval(tool_call_id:, tool_name:, args:, cancellation: nil) request_id = SecureRandom.uuid @mutex.synchronize { @pending_requests[request_id] = true } cancellation&.on_cancel { cancel_request(request_id) } unless cancellation&.cancelled? @server.notify("tool/approvalRequested", { sessionId: @session_id, approvalRequestId: request_id, toolCallId: tool_call_id, toolName: tool_name, args: args }) end @mutex.synchronize do @condition.wait(@mutex) until @answers.key?(request_id) answer = @answers.delete(request_id) @pending_requests.delete(request_id) approved_answer?(answer) end end |