Files
pricebuddy/app/Services/StrategyExtractor.php
Jeremy 8b02abb0aa feat: AI self-healing store config
Add AiConfigHealer to bootstrap or repair a store config via a
tool-using structured agent (fetch/css/regex tools, HealingContext,
shared StrategyExtractor).

- Run healing before the one-shot enhancer; AI-bootstrap a store config
  when product create would otherwise fail; StoreUrl validation defers
  to self-healing when enabled
- Per-feature AI provider resolution (AiFeature enum) and settings
- Per-store self-healing opt-out plus cooldown; log heal attempts and
  unusable proposals
- Heal with AI action with non-persisting preview/apply/discard in the
  store test modal; prompt switches to browser scraping when blocked
2026-06-09 15:07:40 +10:00

51 lines
1.6 KiB
PHP

<?php
namespace App\Services;
use App\Enums\ScraperStrategyType;
use Jez500\WebScraperForLaravel\WebScraperInterface;
class StrategyExtractor
{
/**
* Apply a single scrape-strategy slot to an already-loaded scraper and
* return the extracted string (with prepend/append), or null.
*
* May throw Jez500\WebScraperForLaravel\Exceptions\DomSelectorException on an
* invalid selector/xpath; callers decide whether to swallow or surface it.
*
* @param array<string, mixed> $slot
*/
public static function extract(WebScraperInterface $scraper, array $slot, string $field): ?string
{
$type = data_get($slot, 'type');
$value = data_get($slot, 'value');
if (! is_string($type) || $type === '') {
return null;
}
if (! is_string($value) && $type !== ScraperStrategyType::SchemaOrg->value) {
return null;
}
if ($type === ScraperStrategyType::SchemaOrg->value) {
return SchemaOrgService::parseSchemaOrg($scraper->getSchemaOrg(), $field);
}
$method = ScrapeUrl::getMethodFromType($type);
$args = match ($type) {
ScraperStrategyType::Selector->value => ScrapeUrl::parseSelector($value),
ScraperStrategyType::Regex->value => [ScrapeUrl::ensureRegexDelimiters($value)],
default => [$value],
};
return implode('', [
data_get($slot, 'prepend', ''),
call_user_func_array([$scraper, $method], $args)?->first(),
data_get($slot, 'append', ''),
]);
}
}