Class: Legion::Extensions::Agentic::Memory::Compression::Helpers::InformationChunk

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb

Constant Summary

Constants included from Constants

Constants::CHUNK_TYPES, Constants::COMPRESSION_LABELS, Constants::COMPRESSION_RATE, Constants::DEFAULT_COMPRESSION_RATIO, Constants::FIDELITY_LABELS, Constants::FIDELITY_LOSS_RATE, Constants::MAX_ABSTRACTIONS, Constants::MAX_CHUNKS, Constants::MIN_FIDELITY

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label:, chunk_type: :semantic, original_size: 1.0) ⇒ InformationChunk

Returns a new instance of InformationChunk.



17
18
19
20
21
22
23
24
25
26
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 17

def initialize(label:, chunk_type: :semantic, original_size: 1.0)
  @id                = SecureRandom.uuid
  @label             = label
  @chunk_type        = chunk_type.to_sym
  @original_size     = [original_size.to_f, 0.01].max
  @compressed_size   = @original_size
  @fidelity          = 1.0
  @compression_count = 0
  @created_at        = Time.now.utc
end

Instance Attribute Details

#chunk_typeObject (readonly)

Returns the value of attribute chunk_type.



14
15
16
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 14

def chunk_type
  @chunk_type
end

#compressed_sizeObject (readonly)

Returns the value of attribute compressed_size.



14
15
16
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 14

def compressed_size
  @compressed_size
end

#compression_countObject (readonly)

Returns the value of attribute compression_count.



14
15
16
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 14

def compression_count
  @compression_count
end

#created_atObject (readonly)

Returns the value of attribute created_at.



14
15
16
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 14

def created_at
  @created_at
end

#fidelityObject (readonly)

Returns the value of attribute fidelity.



14
15
16
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 14

def fidelity
  @fidelity
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 14

def id
  @id
end

#labelObject (readonly)

Returns the value of attribute label.



14
15
16
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 14

def label
  @label
end

#original_sizeObject (readonly)

Returns the value of attribute original_size.



14
15
16
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 14

def original_size
  @original_size
end

Instance Method Details

#compress!(amount: COMPRESSION_RATE) ⇒ Object



34
35
36
37
38
39
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 34

def compress!(amount: COMPRESSION_RATE)
  @compressed_size = (@compressed_size * (1.0 - amount)).clamp(0.01, @original_size).round(10)
  @fidelity = (@fidelity - FIDELITY_LOSS_RATE).clamp(MIN_FIDELITY, 1.0).round(10)
  @compression_count += 1
  self
end

#compression_labelObject



50
51
52
53
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 50

def compression_label
  match = COMPRESSION_LABELS.find { |range, _| range.cover?(compression_ratio) }
  match ? match.last : :raw
end

#compression_ratioObject



28
29
30
31
32
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 28

def compression_ratio
  return 0.0 if @original_size.zero?

  (1.0 - (@compressed_size / @original_size)).clamp(0.0, 1.0).round(10)
end

#decompress!(amount: COMPRESSION_RATE) ⇒ Object



41
42
43
44
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 41

def decompress!(amount: COMPRESSION_RATE)
  @compressed_size = (@compressed_size / (1.0 - amount)).clamp(0.01, @original_size).round(10)
  self
end

#fidelity_labelObject



55
56
57
58
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 55

def fidelity_label
  match = FIDELITY_LABELS.find { |range, _| range.cover?(@fidelity) }
  match ? match.last : :degraded
end

#highly_compressed?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 46

def highly_compressed?
  compression_ratio >= 0.8
end

#to_hObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 60

def to_h
  {
    id:                @id,
    label:             @label,
    chunk_type:        @chunk_type,
    original_size:     @original_size,
    compressed_size:   @compressed_size,
    compression_ratio: compression_ratio,
    compression_label: compression_label,
    fidelity:          @fidelity,
    fidelity_label:    fidelity_label,
    compression_count: @compression_count,
    created_at:        @created_at
  }
end