Class: Kward::Tools::AskUserQuestion

Inherits:
Base
  • Object
show all
Defined in:
lib/kward/tools/ask_user_question.rb

Overview

Tool wrapper for structured clarification questions to the user.

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#schema

Constructor Details

#initialize(prompt:) ⇒ AskUserQuestion

Builds the tool schema and stores the execution dependency.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/kward/tools/ask_user_question.rb', line 11

def initialize(prompt:)
  @prompt = prompt
  super(
    "ask_user_question",
    "Ask the user one to four structured clarification questions in interactive mode. Supports single-select choices and custom typed answers.",
    properties: {
      questions: {
        type: "array",
        minItems: 1,
        maxItems: 4,
        items: {
          type: "object",
          properties: {
            question: { type: "string", description: "The question to ask." },
            header: { type: "string", description: "Short label shown in the overlay." },
            options: {
              type: "array",
              minItems: 2,
              maxItems: 4,
              items: {
                type: "object",
                properties: {
                  label: { type: "string", description: "Choice label." },
                  description: { type: "string", description: "Choice explanation." }
                },
                required: ["label", "description"],
                additionalProperties: false
              }
            }
          },
          required: ["question", "header", "options"],
          additionalProperties: false
        }
      }
    },
    required: ["questions"]
  )
end

Instance Method Details

#call(args, _conversation, cancellation: nil) ⇒ Object

Executes the tool and returns model-facing output text.



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/kward/tools/ask_user_question.rb', line 51

def call(args, _conversation, cancellation: nil)
  return "Error: ask_user_question requires interactive prompt support." unless @prompt.respond_to?(:ask_user_question)

  questions = validated_questions(args)
  return questions if questions.is_a?(String)

  answers = prompt_ask_user_question(questions, cancellation: cancellation)
  return "Cancelled." if answers.nil?

  answers.map { |answer| "#{answer[:question]}: #{answer[:answer]}" }.join("\n")
end