mirror of
https://github.com/jez500/pricebuddy.git
synced 2026-11-24 19:58:13 +01:00
The branch passed neither `lando phpcs` nor `lando artisan test`:
- SearchService::sanitizeUtf8 compared mb_convert_encoding() against false,
which PHPStan rejects as always-true (it never returns false on PHP 8).
- ScrapeUrlTest used Http::fake() without importing the facade, so the
soft-404 test errored instead of asserting. The feature was untested.
Soft-404 detection was also too loose and, where it did fire, ineffective:
- A bare "404" in a title matched real model numbers ("Roland SP-404",
"Peugeot 404"), nulling the price of live products and marking them
discontinued. It now only counts alongside error wording or alone.
- The template-404 body marker was unanchored, so a theme stylesheet
mentioning it could condemn a product page. Each marker is now anchored
to the attribute it belongs to.
- The injected schema.org Discontinued value was re-read through the
store's availability match config, which finds no rule for it and falls
back to its default (usually InStock) - losing the signal on exactly the
well-configured stores. ScrapeUrl::resolveStockStatus() now honours an
explicit not_found flag first, and all availability call sites use it.
Also: cover the progress-polling fix (it had no tests), extract the
logic duplicated across both search components into a concern, sanitize
title/image as well as html, and fix an unrelated flaky UrlTest that
compared a created_at-ordered relation against an id-ordered assertion.
94 lines
3.1 KiB
PHP
94 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Enums\AiFeature;
|
|
use App\Exceptions\AiProviderException;
|
|
use App\Models\Url;
|
|
use App\Services\Helpers\IntegrationHelper;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class AiScrapeEnhancer
|
|
{
|
|
public const float MIN_CONFIDENCE = 0.6;
|
|
|
|
public function __construct(protected AiExtractionService $extraction) {}
|
|
|
|
public static function new(): self
|
|
{
|
|
return resolve(static::class);
|
|
}
|
|
|
|
/**
|
|
* Backfill a missing price via AI when enabled and allowed. AI is purely additive:
|
|
* any guard failure returns the scrape result exactly as it was scraped.
|
|
*
|
|
* @param array<string, mixed> $scrapeResult
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function enhance(Url $url, array $scrapeResult): array
|
|
{
|
|
// Only fill a genuine gap. The scraped price is a raw string here, so filled()
|
|
// treats null/'' as a gap and any non-empty value as already-present.
|
|
if (filled(data_get($scrapeResult, 'price'))) {
|
|
return $scrapeResult;
|
|
}
|
|
|
|
$store = $url->store;
|
|
|
|
// Enablement is per-store; a product inherits it from the store of each URL.
|
|
if (! $store?->ai_extraction_enabled) {
|
|
return $scrapeResult;
|
|
}
|
|
|
|
// Resolve the extraction provider (honours the global per-feature select
|
|
// and the store's provider override).
|
|
$provider = IntegrationHelper::resolveFeatureProvider(AiFeature::Extraction, $store);
|
|
|
|
if ($provider === null) {
|
|
return $scrapeResult;
|
|
}
|
|
|
|
$html = data_get($scrapeResult, 'body');
|
|
|
|
if (blank($html)) {
|
|
return $scrapeResult;
|
|
}
|
|
|
|
// An out-of-stock item has no purchasable price; don't spend tokens.
|
|
$availabilityStrategy = data_get($store, 'scrape_strategy.availability');
|
|
$isUnavailable = ScrapeUrl::resolveStockStatus($scrapeResult, $availabilityStrategy)
|
|
->isUnavailable();
|
|
|
|
if ($isUnavailable) {
|
|
return $scrapeResult;
|
|
}
|
|
|
|
try {
|
|
$result = $this->extraction->extract($html, provider: $provider);
|
|
} catch (AiProviderException $e) {
|
|
// @phpstan-ignore-next-line - withContext is valid.
|
|
Log::channel('db')->withContext(['url' => $url->url])
|
|
->warning('AI provider error during scrape enhancement; leaving price unchanged.', ['error' => $e->getMessage()]);
|
|
|
|
return $scrapeResult;
|
|
}
|
|
|
|
if ($result === null || $result->price === null || $result->confidence < self::MIN_CONFIDENCE) {
|
|
// @phpstan-ignore-next-line - withContext is valid.
|
|
Log::channel('db')->withContext(['url' => $url->url])
|
|
->debug('AI could not recover a confident price.');
|
|
|
|
return $scrapeResult;
|
|
}
|
|
|
|
data_set($scrapeResult, 'price', $result->price);
|
|
|
|
// @phpstan-ignore-next-line - withContext is valid.
|
|
Log::channel('db')->withContext(['url' => $url->url])
|
|
->info('Price recovered via AI (confidence '.number_format($result->confidence, 2).').');
|
|
|
|
return $scrapeResult;
|
|
}
|
|
}
|