Class: SwarmSDK::V3::Memory::Card
- Inherits:
-
Object
- Object
- SwarmSDK::V3::Memory::Card
- Defined in:
- lib/swarm_sdk/v3/memory/card.rb
Overview
A self-contained memory unit (<=250 words)
Cards are the atomic unit of the memory system. Each card captures a single idea, fact, decision, or concept with enough context to be understood in isolation.
Constant Summary collapse
- TYPES =
[:fact, :concept, :decision, :constraint, :preference, :incident].freeze
Instance Attribute Summary collapse
-
#access_count ⇒ Integer
Number of times this card was retrieved.
-
#canonical_id ⇒ String?
ID of canonical card if this was merged.
-
#compression_level ⇒ Integer
Compression level (0=raw, 1=summary, 2=bullets, 3=schema, 4=embedding-only).
-
#confidence ⇒ Float
Confidence score (0.0-1.0, default 1.0).
-
#created_at ⇒ Time
readonly
Creation timestamp.
-
#dwell ⇒ Float
Dwell time weight (how long card was in context).
-
#embedding ⇒ Array<Float>?
Embedding vector (384 dimensions for MiniLM).
-
#entities ⇒ Array<String>
Named entities mentioned in the card.
-
#id ⇒ String
readonly
Unique card identifier (card_<hex>).
-
#importance ⇒ Float
Importance score (0.0-1.0).
-
#last_accessed ⇒ Time?
Last time this card was accessed.
-
#source_turn_ids ⇒ Array<String>
Turn IDs that contributed to this card.
-
#text ⇒ String
Card content (<=250 words, self-contained).
-
#type ⇒ Symbol
readonly
Card type (fact, concept, decision, constraint, preference, incident).
-
#updated_at ⇒ Time
Last update timestamp.
Class Method Summary collapse
-
.from_h(hash) ⇒ Card
Deserialize from a hash.
Instance Method Summary collapse
-
#initialize(text:, type: :fact, entities: [], source_turn_ids: [], id: nil, embedding: nil, importance: 0.5, confidence: 1.0, access_count: 0, last_accessed: nil, dwell: 0.0, compression_level: 0, canonical_id: nil, created_at: nil, updated_at: nil) ⇒ Card
constructor
Create a new memory card.
-
#merged? ⇒ Boolean
Whether this card has been merged into a canonical card.
-
#record_access!(dwell_increment: 1.0) ⇒ void
Record an access to this card (included in working context).
-
#to_h ⇒ Hash
Serialize to a hash for JSON storage.
Constructor Details
#initialize(text:, type: :fact, entities: [], source_turn_ids: [], id: nil, embedding: nil, importance: 0.5, confidence: 1.0, access_count: 0, last_accessed: nil, dwell: 0.0, compression_level: 0, canonical_id: nil, created_at: nil, updated_at: nil) ⇒ Card
Create a new memory card
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/swarm_sdk/v3/memory/card.rb', line 86 def initialize( text:, type: :fact, entities: [], source_turn_ids: [], id: nil, embedding: nil, importance: 0.5, confidence: 1.0, access_count: 0, last_accessed: nil, dwell: 0.0, compression_level: 0, canonical_id: nil, created_at: nil, updated_at: nil ) @id = id || "card_#{SecureRandom.hex(6)}" @text = text @type = type.to_sym @entities = Array(entities) @source_turn_ids = Array(source_turn_ids) @embedding = @importance = importance.to_f @confidence = confidence.to_f @access_count = access_count.to_i @last_accessed = last_accessed @dwell = dwell.to_f @compression_level = compression_level.to_i @canonical_id = canonical_id @created_at = created_at || Time.now @updated_at = updated_at || Time.now validate! end |
Instance Attribute Details
#access_count ⇒ Integer
Returns Number of times this card was retrieved.
47 48 49 |
# File 'lib/swarm_sdk/v3/memory/card.rb', line 47 def access_count @access_count end |
#canonical_id ⇒ String?
Returns ID of canonical card if this was merged.
59 60 61 |
# File 'lib/swarm_sdk/v3/memory/card.rb', line 59 def canonical_id @canonical_id end |
#compression_level ⇒ Integer
Returns Compression level (0=raw, 1=summary, 2=bullets, 3=schema, 4=embedding-only).
56 57 58 |
# File 'lib/swarm_sdk/v3/memory/card.rb', line 56 def compression_level @compression_level end |
#confidence ⇒ Float
Returns Confidence score (0.0-1.0, default 1.0).
44 45 46 |
# File 'lib/swarm_sdk/v3/memory/card.rb', line 44 def confidence @confidence end |
#created_at ⇒ Time (readonly)
Returns Creation timestamp.
62 63 64 |
# File 'lib/swarm_sdk/v3/memory/card.rb', line 62 def created_at @created_at end |
#dwell ⇒ Float
Returns Dwell time weight (how long card was in context).
53 54 55 |
# File 'lib/swarm_sdk/v3/memory/card.rb', line 53 def dwell @dwell end |
#embedding ⇒ Array<Float>?
Returns Embedding vector (384 dimensions for MiniLM).
38 39 40 |
# File 'lib/swarm_sdk/v3/memory/card.rb', line 38 def @embedding end |
#entities ⇒ Array<String>
Returns Named entities mentioned in the card.
32 33 34 |
# File 'lib/swarm_sdk/v3/memory/card.rb', line 32 def entities @entities end |
#id ⇒ String (readonly)
Returns Unique card identifier (card_<hex>).
23 24 25 |
# File 'lib/swarm_sdk/v3/memory/card.rb', line 23 def id @id end |
#importance ⇒ Float
Returns Importance score (0.0-1.0).
41 42 43 |
# File 'lib/swarm_sdk/v3/memory/card.rb', line 41 def importance @importance end |
#last_accessed ⇒ Time?
Returns Last time this card was accessed.
50 51 52 |
# File 'lib/swarm_sdk/v3/memory/card.rb', line 50 def last_accessed @last_accessed end |
#source_turn_ids ⇒ Array<String>
Returns Turn IDs that contributed to this card.
35 36 37 |
# File 'lib/swarm_sdk/v3/memory/card.rb', line 35 def source_turn_ids @source_turn_ids end |
#text ⇒ String
Returns Card content (<=250 words, self-contained).
26 27 28 |
# File 'lib/swarm_sdk/v3/memory/card.rb', line 26 def text @text end |
#type ⇒ Symbol (readonly)
Returns Card type (fact, concept, decision, constraint, preference, incident).
29 30 31 |
# File 'lib/swarm_sdk/v3/memory/card.rb', line 29 def type @type end |
#updated_at ⇒ Time
Returns Last update timestamp.
65 66 67 |
# File 'lib/swarm_sdk/v3/memory/card.rb', line 65 def updated_at @updated_at end |
Class Method Details
.from_h(hash) ⇒ Card
Deserialize from a hash
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/swarm_sdk/v3/memory/card.rb', line 172 def from_h(hash) hash = hash.transform_keys(&:to_sym) new( id: hash[:id], text: hash[:text], type: hash[:type]&.to_sym || :fact, entities: hash[:entities] || [], source_turn_ids: hash[:source_turn_ids] || [], embedding: hash[:embedding], importance: hash[:importance] || 0.5, confidence: hash[:confidence] || 1.0, access_count: hash[:access_count] || 0, last_accessed: hash[:last_accessed] ? Time.parse(hash[:last_accessed]) : nil, dwell: hash[:dwell] || 0.0, compression_level: hash[:compression_level] || 0, canonical_id: hash[:canonical_id], created_at: hash[:created_at] ? Time.parse(hash[:created_at]) : nil, updated_at: hash[:updated_at] ? Time.parse(hash[:updated_at]) : nil, ) end |
Instance Method Details
#merged? ⇒ Boolean
Whether this card has been merged into a canonical card
140 141 142 |
# File 'lib/swarm_sdk/v3/memory/card.rb', line 140 def merged? !@canonical_id.nil? end |
#record_access!(dwell_increment: 1.0) ⇒ void
This method returns an undefined value.
Record an access to this card (included in working context)
Increments access count, updates last accessed time, and increases dwell weight to track how often this card has been included in the working context.
130 131 132 133 134 135 |
# File 'lib/swarm_sdk/v3/memory/card.rb', line 130 def record_access!(dwell_increment: 1.0) @access_count += 1 @last_accessed = Time.now @dwell += dwell_increment @updated_at = Time.now end |
#to_h ⇒ Hash
Serialize to a hash for JSON storage
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/swarm_sdk/v3/memory/card.rb', line 147 def to_h { id: @id, text: @text, type: @type.to_s, entities: @entities, source_turn_ids: @source_turn_ids, embedding: @embedding, importance: @importance, confidence: @confidence, access_count: @access_count, last_accessed: @last_accessed&.iso8601, dwell: @dwell, compression_level: @compression_level, canonical_id: @canonical_id, created_at: @created_at.iso8601, updated_at: @updated_at.iso8601, } end |