Module: OllamaChat::Dialog

Included in:
Chat
Defined in:
lib/ollama_chat/dialog.rb

Overview

A module that provides interactive selection and configuration functionality for OllamaChat.

The Dialog module encapsulates various helper methods for choosing models, system prompts, document policies, and voices, as well as displaying information and managing chat sessions. It leverages user interaction components like choosers and prompts to enable dynamic configuration during runtime.

Instance Method Summary collapse

Instance Method Details

#ask?(prompt:, prefill: nil) ⇒ String

The ask? method prompts the user with a question and returns their input.

Parameters:

  • prompt (String)

    the message to display to the user

Returns:

  • (String)

    the user’s response with trailing newline removed



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ollama_chat/dialog.rb', line 15

def ask?(prompt:, prefill: nil)
  if prefill
    old_pre_input_hook = Reline.pre_input_hook
    Reline.pre_input_hook = -> { Reline.insert_text prefill.to_s }
  end
  Reline.readline(prompt, true)&.chomp
rescue Interrupt
  return nil
ensure
  prefill and Reline.pre_input_hook = old_pre_input_hook
end

#confirm?(prompt:, timeout: nil, default: nil, yes: nil, output: STDOUT) ⇒ Object

The confirm? method displays a prompt and reads a single character input from the user in raw mode, then returns that character. This is best used for confirmation prompts.

Parameters:

  • prompt (String)

    the prompt to display to the user

  • timeout (Integer, nil) (defaults to: nil)

    optional timeout in seconds; if nil, the method blocks until input, if 0 the method immediately returns the default value.

  • default (Object, nil) (defaults to: nil)

    value returned when the timeout expires (defaults to ‘nil`)

  • yes (Object, nil) (defaults to: nil)

    value that is considered a positive response

  • output (IO) (defaults to: STDOUT)

    the IO object to write the prompt to

Returns:

  • (Object)

    the character entered by the user, or the ‘default` value if a timeout occurs



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ollama_chat/dialog.rb', line 42

def confirm?(prompt:, timeout: nil, default: nil, yes: nil, output: STDOUT)
  return default if timeout&.zero?
  if prompt.include?('%s')
    prompt = prompt % (timeout ? ('timeout in %us' % timeout) : 'no timeout')
  end
  print prompt
  system 'stty raw'
  keypress = nil
  c = if timeout
        keypress = !!IO.select([ STDIN ], nil, nil, timeout)
        keypress ? STDIN.getc : nil
      else
        keypress = true
        STDIN.getc
      end
  system 'stty cooked'
  answer = c || default
  case
  when yes.nil?
    if keypress
      output.puts "⌨️ #{answer}"
    else
      output.puts "⌛️ #{answer}"
    end
    answer
  when answer =~ yes
    if keypress
      output.puts "#{answer}"
    else
      output.puts "☑️  #{answer}"
    end
    answer
  else
    if keypress
      output.puts "🚫 #{answer}"
    else
      output.puts "⌛️ #{answer}"
    end
    nil
  end
end