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>
(function() {
const log = document.getElementById('live');
function appendRow(text, cls) {
let placeholder = log.firstChild;
function setPlaceholder(text, cls) {
log.innerHTML = '';
const row = document.createElement('span');
if (cls) row.className = cls;
row.textContent = text + '\n';
log.appendChild(row);
const span = document.createElement('span');
span.className = cls || 'meta';
span.textContent = text;
log.appendChild(span);
placeholder = span;
}
function appendLine(text, cls) {
if (log.firstChild && log.firstChild.className === 'meta') log.innerHTML = '';
if (placeholder) { log.innerHTML = ''; placeholder = null; }
const row = document.createElement('span');
if (cls) row.className = cls;
row.textContent = text + '\n';
@ -144,6 +146,7 @@ const LIVE_PANEL: &str = r#"
return '';
}
const es = new EventSource('/events/stream');
es.onopen = function() { setPlaceholder('(connected waiting for events)'); };
es.onmessage = function(e) {
try {
const ev = JSON.parse(e.data);
@ -152,7 +155,13 @@ const LIVE_PANEL: &str = r#"
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>
"#;
@ -194,7 +203,13 @@ fn render_login_in_progress(session: &Arc<LoginSession>) -> String {
async fn events_stream(
State(state): State<AppState>,
) -> Sse<impl Stream<Item = Result<Event, Infallible>>> {
tracing::info!("sse: client subscribed");
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 ev = res.ok()?;
let json = serde_json::to_string(&ev).ok()?;