Module: Legion::Extensions::Agentic::Social::Calibration::Runners::Calibration

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

Instance Method Summary collapse

Instance Method Details

#calibration_statsObject



42
43
44
45
46
47
48
49
# File 'lib/legion/extensions/agentic/social/calibration/runners/calibration.rb', line 42

def calibration_stats(**)
  {
    success:        true,
    weights:        calibration_store.calibration_weights,
    history_counts: calibration_store.history.transform_values(&:size),
    dirty:          calibration_store.dirty?
  }
end

#calibration_weightsObject



38
39
40
# File 'lib/legion/extensions/agentic/social/calibration/runners/calibration.rb', line 38

def calibration_weights(**)
  { success: true, weights: calibration_store.calibration_weights }
end

#detect_explicit_feedback(content:) ⇒ Object



33
34
35
36
# File 'lib/legion/extensions/agentic/social/calibration/runners/calibration.rb', line 33

def detect_explicit_feedback(content:, **)
  feedback = calibration_store.detect_explicit_feedback(content)
  { success: true, feedback: feedback }
end

#extract_preferences_via_llmObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/legion/extensions/agentic/social/calibration/runners/calibration.rb', line 60

def extract_preferences_via_llm(**)
  return { success: true, skipped: :too_soon } unless should_extract_preferences?
  return { success: true, skipped: :llm_unavailable } unless llm_available?

  traces = retrieve_interaction_traces
  return { success: true, skipped: :insufficient_data } if traces.empty?

  context = summarize_traces(traces)
  prompt = build_preference_prompt(context)
  result = Legion::LLM.chat(
    message: [
      { role: 'system', content: 'You are a preference inference engine. Respond ONLY with JSON.' },
      { role: 'user', content: prompt }
    ],
    caller:  { extension: 'lex-agentic-social', mode: :calibration }
  )
  return { success: false, error: :llm_failed } unless result&.content

  parsed = parse_preference_response(result.content)
  return { success: false, error: :parse_failed } unless parsed

  store_llm_preferences(parsed)
  @last_preference_extraction_at = Time.now.utc
  { success: true, preferences_extracted: parsed.size }
rescue StandardError => e
  { success: false, error: e.message }
end

#promote_partner_knowledgeObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/legion/extensions/agentic/social/calibration/runners/calibration.rb', line 88

def promote_partner_knowledge(**)
  return { success: true, skipped: :shutting_down } if shutting_down?
  return { success: true, skipped: :local_unavailable } unless apollo_local_available?

  total = 0
  Helpers::Constants::PROMOTABLE_TAGS.each do |tags|
    return { success: true, promoted: total, stopped: :shutting_down } if shutting_down?
    return { success: true, promoted: total, stopped: :local_unavailable } unless apollo_local_available?

    result = Legion::Apollo::Local.promote_to_global(tags: tags, min_confidence: Helpers::Constants::PROMOTION_MIN_CONFIDENCE)
    total += result[:promoted] if result[:success]
  end

  { success: true, promoted: total }
rescue StandardError => e
  { success: false, error: e.message }
end

#record_advisory_meta(advisory_id:, advisory_types:, applied: nil) ⇒ Object



28
29
30
31
# File 'lib/legion/extensions/agentic/social/calibration/runners/calibration.rb', line 28

def record_advisory_meta(advisory_id:, advisory_types:, applied: nil, **)
  calibration_store.record_advisory(advisory_id: advisory_id, advisory_types: advisory_types, applied: applied)
  { success: true }
end

#sync_partner_knowledgeObject



51
52
53
54
55
56
57
58
# File 'lib/legion/extensions/agentic/social/calibration/runners/calibration.rb', line 51

def sync_partner_knowledge(**)
  results = {}
  results[:preferences] = extract_preferences_via_llm
  results[:promotion] = promote_partner_knowledge
  { success: true, results: results }
rescue StandardError => e
  { success: false, error: e.message }
end

#update_calibration(observation: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/legion/extensions/agentic/social/calibration/runners/calibration.rb', line 13

def update_calibration(observation: nil, **)
  return { success: true, skipped: :no_observation } unless observation

  calibration_store.update_baseline(
    latency: observation[:latency] || 0,
    length:  observation[:content_length] || 0
  )

  result = calibration_store.evaluate_reaction(observation: observation)
  return { success: true, skipped: :no_advisory } unless result

  { success: true, deltas: result[:deltas], weights: calibration_store.calibration_weights,
    reaction_score: result[:reaction_score], applied: result[:applied] }
end