optimize code
This commit is contained in:
@@ -366,7 +366,10 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
const res = await fetch('/ask-sse', {
|
const res = await fetch('/ask-sse', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {'Content-Type': 'application/json'},
|
headers: {'Content-Type': 'application/json'},
|
||||||
body: JSON.stringify({ prompt }),
|
body: JSON.stringify({
|
||||||
|
prompt,
|
||||||
|
fullContext: false,
|
||||||
|
}),
|
||||||
signal: state.abortController.signal,
|
signal: state.abortController.signal,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ final readonly class AgentRunner
|
|||||||
$this->systemMsgOn = true;
|
$this->systemMsgOn = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function run(string $prompt, string $userId, bool $includeFullContext = false): Generator
|
public function run(string $prompt, string $userId, bool $forceFullContext = false): Generator
|
||||||
{
|
{
|
||||||
$prompt = trim($prompt);
|
$prompt = trim($prompt);
|
||||||
|
|
||||||
@@ -66,7 +66,7 @@ final readonly class AgentRunner
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if ($includeFullContext) {
|
if ($forceFullContext) {
|
||||||
// Full context mode is already passed to PromptBuilder.
|
// Full context mode is already passed to PromptBuilder.
|
||||||
// Additional context strategies can be added here later.
|
// Additional context strategies can be added here later.
|
||||||
}
|
}
|
||||||
@@ -164,7 +164,7 @@ final readonly class AgentRunner
|
|||||||
urlContent: $urlContent,
|
urlContent: $urlContent,
|
||||||
knowledgeChunks: $knowledgeChunks,
|
knowledgeChunks: $knowledgeChunks,
|
||||||
shopResults: $shopResults,
|
shopResults: $shopResults,
|
||||||
fullContext: $includeFullContext,
|
fullContext: $forceFullContext,
|
||||||
swagFullOutPut: $optimizedShopQuery
|
swagFullOutPut: $optimizedShopQuery
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -187,7 +187,7 @@ final readonly class AgentRunner
|
|||||||
'userId' => $userId,
|
'userId' => $userId,
|
||||||
'context' => $this->contextService->buildUserContext(
|
'context' => $this->contextService->buildUserContext(
|
||||||
$userId,
|
$userId,
|
||||||
$includeFullContext
|
$forceFullContext
|
||||||
),
|
),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
@@ -217,7 +217,7 @@ final readonly class AgentRunner
|
|||||||
$this->agentLogger->info('Agent run finished', [
|
$this->agentLogger->info('Agent run finished', [
|
||||||
'userId' => $userId,
|
'userId' => $userId,
|
||||||
'outputLength' => mb_strlen($fullOutput),
|
'outputLength' => mb_strlen($fullOutput),
|
||||||
'contextMode' => $includeFullContext ? 'full' : 'recent',
|
'contextMode' => $forceFullContext ? 'full' : 'recent',
|
||||||
'commerceIntent' => $commerceIntent,
|
'commerceIntent' => $commerceIntent,
|
||||||
'primaryShopResultsCount' => count($primaryShopResults),
|
'primaryShopResultsCount' => count($primaryShopResults),
|
||||||
'shopResultsCount' => count($shopResults),
|
'shopResultsCount' => count($shopResults),
|
||||||
|
|||||||
@@ -16,7 +16,8 @@ final readonly class AskSseController
|
|||||||
public function __construct(
|
public function __construct(
|
||||||
private AgentRunner $agentRunner,
|
private AgentRunner $agentRunner,
|
||||||
private ClientIdResolver $clientIdResolver,
|
private ClientIdResolver $clientIdResolver,
|
||||||
) {}
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
#[Route('/ask-sse', name: 'ask_sse', methods: ['POST'])]
|
#[Route('/ask-sse', name: 'ask_sse', methods: ['POST'])]
|
||||||
public function stream(Request $request): StreamedResponse
|
public function stream(Request $request): StreamedResponse
|
||||||
@@ -24,29 +25,29 @@ final readonly class AskSseController
|
|||||||
$data = json_decode($request->getContent(), true);
|
$data = json_decode($request->getContent(), true);
|
||||||
$prompt = trim((string) ($data['prompt'] ?? ''));
|
$prompt = trim((string) ($data['prompt'] ?? ''));
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default:
|
||||||
|
* - Browser chat uses budgeted recent context
|
||||||
|
* - Full context must be explicitly requested
|
||||||
|
*/
|
||||||
|
$includeFullContext = filter_var(
|
||||||
|
$data['fullContext'] ?? false,
|
||||||
|
FILTER_VALIDATE_BOOL
|
||||||
|
);
|
||||||
|
|
||||||
$cookieResponse = new Response();
|
$cookieResponse = new Response();
|
||||||
$clientId = $this->clientIdResolver->resolve($request, $cookieResponse);
|
$clientId = $this->clientIdResolver->resolve($request, $cookieResponse);
|
||||||
|
|
||||||
return new StreamedResponse(
|
return new StreamedResponse(
|
||||||
function () use ($prompt, $clientId, $cookieResponse): void {
|
function () use ($prompt, $clientId, $cookieResponse, $includeFullContext): void {
|
||||||
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
// Disable all PHP output buffering
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
while (ob_get_level() > 0) {
|
while (ob_get_level() > 0) {
|
||||||
ob_end_flush();
|
ob_end_flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
// Forward cookies
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
foreach ($cookieResponse->headers->getCookies() as $cookie) {
|
foreach ($cookieResponse->headers->getCookies() as $cookie) {
|
||||||
header('Set-Cookie: ' . $cookie, false);
|
header('Set-Cookie: ' . $cookie, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
// SSE prelude
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
echo "retry: 3000\n\n";
|
echo "retry: 3000\n\n";
|
||||||
flush();
|
flush();
|
||||||
|
|
||||||
@@ -56,11 +57,8 @@ final readonly class AskSseController
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
// 🔥 FIXED: Sende Chunks direkt (behält \n!)
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
try {
|
try {
|
||||||
foreach ($this->agentRunner->run($prompt, $clientId, true) as $chunk) {
|
foreach ($this->agentRunner->run($prompt, $clientId, $includeFullContext) as $chunk) {
|
||||||
$chunk = str_replace(["\r\n", "\r"], "\n", $chunk);
|
$chunk = str_replace(["\r\n", "\r"], "\n", $chunk);
|
||||||
$this->sendData($chunk);
|
$this->sendData($chunk);
|
||||||
}
|
}
|
||||||
@@ -71,9 +69,6 @@ final readonly class AskSseController
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
// Signal completion
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
$this->sendEvent('done', '[DONE]');
|
$this->sendEvent('done', '[DONE]');
|
||||||
},
|
},
|
||||||
200,
|
200,
|
||||||
@@ -86,29 +81,18 @@ final readonly class AskSseController
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* FIXED: Behält Markdown-Struktur (\n) bei
|
|
||||||
*
|
|
||||||
* SSE erlaubt mehrere "data:"-Zeilen pro Event.
|
|
||||||
* Jede Zeile wird als separate data-Zeile gesendet.
|
|
||||||
*/
|
|
||||||
private function sendData(string $data): void
|
private function sendData(string $data): void
|
||||||
{
|
{
|
||||||
// Split by \n und sende jede Zeile einzeln
|
|
||||||
$lines = explode("\n", $data);
|
$lines = explode("\n", $data);
|
||||||
|
|
||||||
foreach ($lines as $line) {
|
foreach ($lines as $line) {
|
||||||
echo 'data: ' . $line . "\n";
|
echo 'data: ' . $line . "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Leere Zeile = Ende der SSE-Message
|
|
||||||
echo "\n\n";
|
echo "\n\n";
|
||||||
flush();
|
flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Sends a named SSE event.
|
|
||||||
*/
|
|
||||||
private function sendEvent(string $event, string $data): void
|
private function sendEvent(string $event, string $data): void
|
||||||
{
|
{
|
||||||
$safe = str_replace(["\r", "\n"], ' ', $data);
|
$safe = str_replace(["\r", "\n"], ' ', $data);
|
||||||
|
|||||||
Reference in New Issue
Block a user