Class: Kreator::Session
- Inherits:
-
Object
- Object
- Kreator::Session
- Defined in:
- lib/kreator/session.rb
Constant Summary collapse
- VERSION =
1
Instance Attribute Summary collapse
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #append_compaction(summary:, original_count:, kept_count:) ⇒ Object
- #append_entries(entries) ⇒ Object
- #append_label(label) ⇒ Object
- #append_message(message) ⇒ Object
- #append_model_change(provider:, model:) ⇒ Object
- #append_model_change_unless_current(provider:, model:) ⇒ Object
- #append_parent_id(parent_id:, parent_path: nil, forked_entry_index: nil) ⇒ Object
- #append_session_info(info) ⇒ Object
- #compact!(keep_last: Compactor::DEFAULT_KEEP_LAST) ⇒ Object
- #compacted_messages ⇒ Object
- #compaction_entries ⇒ Object
- #cwd ⇒ Object
- #entries ⇒ Object
- #id ⇒ Object
-
#initialize(path:, header:) ⇒ Session
constructor
A new instance of Session.
- #label_entries ⇒ Object
- #labels ⇒ Object
- #messages ⇒ Object
- #model_change_entries ⇒ Object
- #parent_entries ⇒ Object
- #parent_id ⇒ Object
Constructor Details
#initialize(path:, header:) ⇒ Session
Returns a new instance of Session.
12 13 14 15 |
# File 'lib/kreator/session.rb', line 12 def initialize(path:, header:) @path = path @header = header end |
Instance Attribute Details
#header ⇒ Object (readonly)
Returns the value of attribute header.
10 11 12 |
# File 'lib/kreator/session.rb', line 10 def header @header end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
10 11 12 |
# File 'lib/kreator/session.rb', line 10 def path @path end |
Instance Method Details
#append_compaction(summary:, original_count:, kept_count:) ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/kreator/session.rb', line 63 def append_compaction(summary:, original_count:, kept_count:) append_entry( "compaction", "summary" => summary, "original_count" => original_count, "kept_count" => kept_count ) end |
#append_entries(entries) ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/kreator/session.rb', line 76 def append_entries(entries) File.open(path, "a") do |file| file.flock(File::LOCK_EX) entries.each { |entry| file.puts JSON.generate(entry) } ensure file&.flock(File::LOCK_UN) end end |
#append_label(label) ⇒ Object
48 49 50 |
# File 'lib/kreator/session.rb', line 48 def append_label(label) append_entry("label", "label" => label.to_s) end |
#append_message(message) ⇒ Object
29 30 31 |
# File 'lib/kreator/session.rb', line 29 def () append_entry("message", "message" => ().to_h) end |
#append_model_change(provider:, model:) ⇒ Object
33 34 35 |
# File 'lib/kreator/session.rb', line 33 def append_model_change(provider:, model:) append_entry("model_change", "provider" => provider, "model" => model) end |
#append_model_change_unless_current(provider:, model:) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/kreator/session.rb', line 37 def append_model_change_unless_current(provider:, model:) current = model_change_entries.last return if current && current["provider"] == provider && current["model"] == model append_model_change(provider: provider, model: model) end |
#append_parent_id(parent_id:, parent_path: nil, forked_entry_index: nil) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/kreator/session.rb', line 52 def append_parent_id(parent_id:, parent_path: nil, forked_entry_index: nil) append_entry( "parent_id", { "parent_id" => parent_id, "parent_path" => parent_path, "forked_entry_index" => forked_entry_index }.compact ) end |
#append_session_info(info) ⇒ Object
44 45 46 |
# File 'lib/kreator/session.rb', line 44 def append_session_info(info) append_entry("session_info", "info" => info) end |
#compact!(keep_last: Compactor::DEFAULT_KEEP_LAST) ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'lib/kreator/session.rb', line 104 def compact!(keep_last: Compactor::DEFAULT_KEEP_LAST) = compacted = Compactor.compact(, keep_last: keep_last) summary = compacted.first.content kept_count = compacted.length - 1 append_compaction(summary: summary, original_count: .length, kept_count: kept_count) compacted end |
#compacted_messages ⇒ Object
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/kreator/session.rb', line 93 def compaction = compaction_entries.last return unless compaction = original_count = compaction.fetch("original_count") kept_count = compaction.fetch("kept_count") retained_start = [original_count - kept_count, 0].max [Message.system(compaction.fetch("summary"))] + .drop(retained_start) end |
#compaction_entries ⇒ Object
113 114 115 |
# File 'lib/kreator/session.rb', line 113 def compaction_entries entries.select { |entry| entry.fetch("type") == "compaction" } end |
#cwd ⇒ Object
21 22 23 |
# File 'lib/kreator/session.rb', line 21 def cwd header.fetch("cwd") end |
#entries ⇒ Object
72 73 74 |
# File 'lib/kreator/session.rb', line 72 def entries File.foreach(path).drop(1).map { |line| JSON.parse(line) } end |
#id ⇒ Object
17 18 19 |
# File 'lib/kreator/session.rb', line 17 def id header.fetch("id") end |
#label_entries ⇒ Object
125 126 127 |
# File 'lib/kreator/session.rb', line 125 def label_entries entries.select { |entry| entry.fetch("type") == "label" } end |
#labels ⇒ Object
129 130 131 132 |
# File 'lib/kreator/session.rb', line 129 def labels initial = Array(header["labels"]) (initial + label_entries.map { |entry| entry.fetch("label") }).uniq end |
#messages ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/kreator/session.rb', line 85 def entries.filter_map do |entry| next unless entry.fetch("type") == "message" Message.from_h(entry.fetch("message")) end end |
#model_change_entries ⇒ Object
121 122 123 |
# File 'lib/kreator/session.rb', line 121 def model_change_entries entries.select { |entry| entry.fetch("type") == "model_change" } end |
#parent_entries ⇒ Object
117 118 119 |
# File 'lib/kreator/session.rb', line 117 def parent_entries entries.select { |entry| entry.fetch("type") == "parent_id" } end |
#parent_id ⇒ Object
25 26 27 |
# File 'lib/kreator/session.rb', line 25 def parent_id header["parent_id"] || parent_entries.last&.fetch("parent_id", nil) end |