Module: Legion::Extensions::Llm::Ledger::Runners::Reconciliation
- Extended by:
- Reconciliation, Logging::Helper
- Included in:
- Reconciliation
- Defined in:
- lib/legion/extensions/llm/ledger/runners/reconciliation.rb
Constant Summary collapse
- BATCH_SIZE =
200- LOOKBACK_SECONDS =
300
Instance Method Summary collapse
Instance Method Details
#link_metering_messages ⇒ Object
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/runners/reconciliation.rb', line 44 def db = ::Legion::Data.connection linked = 0 = db[:llm_message_inference_requests] .where(latest_message_id: nil) .where { inserted_at >= Time.now.utc - LOOKBACK_SECONDS } .limit(BATCH_SIZE) .all .each do |request| next unless request[:conversation_id] = db[:llm_messages] .where(conversation_id: request[:conversation_id]) .order(Sequel.desc(:seq)) .first next unless db[:llm_message_inference_requests] .where(id: request[:id]) .update(latest_message_id: [:id]) linked += 1 end log.info("[ledger] reconciliation: linked #{linked} metering requests to messages") if linked.positive? { result: :ok, linked: linked } rescue StandardError => e handle_exception(e, level: :error, handled: true, operation: 'reconciliation.metering_messages') { result: :error, error: e. } end |
#link_orphaned_tool_calls ⇒ 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 |
# File 'lib/legion/extensions/llm/ledger/runners/reconciliation.rb', line 17 def link_orphaned_tool_calls db = ::Legion::Data.connection linked = 0 orphans = db[:llm_tool_calls] .where(message_inference_response_id: nil) .where { inserted_at >= Time.now.utc - LOOKBACK_SECONDS } .limit(BATCH_SIZE) .all orphans.each do |tool_call| response = find_response_for_tool_call(db, tool_call) next unless response db[:llm_tool_calls].where(id: tool_call[:id]).update( message_inference_response_id: response[:id] ) linked += 1 end log.info("[ledger] reconciliation: linked #{linked} orphaned tool calls") if linked.positive? { result: :ok, linked: linked } rescue StandardError => e handle_exception(e, level: :error, handled: true, operation: 'reconciliation.tool_calls') { result: :error, error: e. } end |