Module: Legion::LLM::Pipeline::Steps::GaiaAdvisory

Includes:
Legion::Logging::Helper
Included in:
Executor
Defined in:
lib/legion/llm/pipeline/steps/gaia_advisory.rb

Instance Method Summary collapse

Instance Method Details

#build_partner_context(identity) ⇒ Object

Exposed as a public method so specs can stub it on instances.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/legion/llm/pipeline/steps/gaia_advisory.rb', line 63

def build_partner_context(identity)
  return default_partner_context unless apollo_local_available?

  entries = ::Legion::Apollo::Local.query(
    text:  identity,
    tags:  ['partner'],
    limit: 5
  )

  results = entries.is_a?(Hash) ? (entries[:results] || []) : Array(entries)

  {
    standing:            extract_standing(results),
    compatibility:       extract_compatibility(results),
    recent_sentiment:    extract_sentiment(results),
    interaction_pattern: extract_interaction_pattern(results)
  }
rescue StandardError => e
  handle_exception(e, level: :debug)
  nil
end

#step_gaia_advisoryObject



11
12
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
54
55
56
57
58
59
60
# File 'lib/legion/llm/pipeline/steps/gaia_advisory.rb', line 11

def step_gaia_advisory
  unless defined?(::Legion::Gaia) && ::Legion::Gaia.started?
    @warnings << 'GAIA unavailable for pre-request shaping'
    return
  end

  advisory = ::Legion::Gaia.advise(
    conversation_id: @request.conversation_id,
    messages:        @request.messages,
    caller:          @request.caller
  )

  return if advisory.nil? || advisory.empty?

  enrich_advisory_with_partner_context(advisory)

  calibration_weights = fetch_calibration_weights
  advisory[:calibration_weights] = calibration_weights if calibration_weights

  @enrichments['gaia:advisory'] = {
    content:   advisory_summary(advisory),
    data:      advisory,
    timestamp: Time.now
  }

  if advisory[:system_prompt]
    @enrichments['gaia:system_prompt'] = {
      content:   advisory[:system_prompt],
      timestamp: Time.now
    }
  end

  if advisory[:routing_hint]
    @enrichments['gaia:routing_hint'] = {
      data:      advisory[:routing_hint],
      timestamp: Time.now
    }
  end

  @timeline.record(
    category: :enrichment, key: 'gaia:advisory',
    direction: :inbound, detail: advisory_summary(advisory),
    from: 'gaia', to: 'pipeline'
  )

  record_advisory_meta_to_gaia(advisory)
rescue StandardError => e
  @warnings << "GAIA advisory error: #{e.message}"
  handle_exception(e, level: :warn, operation: 'llm.pipeline.steps.gaia_advisory')
end