Module: Legion::Extensions::Llm::Ledger::Runners::Escalations

Extended by:
Escalations
Includes:
Helpers::Lex
Included in:
Escalations
Defined in:
lib/legion/extensions/llm/ledger/runners/escalations.rb

Instance Method Summary collapse

Instance Method Details

#write_escalation_record(payload = nil, metadata = {}, **message) ⇒ Object



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
# File 'lib/legion/extensions/llm/ledger/runners/escalations.rb', line 17

def write_escalation_record(payload = nil,  = {}, **message)
  payload,  = normalize_runner_args(payload, , message)
  headers = Helpers::SubscriptionMessage.extract_headers(payload, )
  props   = [:properties] || {}

  body = payload.is_a?(Hash) ? payload : Helpers::Decryption.decrypt_if_needed(payload, )

  db = ::Legion::Data.connection
  record = build_escalation_record(db, body, props, headers)

  Helpers::PersistenceLogging.insert_row(
    db, :llm_escalation_events, record,
    operation: 'write_escalation_record'
  )
  { result: :ok }
rescue Sequel::UniqueConstraintViolation => e
  log.warn("write_escalation_record duplicate insert ignored: #{e.message}")
  { result: :duplicate }
rescue Helpers::DecryptionUnavailable => e
  handle_exception(e, level: :warn, handled: true, operation: 'write_escalation_record.decrypt')
  raise
rescue Helpers::DecryptionFailed => e
  handle_exception(e, level: :error, handled: true, operation: 'write_escalation_record.decrypt')
  raise
rescue StandardError => e
  handle_exception(e, level: :error, handled: true, operation: 'write_escalation_record')
  { result: :error, error: e.message }
end