Module: Legion::LLM::Inference::EnrichmentInjector

Extended by:
Legion::Logging::Helper
Defined in:
lib/legion/llm/inference/enrichment_injector.rb

Class Method Summary collapse

Class Method Details

.format_history_message(message) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/legion/llm/inference/enrichment_injector.rb', line 63

def format_history_message(message)
  return message.to_s unless message.is_a?(Hash)

  role = message[:role] || message['role'] || 'unknown'
  content = message[:content] || message['content']
  content = content.filter_map { |part| part.is_a?(Hash) ? part[:text] || part['text'] : part.to_s }.join if content.is_a?(Array)
  "[#{role}] #{content}"
end

.inject(system:, enrichments:) ⇒ 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
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/legion/llm/inference/enrichment_injector.rb', line 13

def inject(system:, enrichments:)
  parts = []

  # Settings-driven baseline (universal foundation, overridable via config)
  baseline = resolve_baseline
  parts << baseline if baseline

  # GAIA system prompt (highest priority enrichment)
  if (gaia = enrichments.dig('gaia:system_prompt', :content))
    parts << gaia
  end

  # Prior conversation history loaded by the context step.
  if (history = enrichments['context:conversation_history'])
    history_text = Array(history).map { |msg| format_history_message(msg) }.reject(&:empty?).join("\n")
    parts << "Prior conversation history:\n#{history_text}" unless history_text.empty?
  end

  # RAG context
  if (rag = enrichments.dig('rag:context_retrieval', :data, :entries))
    context_text = rag.map { |e| "[#{e[:content_type]}] #{e[:content]}" }.join("\n")
    parts << "Relevant context:\n#{context_text}" unless context_text.empty?
  end

  # Skill injection — active skill's step output appended after the RAG context
  if (skill = enrichments['skill:active'])
    parts << skill
  end

  # Tool call history — BEFORE the empty-parts guard so it reaches the LLM
  # even when no other enrichments are present
  if (history_block = enrichments.dig('tool:call_history', :content))
    parts << history_block
  end

  return system if parts.empty?

  parts << system if system
  log.info("[llm][pipeline] enrichments_injected parts=#{parts.size} baseline=#{!baseline.nil?} system_present=#{!system.nil?}")
  parts.join("\n\n")
end

.resolve_baselineObject



55
56
57
58
59
60
61
# File 'lib/legion/llm/inference/enrichment_injector.rb', line 55

def resolve_baseline
  value = Legion::LLM::Settings.value(:system_baseline)
  value.is_a?(String) && !value.strip.empty? ? value : nil
rescue StandardError => e
  handle_exception(e, level: :warn, operation: 'llm.pipeline.enrichment_injector.resolve_baseline')
  nil
end