add history to shop search
This commit is contained in:
@@ -17,6 +17,8 @@ use Throwable;
|
||||
|
||||
final readonly class AgentRunner
|
||||
{
|
||||
private const COMMERCE_HISTORY_BUDGET_CHARS = 1000;
|
||||
|
||||
private bool $systemMsgOn;
|
||||
|
||||
public function __construct(
|
||||
@@ -46,7 +48,6 @@ final readonly class AgentRunner
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
$shopResults = [];
|
||||
$sources = [];
|
||||
$optimizedShopQuery = '';
|
||||
@@ -94,7 +95,18 @@ final readonly class AgentRunner
|
||||
if ($this->isCommerceIntent($commerceIntent)) {
|
||||
yield $this->systemMsg('Ich optimiere die Recherche...', 'think');
|
||||
|
||||
$optimizedShopQuery = $this->buildOptimizedShopQuery($prompt, $userId);
|
||||
$commerceHistoryContext = $this->buildCommerceHistoryContext($userId);
|
||||
|
||||
if($commerceHistoryContext){
|
||||
$this->addSource($sources, 'Chatverlauf');
|
||||
}
|
||||
|
||||
$optimizedShopQuery = $this->buildOptimizedShopQuery(
|
||||
$prompt,
|
||||
$userId,
|
||||
$commerceHistoryContext
|
||||
);
|
||||
|
||||
$shopSearchQuery = $optimizedShopQuery !== '' ? $optimizedShopQuery : $prompt;
|
||||
|
||||
yield $this->systemMsg(
|
||||
@@ -102,7 +114,12 @@ final readonly class AgentRunner
|
||||
'think'
|
||||
);
|
||||
|
||||
$shopResults = $this->searchShop($shopSearchQuery, $commerceIntent, $userId);
|
||||
$shopResults = $this->searchShop(
|
||||
$shopSearchQuery,
|
||||
$commerceIntent,
|
||||
$userId,
|
||||
$commerceHistoryContext
|
||||
);
|
||||
|
||||
if ($shopResults !== []) {
|
||||
$this->addSource($sources, 'Shopsystem');
|
||||
@@ -157,8 +174,8 @@ final readonly class AgentRunner
|
||||
yield $this->emitSources($sources, 'Quellen: ');
|
||||
}
|
||||
|
||||
if($this->debug){
|
||||
yield $this->systemMsg($this->systemMsg($finalPrompt), 'debug');
|
||||
if ($this->debug) {
|
||||
yield $this->systemMsg($finalPrompt, 'debug');
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------
|
||||
@@ -205,9 +222,15 @@ final readonly class AgentRunner
|
||||
|| $commerceIntent === CommerceIntentLite::ADVISORY_PRODUCT_SEARCH;
|
||||
}
|
||||
|
||||
private function buildOptimizedShopQuery(string $prompt, string $userId): string
|
||||
{
|
||||
$shopPrompt = trim($this->agentRunnerConfig->getShopPrompt($prompt));
|
||||
private function buildOptimizedShopQuery(
|
||||
string $prompt,
|
||||
string $userId,
|
||||
string $commerceHistoryContext = ''
|
||||
): string {
|
||||
$shopPrompt = trim($this->agentRunnerConfig->getShopPrompt(
|
||||
$prompt,
|
||||
$commerceHistoryContext
|
||||
));
|
||||
|
||||
if ($shopPrompt === '') {
|
||||
return '';
|
||||
@@ -242,10 +265,18 @@ final readonly class AgentRunner
|
||||
return trim($optimizedQuery);
|
||||
}
|
||||
|
||||
private function searchShop(string $query, string $commerceIntent, string $userId): array
|
||||
{
|
||||
private function searchShop(
|
||||
string $query,
|
||||
string $commerceIntent,
|
||||
string $userId,
|
||||
string $commerceHistoryContext = ''
|
||||
): array {
|
||||
try {
|
||||
return $this->shopSearchService->search($query, $commerceIntent);
|
||||
return $this->shopSearchService->search(
|
||||
$query,
|
||||
$commerceIntent,
|
||||
$commerceHistoryContext
|
||||
);
|
||||
} catch (Throwable $e) {
|
||||
$this->agentLogger->warning('Shop search failed, continuing without shop results', [
|
||||
'userId' => $userId,
|
||||
@@ -258,6 +289,14 @@ final readonly class AgentRunner
|
||||
}
|
||||
}
|
||||
|
||||
private function buildCommerceHistoryContext(string $userId): string
|
||||
{
|
||||
return $this->contextService->buildUserContextWithinBudget(
|
||||
$userId,
|
||||
self::COMMERCE_HISTORY_BUDGET_CHARS
|
||||
);
|
||||
}
|
||||
|
||||
private function limitKnowledgeChunks(array $knowledgeChunks, string $commerceIntent): array
|
||||
{
|
||||
return match ($commerceIntent) {
|
||||
@@ -361,7 +400,7 @@ final readonly class AgentRunner
|
||||
'err' => '<span class="text-danger">' . $msg . "</span>\n<hr>\n",
|
||||
'think' => '<span class="text-info think">' . $msg . "</span>\n",
|
||||
'info' => "\n\n<span class=\"text-info fw-bolder\">" . $msg . "</span>\n",
|
||||
'debug' => "\n\nDEBUG: <code>" . $msg . "</code>\n",
|
||||
'debug' => "\n\nDEBUG: <code>" . htmlspecialchars($msg, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . "</code>\n",
|
||||
default => $msg,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user