Class: Legion::Extensions::Agentic::Self::NarrativeSelf::Helpers::Episode

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/self/narrative_self/helpers/episode.rb

Constant Summary

Constants included from Constants

Constants::DEFAULT_SIGNIFICANCE, Constants::EMOTIONAL_BOOST, Constants::EPISODE_DECAY, Constants::EPISODE_TYPES, Constants::MAX_CHAPTER_SIZE, Constants::MAX_EPISODES, Constants::MAX_SELF_CONCEPT_TRAITS, Constants::MAX_THREADS, Constants::MAX_TIMELINE_WINDOW, Constants::SIGNIFICANCE_ALPHA, Constants::SIGNIFICANCE_FLOOR, Constants::SIGNIFICANCE_LABELS, Constants::THREAD_DECAY, Constants::THREAD_MATCH_THRESHOLD, Constants::TRAIT_ALPHA

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(description:, episode_type: :insight, domain: :general, significance: nil, emotional_valence: 0.0, tags: []) ⇒ Episode

Returns a new instance of Episode.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/legion/extensions/agentic/self/narrative_self/helpers/episode.rb', line 17

def initialize(description:, episode_type: :insight, domain: :general,
               significance: nil, emotional_valence: 0.0, tags: [])
  @id                = SecureRandom.uuid
  @description       = description
  @episode_type      = episode_type
  @domain            = domain
  @significance      = significance || DEFAULT_SIGNIFICANCE
  @emotional_valence = emotional_valence.clamp(-1.0, 1.0)
  @tags              = tags
  @created_at        = Time.now.utc
  @thread_ids        = []
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



14
15
16
# File 'lib/legion/extensions/agentic/self/narrative_self/helpers/episode.rb', line 14

def created_at
  @created_at
end

#descriptionObject (readonly)

Returns the value of attribute description.



14
15
16
# File 'lib/legion/extensions/agentic/self/narrative_self/helpers/episode.rb', line 14

def description
  @description
end

#domainObject (readonly)

Returns the value of attribute domain.



14
15
16
# File 'lib/legion/extensions/agentic/self/narrative_self/helpers/episode.rb', line 14

def domain
  @domain
end

#emotional_valenceObject (readonly)

Returns the value of attribute emotional_valence.



14
15
16
# File 'lib/legion/extensions/agentic/self/narrative_self/helpers/episode.rb', line 14

def emotional_valence
  @emotional_valence
end

#episode_typeObject (readonly)

Returns the value of attribute episode_type.



14
15
16
# File 'lib/legion/extensions/agentic/self/narrative_self/helpers/episode.rb', line 14

def episode_type
  @episode_type
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/legion/extensions/agentic/self/narrative_self/helpers/episode.rb', line 14

def id
  @id
end

#significanceObject (readonly)

Returns the value of attribute significance.



14
15
16
# File 'lib/legion/extensions/agentic/self/narrative_self/helpers/episode.rb', line 14

def significance
  @significance
end

#tagsObject (readonly)

Returns the value of attribute tags.



14
15
16
# File 'lib/legion/extensions/agentic/self/narrative_self/helpers/episode.rb', line 14

def tags
  @tags
end

#thread_idsObject (readonly)

Returns the value of attribute thread_ids.



14
15
16
# File 'lib/legion/extensions/agentic/self/narrative_self/helpers/episode.rb', line 14

def thread_ids
  @thread_ids
end

Instance Method Details

#boost(amount) ⇒ Object



30
31
32
33
# File 'lib/legion/extensions/agentic/self/narrative_self/helpers/episode.rb', line 30

def boost(amount)
  emotional_factor = @emotional_valence.abs * EMOTIONAL_BOOST
  @significance = [(@significance + amount + emotional_factor), 1.0].min
end

#decayObject



35
36
37
# File 'lib/legion/extensions/agentic/self/narrative_self/helpers/episode.rb', line 35

def decay
  @significance = [(@significance - EPISODE_DECAY), 0.0].max
end

#faded?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/legion/extensions/agentic/self/narrative_self/helpers/episode.rb', line 39

def faded?
  @significance < SIGNIFICANCE_FLOOR
end

#labelObject



43
44
45
46
47
48
# File 'lib/legion/extensions/agentic/self/narrative_self/helpers/episode.rb', line 43

def label
  SIGNIFICANCE_LABELS.each do |range, lbl|
    return lbl if range.cover?(@significance)
  end
  :minor
end


50
51
52
# File 'lib/legion/extensions/agentic/self/narrative_self/helpers/episode.rb', line 50

def link_thread(thread_id)
  @thread_ids << thread_id unless @thread_ids.include?(thread_id)
end

#matches_tags?(query_tags) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
# File 'lib/legion/extensions/agentic/self/narrative_self/helpers/episode.rb', line 54

def matches_tags?(query_tags)
  return false if query_tags.empty? || @tags.empty?

  overlap = (@tags & query_tags).size
  overlap.to_f / query_tags.size
end

#to_hObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/legion/extensions/agentic/self/narrative_self/helpers/episode.rb', line 61

def to_h
  {
    id:                @id,
    description:       @description,
    episode_type:      @episode_type,
    domain:            @domain,
    significance:      @significance,
    emotional_valence: @emotional_valence,
    label:             label,
    tags:              @tags.dup,
    thread_ids:        @thread_ids.dup,
    created_at:        @created_at
  }
end