Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
1.8 KB
Raw Blame
# Who an AI call is for and what it belongs to, set around every transport call.
# It's a thin layer over RubyLLM's documented scopes, so RubyLLM's own events carry it:
#
# - `RubyLLM.with_usage_owner(family)`: `usage.ruby_llm` carries the family as `owner`;
# - `RubyLLM.workflow(name, id:, metadata:)`: every RubyLLM event carries `workflow_id` (the
#   call id) and `workflow_metadata`.
#
# Sure keeps its own copy in fiber storage, which threads and fibers started inside inherit,
# so `.ai` events and the OpenTelemetry processor never read RubyLLM's internals. RubyLLM
# 2.1.1 keeps the workflow in Thread.current, which tool threads don't inherit; 2.2 moves it to
# fiber storage (RubyLLM #1327).
module Ai::CallContext
  CALL_KEY = :ai_call_context

  Call = Data.define(:feature, :call_id, :family_id, :endpoint, :model, :deadline) do
    # Seconds left in the attempt budget, or nil when the call has none.
    def remaining = deadline && (deadline - Ai::CallContext.now)

    def expired? = !deadline.nil? && remaining <= 0

    def metadata = { feature:, call_id:, family_id:, endpoint:, model: }.compact.transform_values(&:to_s)
  end

  module_function

  def current = Fiber[CALL_KEY]

  # Around one transport call. `budget` is the seconds one attempt may take, retries included.
  def call(feature:, model:, family: nil, budget: nil)
    call = Call.new(feature: feature.to_s, call_id: SecureRandom.uuid, family_id: family&.id,
                    endpoint: model.endpoint.name, model: model.name, deadline: budget && now + budget)
    previous = Fiber[CALL_KEY]
    Fiber[CALL_KEY] = call

    RubyLLM.with_usage_owner(family) do
      RubyLLM.workflow("ai.#{call.feature}", id: call.call_id, metadata: call.metadata) { yield call }
    end
  ensure
    Fiber[CALL_KEY] = previous
  end

  def now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
end