diff --git a/crates/desktop-protocol/src/astrbot.rs b/crates/desktop-protocol/src/astrbot.rs index e49544f..4e9b62b 100644 --- a/crates/desktop-protocol/src/astrbot.rs +++ b/crates/desktop-protocol/src/astrbot.rs @@ -430,24 +430,42 @@ impl AstrBotStreamMapper { events.extend(self.emit_delta(chunk)); return events; } - // `streaming: false` means the frame carries a whole segment rather than - // a fragment of one. Appending it to the open accumulation is what made - // segmented replies collapse into a single bubble, and then lose all but - // one segment when `complete` replaced the accumulation wholesale. - events.extend(self.emit_block(chunk)); + // `streaming: false` carries a whole block rather than a fragment, and + // the block may be new content or a restatement of text the delta + // stream already delivered — AstrBot does both, sometimes in one reply. + // Appending it blindly is what made segmented replies collapse into one + // bubble; emitting it blindly would show restated text twice. + events.extend(self.reconcile(chunk)); events } - /// Emits a self-contained segment as a message of its own. - fn emit_block(&mut self, chunk: String) -> Vec { - let mut events = Vec::new(); - if self.saw_assistant_delta { - // Streamed text is open under this id; the segment is not part of - // it, so close it rather than merging the two. - events.extend(self.complete_if_needed(FinishReason::Stop)); + /// Shows `text`, or nothing if the client has already been shown it. + /// + /// Shared by the `streaming: false` plain frames and by `complete`, because + /// both restate text: sometimes verbatim, sometimes with different line + /// breaks, and sometimes carrying content the delta stream never delivered. + /// The three cases are ordered so that no branch can lose text, and none can + /// show the same text twice. + fn reconcile(&mut self, text: String) -> Vec { + if text.trim().is_empty() { + return Vec::new(); } - events.extend(self.emit_delta(chunk)); - events.extend(self.complete_if_needed(FinishReason::Stop)); + // 1. Extends what was shown, byte for byte — the streaming case, where + // deltas are fragments. Only the tail is new; an empty tail emits + // nothing, which is also how a verbatim restatement lands here. + if let Some(tail) = text.strip_prefix(self.delivered.as_str()) { + return self.emit_delta(tail.to_owned()); + } + // 2. Already shown, allowing for the same text rendered with different + // whitespace. + if squeeze_whitespace(&self.delivered).contains(&squeeze_whitespace(&text)) { + return Vec::new(); + } + // 3. Genuinely new. It gets a message of its own: appending it to the + // open one would glue two independent blocks into a single bubble, + // which is the shape of the bug this replaced. + let mut events = self.complete_if_needed(FinishReason::Stop); + events.extend(self.emit_delta(text)); events } @@ -456,16 +474,22 @@ impl AstrBotStreamMapper { let Some(message_id) = message_id else { return Vec::new(); }; - if self - .active_base_id() - .is_some_and(|current| current != message_id) - { - let events = self.complete_if_needed(FinishReason::Stop); - self.start_message(message_id); - return events; + match self.active_base_id() { + Some(current) if current != message_id => { + let events = self.complete_if_needed(FinishReason::Stop); + self.start_message(message_id); + events + } + // Same base id: leave the active id alone. It may carry a reopen + // suffix that is the only thing distinguishing a later block from an + // earlier one under the server's single id, and assigning the bare + // id back over it merges them again. + Some(_) => Vec::new(), + None => { + self.active_message_id = Some(message_id); + Vec::new() + } } - self.active_message_id = Some(message_id); - Vec::new() } /// Begins a fresh message under a server-provided id. @@ -482,39 +506,11 @@ impl AstrBotStreamMapper { /// This used to assign `accumulated = final_text` outright. That is right /// when the frame restates a streamed reply in full, and wrong in every /// other case: for a segmented reply whose `complete` frame carries one - /// segment, the assignment threw away every other segment. The three cases - /// below are ordered so that no branch can lose text the client has seen. + /// segment, the assignment threw away every other segment. fn map_complete(&mut self, frame: AstrBotStreamFrame) -> Vec { let mut events = self.adopt_message_id(frame.message_id.clone()); - let final_text = text_from_value(&frame.data).unwrap_or_default(); - if final_text.trim().is_empty() { - events.extend(self.complete_if_needed(FinishReason::Stop)); - return events; - } - - // 1. The frame extends what was shown, byte for byte — the streaming - // case, where deltas are fragments and `complete` carries the whole. - // Emitting only the tail keeps the open message intact. - if let Some(tail) = final_text.strip_prefix(self.delivered.as_str()) { - if !tail.is_empty() { - events.extend(self.emit_delta(tail.to_owned())); - } - events.extend(self.complete_if_needed(FinishReason::Stop)); - return events; - } - - // 2. Everything in the frame has already been shown. Compared with - // whitespace squeezed so that a differently wrapped restatement of - // the same text is not mistaken for new content. - if squeeze_whitespace(&self.delivered).contains(&squeeze_whitespace(&final_text)) { - events.extend(self.complete_if_needed(FinishReason::Stop)); - return events; - } - - // 3. The frame diverges from what was shown. Keep both: close the open - // message with what it has, then give the frame's text its own. + events.extend(self.reconcile(text_from_value(&frame.data).unwrap_or_default())); events.extend(self.complete_if_needed(FinishReason::Stop)); - events.extend(self.emit_block(final_text)); events } @@ -848,6 +844,49 @@ mod tests { assert_eq!(done[1].1, "乙"); } + /// A frame whose text is only whitespace must not become a message. + /// + /// Without the guard in `reconcile`, an empty `delivered` makes any text a + /// prefix extension of it, so a `complete` frame carrying two spaces would + /// emit a delta of two spaces and complete a message containing them. + #[test] + fn a_whitespace_only_frame_emits_nothing() { + let events = drive(&[r#"data: {"type":"complete","data":" ","message_id":"m1"}"#]); + assert!(completions(&events).is_empty(), "{events:?}"); + assert!( + !events + .iter() + .any(|event| matches!(event.body, GatewayEventBody::MessageDelta(_))), + "{events:?}" + ); + } + + /// A `streaming: false` block restating the delta stream is not shown twice. + /// + /// Regression for the first attempt at N-9, which treated every + /// non-streaming block as a new segment. AstrBot sends both kinds, sometimes + /// in one reply — this is the shape recorded in the frame-trace fixtures at + /// `astrbot_client.rs`: one block repeating what the deltas carried, one + /// carrying text the deltas never delivered. + #[test] + fn a_block_restating_the_delta_stream_is_not_shown_twice() { + let events = drive(&[ + r#"data: {"type":"plain","data":"ALPHA","streaming":true,"message_id":"m1"}"#, + r#"data: {"type":"plain","data":"ALPHA","streaming":false,"message_id":"m1"}"#, + r#"data: {"type":"plain","data":"BETA","streaming":true,"message_id":"m1"}"#, + r#"data: {"type":"plain","data":"GAMMA","streaming":false,"message_id":"m1"}"#, + r#"data: {"type":"complete","data":"ALPHABETA","message_id":"m1"}"#, + ]); + + let done = completions(&events); + // ALPHA is not repeated, and GAMMA is not glued onto the end of BETA. + assert_eq!(done.len(), 2, "{done:?}"); + assert_eq!(done[0].1, "ALPHABETA"); + assert_eq!(done[1].1, "GAMMA"); + assert_eq!(done[0].0, "m1"); + assert!(done[1].0.starts_with("m1#part-"), "{done:?}"); + } + /// Whitespace-only differences are not new content. #[test] fn a_reflowed_restatement_is_not_shown_twice() { diff --git a/src/features/pet/petEntry.test.ts b/src/features/pet/petEntry.test.ts index ff9cfd1..a9e673b 100644 --- a/src/features/pet/petEntry.test.ts +++ b/src/features/pet/petEntry.test.ts @@ -1,7 +1,11 @@ -import { readFileSync } from "node:fs"; - import { describe, expect, it } from "vitest"; +// Imported as raw text rather than read with node:fs. tsconfig.app.json sets an +// explicit `types` array, so Node globals are not in scope here — and adding +// them would put Node's API surface in front of the browser code in src/. +import petHtml from "../../../pet.html?raw"; +import tauriConfigJson from "../../../src-tauri/tauri.conf.json?raw"; + /** * The pet window's transparency rests entirely on `pet.html` carrying its * marker attributes statically, and on the Tauri window config pointing at that @@ -10,7 +14,7 @@ import { describe, expect, it } from "vitest"; * and the same thing happens with the file still sitting there looking correct. */ describe("the pet window entry point", () => { - const html = readFileSync("pet.html", "utf8"); + const html = petHtml; it("marks the document before any script runs", () => { const openingTag = html.match(/]*>/)?.[0] ?? ""; @@ -30,9 +34,9 @@ describe("the pet window entry point", () => { }); it("is the file the pet window actually loads", () => { - const config = JSON.parse( - readFileSync("src-tauri/tauri.conf.json", "utf8"), - ) as { app: { windows: Array> } }; + const config = JSON.parse(tauriConfigJson) as { + app: { windows: Array> }; + }; const pet = config.app.windows.find( (window) => window.label === "desktop-pet", );