Module: Legion::CLI::Chat::SessionStore
- Defined in:
- lib/legion/cli/chat/session_store.rb
Constant Summary collapse
- SESSIONS_DIR =
File.('~/.legion/sessions')
Class Method Summary collapse
- .delete(name) ⇒ Object
- .latest ⇒ Object
- .list ⇒ Object
- .load(name) ⇒ Object
- .restore(session, data) ⇒ Object
- .save(session, name) ⇒ Object
- .session_path(name) ⇒ Object
Class Method Details
.delete(name) ⇒ Object
83 84 85 86 87 88 |
# File 'lib/legion/cli/chat/session_store.rb', line 83 def delete(name) path = session_path(name) raise CLI::Error, "Session not found: #{name}" unless File.exist?(path) File.delete(path) end |
.latest ⇒ Object
76 77 78 79 80 81 |
# File 'lib/legion/cli/chat/session_store.rb', line 76 def latest sessions = list raise CLI::Error, 'No saved sessions found.' if sessions.empty? sessions.first[:name] end |
.list ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/legion/cli/chat/session_store.rb', line 56 def list return [] unless Dir.exist?(SESSIONS_DIR) sessions = Dir.glob(File.join(SESSIONS_DIR, '*.json')).map do |path| name = File.basename(path, '.json') stat = File.stat(path) = (path) { name: name, size: stat.size, modified: stat.mtime, message_count: [:message_count], summary: [:summary], model: [:model], cwd: [:cwd] } end sessions.sort_by { |s| s[:modified] }.reverse end |
.load(name) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/legion/cli/chat/session_store.rb', line 35 def load(name) path = session_path(name) raise CLI::Error, "Session not found: #{name}" unless File.exist?(path) Legion::JSON.load(File.read(path)) end |
.restore(session, data) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/legion/cli/chat/session_store.rb', line 42 def restore(session, data) require 'legion/cli/chat/session_recovery' recovery = Chat::SessionRecovery.recover(data[:messages] || []) session.chat. recovery[:messages].each do |msg| session.chat.(msg) end data[:recovery_state] = recovery[:state] data[:recovery_message] = recovery[:recovery_message] data end |
.save(session, name) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/legion/cli/chat/session_store.rb', line 13 def save(session, name) FileUtils.mkdir_p(SESSIONS_DIR) = session.chat..map(&:to_h) data = { name: name, model: session.model_id, stats: session.stats, saved_at: Time.now.iso8601, cwd: Dir.pwd, message_count: .size, summary: generate_summary(), model_usage: session.respond_to?(:model_usage) ? session.model_usage : {}, cache_hits_tokens: session.respond_to?(:cache_hits_tokens) ? session.cache_hits_tokens : 0, messages: } path = session_path(name) File.write(path, Legion::JSON.dump(data)) path end |
.session_path(name) ⇒ Object
90 91 92 |
# File 'lib/legion/cli/chat/session_store.rb', line 90 def session_path(name) File.join(SESSIONS_DIR, "#{name}.json") end |