diff --git a/crates/desktop-protocol/src/astrbot.rs b/crates/desktop-protocol/src/astrbot.rs index b38fb40..a209ea8 100644 --- a/crates/desktop-protocol/src/astrbot.rs +++ b/crates/desktop-protocol/src/astrbot.rs @@ -222,6 +222,12 @@ struct PendingMedia { media_type: String, } +/// Separates a message id from its reopen index. +/// +/// Content that arrives after `complete` reuses the server's message id, so +/// the reopened message needs a suffix the client will read as a new message. +const REOPEN_SEPARATOR: &str = "#part-"; + #[derive(Debug, Default)] pub struct AstrBotStreamMapper { server_instance_id: String, @@ -233,6 +239,17 @@ pub struct AstrBotStreamMapper { sequence: u64, saw_assistant_delta: bool, pending_media: Option, + /// True once the active message has been completed. + /// + /// AstrBot can deliver content *after* the `complete` frame — an observed + /// stream ended `complete(272 chars)` → `image` → `attachment_saved` → + /// `end`, all under one message id. Re-accumulating that late content under + /// the finished id made `end` emit a second completion whose body was the + /// attachment marker alone, and the client overwrote the whole reply with + /// it. Late content therefore has to open a message of its own. + active_message_completed: bool, + /// Distinguishes the ids of messages reopened after a completion. + reopen_index: u32, } impl AstrBotStreamMapper { @@ -340,14 +357,14 @@ impl AstrBotStreamMapper { let mut events = Vec::new(); if let Some(message_id) = frame.message_id { if self - .active_message_id - .as_ref() - .is_some_and(|current| current != &message_id) + .active_base_id() + .is_some_and(|current| current != message_id) { events = self.complete_if_needed(FinishReason::Stop); self.accumulated.clear(); self.delta_index = 0; self.saw_assistant_delta = false; + self.active_message_completed = false; } self.active_message_id = Some(message_id); } @@ -384,9 +401,8 @@ impl AstrBotStreamMapper { } if let Some(message_id) = frame.message_id.clone() { if self - .active_message_id - .as_ref() - .is_some_and(|current| current != &message_id) + .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); @@ -394,6 +410,7 @@ impl AstrBotStreamMapper { 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; } @@ -405,15 +422,15 @@ impl AstrBotStreamMapper { fn map_complete(&mut self, frame: AstrBotStreamFrame) -> Vec { if let Some(message_id) = frame.message_id.clone() { if self - .active_message_id - .as_ref() - .is_some_and(|current| current != &message_id) + .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; @@ -433,10 +450,41 @@ impl AstrBotStreamMapper { self.complete_if_needed(FinishReason::Stop) } + /// The active id without any reopen suffix, for comparing against a frame. + fn active_base_id(&self) -> Option<&str> { + self.active_message_id + .as_deref() + .map(|id| id.split_once(REOPEN_SEPARATOR).map_or(id, |(head, _)| head)) + } + + /// Starts a new message after the active one has already been completed. + /// + /// The server keeps the same message id for content that arrives after + /// `complete`, so the id has to be made distinct here or the client merges + /// the late content into the finished message and overwrites it. + fn reopen_message(&mut self) { + let base = self + .active_base_id() + .map(str::to_owned) + .unwrap_or_else(|| self.stream_id.clone()); + self.reopen_index = self.reopen_index.saturating_add(1); + self.active_message_id = Some(format!( + "{base}{REOPEN_SEPARATOR}{index}", + index = self.reopen_index + )); + self.accumulated.clear(); + self.delta_index = 0; + self.saw_assistant_delta = false; + self.active_message_completed = false; + } + fn emit_delta(&mut self, chunk: String) -> Vec { if chunk.is_empty() { return Vec::new(); } + if self.active_message_completed { + self.reopen_message(); + } self.accumulated.push_str(&chunk); self.saw_assistant_delta = true; let index = self.delta_index; @@ -457,6 +505,7 @@ impl AstrBotStreamMapper { let content = std::mem::take(&mut self.accumulated); self.saw_assistant_delta = false; self.delta_index = 0; + self.active_message_completed = true; let message_id = self.active_message_id.clone(); vec![self.event( message_id, @@ -660,6 +709,49 @@ mod tests { assert_eq!(delta, "![image](astrbot-attachment://att-1)"); } + /// Replays the frame order of a captured stream that destroyed its own reply. + /// + /// The server sent `complete` and then continued: `image`, + /// `attachment_saved`, `end`, all under the same message id. The attachment + /// marker re-accumulated onto the just-emptied buffer and `end` emitted a + /// second completion for the finished message whose body was the marker + /// alone, so the client replaced 272 characters of reply with an image tag. + #[test] + fn content_after_complete_does_not_overwrite_the_finished_message() { + let mut mapper = AstrBotStreamMapper::new("astrbot-test", "stream-a"); + let mut events = Vec::new(); + for line in [ + r#"data: {"type":"plain","data":"完整的回复正文","streaming":true,"message_id":"m1"}"#, + r#"data: {"type":"complete","data":"完整的回复正文","message_id":"m1"}"#, + r#"data: {"type":"image","data":"[IMAGE]sticker.png","message_id":"m1"}"#, + r#"data: {"type":"attachment_saved","data":{"id":"att-9","type":"image"},"message_id":"m1"}"#, + ] { + events.extend(mapper.push_line(line).expect("frame")); + } + events.extend(mapper.finish_stream()); + + let completions: Vec<(&str, &str)> = events + .iter() + .filter_map(|event| match &event.body { + GatewayEventBody::MessageCompleted(payload) => Some(( + event.metadata.message_id.as_deref().unwrap_or(""), + payload.content.as_deref().unwrap_or(""), + )), + _ => None, + }) + .collect(); + + assert_eq!(completions.len(), 2, "{completions:?}"); + assert_eq!(completions[0], ("m1", "完整的回复正文")); + // The attachment lands in a message of its own, leaving the reply intact. + assert_eq!(completions[1].1, "![image](astrbot-attachment://att-9)"); + assert_ne!( + completions[1].0, "m1", + "the attachment must not reuse the finished message id" + ); + assert!(completions[1].0.starts_with("m1#part-")); + } + #[test] fn consecutive_streams_do_not_reuse_event_ids() { // Regression: a client keeps one deduplicator for its whole lifetime,