Class: MyAsk::PromptHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/myask/prompt_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(global_options, options, args) ⇒ PromptHelper

Returns a new instance of PromptHelper.



3
4
5
6
7
# File 'lib/myask/prompt_helper.rb', line 3

def initialize(global_options, options, args)
  @global_options = global_options
  @options = options
  @args = args
end

Instance Method Details

#get_initial_questionObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/myask/prompt_helper.rb', line 59

def get_initial_question
  file_contents = []

  current_directory = Dir.pwd

  if @options[:input_file]&.any?
    @options[:input_file].each do |file_path|
      begin
        file_path = file_path[0] == "/" ? file_path : "#{current_directory}/#{file_path}"
        content = File.read(file_path).strip
        file_contents << "**File**: #{file_path}**\n#{content}"
      rescue => e
        raise "❌ Failed to read input file: #{file_path}"
      end
    end
  end

  combined_question = "#{@options[:prompt]}\n\n---\n#{file_contents.join("\n\n---\n")}" unless file_contents.empty?
  combined_question ||= @options[:prompt] || TTY::Prompt.new.ask('Please enter a prompt:').strip

  combined_question.empty? ? nil : combined_question
end

#poll_for_response(api_host, auth_token, question_id) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/myask/prompt_helper.rb', line 32

def poll_for_response(api_host, auth_token, question_id)
  unless @global_options[:silent]
    spinner = TTY::Spinner.new('🔄 Waiting for response from MyAsk API... [:spinner]', format: :dots)
    spinner.auto_spin
  end

  max_retries = 50
  attempt = 0

  while attempt < max_retries
    sleep 3
    uri = URI("#{api_host}/api/responses/#{question_id}")
    response = Net::HTTP.get(uri, { 'Authorization' => "Bearer #{auth_token}" })
    parsed_response = JSON.parse(response)

    if parsed_response.key?('content')
      spinner.success('✅ Response received!') unless @global_options[:silent]
      return parsed_response['content']
    end

    attempt += 1
  end

  spinner.error('❌ Timed out waiting for MyAsk API response.') unless @global_options[:silent]
  raise 'No response received from MyAsk API after multiple attempts.'
end

#submit_question(api_host, auth_token, project_id, content, context_ids) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/myask/prompt_helper.rb', line 9

def submit_question(api_host, auth_token, project_id, content, context_ids)
  uri = URI("#{api_host}/api/questions")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = (uri.scheme == "https")

  request = Net::HTTP::Post.new(uri.path)
  request["Authorization"] = "Bearer #{auth_token}"
  request["Content-Type"] = "application/json"

  request.body = {
    question: {
      project_id: project_id,
      content: content,
      context_ids: context_ids
    }
  }.to_json

  response = http.request(request)
  parsed_response = JSON.parse(response.body)

  parsed_response['id'] || nil
end