Class: Legion::Extensions::Agentic::Language::NarrativeReasoning::Helpers::Narrative
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Language::NarrativeReasoning::Helpers::Narrative
- 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
-
#arc_stage ⇒ Object
readonly
Returns the value of attribute arc_stage.
-
#characters ⇒ Object
readonly
Returns the value of attribute characters.
-
#coherence ⇒ Object
readonly
Returns the value of attribute coherence.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#domain ⇒ Object
readonly
Returns the value of attribute domain.
-
#events ⇒ Object
readonly
Returns the value of attribute events.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#last_updated_at ⇒ Object
readonly
Returns the value of attribute last_updated_at.
-
#themes ⇒ Object
readonly
Returns the value of attribute themes.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
- #add_event(event) ⇒ Object
- #add_theme(theme) ⇒ Object
- #advance_arc! ⇒ Object
- #causal_chain ⇒ Object
- #coherence_label ⇒ Object
- #complete? ⇒ Boolean
- #decay_coherence ⇒ Object
-
#initialize(title:, domain: nil, id: nil) ⇒ Narrative
constructor
A new instance of Narrative.
- #to_h ⇒ Object
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_stage ⇒ Object (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 |
#characters ⇒ Object (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 |
#coherence ⇒ Object (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_at ⇒ Object (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 |
#domain ⇒ Object (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 |
#events ⇒ Object (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 |
#id ⇒ Object (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_at ⇒ Object (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 |
#themes ⇒ Object (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 |
#title ⇒ Object (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_chain ⇒ Object
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_label ⇒ Object
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
83 84 85 |
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/helpers/narrative.rb', line 83 def complete? @arc_stage == :resolution end |
#decay_coherence ⇒ Object
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_h ⇒ Object
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 |