Class: Legion::Extensions::Agentic::Language::NarrativeReasoning::Helpers::Narrative

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/language/narrative_reasoning/helpers/narrative.rb

Constant Summary collapse

ARC_STAGES =
%i[beginning rising_action climax falling_action resolution].freeze
COHERENCE_LABELS =
{
  (0.8..)     => :compelling,
  (0.6...0.8) => :coherent,
  (0.4...0.6) => :developing,
  (0.2...0.4) => :fragmented,
  (..0.2)     => :incoherent
}.freeze
DEFAULT_COHERENCE =
0.5
COHERENCE_FLOOR =
0.0
COHERENCE_CEILING =
1.0
COHERENCE_BOOST =
0.1
DECAY_RATE =
0.02

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title:, domain: nil, id: nil) ⇒ Narrative

Returns a new instance of Narrative.



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/helpers/narrative.rb', line 31

def initialize(title:, domain: nil, id: nil)
  @id             = id || SecureRandom.uuid
  @title          = title
  @domain         = domain
  @events         = []
  @characters     = []
  @themes         = []
  @arc_stage      = ARC_STAGES.first
  @coherence      = DEFAULT_COHERENCE
  @created_at     = Time.now.utc
  @last_updated_at = Time.now.utc
end

Instance Attribute Details

#arc_stageObject (readonly)

Returns the value of attribute arc_stage.



28
29
30
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/helpers/narrative.rb', line 28

def arc_stage
  @arc_stage
end

#charactersObject (readonly)

Returns the value of attribute characters.



28
29
30
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/helpers/narrative.rb', line 28

def characters
  @characters
end

#coherenceObject (readonly)

Returns the value of attribute coherence.



28
29
30
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/helpers/narrative.rb', line 28

def coherence
  @coherence
end

#created_atObject (readonly)

Returns the value of attribute created_at.



28
29
30
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/helpers/narrative.rb', line 28

def created_at
  @created_at
end

#domainObject (readonly)

Returns the value of attribute domain.



28
29
30
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/helpers/narrative.rb', line 28

def domain
  @domain
end

#eventsObject (readonly)

Returns the value of attribute events.



28
29
30
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/helpers/narrative.rb', line 28

def events
  @events
end

#idObject (readonly)

Returns the value of attribute id.



28
29
30
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/helpers/narrative.rb', line 28

def id
  @id
end

#last_updated_atObject (readonly)

Returns the value of attribute last_updated_at.



28
29
30
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/helpers/narrative.rb', line 28

def last_updated_at
  @last_updated_at
end

#themesObject (readonly)

Returns the value of attribute themes.



28
29
30
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/helpers/narrative.rb', line 28

def themes
  @themes
end

#titleObject (readonly)

Returns the value of attribute title.



28
29
30
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/helpers/narrative.rb', line 28

def title
  @title
end

Instance Method Details

#add_event(event) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/helpers/narrative.rb', line 44

def add_event(event)
  @events << event
  event.characters.each { |chr| @characters << chr unless @characters.include?(chr) }
  auto_advance_arc
  @coherence = (@coherence + COHERENCE_BOOST).clamp(COHERENCE_FLOOR, COHERENCE_CEILING)
  @last_updated_at = Time.now.utc
  event
end

#add_theme(theme) ⇒ Object



53
54
55
56
57
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/helpers/narrative.rb', line 53

def add_theme(theme)
  @themes << theme unless @themes.include?(theme)
  @last_updated_at = Time.now.utc
  theme
end

#advance_arc!Object



59
60
61
62
63
64
65
66
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/helpers/narrative.rb', line 59

def advance_arc!
  idx = ARC_STAGES.index(@arc_stage)
  return @arc_stage if idx.nil? || idx >= ARC_STAGES.size - 1

  @arc_stage = ARC_STAGES[idx + 1]
  @last_updated_at = Time.now.utc
  @arc_stage
end

#causal_chainObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/helpers/narrative.rb', line 68

def causal_chain
  chain = []
  @events.each do |event|
    event.causes.each do |cause_id|
      cause = @events.find { |e| e.id == cause_id }
      chain << { cause: cause&.to_h, effect: event.to_h } if cause
    end
  end
  chain
end

#coherence_labelObject



79
80
81
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/helpers/narrative.rb', line 79

def coherence_label
  COHERENCE_LABELS.find { |range, _label| range.cover?(@coherence) }&.last || :incoherent
end

#complete?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/helpers/narrative.rb', line 83

def complete?
  @arc_stage == :resolution
end

#decay_coherenceObject



87
88
89
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/helpers/narrative.rb', line 87

def decay_coherence
  @coherence = (@coherence - DECAY_RATE).clamp(COHERENCE_FLOOR, COHERENCE_CEILING)
end

#to_hObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/helpers/narrative.rb', line 91

def to_h
  {
    id:              @id,
    title:           @title,
    domain:          @domain,
    events:          @events.map(&:to_h),
    characters:      @characters,
    themes:          @themes,
    arc_stage:       @arc_stage,
    coherence:       @coherence,
    coherence_label: coherence_label,
    complete:        complete?,
    created_at:      @created_at,
    last_updated_at: @last_updated_at
  }
end