From e01cb5a3baaf3377b32302afb34569454dd542a5 Mon Sep 17 00:00:00 2001 From: YokiiroBW Date: Mon, 27 Jul 2026 00:59:31 +0800 Subject: [PATCH] feat(astrbot): trace sse frame shapes at debug level MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two reported defects cannot be diagnosed from the code alone. A reply sometimes renders as two bubbles during generation, which happens when a streaming frame carries no message_id — the client then keys it by event_id, which is unique per event. And images sent by plugins never appear, which happens when an image frame is not followed by the attachment_saved frame the mapper waits for. Both depend on what the server actually sends, and picking the wrong one would produce a fix that looks right until another conversation path hits the other. Log the shape of every SSE frame: its type, whether message_id and session_id are present, the streaming flag, and the key names inside data. No payload text is recorded, so this is safe to leave enabled in the field. Off unless RUST_LOG selects debug, and gated on tracing::enabled! so a disabled level costs one atomic read rather than a JSON parse. Co-Authored-By: Claude Opus 5 --- src-tauri/src/networking/astrbot_client.rs | 52 ++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src-tauri/src/networking/astrbot_client.rs b/src-tauri/src/networking/astrbot_client.rs index 16be14d..8b4ab03 100644 --- a/src-tauri/src/networking/astrbot_client.rs +++ b/src-tauri/src/networking/astrbot_client.rs @@ -614,6 +614,7 @@ impl AstrBotClient { .await; } while let Some(line) = buffer.next_line() { + trace_frame_shape(&line); match mapper.push_line(&line) { Ok(events) => self.dispatch_events(events, sender).await?, Err(error) => { @@ -632,6 +633,7 @@ impl AstrBotClient { } if let Some(remainder) = buffer.take_remainder() { + trace_frame_shape(&remainder); match mapper.push_line(&remainder) { Ok(events) => self.dispatch_events(events, sender).await?, Err(error) => { @@ -720,6 +722,56 @@ impl AstrBotClient { } } +/// Records the *shape* of one SSE frame at debug level. +/// +/// Deliberately logs no payload text — only the frame type, which identity +/// fields are present, and the key names inside `data`. That is enough to +/// answer the two questions the mapper cannot answer on its own: whether every +/// streaming frame carries a `message_id` (if not, the client cannot key a +/// reply to a single message and splits it), and what shape a plugin's +/// attachment frames actually take. +/// +/// Enable with `RUST_LOG=astrbot_desktop=debug`. Off by default. +fn trace_frame_shape(line: &str) { + if !tracing::enabled!(tracing::Level::DEBUG) { + return; + } + let Some(body) = line.strip_prefix("data:") else { + return; + }; + let Ok(value) = serde_json::from_str::(body.trim()) else { + return; + }; + let data = value.get("data"); + let data_keys = match data { + Some(Value::Object(map)) => map.keys().cloned().collect::>().join(","), + Some(Value::Array(_)) => "".to_owned(), + Some(Value::String(_)) => "".to_owned(), + Some(Value::Null) | None => "".to_owned(), + Some(_) => "".to_owned(), + }; + let frame_type = value + .get("type") + .and_then(|field| field.as_str()) + .unwrap_or(""); + let has_message_id = value + .get("message_id") + .is_some_and(|field| !field.is_null()); + let has_session_id = value + .get("session_id") + .is_some_and(|field| !field.is_null()); + let streaming = value.get("streaming").and_then(|field| field.as_bool()); + tracing::debug!( + target: "astrbot::frame", + frame_type, + has_message_id, + has_session_id, + ?streaming, + data_keys = data_keys.as_str(), + "astrbot sse frame" + ); +} + fn protocol_slot(slot: SlotKind) -> desktop_protocol::ActiveSlotKind { match slot { SlotKind::Companion => desktop_protocol::ActiveSlotKind::Companion,