Provider merchants enhancement

Merged
SO
8mo ago
+735-14

Plan: Enhance Provider Merchants with AI

Context

Providers like Lunchflow create a ProviderMerchant for every imported transaction with only a name and source — no website_url or logo_url. This
means these merchants show up as plain text with generic initials instead of logos in the UI.

Why the existing AI auto-detect rule doesn't work for this

The existing Family::AutoMerchantDetector has Case 2 logic that should enhance provider merchants, but it fails in practice for two reasons:

  1. Line 33 guard blocks enhancement: next unless auto_detection&.business_name.present? && auto_detection&.business_url.present? — requires BOTH
    fields from the LLM. For Case 2 (enhancing an existing merchant), only business_url is needed. If the LLM returns null for either field, the merchant
    is silently skipped.
  2. Transaction-level prompt is suboptimal for website detection: The LLM receives transaction data (amount, description, classification) when all it
    needs for enhancement is the merchant name. A merchant-focused prompt ("what is the website for Walmart?") yields better results than a
    transaction-focused one.
  3. No locking on skip causes wasteful re-processing: When Case 2 succeeds for one transaction but a subsequent transaction has the same
    (now-enhanced) merchant, enhance_provider_merchant returns false and the transaction's merchant_id remains unlocked — causing it to be re-processed
    on every future rule run.

Note: merchant_id is NOT locked by the provider import (enrich_attribute does not call lock_attr!), so Lunchflow transactions ARE enrichable. The
problem is in the auto-detect logic, not in the import locking.

Implementation

Approach: Add an "Enhance with AI" button to the provider merchants section of the Settings > Merchants page. This triggers a background job that
finds all provider merchants assigned to the family without website_url, batches their names to the LLM, and updates them with website_url +
logo_url. Operates at the merchant level (not transaction level) for efficiency and uses a targeted prompt for better results.

Screenshot 2026-03-23 at 09 32 19

Summary by CodeRabbit

  • New Features

    • AI-powered merchant enhancement to auto-fill missing website info, generate logos, and deduplicate merchants
    • "Enhance with AI" action and background job to start enhancement runs; UI shows available count when applicable
  • Bug Fixes / Reliability

    • Prevents concurrent enhancement runs and enqueues work to run in background
    • Respects provider availability before running enhancements
  • UI / Copy

    • Website field always visible in merchant form; added localized success and already-running alerts
  • Tests

    • Added tests covering enhancement flow, logo generation, and deduplication
  • Other

    • Improved menu positioning behavior for better placement on narrow screens