agent ui: handle SSE open + emit hello note on subscribe

This commit is contained in:
müde 2026-05-15 15:23:35 +02:00
parent 09787659ab
commit 3c493934da

View file

@ -90,15 +90,17 @@ const LIVE_PANEL: &str = r#"
<script> <script>
(function() { (function() {
const log = document.getElementById('live'); const log = document.getElementById('live');
function appendRow(text, cls) { let placeholder = log.firstChild;
function setPlaceholder(text, cls) {
log.innerHTML = ''; log.innerHTML = '';
const row = document.createElement('span'); const span = document.createElement('span');
if (cls) row.className = cls; span.className = cls || 'meta';
row.textContent = text + '\n'; span.textContent = text;
log.appendChild(row); log.appendChild(span);
placeholder = span;
} }
function appendLine(text, cls) { function appendLine(text, cls) {
if (log.firstChild && log.firstChild.className === 'meta') log.innerHTML = ''; if (placeholder) { log.innerHTML = ''; placeholder = null; }
const row = document.createElement('span'); const row = document.createElement('span');
if (cls) row.className = cls; if (cls) row.className = cls;
row.textContent = text + '\n'; row.textContent = text + '\n';
@ -144,6 +146,7 @@ const LIVE_PANEL: &str = r#"
return ''; return '';
} }
const es = new EventSource('/events/stream'); const es = new EventSource('/events/stream');
es.onopen = function() { setPlaceholder('(connected waiting for events)'); };
es.onmessage = function(e) { es.onmessage = function(e) {
try { try {
const ev = JSON.parse(e.data); const ev = JSON.parse(e.data);
@ -152,7 +155,13 @@ const LIVE_PANEL: &str = r#"
appendLine('[parse err] ' + e.data, 'meta'); appendLine('[parse err] ' + e.data, 'meta');
} }
}; };
es.onerror = function() { appendLine('[disconnected retrying]', 'meta'); }; es.onerror = function() {
if (es.readyState === EventSource.CONNECTING) {
setPlaceholder('(reconnecting)');
} else {
appendLine('[disconnected]', 'meta');
}
};
})(); })();
</script> </script>
"#; "#;
@ -194,7 +203,13 @@ fn render_login_in_progress(session: &Arc<LoginSession>) -> String {
async fn events_stream( async fn events_stream(
State(state): State<AppState>, State(state): State<AppState>,
) -> Sse<impl Stream<Item = Result<Event, Infallible>>> { ) -> Sse<impl Stream<Item = Result<Event, Infallible>>> {
tracing::info!("sse: client subscribed");
let rx = state.bus.subscribe(); let rx = state.bus.subscribe();
// Drop a "hello" note into the bus so every new subscriber sees at
// least one event immediately and can clear the connecting placeholder.
state
.bus
.emit(crate::events::LiveEvent::Note("live stream attached".into()));
let stream = BroadcastStream::new(rx).filter_map(|res| { let stream = BroadcastStream::new(rx).filter_map(|res| {
let ev = res.ok()?; let ev = res.ok()?;
let json = serde_json::to_string(&ev).ok()?; let json = serde_json::to_string(&ev).ok()?;