fix p51
This commit is contained in:
@@ -292,6 +292,26 @@ final readonly class AgentRunner
|
||||
$optimizedShopQuery = '';
|
||||
}
|
||||
|
||||
$referentialAnchoredShopSearchQuery = $this->guardReferentialShopQueryFallbackWithHistoryAnchor(
|
||||
prompt: $originalPrompt,
|
||||
shopSearchQuery: $shopSearchQuery,
|
||||
commerceHistoryContext: $shopQueryHistoryContext
|
||||
);
|
||||
|
||||
if ($referentialAnchoredShopSearchQuery !== $shopSearchQuery) {
|
||||
$this->agentLogger->info('Enriched referential shop fallback query with history anchor', [
|
||||
'userId' => $userId,
|
||||
'prompt' => $prompt,
|
||||
'routingPrompt' => $routingPrompt,
|
||||
'optimizedShopQuery' => $optimizedShopQuery,
|
||||
'shopSearchQuery' => $shopSearchQuery,
|
||||
'referentialAnchoredShopSearchQuery' => $referentialAnchoredShopSearchQuery,
|
||||
]);
|
||||
|
||||
$shopSearchQuery = $referentialAnchoredShopSearchQuery;
|
||||
$optimizedShopQuery = '';
|
||||
}
|
||||
|
||||
$ragAnchoredShopSearchQuery = $this->enrichShopSearchQueryWithRagAnchor(
|
||||
prompt: $originalPrompt,
|
||||
shopSearchQuery: $shopSearchQuery,
|
||||
@@ -2581,6 +2601,83 @@ final readonly class AgentRunner
|
||||
return trim($query);
|
||||
}
|
||||
|
||||
private function guardReferentialShopQueryFallbackWithHistoryAnchor(
|
||||
string $prompt,
|
||||
string $shopSearchQuery,
|
||||
string $commerceHistoryContext
|
||||
): string {
|
||||
if (!$this->agentRunnerConfig->isShopQueryContextAnchorEnrichmentEnabled()) {
|
||||
return $shopSearchQuery;
|
||||
}
|
||||
|
||||
if (trim($commerceHistoryContext) === '') {
|
||||
return $shopSearchQuery;
|
||||
}
|
||||
|
||||
if (!$this->shouldUseCommerceHistoryForShopQuery($prompt)) {
|
||||
return $shopSearchQuery;
|
||||
}
|
||||
|
||||
$combined = trim($shopSearchQuery . ' ' . $prompt);
|
||||
if (!$this->containsConfiguredShopQueryAnchorTrigger($combined)) {
|
||||
return $shopSearchQuery;
|
||||
}
|
||||
|
||||
$anchor = $this->normalizeShopQueryAnchor(
|
||||
$this->extractLatestConfiguredShopQueryContextAnchor($commerceHistoryContext)
|
||||
);
|
||||
|
||||
if ($anchor === '' || $this->queryAlreadyContainsAllAnchorTokens($shopSearchQuery, $anchor)) {
|
||||
return $shopSearchQuery;
|
||||
}
|
||||
|
||||
$referentialQuery = $this->extractReferentialShopQueryTriggerTerms($combined);
|
||||
if ($referentialQuery === '') {
|
||||
return $shopSearchQuery;
|
||||
}
|
||||
|
||||
$template = $this->agentRunnerConfig->getShopQueryContextAnchorEnrichmentTemplate();
|
||||
$enriched = $this->renderAgentTemplate($template, [
|
||||
'anchor' => $anchor,
|
||||
'query' => $referentialQuery,
|
||||
]);
|
||||
$enriched = preg_replace('/\s+/u', ' ', $enriched) ?? $enriched;
|
||||
$enriched = trim($enriched);
|
||||
|
||||
return $enriched !== '' ? $enriched : $shopSearchQuery;
|
||||
}
|
||||
|
||||
private function extractReferentialShopQueryTriggerTerms(string $text): string
|
||||
{
|
||||
$tokens = $this->tokenizeShopQueryCandidate($text);
|
||||
|
||||
if ($tokens === []) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$triggerTokens = [];
|
||||
foreach ($this->agentRunnerConfig->getShopQueryContextAnchorEnrichmentTriggerTerms() as $term) {
|
||||
foreach ($this->tokenizeShopQueryCandidate($term) as $termToken) {
|
||||
$triggerTokens[$termToken] = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($triggerTokens === []) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$out = [];
|
||||
foreach ($tokens as $token) {
|
||||
if (!isset($triggerTokens[$token]) || isset($out[$token])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$out[$token] = $token;
|
||||
}
|
||||
|
||||
return implode(' ', array_values($out));
|
||||
}
|
||||
|
||||
private function enrichReferentialShopQueryFromHistory(
|
||||
string $query,
|
||||
string $sourcePrompt,
|
||||
@@ -2959,18 +3056,29 @@ final readonly class AgentRunner
|
||||
return $shopResults;
|
||||
}
|
||||
|
||||
$filtered = [];
|
||||
$primaryMatches = [];
|
||||
$corpusMatches = [];
|
||||
|
||||
foreach ($shopResults as $product) {
|
||||
if (!$product instanceof ShopProductResult) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($this->shopProductPrimaryIdentityMatchesAnyDirectProductTerm($product, $requestedTerms)) {
|
||||
$primaryMatches[] = $product;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($this->shopProductMatchesAnyDirectProductTerm($product, $requestedTerms)) {
|
||||
$filtered[] = $product;
|
||||
$corpusMatches[] = $product;
|
||||
}
|
||||
}
|
||||
|
||||
return $filtered;
|
||||
if ($this->agentRunnerConfig->shouldPreferDirectShopResultGuardPrimaryIdentityMatches()) {
|
||||
return $primaryMatches;
|
||||
}
|
||||
|
||||
return array_values(array_merge($primaryMatches, $corpusMatches));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -3226,6 +3334,19 @@ final readonly class AgentRunner
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $requestedTerms
|
||||
*/
|
||||
private function shopProductPrimaryIdentityMatchesAnyDirectProductTerm(ShopProductResult $product, array $requestedTerms): bool
|
||||
{
|
||||
$primaryText = trim(implode(' ', array_filter([
|
||||
$product->name,
|
||||
$product->url,
|
||||
])));
|
||||
|
||||
return $this->textMatchesAnyDirectProductTerm($primaryText, $requestedTerms);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $requestedTerms
|
||||
*/
|
||||
@@ -3236,14 +3357,27 @@ final readonly class AgentRunner
|
||||
$product->description,
|
||||
implode(' ', $product->highlights),
|
||||
$product->customFields,
|
||||
$product->url,
|
||||
])));
|
||||
|
||||
return $this->textMatchesAnyDirectProductTerm($productText, $requestedTerms);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $requestedTerms
|
||||
*/
|
||||
private function textMatchesAnyDirectProductTerm(string $text, array $requestedTerms): bool
|
||||
{
|
||||
if (trim($text) === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($requestedTerms as $term) {
|
||||
if ($this->containsAllShopQueryTokens($productText, $term)) {
|
||||
if ($this->containsAllShopQueryTokens($text, $term)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->containsAllShopQueryTokensWithCompoundPrefixes($productText, $term)) {
|
||||
if ($this->containsAllShopQueryTokensWithCompoundPrefixes($text, $term)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user