Module: Legion::Extensions::Agentic::Language::Narrator::Runners::Narrator

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

Instance Method Summary collapse

Instance Method Details

#current_narrativeObject



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/legion/extensions/agentic/language/narrator/runners/narrator.rb', line 73

def current_narrative(**)
  entry = journal.entries.last
  return { narrative: 'No cognitive activity recorded yet.', mood: :dormant } unless entry

  {
    narrative:   entry[:narrative],
    mood:        entry[:mood],
    timestamp:   entry[:timestamp],
    age_seconds: (Time.now.utc - entry[:timestamp]).round(1)
  }
end

#entries_since(since:) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/legion/extensions/agentic/language/narrator/runners/narrator.rb', line 53

def entries_since(since:, **)
  timestamp = since.is_a?(Time) ? since : Time.parse(since.to_s)
  entries = journal.since(timestamp)
  {
    entries: entries.map { |e| format_entry(e) },
    count:   entries.size,
    since:   timestamp
  }
end

#mood_history(mood: nil, limit: 20) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/legion/extensions/agentic/language/narrator/runners/narrator.rb', line 63

def mood_history(mood: nil, limit: 20, **)
  entries = mood ? journal.by_mood(mood.to_sym) : journal.entries
  recent = entries.last(limit)
  {
    entries: recent.map { |e| { timestamp: e[:timestamp], mood: e[:mood], narrative: e[:narrative] } },
    count:   recent.size,
    mood:    mood
  }
end

#narrate(tick_results: {}, cognitive_state: {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/legion/extensions/agentic/language/narrator/runners/narrator.rb', line 13

def narrate(tick_results: {}, cognitive_state: {}, **)
  entry = Helpers::Synthesizer.narrate(tick_results: tick_results, cognitive_state: cognitive_state)

  if Helpers::LlmEnhancer.available?
    sections_data = build_llm_sections_data(tick_results, cognitive_state, entry)
    llm_result = Helpers::LlmEnhancer.narrate(sections_data: sections_data)
    if llm_result
      entry = entry.merge(narrative: llm_result, source: :llm)
      journal.append(entry)
      log.debug "[narrator] mood=#{entry[:mood]} source=llm sections=#{entry[:sections].keys.size}"
      return {
        narrative: entry[:narrative],
        mood:      entry[:mood],
        timestamp: entry[:timestamp],
        sections:  entry[:sections],
        source:    :llm
      }
    end
  end

  journal.append(entry)
  log.debug "[narrator] mood=#{entry[:mood]} sections=#{entry[:sections].keys.size}"

  {
    narrative: entry[:narrative],
    mood:      entry[:mood],
    timestamp: entry[:timestamp],
    sections:  entry[:sections]
  }
end

#narrator_statsObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/legion/extensions/agentic/language/narrator/runners/narrator.rb', line 85

def narrator_stats(**)
  stats = journal.stats
  mood_summary = stats[:moods] || {}
  dominant_mood = mood_summary.max_by { |_, count| count }&.first

  {
    journal_size:  journal.size,
    capacity:      Helpers::Constants::MAX_JOURNAL_SIZE,
    dominant_mood: dominant_mood,
    mood_counts:   mood_summary,
    oldest:        stats[:oldest],
    newest:        stats[:newest]
  }
end

#recent_entries(limit: 10) ⇒ Object



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

def recent_entries(limit: 10, **)
  entries = journal.recent(limit: limit)
  {
    entries: entries.map { |e| format_entry(e) },
    count:   entries.size,
    total:   journal.size
  }
end