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



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

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_voiceString (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.

Returns:

  • (String)

    the full name of the chosen voice



130
131
132
# File 'lib/ollama_chat/dialog.rb', line 130

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.

Parameters:

  • patterns (Array<String>)

    optional glob patterns to match; defaults to '**/*'.

Returns:

  • (Set<Pathname>)

    a set of expanded Pathname objects for each selected file.



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ollama_chat/dialog.rb', line 103

def choose_file_set(patterns)
  patterns ||= '**/*'
  patterns = Array(patterns).map { Pathname.new(_1).expand_path }
  files = Set[]
  choose_with_state do
    while filename = choose_filename(patterns, chosen: files)
      files << filename.expand_path
    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.

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 and the answer to

Returns:

  • (Object)

    the character entered by the user, the default value if a timeout occurs, or nil if the read is interrupted (e.g. Ctrl-C)



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
83
84
85
86
87
88
89
# File 'lib/ollama_chat/dialog.rb', line 44

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
  output.print prompt
  min    = 1
  time   = 0
  if timeout
    min  = 0
    time = timeout
  end
  exceptions = [ (IRB::Abort if defined? IRB), Interrupt ].compact
  begin
    keypress = STDIN.raw(min:, time:, intr: true) do |io|
      io.getc
    end
  rescue *exceptions
    output.puts "\u274C\uFE0F"
    return
  end
  answer = keypress || default
  case
  when yes.nil?
    if keypress
      output.puts "\u2328\uFE0F #{answer}"
    else
      output.puts "\u231B\uFE0F #{answer}"
    end
    answer
  when answer =~ yes
    if keypress
      output.puts "\u2705\uFE0F #{answer}"
    else
      output.puts "\u2611\uFE0F #{answer}"
    end
    answer
  else
    if keypress
      output.puts "\u{1F6AB} #{answer}"
    else
      output.puts "\u231B\uFE0F #{answer}"
    end
    nil
  end
end

#connect_message(model, base_url) ⇒ Object (private)

The connect_message method displays a connection status message.

Parameters:

  • model (String)

    the model name to connect to

  • base_url (String)

    the base URL of the connection



119
120
121
122
123
# File 'lib/ollama_chat/dialog.rb', line 119

def connect_message(model, base_url)
  msg = "Connecting to #{model}@#{base_url} now…"
  log(:info, msg, data: { model:, base_url: })
  STDOUT.puts green { msg }
end

#go_command(s, opt, defaults: {}) ⇒ Hash{String => Object} (private)

Parses and executes a command using Tins::GO.

Parameters:

  • s (String)

    The Tins::GO option pattern string where each character represents an option, and ':' indicates the option requires an argument.

  • opt (Object)

    The arguments to be parsed. This object is converted to a string, stripped of whitespace, and split into an array of strings.

Returns:

  • (Hash{String => Object})

    A hash mapping option names to their values.



149
150
151
# File 'lib/ollama_chat/dialog.rb', line 149

def go_command(s, opt, defaults: {})
  Tins::GO.go(s, opt.to_s.strip.split(/\s+/), defaults:)
end

#message_listMessageList (private)

The message_list method creates and returns a new MessageList instance initialized with the current object as its argument.

Returns:



138
139
140
# File 'lib/ollama_chat/dialog.rb', line 138

def message_list
  MessageList.new(self)
end