Module: Legion::CLI::Chat::Checkpoint
- Defined in:
- lib/legion/cli/chat/checkpoint.rb
Defined Under Namespace
Classes: Entry
Class Attribute Summary collapse
-
.entries ⇒ Object
readonly
Returns the value of attribute entries.
-
.max_depth ⇒ Object
Returns the value of attribute max_depth.
-
.mode ⇒ Object
Returns the value of attribute mode.
Class Method Summary collapse
- .clear ⇒ Object
- .configure(max_depth: 10, mode: :per_edit) ⇒ Object
- .count ⇒ Object
- .list ⇒ Object
- .rewind(steps = 1) ⇒ Object
- .rewind_file(path) ⇒ Object
- .save(path) ⇒ Object
Class Attribute Details
.entries ⇒ Object (readonly)
Returns the value of attribute entries.
20 21 22 |
# File 'lib/legion/cli/chat/checkpoint.rb', line 20 def entries @entries end |
.max_depth ⇒ Object
Returns the value of attribute max_depth.
19 20 21 |
# File 'lib/legion/cli/chat/checkpoint.rb', line 19 def max_depth @max_depth end |
.mode ⇒ Object
Returns the value of attribute mode.
19 20 21 |
# File 'lib/legion/cli/chat/checkpoint.rb', line 19 def mode @mode end |
Class Method Details
.clear ⇒ Object
76 77 78 79 |
# File 'lib/legion/cli/chat/checkpoint.rb', line 76 def clear cleanup_storage @entries.clear end |
.configure(max_depth: 10, mode: :per_edit) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/legion/cli/chat/checkpoint.rb', line 22 def configure(max_depth: 10, mode: :per_edit) @max_depth = max_depth @mode = mode @entries = [] @storage_dir = nil end |
.count ⇒ Object
81 82 83 |
# File 'lib/legion/cli/chat/checkpoint.rb', line 81 def count @entries.length end |
.list ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/legion/cli/chat/checkpoint.rb', line 66 def list @entries.map do |e| { path: e.path, existed: e.existed, timestamp: e. } end end |
.rewind(steps = 1) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/legion/cli/chat/checkpoint.rb', line 43 def rewind(steps = 1) return [] if @entries.empty? steps = [steps, @entries.length].min restored = [] steps.times do entry = @entries.pop restore_entry(entry) restored << entry end restored end |
.rewind_file(path) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/legion/cli/chat/checkpoint.rb', line 56 def rewind_file(path) = File.(path) idx = @entries.rindex { |e| e.path == } return nil unless idx entry = @entries.delete_at(idx) restore_entry(entry) entry end |
.save(path) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/legion/cli/chat/checkpoint.rb', line 29 def save(path) = File.(path) entry = Entry.new( path: , content: File.exist?() ? File.read(, encoding: 'utf-8') : nil, existed: File.exist?(), timestamp: Time.now ) @entries.push(entry) @entries.shift while @entries.length > @max_depth persist_entry(entry) entry end |