Module: OllamaChat::Commands

Includes:
CommandConcern
Included in:
Chat
Defined in:
lib/ollama_chat/commands.rb

Instance Method Summary collapse

Instance Method Details

#collectionObject

Collection



485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/ollama_chat/commands.rb', line 485

command(
  name: :collection,
  regexp: %r(^/collection(?:\s+(change|clear|list|rename|update))?$),
  complete: [ 'collection', %w[ change clear list rename update ] ],
  optional: true,
  help: <<~EOT
    Manage the current RAG document collection: change, clear, list,
    rename, update and show
  EOT
) do |subcommand|
  case subcommand
  when 'clear'
    clear_collection
  when 'change'
    choose_collection(collection)
  when 'list'
    list_collections
  when 'rename'
    rename_collection(collection)
  when 'update'
    results = update_collection and next results
  when nil
    collection_stats
  end
  :next
end

#composeObject

Input



628
629
630
631
632
633
634
# File 'lib/ollama_chat/commands.rb', line 628

command(
  name: :compose,
  regexp: %r(^/compose$),
  help: 'Compose a message using the text editor'
) do
  edit_text.full? or :next
end

#configObject

Settings



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ollama_chat/commands.rb', line 36

command(
  name: :config,
  regexp: %r(^/config(?:\s+(edit|reload))?$),
  complete: [ 'config', %w[ edit reload ] ],
  optional: true,
  help: 'View, edit, or reload configuration'
) do |subcommand|
  case subcommand
  when 'edit'
    edit_config
  when 'reload'
    reload_config
  else
    display_config
  end
  :next
end

#copyObject

Clipboard



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ollama_chat/commands.rb', line 8

command(
  name: :copy,
  regexp: %r(^/copy(\s+-e)?\s*$),
  help: <<~EOT
      Copy the last response to the clipboard.
       Options: -e to edit before copying.
  EOT
) do |opts|
  opts = go_command('e', opts)
  copy_to_clipboard(edit: opts[?e])
  :next
end

#listObject

Conversation



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/ollama_chat/commands.rb', line 303

command(
  name: :list,
  regexp: %r(^/list((?:\s+(?:-[ts]))*)(?:\s+(\d*))?$),
  options: '[-t|-s|n=1]',
  help: <<~EOT
    List the last n or all conversation exchanges.
    Options: -t (force show thinking), -s (suppress thinking).
  EOT
) do |opts,number|
  opts = go_command('ts', opts.to_s)
  n    = 2 * number.to_i if number
  think_loud = if opts[?t]
                 true
               elsif opts[?s]
                 false
               else
                 self.think_loud.on?
               end
  messages.list_conversation(n, think_loud:)
  :next
end

#pipeObject

Output



757
758
759
760
761
762
763
764
765
766
767
768
769
# File 'lib/ollama_chat/commands.rb', line 757

command(
  name: :pipe,
  regexp: %r(^/pipe(\s+-e)?\s+(.+)$),
  options: 'path',
  help: <<~EOT
   Pipe the last response into another command's stdin.
    Options: -e to edit before piping.
  EOT
) do |opts, command|
  opts = go_command('e', opts)
  pipe(command, edit: opts[?e])
  :next
end

#reconnectObject

Actions



800
801
802
803
804
805
806
807
808
809
# File 'lib/ollama_chat/commands.rb', line 800

command(
  name: :reconnect,
  regexp: %r(^/reconnect$),
  help: 'Reconnect to the Ollama server'
) do
  STDERR.print green { "Reconnecting to ollama #{base_url.to_s.inspect}" }
  connect_ollama
  STDERR.puts green { " Done." }
  :next
end

#sessionObject

Session



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/ollama_chat/commands.rb', line 221

command(
  name: :session,
  regexp: %r(^/session(?:\s+(change|previous|list|new|duplicate|rename|summarize|delete|model options change|model options))?((?:\s+-(?:[sf]|p\s*\w+))*)(?:\s+(.+))?$),
  complete: [ 'session', %w[ change previous list new duplicate rename summarize delete model\ options\ change model\ options ] ],
  optional: true,
  options: '[-s|-f|-p profile] [name]',
  help: <<~EOT
    Manage chat sessions (change, previous, list, new, duplicate, rename, summarize,
    delete, model options).
    For summarize: -s (single sentence), -f (output to markdown file)
  EOT
) do |subcommand, opts, name|
  case subcommand
  when nil
    show_session
  when 'list'
    list_sessions
  when 'new'
    set_new_session
  when 'duplicate'
    duplicate_session
  when 'delete'
    delete_session
  when 'rename'
    rename_session
  when 'summarize'
    opts = go_command('fs', opts)
    if opts[?f] and
        filename = ask?(prompt: "❓ Enter filename: ").full? { Pathname.new(_1) }
      then
      if filename.exist? && !confirm?(
          prompt: "🔔 File #{filename.to_s.inspect} already exists, overwrite? (y/n) ",
          yes: /\Ay/i
        )
      then
        STDERR.puts "File not written!"
        next :next
      end
      summary = summarize_session(pretty: true, sentence: opts[?s]) do |content|
        infobar.puts kramdown_ansi_parse(content)
      end
      if summary.full?
        filename.write(summary)
        STDOUT.puts "File successfully written."
        next :next
      else
        STDERR.puts "Nothing to summarize!"
        next :next
      end
    end
    summary = summarize_session(pretty: true, sentence: opts[?s]) do |content|
      infobar.puts kramdown_ansi_parse(content) << ?\n
    end
    if summary.full?
      use_pager do |output|
        output.puts kramdown_ansi_parse(summary)
      end
    else
      STDERR.puts "Nothing to summarize!"
      next :next
    end
  when 'change'
    change_session(name)
  when 'model options'
    edit_session_model_options
  when 'model options change'
    opts = go_command('p:', opts)
    if profile = opts[?p] || choose_profile_for_model(@model)
      copy_model_options_to_session(profile:)
    end
  when 'previous'
    if prev = previous_session
      change_session(prev.id)
    else
      STDOUT.puts "No previous session defined."
    end
  end
  :next
end