Module: Legion::Extensions::Agentic::Self::Reflection::Helpers::LlmEnhancer

Defined in:
lib/legion/extensions/agentic/self/reflection/helpers/llm_enhancer.rb

Constant Summary collapse

SYSTEM_PROMPT =
<<~PROMPT
  You are the metacognitive reflection engine for an autonomous AI agent built on LegionIO.
  You analyze post-tick cognitive metrics and produce insightful observations.
  Be analytical and specific. Reference the actual numbers. Identify correlations between metrics.
  Write as internal reflection, not a report. Present tense, first person.
PROMPT
CATEGORY_LABEL_MAP =

Maps prompt label -> actual category symbol from Constants::CATEGORIES

{
  'EMOTION'    => :emotional_stability,
  'PREDICTION' => :prediction_calibration,
  'MEMORY'     => :memory_health,
  'TRUST'      => :trust_drift,
  'CURIOSITY'  => :curiosity_effectiveness,
  'IDENTITY'   => :mode_patterns
}.freeze
REFLECTION_CATEGORIES =
CATEGORY_LABEL_MAP.keys.freeze

Class Method Summary collapse

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


31
32
33
34
35
# File 'lib/legion/extensions/agentic/self/reflection/helpers/llm_enhancer.rb', line 31

def available?
  defined?(Legion::LLM) && Legion::LLM.respond_to?(:started?) && Legion::LLM.started?
rescue StandardError => _e
  false
end

.enhance(prompt, phase: 'reflection') ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/legion/extensions/agentic/self/reflection/helpers/llm_enhancer.rb', line 45

def enhance(prompt, phase: 'reflection', **)
  return nil unless available?

  if pipeline_available?
    response = Legion::LLM::Pipeline::GaiaCaller.chat(
      message: prompt, phase: phase, **
    )
    response&.message&.dig(:content)
  else
    chat = Legion::LLM.chat
    response = chat.ask(prompt)
    response&.content
  end
rescue StandardError => e
  Legion::Logging.warn("[reflection:llm] enhance failed: #{e.message}")
  nil
end

.enhance_reflection(monitors_data:, health_scores:) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/legion/extensions/agentic/self/reflection/helpers/llm_enhancer.rb', line 63

def enhance_reflection(monitors_data:, health_scores:)
  prompt = build_enhance_reflection_prompt(monitors_data: monitors_data, health_scores: health_scores)
  response = llm_ask(prompt)
  parse_enhance_reflection_response(response)
rescue StandardError => e
  Legion::Logging.warn "[reflection:llm] enhance_reflection failed: #{e.message}"
  nil
end

.pipeline_available?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
# File 'lib/legion/extensions/agentic/self/reflection/helpers/llm_enhancer.rb', line 37

def pipeline_available?
  !!(defined?(Legion::LLM::Pipeline::GaiaCaller) &&
     Legion::LLM.respond_to?(:pipeline_enabled?) &&
     Legion::LLM.pipeline_enabled?)
rescue StandardError => _e
  false
end

.reflect_on_dream(dream_results:) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/legion/extensions/agentic/self/reflection/helpers/llm_enhancer.rb', line 72

def reflect_on_dream(dream_results:)
  prompt = build_reflect_on_dream_prompt(dream_results: dream_results)
  response = llm_ask(prompt)
  parse_reflect_on_dream_response(response)
rescue StandardError => e
  Legion::Logging.warn "[reflection:llm] reflect_on_dream failed: #{e.message}"
  nil
end