Class: Brute::Store::Session
- Inherits:
-
Object
- Object
- Brute::Store::Session
- Defined in:
- lib/brute/store/session.rb
Overview
Manages session persistence. Each session is a conversation that can be saved to disk and resumed later.
Storage layout (per-session directory):
~/.brute/sessions/{session-id}/
session.meta.json # session metadata
context.json # serialized conversation history
msg_0001.json # structured messages (OpenCode format)
msg_0002.json
...
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Class Method Summary collapse
-
.list(dir: nil) ⇒ Object
List all saved sessions, newest first.
Instance Method Summary collapse
- #delete ⇒ Object
-
#initialize(id: nil, dir: nil) ⇒ Session
constructor
A new instance of Session.
- #message_store ⇒ Object
-
#save_messages(messages, title: nil, metadata: {}) ⇒ Object
Serialize an array of LLM::Message objects to disk as JSON.
Constructor Details
#initialize(id: nil, dir: nil) ⇒ Session
Returns a new instance of Session.
24 25 26 27 28 29 30 31 32 |
# File 'lib/brute/store/session.rb', line 24 def initialize(id: nil, dir: nil) @id = id || SecureRandom.uuid @base_dir = dir || File.join(Dir.home, ".brute", "sessions") @session_dir = File.join(@base_dir, @id) @path = File.join(@session_dir, "context.json") @title = nil @metadata = {} FileUtils.mkdir_p(@session_dir) end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
22 23 24 |
# File 'lib/brute/store/session.rb', line 22 def id @id end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
22 23 24 |
# File 'lib/brute/store/session.rb', line 22 def path @path end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
22 23 24 |
# File 'lib/brute/store/session.rb', line 22 def title @title end |
Class Method Details
.list(dir: nil) ⇒ Object
List all saved sessions, newest first.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/brute/store/session.rb', line 54 def self.list(dir: nil) dir ||= File.join(Dir.home, ".brute", "sessions") if File.directory?(dir) sessions = Dir.glob(File.join(dir, "*", "session.meta.json")).filter_map do || data = JSON.parse(File.read(), symbolize_names: true) id = data[:id] next unless id { id: id, title: data[:title], saved_at: data[:saved_at], path: File.join(File.dirname(), "context.json"), } end sessions.sort_by { |s| s[:saved_at] || "" }.reverse else [] end end |
Instance Method Details
#delete ⇒ Object
76 77 78 |
# File 'lib/brute/store/session.rb', line 76 def delete FileUtils.rm_rf(@session_dir) if File.directory?(@session_dir) end |
#message_store ⇒ Object
34 35 36 |
# File 'lib/brute/store/session.rb', line 34 def @message_store ||= MessageStore.new(session_id: @id, dir: @session_dir) end |
#save_messages(messages, title: nil, metadata: {}) ⇒ Object
Serialize an array of LLM::Message objects to disk as JSON.
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/brute/store/session.rb', line 39 def (, title: nil, metadata: {}) @title = title if title @metadata.merge!() data = { schema_version: 1, messages: .map { |m| { role: m.role.to_s, content: m.content.to_s } }, } FileUtils.mkdir_p(File.dirname(@path)) File.write(@path, JSON.pretty_generate(data)) end |