feat(astrbot): trace sse frame shapes at debug level

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 <noreply@anthropic.com>
这个提交存在于:
YokiiroBW 2026-07-27 00:59:31 +08:00
父节点 0bfd12ed1a
当前提交 e01cb5a3ba

查看文件

@ -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::<Value>(body.trim()) else {
return;
};
let data = value.get("data");
let data_keys = match data {
Some(Value::Object(map)) => map.keys().cloned().collect::<Vec<_>>().join(","),
Some(Value::Array(_)) => "<array>".to_owned(),
Some(Value::String(_)) => "<string>".to_owned(),
Some(Value::Null) | None => "<none>".to_owned(),
Some(_) => "<scalar>".to_owned(),
};
let frame_type = value
.get("type")
.and_then(|field| field.as_str())
.unwrap_or("<absent>");
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,