optimize code
This commit is contained in:
@@ -14,39 +14,40 @@ use Symfony\Component\Routing\Annotation\Route;
|
||||
final readonly class AskSseController
|
||||
{
|
||||
public function __construct(
|
||||
private AgentRunner $agentRunner,
|
||||
private AgentRunner $agentRunner,
|
||||
private ClientIdResolver $clientIdResolver,
|
||||
) {}
|
||||
) {
|
||||
}
|
||||
|
||||
#[Route('/ask-sse', name: 'ask_sse', methods: ['POST'])]
|
||||
public function stream(Request $request): StreamedResponse
|
||||
{
|
||||
$data = json_decode($request->getContent(), true);
|
||||
$data = json_decode($request->getContent(), true);
|
||||
$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();
|
||||
$clientId = $this->clientIdResolver->resolve($request, $cookieResponse);
|
||||
|
||||
return new StreamedResponse(
|
||||
function () use ($prompt, $clientId, $cookieResponse): void {
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// Disable all PHP output buffering
|
||||
// ---------------------------------------------------------
|
||||
function () use ($prompt, $clientId, $cookieResponse, $includeFullContext): void {
|
||||
while (ob_get_level() > 0) {
|
||||
ob_end_flush();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// Forward cookies
|
||||
// ---------------------------------------------------------
|
||||
foreach ($cookieResponse->headers->getCookies() as $cookie) {
|
||||
header('Set-Cookie: ' . $cookie, false);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// SSE prelude
|
||||
// ---------------------------------------------------------
|
||||
echo "retry: 3000\n\n";
|
||||
flush();
|
||||
|
||||
@@ -56,11 +57,8 @@ final readonly class AskSseController
|
||||
return;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// 🔥 FIXED: Sende Chunks direkt (behält \n!)
|
||||
// ---------------------------------------------------------
|
||||
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);
|
||||
$this->sendData($chunk);
|
||||
}
|
||||
@@ -71,44 +69,30 @@ final readonly class AskSseController
|
||||
);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// Signal completion
|
||||
// ---------------------------------------------------------
|
||||
$this->sendEvent('done', '[DONE]');
|
||||
},
|
||||
200,
|
||||
[
|
||||
'Content-Type' => 'text/event-stream; charset=utf-8',
|
||||
'Cache-Control' => 'no-cache, no-store, must-revalidate',
|
||||
'Connection' => 'keep-alive',
|
||||
'Content-Type' => 'text/event-stream; charset=utf-8',
|
||||
'Cache-Control' => 'no-cache, no-store, must-revalidate',
|
||||
'Connection' => 'keep-alive',
|
||||
'X-Accel-Buffering' => 'no',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
// Split by \n und sende jede Zeile einzeln
|
||||
$lines = explode("\n", $data);
|
||||
|
||||
foreach ($lines as $line) {
|
||||
echo 'data: ' . $line . "\n";
|
||||
}
|
||||
|
||||
// Leere Zeile = Ende der SSE-Message
|
||||
echo "\n\n";
|
||||
flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a named SSE event.
|
||||
*/
|
||||
private function sendEvent(string $event, string $data): void
|
||||
{
|
||||
$safe = str_replace(["\r", "\n"], ' ', $data);
|
||||
|
||||
Reference in New Issue
Block a user