This commit is contained in:
gr
2026-10-27 16:33:30 +08:00
423 changed files with 72666 additions and 5389 deletions
@@ -9,8 +9,12 @@ namespace app\adminapi\service;
*/
final class AssistantSseProtocol
{
private const HEARTBEAT_INTERVAL_NS = 10_000_000_000;
private int $seq = 0;
private int $lastOutputAtNs = 0;
private bool $started = false;
private bool $terminal = false;
@@ -36,12 +40,23 @@ final class AssistantSseProtocol
$this->seq = $nextSeq;
$this->started = true;
$this->lastOutputAtNs = hrtime(true);
if (in_array($event, ['done', 'error'], true)) {
$this->terminal = true;
}
return 'event: ' . $event . "\n" . 'data: ' . $encoded . "\n\n";
}
/** Keep an idle authenticated stream open without inventing application events. */
public function heartbeat(): ?string
{
if (!$this->started || $this->terminal) return null;
$now = hrtime(true);
if ($now - $this->lastOutputAtNs < self::HEARTBEAT_INTERVAL_NS) return null;
$this->lastOutputAtNs = $now;
return ": keep-alive\n\n";
}
public function isTerminal(): bool
{
return $this->terminal;