Class: SwarmSDK::V3::Memory::Edge

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm_sdk/v3/memory/edge.rb

Overview

A typed relationship between two memory cards

Edges form a knowledge graph connecting related cards. They enable graph-based retrieval — when a card is relevant, its neighbors can be pulled in for richer context.

Examples:

Create a dependency edge

edge = Edge.new(
  from_id: "card_a1b2c3",
  to_id: "card_d4e5f6",
  type: :depends_on,
  weight: 0.8,
)

Constant Summary collapse

TYPES =
[
  :elaborates,
  :depends_on,
  :supports,
  :contradicts,
  :same_entity,
  :same_episode,
  :decision_reason,
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from_id:, to_id:, type:, weight: 1.0, created_at: nil) ⇒ Edge

Create a new edge

Parameters:

  • from_id (String)

    Source card ID

  • to_id (String)

    Target card ID

  • type (Symbol)

    Relationship type

  • weight (Float) (defaults to: 1.0)

    Edge weight (0.0-1.0)

  • created_at (Time, nil) (defaults to: nil)

    Creation time

Raises:

  • (ArgumentError)

    If type is invalid



54
55
56
57
58
59
60
61
62
# File 'lib/swarm_sdk/v3/memory/edge.rb', line 54

def initialize(from_id:, to_id:, type:, weight: 1.0, created_at: nil)
  @from_id = from_id
  @to_id = to_id
  @type = type.to_sym
  @weight = weight.to_f.clamp(0.0, 1.0)
  @created_at = created_at || Time.now

  validate!
end

Instance Attribute Details

#created_atTime (readonly)

Returns Creation timestamp.

Returns:

  • (Time)

    Creation timestamp



43
44
45
# File 'lib/swarm_sdk/v3/memory/edge.rb', line 43

def created_at
  @created_at
end

#from_idString (readonly)

Returns Source card ID.

Returns:

  • (String)

    Source card ID



31
32
33
# File 'lib/swarm_sdk/v3/memory/edge.rb', line 31

def from_id
  @from_id
end

#to_idString (readonly)

Returns Target card ID.

Returns:

  • (String)

    Target card ID



34
35
36
# File 'lib/swarm_sdk/v3/memory/edge.rb', line 34

def to_id
  @to_id
end

#typeSymbol (readonly)

Returns Relationship type.

Returns:

  • (Symbol)

    Relationship type



37
38
39
# File 'lib/swarm_sdk/v3/memory/edge.rb', line 37

def type
  @type
end

#weightFloat (readonly)

Returns Edge weight (0.0-1.0).

Returns:

  • (Float)

    Edge weight (0.0-1.0)



40
41
42
# File 'lib/swarm_sdk/v3/memory/edge.rb', line 40

def weight
  @weight
end

Class Method Details

.from_h(hash) ⇒ Edge

Deserialize from a hash

Parameters:

  • hash (Hash)

    Serialized edge data

Returns:



82
83
84
85
86
87
88
89
90
91
# File 'lib/swarm_sdk/v3/memory/edge.rb', line 82

def from_h(hash)
  hash = hash.transform_keys(&:to_sym)
  new(
    from_id: hash[:from_id],
    to_id: hash[:to_id],
    type: hash[:type]&.to_sym,
    weight: hash[:weight] || 1.0,
    created_at: hash[:created_at] ? Time.parse(hash[:created_at]) : nil,
  )
end

Instance Method Details

#to_hHash

Serialize to a hash

Returns:

  • (Hash)


67
68
69
70
71
72
73
74
75
# File 'lib/swarm_sdk/v3/memory/edge.rb', line 67

def to_h
  {
    from_id: @from_id,
    to_id: @to_id,
    type: @type.to_s,
    weight: @weight,
    created_at: @created_at.iso8601,
  }
end