diff --git a/crates/desktop-protocol/src/astrbot.rs b/crates/desktop-protocol/src/astrbot.rs index 077f111..403a754 100644 --- a/crates/desktop-protocol/src/astrbot.rs +++ b/crates/desktop-protocol/src/astrbot.rs @@ -824,7 +824,10 @@ mod tests { assert_eq!(completions[0], ("m1", "完整的回复正文")); // The attachment lands in a message of its own, leaving the reply intact, // and names the file rather than the id of the frame that followed. - assert_eq!(completions[1].1, "![image](astrbot-attachment://sticker.png)"); + assert_eq!( + completions[1].1, + "![image](astrbot-attachment://sticker.png)" + ); assert_ne!( completions[1].0, "m1", "the attachment must not reuse the finished message id" diff --git a/src-tauri/src/networking/astrbot_client.rs b/src-tauri/src/networking/astrbot_client.rs index 6339b67..1b97e44 100644 --- a/src-tauri/src/networking/astrbot_client.rs +++ b/src-tauri/src/networking/astrbot_client.rs @@ -873,6 +873,16 @@ impl FrameTrace { trace_frame_fields(&value); } + /// Drops whitespace so a block differing only in line breaks still matches. + /// + /// Exact containment could not separate "the same text formatted + /// differently" from "content the stream never carried": one capture had a + /// 278-character block against 286 streamed characters and reported no + /// containment either way. + fn squeezed(text: &str) -> String { + text.chars().filter(|c| !c.is_whitespace()).collect() + } + fn report(&self, final_text: &str) { let repeated = self .blocks @@ -884,12 +894,19 @@ impl FrameTrace { .iter() .filter(|block| self.streamed.contains(block.as_str())) .count(); + let squeezed_final = Self::squeezed(final_text); + let squeezed_repeated = self + .blocks + .iter() + .filter(|block| squeezed_final.contains(&Self::squeezed(block))) + .count(); let joined = self.blocks.concat(); tracing::debug!( target: FRAME_TRACE_TARGET, blocks = self.blocks.len(), blocks_repeated_in_final = repeated, blocks_repeated_in_stream = in_streamed, + blocks_repeated_ignoring_whitespace = squeezed_repeated, streamed_len = self.streamed.chars().count(), final_len = final_text.chars().count(), blocks_len = joined.chars().count(), @@ -1143,6 +1160,7 @@ mod tests { assert!(output.contains("blocks=2")); assert!(output.contains("blocks_repeated_in_final=1")); assert!(output.contains("blocks_repeated_in_stream=1")); + assert!(output.contains("blocks_repeated_ignoring_whitespace=1")); assert!(output.contains("streamed_len=17")); assert!(output.contains("final_len=17")); assert!(output.contains("blocks_len=18")); @@ -1175,6 +1193,40 @@ mod tests { assert_eq!(sniff_image_mime(b""), None); } + /// A block that differs from the streamed text only in line breaks is the + /// same content, and must not be counted as content the stream never + /// carried. Exact containment says otherwise, which is why both are logged. + #[test] + fn stream_summary_sees_through_formatting_differences() { + let sink = CapturedLog::default(); + let subscriber = tracing_subscriber::fmt() + .without_time() + .with_ansi(false) + .with_env_filter(tracing_subscriber::EnvFilter::new("astrbot::frame=debug")) + .with_writer(sink.clone()) + .finish(); + + let mut trace = FrameTrace::default(); + tracing::subscriber::with_default(subscriber, || { + for line in [ + r#"data: {"type":"session_id","session_id":"s1"}"#, + r#"data: {"type":"plain","data":"ONE\nTWO","streaming":true}"#, + // Same words, different line breaks. + r#"data: {"type":"plain","data":"ONE TWO","streaming":false}"#, + r#"data: {"type":"complete","data":"ONE\nTWO"}"#, + ] { + trace.observe(line); + } + }); + + let output = sink.contents(); + assert!(output.contains("blocks_repeated_in_final=0"), "{output}"); + assert!( + output.contains("blocks_repeated_ignoring_whitespace=1"), + "{output}" + ); + } + #[test] fn frame_trace_stays_silent_when_the_filter_excludes_it() { let sink = CapturedLog::default();