Module: OllamaChat::Commands

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

Overview

Namespace for all available slash commands within the Ollama Chat application.

This module acts as a central repository where various functional commands are declared using the DSL provided by CommandConcern. Each command definition includes a unique name, a triggering regular expression, and a help string for documentation.

The commands are categorized into several areas:

  • Clipboard management (/copy, /paste)
  • Application settings (/config, /document policy, /toggle)
  • Model & System Prompt configuration (/model, /system, /think)
  • Tooling support (/tools)
  • Session management (/session)
  • Conversation history and manipulation (/list, /last, /drop, /clear, /regenerate)
  • RAG collection management (/collection)
  • Persona & Character roleplay (/persona, /character)
  • Input/Output operations (/compose, /web, /input, /pipe, /vim, /output)
  • System actions and info (/reconnect, /quit, /info, /help)

Instance Method Summary collapse

Instance Method Details

#collectionObject

Collection



632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
# File 'lib/ollama_chat/commands.rb', line 632

command(
  name: :collection,
  regexp: %r(^/collection(?:\s+(change|clear|list|rename|update(?: all)?|new|edit|delete))?$),
  complete: [ 'collection', %w[ change clear list rename update update\ all new edit delete ] ],
  optional: true,
  help: <<~EOT
    📚 Manage RAG collections:
       - change/clear/list/rename
       - update: Re-index modified docs
       - update all: Re-index all collections
       - new: Interactively create a new collection
       - edit: Interactively update an existing collection
       - delete: Permanently remove a collection
       - (no subcommand): Show stats
  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 all'
    results = ''
    all_collections.pluck(:name).each do |collection|
      STDOUT.puts "📝 Updating collection #{collection.inspect}"
      results << update_collection(collection) << ?\n
      STDOUT.puts "✅ Done."
    end
    results.full? and next results
  when 'update'
    if results = update_collection(collection)
      disable_content_parsing
      next results
    end
  when 'new'
    create_collection
  when 'edit'
    edit_collection
  when 'delete'
    delete_collection
  when nil
    collection_stats
  end
  :next
end

#composeObject

Input



806
807
808
809
810
811
812
813
814
# File 'lib/ollama_chat/commands.rb', line 806

command(
  name: :compose,
  regexp: %r(^/compose$),
  help: <<~EOT
    \u270D\uFE0F  Compose message in external editor
  EOT
) do
  edit_text.full? or :next
end

#configObject

Settings



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ollama_chat/commands.rb', line 63

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

#copyObject

Clipboard



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ollama_chat/commands.rb', line 27

command(
  name: :copy,
  regexp: %r(^/copy(\s+-e)?\s*$),
  options: '[-e]',
  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



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/ollama_chat/commands.rb', line 374

command(
  name: :list,
  regexp: %r(^/list((?:\s+(?:-[ts]))*)(?:\s+(\d*))?$),
  options: '[-t|-s|n=1]',
  help: <<~EOT
    📜 List conversation history
       Options: -t (show thinking), -s (hide)
  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



941
942
943
944
945
946
947
948
949
950
951
952
953
# File 'lib/ollama_chat/commands.rb', line 941

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

#reconnectObject

Actions



988
989
990
991
992
993
994
995
996
997
998
999
# File 'lib/ollama_chat/commands.rb', line 988

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

#sessionObject

Session



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/ollama_chat/commands.rb', line 288

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]\n[name]",
  help: <<~EOT
    💬 Manage sessions:
       - list/new/delete/rename/duplicate
       - change [name]/previous
       - summarize (-s sentence, -f save to file)
       - model options/change
  EOT
) do |subcommand, opts, name|
  case subcommand
  when nil
    info_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 = switch_history(: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