Module: OllamaChat::Conversation
- Included in:
- Chat
- Defined in:
- lib/ollama_chat/conversation.rb
Overview
A module that provides conversation persistence functionality for the OllamaChat::Chat class.
This module encapsulates the logic for saving and loading chat conversations
to/from JSON files. It delegates the actual file operations to the messages
object, which is expected to respond to save_conversation and
load_conversation methods.
Instance Method Summary collapse
-
#conversation_clean ⇒ Object
private
Selectively cleans parts of the current conversation.
-
#load_conversation(filename) ⇒ Object
private
Loads a conversation from a JSON file and replaces the current message history.
-
#save_conversation(filename, clean: false) ⇒ Object
private
Saves the current conversation to a JSON file.
Instance Method Details
#conversation_clean ⇒ Object (private)
Selectively cleans parts of the current conversation.
Presents an accumulating chooser of cleanable targets: tool content, images, thinking, messages, history, links. The user picks one or more (or [ALL]), confirms, and the selected items are cleared.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ollama_chat/conversation.rb', line 73 def conversation_clean = %w[ tools images thinking messages history links ] selected = Set.new choose_with_state do loop do remaining = - selected.to_a entries = (remaining.empty? ? [] : ['[ALL]'] + remaining) + ['[DONE]'] choice = choose_entry(entries, prompt: 'What to clean from conversation? %s') case choice when nil, '[DONE]' break when '[ALL]' selected.merge(remaining) break else selected << choice end end end return feedback('Cancelled, nothing selected.', type: :cancel) if selected.empty? what = selected.map { |s| bold{s} }.to_a unless confirm?( prompt: "🔔 Clean #{what * ', '} from conversation? (y/n) ", yes: /\Ay/i ) return feedback('Denied.', type: :denied) end = %w[ tools images thinking ].select { selected.include?(_1) } .clear if selected.include?('messages') .(what: .map(&:to_sym)) unless .empty? clear_history if selected.include?('history') links.clear if selected.include?('links') session_sync feedback("Cleaned #{what * ', '} from conversation.", type: :info) end |
#load_conversation(filename) ⇒ Object (private)
Loads a conversation from a JSON file and replaces the current message history.
This method delegates to the messages object's load_conversation
method, which handles deserialization of messages from JSON format. After
loading, if there are more than one message, it lists the last two messages
for confirmation.
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ollama_chat/conversation.rb', line 54 def load_conversation(filename) success = .load_conversation(filename) if .size > 1 .list_conversation(2) end if success feedback("Loaded conversation from #{filename.to_s.inspect}.", type: :info) else feedback("Loading conversation from "\ "#{filename.to_s.inspect} failed.", type: :warn) end end |
#save_conversation(filename, clean: false) ⇒ Object (private)
Saves the current conversation to a JSON file.
This method delegates to the messages object's save_conversation
method, which handles the actual serialization of messages into JSON
format.
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ollama_chat/conversation.rb', line 28 def save_conversation(filename, clean: false) cleaned = 'cleaned ' if clean filename = Pathname.new(filename) should_overwrite?(filename) or return = clean ? self.. : self.. if self..save_conversation(filename, messages:) feedback("Saved #{cleaned}conversation to #{filename.to_s.inspect}.", type: :info) else feedback("Saving #{cleaned}conversation to "\ "#{filename.to_s.inspect} failed.", type: :warn) end end |