Class: Kodo::Memory::Store
- Inherits:
-
Object
- Object
- Kodo::Memory::Store
- Defined in:
- lib/kodo/memory/store.rb
Constant Summary collapse
- MAX_CONTEXT_MESSAGES =
Keep last N messages per conversation
50
Instance Method Summary collapse
-
#append(chat_id, role:, content:) ⇒ Object
Append a message to a conversation.
-
#clear(chat_id) ⇒ Object
Clear a conversation.
-
#conversation(chat_id) ⇒ Object
Get conversation history in LLM-ready format Returns Array of "user"|"assistant", content: String.
-
#initialize(passphrase: nil) ⇒ Store
constructor
A new instance of Store.
Constructor Details
#initialize(passphrase: nil) ⇒ Store
Returns a new instance of Store.
11 12 13 14 15 16 |
# File 'lib/kodo/memory/store.rb', line 11 def initialize(passphrase: nil) @conversations = {} # chat_id => Array<Hash> @conversations_dir = File.join(Kodo.home_dir, "memory", "conversations") @passphrase = passphrase FileUtils.mkdir_p(@conversations_dir) end |
Instance Method Details
#append(chat_id, role:, content:) ⇒ Object
Append a message to a conversation
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/kodo/memory/store.rb', line 19 def append(chat_id, role:, content:) chat_id = chat_id.to_s @conversations[chat_id] ||= load_conversation(chat_id) @conversations[chat_id] << { "role" => role, "content" => content, "timestamp" => Time.now.iso8601 } # Trim to max context window if @conversations[chat_id].length > MAX_CONTEXT_MESSAGES @conversations[chat_id] = @conversations[chat_id].last(MAX_CONTEXT_MESSAGES) end save_conversation(chat_id) end |
#clear(chat_id) ⇒ Object
Clear a conversation
47 48 49 50 51 52 |
# File 'lib/kodo/memory/store.rb', line 47 def clear(chat_id) chat_id = chat_id.to_s @conversations.delete(chat_id) path = conversation_path(chat_id) File.delete(path) if File.exist?(path) end |
#conversation(chat_id) ⇒ Object
Get conversation history in LLM-ready format Returns Array of "user"|"assistant", content: String
38 39 40 41 42 43 44 |
# File 'lib/kodo/memory/store.rb', line 38 def conversation(chat_id) chat_id = chat_id.to_s @conversations[chat_id] ||= load_conversation(chat_id) @conversations[chat_id].map do |msg| { role: msg["role"], content: msg["content"] } end end |