p42
This commit is contained in:
@@ -1651,12 +1651,122 @@ final readonly class AgentRunner
|
||||
}
|
||||
|
||||
$guardedQuery = $this->guardStandaloneOptimizedShopQuery($prompt, $shopSearchQuery);
|
||||
$query = $guardedQuery !== $shopSearchQuery
|
||||
? $this->preserveCurrentInputShopQueryTerms($prompt, $guardedQuery)
|
||||
: $this->preserveCurrentInputShopQueryTerms($prompt, $shopSearchQuery);
|
||||
|
||||
if ($guardedQuery !== $shopSearchQuery) {
|
||||
return $this->preserveCurrentInputShopQueryTerms($prompt, $guardedQuery);
|
||||
return $this->cleanupDirectProductAttributeShopQuery($prompt, $query);
|
||||
}
|
||||
|
||||
private function cleanupDirectProductAttributeShopQuery(string $prompt, string $shopSearchQuery): string
|
||||
{
|
||||
$shopSearchQuery = trim($shopSearchQuery);
|
||||
|
||||
if (
|
||||
$shopSearchQuery === ''
|
||||
|| !$this->agentRunnerConfig->isShopQueryProductAttributeCleanupEnabled()
|
||||
) {
|
||||
return $shopSearchQuery;
|
||||
}
|
||||
|
||||
return $this->preserveCurrentInputShopQueryTerms($prompt, $shopSearchQuery);
|
||||
$combined = trim($prompt . ' ' . $shopSearchQuery);
|
||||
if (!$this->containsAnyShopQueryTerm($combined, $this->agentRunnerConfig->getShopQueryProductAttributeCleanupProductTypeTerms())) {
|
||||
return $shopSearchQuery;
|
||||
}
|
||||
|
||||
$constraintTokens = $this->extractConfiguredShopQueryConstraintTokens(
|
||||
$combined,
|
||||
$this->agentRunnerConfig->getShopQueryProductAttributeCleanupComparativeConstraintPatterns()
|
||||
);
|
||||
|
||||
if ($constraintTokens === []) {
|
||||
return $shopSearchQuery;
|
||||
}
|
||||
|
||||
$removeTokens = array_fill_keys($constraintTokens, true);
|
||||
foreach ($this->agentRunnerConfig->getShopQueryProductAttributeCleanupStopTerms() as $term) {
|
||||
foreach ($this->tokenizeShopQueryCandidate($term) as $token) {
|
||||
$removeTokens[$token] = true;
|
||||
}
|
||||
}
|
||||
|
||||
$kept = [];
|
||||
foreach ($this->tokenizeShopQueryCandidate($shopSearchQuery) as $token) {
|
||||
if (isset($removeTokens[$token]) || isset($kept[$token])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$kept[$token] = $token;
|
||||
}
|
||||
|
||||
if (count($kept) < max(1, $this->agentRunnerConfig->getShopQueryProductAttributeCleanupMinTokens())) {
|
||||
return $shopSearchQuery;
|
||||
}
|
||||
|
||||
$cleaned = implode(' ', array_values($kept));
|
||||
|
||||
return $cleaned !== '' ? $cleaned : $shopSearchQuery;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $terms
|
||||
*/
|
||||
private function containsAnyShopQueryTerm(string $text, array $terms): bool
|
||||
{
|
||||
$tokens = array_fill_keys($this->tokenizeShopQueryCandidate($text), true);
|
||||
|
||||
if ($tokens === []) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($terms as $term) {
|
||||
$termTokens = $this->tokenizeShopQueryCandidate($term);
|
||||
if ($termTokens === []) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$matches = true;
|
||||
foreach ($termTokens as $termToken) {
|
||||
if (!isset($tokens[$termToken])) {
|
||||
$matches = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($matches) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $patterns
|
||||
* @return string[]
|
||||
*/
|
||||
private function extractConfiguredShopQueryConstraintTokens(string $text, array $patterns): array
|
||||
{
|
||||
$tokens = [];
|
||||
|
||||
foreach ($patterns as $pattern) {
|
||||
if (@preg_match_all($pattern, $text, $matches, PREG_SET_ORDER) === false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($matches as $match) {
|
||||
$value = $match['value'] ?? ($match[1] ?? '');
|
||||
if (!is_scalar($value)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($this->tokenizeShopQueryCandidate((string) $value) as $token) {
|
||||
$tokens[$token] = $token;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return array_values($tokens);
|
||||
}
|
||||
|
||||
private function preserveCurrentInputShopQueryTerms(string $prompt, string $shopSearchQuery): string
|
||||
|
||||
Reference in New Issue
Block a user