Module: OllamaChat::History
- Included in:
- Chat
- Defined in:
- lib/ollama_chat/history.rb
Overview
A module that provides history management functionality for OllamaChat sessions.
The History module encapsulates methods for initializing, saving, and clearing command-line history within the OllamaChat application. It handles persistence of user input history to a file and ensures that chat sessions can maintain state across invocations by loading previous command histories.
Instance Method Summary collapse
-
#clear_history ⇒ Object
private
The clear_history method clears the Readline history array and ensures that the chat history is saved afterwards.
-
#init_chat_history ⇒ Object
private
The init_chat_history method initializes the chat session by loading previously saved command history from a file.
-
#save_history ⇒ Object
private
The save_history method persists the current command history to a file.
Instance Method Details
#clear_history ⇒ Object (private)
The clear_history method clears the Readline history array and ensures that the chat history is saved afterwards.
This method removes all entries from the Readline::HISTORY array, effectively clearing the command history maintained by the readline library. It then calls save_history to persist this cleared state to the configured history file. The method uses an ensure block to guarantee that save_history is called even if an exception occurs during the clearing process.
61 62 63 64 65 |
# File 'lib/ollama_chat/history.rb', line 61 def clear_history Readline::HISTORY.clear ensure save_history end |
#init_chat_history ⇒ Object (private)
The init_chat_history method initializes the chat session by loading previously saved command history from a file.
This method checks for the existence of a chat history file and, if found, loads its contents into the Readline::HISTORY array. It clears the current history and replaces it with the saved history data. Any errors during the loading process are caught and logged as warnings, but do not interrupt the execution flow.
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/ollama_chat/history.rb', line 28 def init_chat_history if OC::OLLAMA::CHAT::HISTORY.exist? history = OC::OLLAMA::CHAT::HISTORY.read history_data = JSON.load(history) Readline::HISTORY.clear Readline::HISTORY.push(*history_data) end rescue => e msg = "Caught #{e.class} while loading #{OC::OLLAMA::CHAT::HISTORY.inspect}: #{e}" log(:error, msg, warn: true) end |
#save_history ⇒ Object (private)
The save_history method persists the current command history to a file.
This method serializes the Readline::HISTORY array into JSON format and writes it to the chat history filename. It handles potential errors during the write operation by catching exceptions and issuing a warning message.
45 46 47 48 49 50 |
# File 'lib/ollama_chat/history.rb', line 45 def save_history File.secure_write(OC::OLLAMA::CHAT::HISTORY, JSON.dump(Readline::HISTORY)) rescue => e msg = "Caught #{e.class} while saving #{OC::OLLAMA::CHAT::HISTORY.inspect}: #{e}" log(:error, msg, warn: true) end |