fix shop research
This commit is contained in:
@@ -39,7 +39,7 @@ final readonly class AgentRunner
|
||||
$this->systemMsgOn = true;
|
||||
}
|
||||
|
||||
public function run(string $prompt, string $userId, bool $forceFullContext = false): Generator
|
||||
public function run(string $prompt, string $userId, bool $forceFullContext = false, string $requestContextHint = ''): Generator
|
||||
{
|
||||
$prompt = trim($prompt);
|
||||
|
||||
@@ -109,7 +109,7 @@ final readonly class AgentRunner
|
||||
if ($this->isCommerceIntent($commerceIntent)) {
|
||||
yield $this->systemMsg($this->agentRunnerConfig->getOptimizeSearchMessage(), 'think');
|
||||
|
||||
$commerceHistoryContext = $this->buildCommerceHistoryContext($userId);
|
||||
$commerceHistoryContext = $this->buildCommerceHistoryContext($userId, $requestContextHint);
|
||||
|
||||
if ($commerceHistoryContext !== '') {
|
||||
$this->addSource($sources, $this->agentRunnerConfig->getConversationHistorySourceLabel());
|
||||
@@ -136,6 +136,7 @@ final readonly class AgentRunner
|
||||
'optimizedShopQuery' => $optimizedShopQuery,
|
||||
'hasCommerceHistoryContext' => $commerceHistoryContext !== '',
|
||||
'commerceHistoryContextLength' => mb_strlen($commerceHistoryContext),
|
||||
'hasRequestContextHint' => trim($requestContextHint) !== '',
|
||||
]);
|
||||
|
||||
yield $this->systemMsg(
|
||||
@@ -925,12 +926,42 @@ final readonly class AgentRunner
|
||||
}
|
||||
}
|
||||
|
||||
private function buildCommerceHistoryContext(string $userId): string
|
||||
private function buildCommerceHistoryContext(string $userId, string $requestContextHint = ''): string
|
||||
{
|
||||
return $this->contextService->buildUserContextWithinBudget(
|
||||
$history = $this->contextService->buildUserContextWithinBudget(
|
||||
$userId,
|
||||
$this->agentRunnerConfig->getCommerceHistoryBudgetChars()
|
||||
);
|
||||
|
||||
$requestContextHint = $this->sanitizeRequestContextHintForCommerce($requestContextHint);
|
||||
|
||||
if ($requestContextHint === '') {
|
||||
return $history;
|
||||
}
|
||||
|
||||
if ($history === '') {
|
||||
return $requestContextHint;
|
||||
}
|
||||
|
||||
return trim($history) . "\n\n" . $requestContextHint;
|
||||
}
|
||||
|
||||
private function sanitizeRequestContextHintForCommerce(string $requestContextHint): string
|
||||
{
|
||||
$requestContextHint = str_replace(["\r\n", "\r"], "\n", $requestContextHint);
|
||||
$requestContextHint = preg_replace('/[\t ]+/u', ' ', $requestContextHint) ?? $requestContextHint;
|
||||
$requestContextHint = preg_replace('/\n{3,}/u', "\n\n", $requestContextHint) ?? $requestContextHint;
|
||||
$requestContextHint = trim($requestContextHint);
|
||||
|
||||
if ($requestContextHint === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (mb_strlen($requestContextHint, 'UTF-8') > 4000) {
|
||||
$requestContextHint = mb_substr($requestContextHint, 0, 4000, 'UTF-8');
|
||||
}
|
||||
|
||||
return trim($requestContextHint);
|
||||
}
|
||||
|
||||
private function limitKnowledgeChunks(array $knowledgeChunks, string $commerceIntent): array
|
||||
|
||||
Reference in New Issue
Block a user