Class: Kreator::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/kreator/session.rb

Constant Summary collapse

VERSION =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#headerObject (readonly)

Returns the value of attribute header.



10
11
12
# File 'lib/kreator/session.rb', line 10

def header
  @header
end

#pathObject (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_message(message)
  append_entry("message", "message" => normalize_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)
  all_messages = messages
  compacted = Compactor.compact(all_messages, keep_last: keep_last)
  summary = compacted.first.content
  kept_count = compacted.length - 1
  append_compaction(summary: summary, original_count: all_messages.length, kept_count: kept_count)
  compacted
end

#compacted_messagesObject



93
94
95
96
97
98
99
100
101
102
# File 'lib/kreator/session.rb', line 93

def compacted_messages
  compaction = compaction_entries.last
  return messages unless compaction

  all_messages = messages
  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"))] + all_messages.drop(retained_start)
end

#compaction_entriesObject



113
114
115
# File 'lib/kreator/session.rb', line 113

def compaction_entries
  entries.select { |entry| entry.fetch("type") == "compaction" }
end

#cwdObject



21
22
23
# File 'lib/kreator/session.rb', line 21

def cwd
  header.fetch("cwd")
end

#entriesObject



72
73
74
# File 'lib/kreator/session.rb', line 72

def entries
  File.foreach(path).drop(1).map { |line| JSON.parse(line) }
end

#idObject



17
18
19
# File 'lib/kreator/session.rb', line 17

def id
  header.fetch("id")
end

#label_entriesObject



125
126
127
# File 'lib/kreator/session.rb', line 125

def label_entries
  entries.select { |entry| entry.fetch("type") == "label" }
end

#labelsObject



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

#messagesObject



85
86
87
88
89
90
91
# File 'lib/kreator/session.rb', line 85

def messages
  entries.filter_map do |entry|
    next unless entry.fetch("type") == "message"

    Message.from_h(entry.fetch("message"))
  end
end

#model_change_entriesObject



121
122
123
# File 'lib/kreator/session.rb', line 121

def model_change_entries
  entries.select { |entry| entry.fetch("type") == "model_change" }
end

#parent_entriesObject



117
118
119
# File 'lib/kreator/session.rb', line 117

def parent_entries
  entries.select { |entry| entry.fetch("type") == "parent_id" }
end

#parent_idObject



25
26
27
# File 'lib/kreator/session.rb', line 25

def parent_id
  header["parent_id"] || parent_entries.last&.fetch("parent_id", nil)
end