Module: Legion::Extensions::Agentic::Language::NarrativeReasoning::Runners::NarrativeReasoning

Includes:
Helpers::Lex
Included in:
Client
Defined in:
lib/legion/extensions/agentic/language/narrative_reasoning/runners/narrative_reasoning.rb

Instance Method Summary collapse

Instance Method Details

#add_narrative_event(narrative_id:, content:, event_type:, characters: [], causes: []) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/runners/narrative_reasoning.rb', line 19

def add_narrative_event(narrative_id:, content:, event_type:, characters: [], causes: [], **)
  unless Helpers::NarrativeEngine::EVENT_TYPES.include?(event_type)
    return { success: false, error: :invalid_event_type, valid_types: Helpers::NarrativeEngine::EVENT_TYPES }
  end

  event = narrative_engine.add_narrative_event(
    narrative_id: narrative_id,
    content:      content,
    event_type:   event_type,
    characters:   characters,
    causes:       causes
  )

  if event
    log.debug "[narrative_reasoning] event added id=#{event.id[0..7]} type=#{event_type}"
    { success: true, event_id: event.id, event_type: event_type, narrative_id: narrative_id }
  else
    log.debug "[narrative_reasoning] add_event failed: narrative #{narrative_id[0..7]} not found"
    { success: false, error: :narrative_not_found }
  end
end

#add_narrative_theme(narrative_id:, theme:) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/runners/narrative_reasoning.rb', line 41

def add_narrative_theme(narrative_id:, theme:, **)
  result = narrative_engine.add_narrative_theme(narrative_id: narrative_id, theme: theme)
  if result
    log.debug "[narrative_reasoning] theme added theme=#{theme} to #{narrative_id[0..7]}"
    { success: true, narrative_id: narrative_id, theme: theme }
  else
    { success: false, error: :narrative_not_found }
  end
end

#advance_narrative_arc(narrative_id:) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/runners/narrative_reasoning.rb', line 51

def advance_narrative_arc(narrative_id:, **)
  new_stage = narrative_engine.advance_narrative(narrative_id: narrative_id)
  if new_stage
    log.debug "[narrative_reasoning] arc advanced to #{new_stage} for #{narrative_id[0..7]}"
    { success: true, narrative_id: narrative_id, arc_stage: new_stage }
  else
    { success: false, error: :narrative_not_found }
  end
end

#complete_narrativesObject



70
71
72
73
74
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/runners/narrative_reasoning.rb', line 70

def complete_narratives(**)
  narratives = narrative_engine.complete_narratives
  log.debug "[narrative_reasoning] complete narratives count=#{narratives.size}"
  { success: true, narratives: narratives.map(&:to_h), count: narratives.size }
end

#create_narrative(title:, domain: nil) ⇒ Object



13
14
15
16
17
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/runners/narrative_reasoning.rb', line 13

def create_narrative(title:, domain: nil, **)
  narrative = narrative_engine.create_narrative(title: title, domain: domain)
  log.debug "[narrative_reasoning] created narrative id=#{narrative.id[0..7]} title=#{title}"
  { success: true, narrative_id: narrative.id, title: narrative.title, arc_stage: narrative.arc_stage }
end

#domain_narratives(domain:) ⇒ Object



76
77
78
79
80
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/runners/narrative_reasoning.rb', line 76

def domain_narratives(domain:, **)
  narratives = narrative_engine.by_domain(domain: domain)
  log.debug "[narrative_reasoning] domain=#{domain} count=#{narratives.size}"
  { success: true, domain: domain, narratives: narratives.map(&:to_h), count: narratives.size }
end

#most_coherent_narratives(limit: 5) ⇒ Object



82
83
84
85
86
87
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/runners/narrative_reasoning.rb', line 82

def most_coherent_narratives(limit: 5, **)
  lim = limit.to_i.clamp(1, 50)
  narratives = narrative_engine.most_coherent(limit: lim)
  log.debug "[narrative_reasoning] most_coherent limit=#{lim} returned=#{narratives.size}"
  { success: true, narratives: narratives.map(&:to_h), count: narratives.size }
end

#narrative_reasoning_statsObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/runners/narrative_reasoning.rb', line 96

def narrative_reasoning_stats(**)
  engine = narrative_engine
  complete = engine.complete_narratives.size
  most_coh = engine.most_coherent(limit: 1).first
  log.debug "[narrative_reasoning] stats count=#{engine.count} complete=#{complete}"
  {
    success:             true,
    total_narratives:    engine.count,
    total_events:        engine.total_events,
    complete_narratives: complete,
    top_coherence:       most_coh&.coherence,
    top_coherence_label: most_coh&.coherence_label
  }
end

#trace_narrative_causes(narrative_id:) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/runners/narrative_reasoning.rb', line 61

def trace_narrative_causes(narrative_id:, **)
  chain = narrative_engine.trace_causal_chain(narrative_id: narrative_id)
  narrative = narrative_engine.get(narrative_id)
  return { success: false, error: :narrative_not_found } unless narrative

  log.debug "[narrative_reasoning] causal chain length=#{chain.size} for #{narrative_id[0..7]}"
  { success: true, narrative_id: narrative_id, chain: chain, link_count: chain.size }
end

#update_narrative_reasoningObject



89
90
91
92
93
94
# File 'lib/legion/extensions/agentic/language/narrative_reasoning/runners/narrative_reasoning.rb', line 89

def update_narrative_reasoning(**)
  narrative_engine.decay_all
  count = narrative_engine.count
  log.debug "[narrative_reasoning] decay cycle complete narratives=#{count}"
  { success: true, narratives_updated: count }
end