Module: Legion::Extensions::Llm::Ledger::Writers::OfficialRouteAttemptWriter
- Extended by:
- Logging::Helper
- Defined in:
- lib/legion/extensions/llm/ledger/writers/official_route_attempt_writer.rb
Class Method Summary collapse
- .identity_canonical_name(body) ⇒ Object
- .resolve_identity_id(db, body) ⇒ Object
- .resolve_identity_principal_id(db, body) ⇒ Object
- .stable_uuid(value) ⇒ Object
-
.write_route_attempts(db, request, response, body) ⇒ Object
Persist route attempt details into llm_route_attempts table.
Class Method Details
.identity_canonical_name(body) ⇒ Object
90 91 92 93 94 95 96 97 |
# File 'lib/legion/extensions/llm/ledger/writers/official_route_attempt_writer.rb', line 90 def identity_canonical_name(body) raw = body.dig(:identity, :canonical_name) || body.dig(:identity, :identity) || body[:caller_identity] return nil unless raw && !raw.to_s.empty? raw.to_s end |
.resolve_identity_id(db, body) ⇒ Object
83 84 85 86 87 88 |
# File 'lib/legion/extensions/llm/ledger/writers/official_route_attempt_writer.rb', line 83 def resolve_identity_id(db, body) OfficialRecordWriter.caller_identity_refs(db, body)[:identity_id] rescue StandardError => e handle_exception(e, level: :warn, handled: true, operation: 'route_attempt_writer.identity') nil end |
.resolve_identity_principal_id(db, body) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/legion/extensions/llm/ledger/writers/official_route_attempt_writer.rb', line 76 def resolve_identity_principal_id(db, body) OfficialRecordWriter.caller_identity_refs(db, body)[:principal_id] rescue StandardError => e handle_exception(e, level: :warn, handled: true, operation: 'route_attempt_writer.identity_principal') nil end |
.stable_uuid(value) ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/legion/extensions/llm/ledger/writers/official_route_attempt_writer.rb', line 99 def stable_uuid(value) raw = value.to_s return raw if raw.length <= 36 hex = Digest::SHA256.hexdigest(raw)[0, 32] "#{hex[0, 8]}-#{hex[8, 4]}-#{hex[12, 4]}-#{hex[16, 4]}-#{hex[20, 12]}" end |
.write_route_attempts(db, request, response, body) ⇒ Object
Persist route attempt details into llm_route_attempts table. Called from write_prompt/write_metering after the response row exists.
Maps emitter keys to table columns:
provider -> provider
instance -> route_target
model -> model_key
operation -> operation
dispatch_path -> dispatch_path
status -> status
failure_reason -> failure_reason
idempotency_key -> idempotency_key
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/extensions/llm/ledger/writers/official_route_attempt_writer.rb', line 29 def write_route_attempts(db, request, response, body) attempts = Array(body[:route_attempt_details]) return if attempts.empty? attempts.each_with_index do |attempt, idx| next unless attempt.is_a?(Hash) attempt_no = (attempt[:attempt_no] || (idx + 1)).to_i uuid = stable_uuid("#{request[:uuid]}:attempt:#{attempt_no}") existing = db[:llm_route_attempts].where(uuid: uuid).first next if existing begin Helpers::PersistenceLogging.insert_row( db, :llm_route_attempts, { uuid: uuid, message_inference_request_id: request[:id], message_inference_response_id: response[:id], attempt_no: attempt_no, provider: attempt[:provider] || body[:provider], model_key: attempt[:model] || attempt[:model_key] || body[:model_id], tier: attempt[:tier] || body[:tier], route_target: attempt[:route_target] || attempt[:instance], status: (attempt[:status] || 'success').to_s, failure_reason: attempt[:failure_reason], latency_ms: (attempt[:latency_ms] || 0).to_i, operation: attempt[:operation], dispatch_path: attempt[:dispatch_path], idempotency_key: attempt[:idempotency_key], started_at: attempt[:started_at], ended_at: attempt[:ended_at], identity_principal_id: resolve_identity_principal_id(db, body), identity_id: resolve_identity_id(db, body), identity_canonical_name: identity_canonical_name(body), inserted_at: Time.now.utc }, operation: 'official_route_attempt_writer.insert' ) rescue Sequel::UniqueConstraintViolation => e log.debug("[ledger] route_attempt collision resolved uuid=#{uuid} error=#{e.class}") end end end |