Module: Legion::LLM::Inference::Steps::GaiaAdvisory

Includes:
Logging, Legion::Logging::Helper
Included in:
Executor
Defined in:
lib/legion/llm/inference/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.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/legion/llm/inference/steps/gaia_advisory.rb', line 77

def build_partner_context(identity)
  unless apollo_local_available?
    log_step_debug(:gaia_advisory, :partner_context_default, reason: :apollo_unavailable)
    return default_partner_context
  end

  log_step_debug(:gaia_advisory, :partner_context_query)
  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



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/legion/llm/inference/steps/gaia_advisory.rb', line 13

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

  log_step_debug(:gaia_advisory, :request_advisory, message_count: @request.messages.size)
  advisory = ::Legion::Gaia.advise(
    conversation_id: @request.conversation_id,
    messages:        @request.messages,
    caller:          @request.caller
  )

  if advisory.nil? || advisory.empty?
    log_step_debug(:gaia_advisory, :skipped, reason: :empty_advisory)
    return
  end

  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)
  log_step_info(
    :gaia_advisory,
    :complete,
    advisory_types:    classify_advisory_types(advisory).join(','),
    has_system_prompt: advisory.key?(:system_prompt),
    has_routing_hint:  advisory.key?(:routing_hint)
  )
rescue StandardError => e
  @warnings << "GAIA advisory error: #{e.message}"
  handle_exception(e, level: :warn, operation: 'llm.pipeline.steps.gaia_advisory')
end