Class: Legion::Extensions::Agentic::Integration::Tapestry::Helpers::LoomEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/integration/tapestry/helpers/loom_engine.rb

Instance Method Summary collapse

Constructor Details

#initializeLoomEngine

Returns a new instance of LoomEngine.



10
11
12
13
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/loom_engine.rb', line 10

def initialize
  @threads    = {}
  @tapestries = {}
end

Instance Method Details

#all_tapestriesObject



96
97
98
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/loom_engine.rb', line 96

def all_tapestries
  @tapestries.values
end

#all_threadsObject



92
93
94
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/loom_engine.rb', line 92

def all_threads
  @threads.values
end

#create_tapestry(name:, pattern:, capacity: 50) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/loom_engine.rb', line 30

def create_tapestry(name:, pattern:, capacity: 50)
  raise ArgumentError, 'tapestry limit reached' if @tapestries.size >= Constants::MAX_TAPESTRIES

  tap = Tapestry.new(name: name, pattern: pattern, capacity: capacity)
  @tapestries[tap.id] = tap
  tap
end

#fray_all!(rate: Constants::FRAY_RATE) ⇒ Object



57
58
59
60
61
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/loom_engine.rb', line 57

def fray_all!(rate: Constants::FRAY_RATE)
  @threads.each_value { |t| t.fray!(rate: rate) }
  snapped = @threads.count { |_, t| t.snap? }
  { total: @threads.size, snapped: snapped }
end

#loose_threadsObject



100
101
102
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/loom_engine.rb', line 100

def loose_threads
  @threads.values.select(&:loose?)
end

#most_coherent(limit: 5) ⇒ Object



67
68
69
70
71
72
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/loom_engine.rb', line 67

def most_coherent(limit: 5)
  threads_arr = @threads.values
  @tapestries.values
             .sort_by { |tap| -tap.coherence_score(threads_arr) }
             .first(limit)
end

#most_complete(limit: 5) ⇒ Object



63
64
65
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/loom_engine.rb', line 63

def most_complete(limit: 5)
  @tapestries.values.sort_by { |tap| -tap.completeness }.first(limit)
end

#spin_thread(thread_type:, domain:, content:, strength: nil, color: nil) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/loom_engine.rb', line 15

def spin_thread(thread_type:, domain:, content:,
                strength: nil, color: nil)
  raise ArgumentError, 'thread limit reached' if @threads.size >= Constants::MAX_THREADS

  t = Helpers::Thread.new(
    thread_type: thread_type,
    domain:      domain,
    content:     content,
    strength:    strength,
    color:       color
  )
  @threads[t.id] = t
  t
end

#tapestry_reportObject



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/loom_engine.rb', line 80

def tapestry_report
  threads_arr = @threads.values
  {
    total_threads:    @threads.size,
    total_tapestries: @tapestries.size,
    loose_count:      loose_threads.size,
    by_type:          thread_inventory,
    fraying_count:    @tapestries.count { |_, tap| tap.fraying?(threads_arr) },
    avg_completeness: avg_completeness
  }
end

#thread_inventoryObject



74
75
76
77
78
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/loom_engine.rb', line 74

def thread_inventory
  counts = Constants::THREAD_TYPES.to_h { |type| [type, 0] }
  @threads.each_value { |t| counts[t.thread_type] += 1 }
  counts
end

#unravel(thread_id:, tapestry_id:) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/loom_engine.rb', line 49

def unravel(thread_id:, tapestry_id:)
  thread   = fetch_thread(thread_id)
  tapestry = fetch_tapestry(tapestry_id)

  tapestry.unravel_thread(thread_id)
  thread.unwoven!
end

#weave(thread_id:, tapestry_id:) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
47
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/loom_engine.rb', line 38

def weave(thread_id:, tapestry_id:)
  thread   = fetch_thread(thread_id)
  tapestry = fetch_tapestry(tapestry_id)

  raise ArgumentError, 'thread already woven' if thread.woven?
  raise ArgumentError, 'tapestry is full'     if tapestry.full?

  tapestry.weave_thread(thread_id)
  thread.woven_into!(tapestry_id)
end