Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
1.6 KB
Raw Blame
# The resolution path: for this feature, family and user, what may run, and if
# nothing may, why. Every feature asks it, through Ai.available? and Ai.availability, instead of
# looking for a provider itself.
#
# It reads only Ai::Config, Ai::Routes and Ai::Consent, and never looks at liveness: down
# endpoints are the caller's concern at run time, so availability doesn't flicker.
class Ai::Router
  def initialize(family:, user: nil, config: Ai::Config.current)
    @family, @user, @config = family, user, config
  end

  # @return [Ai::Plan, Ai::Availability] a plan, or an unavailable availability with its reason
  def resolve(feature)
    feature = feature.to_sym
    spec = Ai::Routes.fetch(feature)
    listed = @config.routes_for(spec[:route_of] || feature)
                    .select { |route| route.agent? == agent_chat?(feature) && route.serves_user?(@user) && route.serves?(@family) }
    return Ai::Availability.unavailable(feature, :not_configured) if listed.empty?   # first: nothing to consent to

    if (reason = Ai::Consent.denial(feature, family: @family, user: @user))
      return Ai::Availability.unavailable(feature, reason)
    end

    capable = listed.select { |route| route.agent? || route.model_capable?(Ai::Routes.needs_for(feature)) }
    return Ai::Availability.unavailable(feature, :capability_missing) if capable.empty?

    Ai::Plan.new(feature:, lane: spec[:lane], routes: capable)
  end

  private
    # An agent endpoint serves chat for families whose chat mode is external, and nothing else.
    def agent_chat?(feature) = feature == :chat && @family&.chat_mode_external? || false
end