53 lines
1.7 KiB
PHP
53 lines
1.7 KiB
PHP
<?php
|
|
// OPUS5 TASK-LOG SSE STREAMING - live event reader
|
|
// Streams new events as they arrive in /tmp/opus5-task-log.jsonl
|
|
|
|
header('Content-Type: text/event-stream');
|
|
header('Cache-Control: no-cache');
|
|
header('X-Accel-Buffering: no');
|
|
header('Access-Control-Allow-Origin: *');
|
|
|
|
$LOG = '/tmp/opus5-task-log.jsonl';
|
|
$session_filter = $_GET['session'] ?? '';
|
|
$max_seconds = min((int)($_GET['duration'] ?? 30), 120);
|
|
|
|
// Initial: send last N events
|
|
if (file_exists($LOG)) {
|
|
$lines = @file($LOG, FILE_IGNORE_NEW_LINES) ?: [];
|
|
$recent = array_slice($lines, -10);
|
|
foreach ($recent as $line) {
|
|
$e = @json_decode($line, true);
|
|
if (!is_array($e)) continue;
|
|
if ($session_filter && ($e['session'] ?? '') !== $session_filter) continue;
|
|
echo "event: history\n";
|
|
echo "data: " . json_encode($e, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES) . "\n\n";
|
|
}
|
|
}
|
|
@ob_flush(); @flush();
|
|
|
|
// Tail for new events
|
|
$fp = @fopen($LOG, 'r');
|
|
if ($fp) {
|
|
fseek($fp, 0, SEEK_END);
|
|
$start = time();
|
|
while (time() - $start < $max_seconds) {
|
|
$line = fgets($fp);
|
|
if ($line === false) {
|
|
if (connection_aborted()) break;
|
|
echo ": heartbeat\n\n";
|
|
@ob_flush(); @flush();
|
|
usleep(500000);
|
|
continue;
|
|
}
|
|
$e = @json_decode(trim($line), true);
|
|
if (!is_array($e)) continue;
|
|
if ($session_filter && ($e['session'] ?? '') !== $session_filter) continue;
|
|
echo "event: task\n";
|
|
echo "data: " . json_encode($e, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES) . "\n\n";
|
|
@ob_flush(); @flush();
|
|
}
|
|
fclose($fp);
|
|
}
|
|
echo "event: done\n";
|
|
echo "data: {}\n\n";
|