Module: OllamaChat::FavouritesManagement

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

Overview

A module that handles favourite operations for OllamaChat.

This module allows users to mark specific entities as favourites and retrieve those favourites for easy access and display in the UI.

Instance Method Summary collapse

Instance Method Details

#add_favourite(type) ⇒ Object (private)

Note:

This method uses a chooser to present options and handles user input for adding new favourites to the database.

The add_favourite method adds a favourite item of a specified type. It iterates through available favourites and allows the user to select from items that haven't been favourited yet.

Parameters:

  • type (String)

    the context type for the favourite



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ollama_chat/favourites_management.rb', line 40

def add_favourite(type)
  all_things = favourite_all_things(type)
  choose_with_state do
    loop do
      selected = models::Favourite.where(context: type).map(&:name)
      to_select = all_things - selected
      if to_select.empty?
        STDOUT.puts "All items are already favourited."
        return
      end
      to_select.unshift('[EXIT]')
      case chosen = choose_entry(to_select, prompt: 'Select an item to mark as favourite: %s')
      when '[EXIT]', nil
        STDOUT.puts "Cancelled."
        return
      when SearchUI::Wrapper
        models::Favourite.create(context: type, name: chosen.value)
        log(:info, "Favourite added", data: { context: type, name: chosen.value })
      end
    end
  end
end

#all_favourited(type) ⇒ Hash (private)

The all_favourited method retrieves all favourited items of a specified type.

Parameters:

  • type (String)

    the type of favourited items to retrieve

Returns:

  • (Hash)

    a hash with favourited item names as keys and true as values



96
97
98
99
# File 'lib/ollama_chat/favourites_management.rb', line 96

def all_favourited(type)
  models::Favourite.where(context: type).
    each_with_object(Hash.new(false)) { |fav, h| h[fav.name] = true }
end

#delete_favourite(type) ⇒ Object (private)

Note:

This method uses a chooser to present options and handles user input for removing favourites from the database.

The delete_favourite method removes favourite items from the database. It iterates through current favourites and allows the user to select items for removal in a loop, maintaining search state between selections.

Parameters:

  • type (String)

    the context type of the favourite to delete



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ollama_chat/favourites_management.rb', line 71

def delete_favourite(type)
  all_things = favourite_all_things(type)
  choose_with_state do
    loop do
      to_select = models::Favourite.where(context: type).map(&:name)
      to_select = all_things.select { to_select.member?(_1.value) }
      to_select = [ '[EXIT]' ] + to_select
      case chosen = choose_entry(to_select, prompt: 'Select a favourite to remove: %s')
      when '[EXIT]', nil
        STDOUT.puts "Cancelled."
        return
      when SearchUI::Wrapper
        models::Favourite.where(context: type, name: chosen.value).destroy
        log(:info, "Favourite removed", data: { context: type, name: chosen.value })
      end
    end
  end
end

#favourite_all_things(type) ⇒ Array<SearchUI::Wrapper> (private)

Retrieves a UI-ready list of all available entities of a given type, decorated with a heart icon if they are marked as favourites.

Parameters:

  • type (String)

    the context type (e.g., 'model', 'prompt', 'system', 'persona')

Returns:

  • (Array<SearchUI::Wrapper>)

    a list of wrappers containing the original value and the decorated display string.



24
25
26
27
28
29
30
# File 'lib/ollama_chat/favourites_management.rb', line 24

def favourite_all_things(type)
  case type
  when 'model'         then all_models
  when 'persona'       then available_personae_names
  else                      all_prompts(context: type)
  end.to_a
end

#prefix_favourite(string, favourited) ⇒ String (private)

Prepend a heart icon to a string if it is marked as a favourite.

Parameters:

  • string (String)

    the string to decorate

  • favourited (Boolean)

    whether the item is a favourite

Returns:

  • (String)

    the decorated string



13
14
15
16
# File 'lib/ollama_chat/favourites_management.rb', line 13

def prefix_favourite(string, favourited)
  fav = favourited ? "\u2764\uFE0F" : '🩶'
  "%s %s" % [ fav, string ]
end