Class: Kreator::Compactor
- Inherits:
-
Object
- Object
- Kreator::Compactor
- Defined in:
- lib/kreator/compactor.rb
Constant Summary collapse
- DEFAULT_THRESHOLD =
24_000- DEFAULT_KEEP_LAST =
8
Class Method Summary collapse
- .compact(messages, keep_last: DEFAULT_KEEP_LAST) ⇒ Object
- .should_compact?(messages, threshold: DEFAULT_THRESHOLD) ⇒ Boolean
- .summary_for(messages) ⇒ Object
Class Method Details
.compact(messages, keep_last: DEFAULT_KEEP_LAST) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/kreator/compactor.rb', line 14 def self.compact(, keep_last: DEFAULT_KEEP_LAST) normalized = .map { || .is_a?(Message) ? : Message.from_h() } kept = normalized.last(keep_last) compacted_count = normalized.length - kept.length summary = summary_for(normalized.first(compacted_count)) [Message.system(summary), kept].flatten end |
.should_compact?(messages, threshold: DEFAULT_THRESHOLD) ⇒ Boolean
8 9 10 11 12 |
# File 'lib/kreator/compactor.rb', line 8 def self.should_compact?(, threshold: DEFAULT_THRESHOLD) return false unless threshold .sum { || .content.length } > threshold.to_i end |
.summary_for(messages) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/kreator/compactor.rb', line 22 def self.summary_for() counts = .group_by(&:role).transform_values(&:length) excerpts = .last(6).map do || content = .content.gsub(/\s+/, " ").strip content = "#{content[0, 180]}..." if content.length > 180 "- #{.role}: #{content}" end [ "Earlier conversation was compacted locally.", "Message counts: #{counts.sort.map { |role, count| "#{role}=#{count}" }.join(', ')}.", "Recent compacted excerpts:", excerpts.join("\n") ].join("\n") end |