Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
4.2 KB
Raw Blame
module Assistant
  Error = Class.new(StandardError)

  REGISTRY = {
    "builtin" => Assistant::Builtin,
    "external" => Assistant::External
  }.freeze

  # Tools for users who opted into preview features in Settings -> Preferences.
  #
  # Statement Vault + provenance tools back the wealth agent-harness workflow
  # documented in docs/llm-guides/wealth-agent-harness.md. GetValuations is the
  # read pair for RecordValuation; GetInsights reads the Insights feed, which is
  # itself preview-gated app-wide.
  #
  # The bills tools back the preview-gated Bills subsystem.
  PREVIEW_FUNCTION_CLASSES = [
    Function::UploadAccountStatement,
    Function::ListAccountStatements,
    Function::GetAccountStatement,
    Function::GetStatementCoverage,
    Function::RecordValuation,
    Function::GetValuations,
    Function::GetInsights,
    # Bills: the whole subsystem is preview-gated, so its tools ride the same
    # per-user flag as the surfaces they operate on. Each tool additionally
    # re-checks the family's recurring feature gate and the user's
    # account-access scope itself.
    Function::GetBills,
    Function::GetBillDetails,
    Function::GetPaycheckPlan,
    Function::GetBillAudit,
    Function::CreateBill,
    Function::UpdateBill,
    Function::RecordBillPayment
  ].freeze

  # Guests may view family configuration (tags, categories, rules) but not
  # change it, so these tools are left out of a guest's registry (chat and MCP
  # alike). PreviewRule writes nothing, but it issues the token ApplyRule needs,
  # and the web blocks guests from a rule's confirmation page too.
  FAMILY_CONFIG_FUNCTION_CLASSES = [
    Function::CreateTag,
    Function::UpdateTag,
    Function::CreateCategory,
    Function::UpdateCategory,
    Function::DeleteCategory,
    Function::CreateRule,
    Function::UpdateRule,
    Function::ApplyRule,
    Function::PreviewRule
  ].freeze

  class << self
    def for_chat(chat)
      implementation_for(chat).for_chat(chat)
    end

    def config_for(chat)
      raise Error, "chat is required" if chat.blank?
      Assistant::Builtin.config_for(chat)
    end

    def available_types
      REGISTRY.keys
    end

    # The single registry behind both the builtin chat and the /mcp endpoint's
    # tools/list — a function class added here is immediately callable by an
    # external agent, so pass the user to keep preview tools out of the default
    # surface.
    def function_classes(user = nil)
      classes = [
        Function::GetTransactions,
        Function::GetRecurringTransactions,
        Function::GetUncategorizedTransactions,
        Function::GetAccounts,
        Function::GetHoldings,
        Function::GetBalanceSheet,
        Function::GetIncomeStatement,
        Function::GetBudget,
        Function::ImportBankStatement,
        Function::SearchFamilyFiles,
        Function::CreateGoal,
        Function::SetOpeningBalance,
        Function::UnmatchTransfer,
        Function::UpdateTransfer,
        Function::GetTags,
        Function::CreateTag,
        Function::UpdateTag,
        Function::GetCategories,
        Function::CreateCategory,
        Function::UpdateCategory,
        Function::DeleteCategory,
        Function::GetMerchants,
        Function::GetRules,
        Function::GetRuleOptions,
        Function::PreviewRule,
        Function::CreateRule,
        Function::UpdateRule,
        Function::ApplyRule,
        Function::UpdateTransaction,
        Function::CreateTransaction,
        Function::DeleteTransaction,
        Function::GetTransferMatchCandidates,
        Function::MatchTransfer,
        Function::ReviewTransfer,
        Function::UpdateBudget
      ]

      classes += PREVIEW_FUNCTION_CLASSES if user&.preview_features_enabled?
      classes -= FAMILY_CONFIG_FUNCTION_CLASSES if user&.guest?
      classes.select { |fn_class| ai_feature_available?(fn_class, user) }
    end

    private
      def ai_feature_available?(fn_class, user)
        feature = fn_class.ai_feature
        feature.nil? || user.nil? || Ai.available?(feature, family: user.family, user:)
      end

      def implementation_for(chat)
        raise Error, "chat is required" if chat.blank?
        type = chat.user&.family&.effective_assistant_type || "builtin"
        REGISTRY.fetch(type) { REGISTRY["builtin"] }
      end
  end
end