diff --git a/crates/desktop-protocol/src/astrbot.rs b/crates/desktop-protocol/src/astrbot.rs index 403a754..e49544f 100644 --- a/crates/desktop-protocol/src/astrbot.rs +++ b/crates/desktop-protocol/src/astrbot.rs @@ -238,6 +238,13 @@ pub struct AstrBotStreamMapper { delta_index: u32, sequence: u64, saw_assistant_delta: bool, + /// Every delta emitted for this stream, concatenated. + /// + /// `accumulated` only holds the message currently open, so it cannot answer + /// "has the client already been shown this?" once a message has closed — + /// and that is exactly the question a `complete` frame poses when the reply + /// arrived as discrete segments. + delivered: String, pending_media: Option, /// True once the active message has been completed. /// @@ -411,59 +418,104 @@ impl AstrBotStreamMapper { fn map_plain(&mut self, frame: AstrBotStreamFrame) -> Vec { let chunk = text_from_value(&frame.data).unwrap_or_default(); - if chunk.is_empty() && frame.streaming.unwrap_or(false) { - // Keep empty streaming markers out of the UI unless they carry text. + if chunk.is_empty() { + // Keep empty markers out of the UI. `emit_delta` would drop an empty + // chunk anyway; returning here also keeps an empty non-streaming + // frame from opening a segment of its own. return Vec::new(); } - if let Some(message_id) = frame.message_id.clone() { - if self - .active_base_id() - .is_some_and(|current| current != message_id) - { - // New assistant message id: complete the previous accumulation first. - let mut events = self.complete_if_needed(FinishReason::Stop); - self.active_message_id = Some(message_id); - self.accumulated.clear(); - self.delta_index = 0; - self.saw_assistant_delta = false; - self.active_message_completed = false; - events.extend(self.emit_delta(chunk)); - return events; - } - self.active_message_id = Some(message_id); + let streaming = frame.streaming.unwrap_or(false); + let mut events = self.adopt_message_id(frame.message_id.clone()); + if streaming { + events.extend(self.emit_delta(chunk)); + return events; } - self.emit_delta(chunk) + // `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)); + 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)); + } + events.extend(self.emit_delta(chunk)); + events.extend(self.complete_if_needed(FinishReason::Stop)); + events + } + + /// Adopts a frame's message id, closing the previous message if it differs. + fn adopt_message_id(&mut self, message_id: Option) -> Vec { + 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; + } + self.active_message_id = Some(message_id); + Vec::new() + } + + /// Begins a fresh message under a server-provided id. + fn start_message(&mut self, message_id: String) { + self.active_message_id = Some(message_id); + self.accumulated.clear(); + self.delta_index = 0; + self.saw_assistant_delta = false; + self.active_message_completed = false; + } + + /// Terminates the reply, reconciling the frame's text against what was shown. + /// + /// 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. fn map_complete(&mut self, frame: AstrBotStreamFrame) -> Vec { - if let Some(message_id) = frame.message_id.clone() { - if self - .active_base_id() - .is_some_and(|current| current != message_id) - { - let mut events = self.complete_if_needed(FinishReason::Stop); - self.active_message_id = Some(message_id); - self.accumulated.clear(); - self.delta_index = 0; - self.saw_assistant_delta = false; - self.active_message_completed = false; - let final_text = text_from_value(&frame.data).unwrap_or_default(); - if !final_text.is_empty() { - self.accumulated = final_text; - self.saw_assistant_delta = true; - } - events.extend(self.complete_if_needed(FinishReason::Stop)); - return events; - } - self.active_message_id = Some(message_id); + 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; } - let final_text = text_from_value(&frame.data).unwrap_or_default(); - if !final_text.is_empty() && self.accumulated != final_text { - self.accumulated = final_text; - self.saw_assistant_delta = true; + // 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; } - self.complete_if_needed(FinishReason::Stop) + + // 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.complete_if_needed(FinishReason::Stop)); + events.extend(self.emit_block(final_text)); + events } /// The active id without any reopen suffix, for comparing against a frame. @@ -501,6 +553,8 @@ impl AstrBotStreamMapper { if self.active_message_completed { self.reopen_message(); } + self.ensure_message_id(); + self.delivered.push_str(&chunk); self.accumulated.push_str(&chunk); self.saw_assistant_delta = true; let index = self.delta_index; @@ -514,10 +568,24 @@ impl AstrBotStreamMapper { )] } + /// Names the active message even when no frame has carried an id. + /// + /// A `MessageDelta` whose metadata has no `message_id` makes the client fall + /// back to `event_id`, which is unique per event — so every such delta became + /// a message of its own. AstrBot does not always send an id on the opening + /// frames of a stream, and the stream id is stable for exactly the span that + /// needs one name. + fn ensure_message_id(&mut self) { + if self.active_message_id.is_none() { + self.active_message_id = Some(self.stream_id.clone()); + } + } + fn complete_if_needed(&mut self, reason: FinishReason) -> Vec { if !self.saw_assistant_delta && self.accumulated.is_empty() { return Vec::new(); } + self.ensure_message_id(); let content = std::mem::take(&mut self.accumulated); self.saw_assistant_delta = false; self.delta_index = 0; @@ -577,6 +645,14 @@ fn attachment_file_name(reference: &str) -> Option { usable.then(|| name.to_owned()) } +/// Collapses whitespace runs and trims, for comparing two renderings of text. +/// +/// Only ever used to decide whether text has already been shown. Stored content +/// is always the server's original bytes. +fn squeeze_whitespace(value: &str) -> String { + value.split_whitespace().collect::>().join(" ") +} + fn string_from_value(value: &Value) -> Option { match value { Value::Null => None, @@ -706,6 +782,125 @@ mod tests { assert_eq!(completed, "Hello world"); } + /// Collects (message_id, content) for every completed message, in order. + fn completions(events: &[GatewayEvent]) -> Vec<(String, String)> { + events + .iter() + .filter_map(|event| match &event.body { + GatewayEventBody::MessageCompleted(payload) => Some(( + event.metadata.message_id.clone().unwrap_or_default(), + payload.content.clone().unwrap_or_default(), + )), + _ => None, + }) + .collect() + } + + fn drive(lines: &[&str]) -> Vec { + let mut mapper = AstrBotStreamMapper::new("astrbot-test", "stream-a"); + let mut events = Vec::new(); + for line in lines { + events.extend(mapper.push_line(line).expect("frame")); + } + events.extend(mapper.finish_stream()); + events + } + + /// A segmented reply keeps every segment, and each becomes its own message. + /// + /// Regression for N-9. `streaming: false` frames carry whole segments, but + /// they were appended to one accumulation and then `complete` assigned its + /// own text over the top of it, so a reply that arrived in three parts was + /// shown as one bubble containing only whichever part `complete` restated. + #[test] + fn a_segmented_reply_keeps_every_segment_as_its_own_message() { + let events = drive(&[ + r#"data: {"type":"plain","data":"第一段。","streaming":false,"message_id":"m1"}"#, + r#"data: {"type":"plain","data":"第二段。","streaming":false,"message_id":"m1"}"#, + r#"data: {"type":"plain","data":"第三段。","streaming":false,"message_id":"m1"}"#, + r#"data: {"type":"complete","data":"第三段。","message_id":"m1"}"#, + ]); + + let done = completions(&events); + assert_eq!(done.len(), 3, "{done:?}"); + assert_eq!(done[0].1, "第一段。"); + assert_eq!(done[1].1, "第二段。"); + assert_eq!(done[2].1, "第三段。"); + // Distinct ids, or the client merges them back into one bubble. + assert_eq!(done[0].0, "m1"); + assert!(done[1].0.starts_with("m1#part-"), "{done:?}"); + assert_ne!(done[1].0, done[2].0); + } + + /// The same reply when `complete` restates the whole thing rather than the + /// last segment: still three messages, and no fourth carrying the join. + #[test] + fn a_complete_frame_restating_every_segment_adds_no_message() { + let events = drive(&[ + r#"data: {"type":"plain","data":"甲","streaming":false,"message_id":"m1"}"#, + r#"data: {"type":"plain","data":"乙","streaming":false,"message_id":"m1"}"#, + r#"data: {"type":"complete","data":"甲乙","message_id":"m1"}"#, + ]); + + let done = completions(&events); + assert_eq!(done.len(), 2, "{done:?}"); + assert_eq!(done[0].1, "甲"); + assert_eq!(done[1].1, "乙"); + } + + /// Whitespace-only differences are not new content. + #[test] + fn a_reflowed_restatement_is_not_shown_twice() { + let events = drive(&[ + r#"data: {"type":"plain","data":"一行\n二行","streaming":false,"message_id":"m1"}"#, + r#"data: {"type":"complete","data":"一行 二行","message_id":"m1"}"#, + ]); + + let done = completions(&events); + assert_eq!(done.len(), 1, "{done:?}"); + assert_eq!(done[0].1, "一行\n二行"); + } + + /// A `complete` frame that agrees with nothing shown must not silently win. + #[test] + fn a_divergent_complete_frame_keeps_both_texts() { + let events = drive(&[ + r#"data: {"type":"plain","data":"已经显示的正文","streaming":true,"message_id":"m1"}"#, + r#"data: {"type":"complete","data":"完全不同的结论","message_id":"m1"}"#, + ]); + + let done = completions(&events); + assert_eq!(done.len(), 2, "{done:?}"); + assert_eq!(done[0].1, "已经显示的正文"); + assert_eq!(done[1].1, "完全不同的结论"); + } + + /// Regression for N-4's mapper half: without a message id the client falls + /// back to `event_id`, which is unique per event, so each delta became a + /// message of its own and the reply appeared as a column of one-word bubbles. + #[test] + fn deltas_without_a_server_message_id_stay_one_message() { + let events = drive(&[ + r#"data: {"type":"plain","data":"甲","streaming":true}"#, + r#"data: {"type":"plain","data":"乙","streaming":true}"#, + r#"data: {"type":"plain","data":"丙","streaming":true}"#, + ]); + + let ids: Vec<_> = events + .iter() + .filter(|event| matches!(event.body, GatewayEventBody::MessageDelta(_))) + .map(|event| event.metadata.message_id.clone()) + .collect(); + assert_eq!(ids.len(), 3); + assert!(ids.iter().all(Option::is_some), "{ids:?}"); + assert_eq!(ids[0], ids[1]); + assert_eq!(ids[1], ids[2]); + + let done = completions(&events); + assert_eq!(done.len(), 1, "{done:?}"); + assert_eq!(done[0].1, "甲乙丙"); + } + #[test] fn maps_error_frames() { let mut mapper = AstrBotStreamMapper::new("astrbot-test", "stream-a");