optimize msg flow
This commit is contained in:
@@ -85,6 +85,28 @@ final readonly class AskSseController
|
||||
return $response;
|
||||
}
|
||||
|
||||
#[Route('/ask-jobs/{jobId}', name: 'ask_job_status', methods: ['GET'], requirements: ['jobId' => '[a-f0-9]{48}'])]
|
||||
public function jobStatus(string $jobId): JsonResponse
|
||||
{
|
||||
$job = $this->readJob($jobId);
|
||||
|
||||
if (!is_array($job)) {
|
||||
return new JsonResponse([
|
||||
'status' => 'missing',
|
||||
'message' => 'Der Antwort-Job wurde nicht gefunden.',
|
||||
'lastEventId' => 0,
|
||||
], Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
return new JsonResponse([
|
||||
'status' => (string) ($job['status'] ?? ''),
|
||||
'message' => is_string($job['message'] ?? null) ? (string) $job['message'] : '',
|
||||
'lastEventId' => max(0, (int) ($job['lastEventId'] ?? 0)),
|
||||
'updatedAt' => max(0, (int) ($job['updatedAt'] ?? 0)),
|
||||
'completedAt' => max(0, (int) ($job['completedAt'] ?? 0)),
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/ask-sse/{jobId}', name: 'ask_sse_job', methods: ['GET'], requirements: ['jobId' => '[a-f0-9]{48}'])]
|
||||
public function streamJob(Request $request, string $jobId): StreamedResponse
|
||||
{
|
||||
@@ -96,7 +118,7 @@ final readonly class AskSseController
|
||||
|
||||
if (($claimed['ok'] ?? false) !== true) {
|
||||
$this->prepareStreamRuntime();
|
||||
echo "retry: 30000\n\n";
|
||||
echo "retry: 3000\n\n";
|
||||
|
||||
if ($this->canReplayOrTailClaimedJob($claimed)) {
|
||||
$this->streamStoredJobResponse($jobId, $lastEventId);
|
||||
@@ -104,7 +126,7 @@ final readonly class AskSseController
|
||||
}
|
||||
|
||||
$this->sendEvent('error', $this->jobClaimErrorMessage($claimed));
|
||||
$this->sendEvent('done', '[DONE]');
|
||||
$this->sendDoneEvent();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -179,13 +201,13 @@ final readonly class AskSseController
|
||||
}
|
||||
}
|
||||
|
||||
echo "retry: 15000\n\n";
|
||||
echo "retry: 3000\n\n";
|
||||
$this->sendComment('stream-open');
|
||||
|
||||
if ($prompt === '') {
|
||||
$this->markJobStatus($jobId, self::JOB_STATUS_FAILED, 'Empty prompt');
|
||||
$this->sendEvent('error', 'Empty prompt');
|
||||
$this->sendEvent('done', '[DONE]');
|
||||
$this->sendDoneEvent();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -216,12 +238,12 @@ final readonly class AskSseController
|
||||
$this->appendHistoryFailure($clientId, $prompt, $message);
|
||||
}
|
||||
|
||||
$this->sendEvent('done', '[DONE]');
|
||||
$this->sendDoneEvent();
|
||||
return;
|
||||
}
|
||||
|
||||
$this->markJobStatus($jobId, self::JOB_STATUS_COMPLETED);
|
||||
$this->sendEvent('done', '[DONE]');
|
||||
$this->sendDoneEvent();
|
||||
}
|
||||
|
||||
private function appendHistoryFailure(string $clientId, string $prompt, string $message): void
|
||||
@@ -267,7 +289,7 @@ final readonly class AskSseController
|
||||
|
||||
$this->markJobStatus($jobId, self::JOB_STATUS_FAILED, $message);
|
||||
$this->sendEvent('error', $message);
|
||||
$this->sendEvent('done', '[DONE]');
|
||||
$this->sendDoneEvent();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -355,6 +377,15 @@ final readonly class AskSseController
|
||||
$this->flushOutput();
|
||||
}
|
||||
|
||||
private function sendDoneEvent(): void
|
||||
{
|
||||
$this->sendEvent('done', '[DONE]');
|
||||
|
||||
if (function_exists('fastcgi_finish_request')) {
|
||||
@fastcgi_finish_request();
|
||||
}
|
||||
}
|
||||
|
||||
private function sendComment(string $comment): void
|
||||
{
|
||||
$safe = str_replace(["\r", "\n"], ' ', $comment);
|
||||
@@ -605,7 +636,7 @@ final readonly class AskSseController
|
||||
|
||||
if ($status === self::JOB_STATUS_COMPLETED) {
|
||||
$this->sendComment('replayed-completed-stream');
|
||||
$this->sendEvent('done', '[DONE]');
|
||||
$this->sendDoneEvent();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -616,7 +647,7 @@ final readonly class AskSseController
|
||||
? 'Der Antwort-Stream ist fehlgeschlagen: ' . $message
|
||||
: 'Der Antwort-Stream ist fehlgeschlagen. Bitte sende die Anfrage erneut.'
|
||||
);
|
||||
$this->sendEvent('done', '[DONE]');
|
||||
$this->sendDoneEvent();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -627,7 +658,7 @@ final readonly class AskSseController
|
||||
? $message
|
||||
: 'Der Antwort-Stream wurde durch einen Verbindungsabbruch unterbrochen. Bitte sende die Anfrage erneut, falls die Antwort unvollständig ist.'
|
||||
);
|
||||
$this->sendEvent('done', '[DONE]');
|
||||
$this->sendDoneEvent();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -637,7 +668,7 @@ final readonly class AskSseController
|
||||
'status' => $status,
|
||||
'message' => $message,
|
||||
]));
|
||||
$this->sendEvent('done', '[DONE]');
|
||||
$this->sendDoneEvent();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user