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
-
#ask?(prompt:, prefill: nil) ⇒ String
The ask? method prompts the user with a question and returns their input.
-
#change_voice ⇒ String
private
The change_voice method allows the user to select a voice from a list of available options.
-
#choose_file_set(patterns) ⇒ Set<Pathname>
private
The choose_file_set method aggregates all files matching the given patterns by repeatedly invoking choose_filename and collecting their expanded paths into a Set.
-
#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.
-
#connect_message(model, base_url) ⇒ Object
private
The connect_message method displays a connection status message.
-
#go_command(s, opt) ⇒ Hash{String => Object}
private
Parses and executes a command using Tins::GO.
-
#message_list ⇒ MessageList
private
The message_list method creates and returns a new MessageList instance initialized with the current object as its argument.
Instance Method Details
#ask?(prompt:, prefill: nil) ⇒ String
The ask? method prompts the user with a question and returns their input.
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 |
#change_voice ⇒ String (private)
The change_voice method allows the user to select a voice from a list of available options. It uses the chooser to present the options and sets the selected voice as the current voice.
123 124 125 |
# File 'lib/ollama_chat/dialog.rb', line 123 def change_voice voices.choose end |
#choose_file_set(patterns) ⇒ Set<Pathname> (private)
The choose_file_set method aggregates all files matching the given patterns by repeatedly invoking choose_filename and collecting their expanded paths into a Set.
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/ollama_chat/dialog.rb', line 96 def choose_file_set(patterns) patterns ||= '**/*' patterns = Array(patterns).map { Pathname.new(_1). } files = Set[] choose_with_state do while filename = choose_filename(patterns, chosen: files) files << filename. end end files 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.
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 |
#connect_message(model, base_url) ⇒ Object (private)
The connect_message method displays a connection status message.
112 113 114 115 116 |
# File 'lib/ollama_chat/dialog.rb', line 112 def (model, base_url) msg = "Connecting to #{model}@#{base_url} now…" log(:info, msg) STDOUT.puts green { msg } end |
#go_command(s, opt) ⇒ Hash{String => Object} (private)
Parses and executes a command using Tins::GO.
142 143 144 |
# File 'lib/ollama_chat/dialog.rb', line 142 def go_command(s, opt) Tins::GO.go(s, opt.to_s.strip.split(/\s+/)) end |
#message_list ⇒ MessageList (private)
The message_list method creates and returns a new MessageList instance initialized with the current object as its argument.
131 132 133 |
# File 'lib/ollama_chat/dialog.rb', line 131 def MessageList.new(self) end |