fix(protocol): keep every segment of a segmented reply
A `streaming: false` plain frame carries a whole segment, not a fragment of one, but the mapper appended it to the open accumulation and then `complete` assigned its own text over the top. A reply that arrived in three parts was shown as one bubble holding only whichever part `complete` restated; the rest was never displayed and never stored. Non-streaming frames now become messages of their own, and `complete` reconciles instead of overwriting. Three ordered cases, none of which can lose text the client has seen: the frame extends what was shown byte for byte, so only the tail is emitted (the streaming case the existing reconcile test pins); the frame restates text already shown, compared with whitespace squeezed, so nothing is emitted; or the frame diverges from what was shown, so both are kept and the frame's text opens its own message. Tracking that needs the whole stream's delivered text, since `accumulated` empties as each message closes. Also stops emitting deltas with no message id. Metadata without one makes the client fall back to `event_id`, which is unique per event, so each such delta became a message of its own — and AstrBot does not always put an id on the opening frames of a stream. The stream id is stable across exactly the span that needs one name. This is the mapper half of N-4; the duplicate-bubble report is not yet confirmed to be this, and the client's own `messageId || eventId` fallback is untouched. Protocol tests 12 -> 17. All five new assertions were confirmed to fail under a deliberate regression. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
这个提交存在于:
父节点
9b2ec64502
当前提交
64dab459fc
共有 1 个文件被更改,包括 239 次插入 和 44 次删除
|
|
@ -238,6 +238,13 @@ pub struct AstrBotStreamMapper {
|
||||||
delta_index: u32,
|
delta_index: u32,
|
||||||
sequence: u64,
|
sequence: u64,
|
||||||
saw_assistant_delta: bool,
|
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<PendingMedia>,
|
pending_media: Option<PendingMedia>,
|
||||||
/// True once the active message has been completed.
|
/// True once the active message has been completed.
|
||||||
///
|
///
|
||||||
|
|
@ -411,59 +418,104 @@ impl AstrBotStreamMapper {
|
||||||
|
|
||||||
fn map_plain(&mut self, frame: AstrBotStreamFrame) -> Vec<GatewayEvent> {
|
fn map_plain(&mut self, frame: AstrBotStreamFrame) -> Vec<GatewayEvent> {
|
||||||
let chunk = text_from_value(&frame.data).unwrap_or_default();
|
let chunk = text_from_value(&frame.data).unwrap_or_default();
|
||||||
if chunk.is_empty() && frame.streaming.unwrap_or(false) {
|
if chunk.is_empty() {
|
||||||
// Keep empty streaming markers out of the UI unless they carry text.
|
// 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();
|
return Vec::new();
|
||||||
}
|
}
|
||||||
if let Some(message_id) = frame.message_id.clone() {
|
let streaming = frame.streaming.unwrap_or(false);
|
||||||
if self
|
let mut events = self.adopt_message_id(frame.message_id.clone());
|
||||||
.active_base_id()
|
if streaming {
|
||||||
.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));
|
events.extend(self.emit_delta(chunk));
|
||||||
return events;
|
return events;
|
||||||
}
|
}
|
||||||
self.active_message_id = Some(message_id);
|
// `streaming: false` means the frame carries a whole segment rather than
|
||||||
}
|
// a fragment of one. Appending it to the open accumulation is what made
|
||||||
self.emit_delta(chunk)
|
// 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
|
||||||
}
|
}
|
||||||
|
|
||||||
fn map_complete(&mut self, frame: AstrBotStreamFrame) -> Vec<GatewayEvent> {
|
/// Emits a self-contained segment as a message of its own.
|
||||||
if let Some(message_id) = frame.message_id.clone() {
|
fn emit_block(&mut self, chunk: String) -> Vec<GatewayEvent> {
|
||||||
|
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<String>) -> Vec<GatewayEvent> {
|
||||||
|
let Some(message_id) = message_id else {
|
||||||
|
return Vec::new();
|
||||||
|
};
|
||||||
if self
|
if self
|
||||||
.active_base_id()
|
.active_base_id()
|
||||||
.is_some_and(|current| current != message_id)
|
.is_some_and(|current| current != message_id)
|
||||||
{
|
{
|
||||||
let mut events = self.complete_if_needed(FinishReason::Stop);
|
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.active_message_id = Some(message_id);
|
||||||
self.accumulated.clear();
|
self.accumulated.clear();
|
||||||
self.delta_index = 0;
|
self.delta_index = 0;
|
||||||
self.saw_assistant_delta = false;
|
self.saw_assistant_delta = false;
|
||||||
self.active_message_completed = 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<GatewayEvent> {
|
||||||
|
let mut events = self.adopt_message_id(frame.message_id.clone());
|
||||||
let final_text = text_from_value(&frame.data).unwrap_or_default();
|
let final_text = text_from_value(&frame.data).unwrap_or_default();
|
||||||
if !final_text.is_empty() {
|
if final_text.trim().is_empty() {
|
||||||
self.accumulated = final_text;
|
events.extend(self.complete_if_needed(FinishReason::Stop));
|
||||||
self.saw_assistant_delta = true;
|
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));
|
events.extend(self.complete_if_needed(FinishReason::Stop));
|
||||||
return events;
|
return events;
|
||||||
}
|
}
|
||||||
self.active_message_id = Some(message_id);
|
|
||||||
|
// 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
let final_text = text_from_value(&frame.data).unwrap_or_default();
|
// 3. The frame diverges from what was shown. Keep both: close the open
|
||||||
if !final_text.is_empty() && self.accumulated != final_text {
|
// message with what it has, then give the frame's text its own.
|
||||||
self.accumulated = final_text;
|
events.extend(self.complete_if_needed(FinishReason::Stop));
|
||||||
self.saw_assistant_delta = true;
|
events.extend(self.emit_block(final_text));
|
||||||
}
|
events
|
||||||
self.complete_if_needed(FinishReason::Stop)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The active id without any reopen suffix, for comparing against a frame.
|
/// The active id without any reopen suffix, for comparing against a frame.
|
||||||
|
|
@ -501,6 +553,8 @@ impl AstrBotStreamMapper {
|
||||||
if self.active_message_completed {
|
if self.active_message_completed {
|
||||||
self.reopen_message();
|
self.reopen_message();
|
||||||
}
|
}
|
||||||
|
self.ensure_message_id();
|
||||||
|
self.delivered.push_str(&chunk);
|
||||||
self.accumulated.push_str(&chunk);
|
self.accumulated.push_str(&chunk);
|
||||||
self.saw_assistant_delta = true;
|
self.saw_assistant_delta = true;
|
||||||
let index = self.delta_index;
|
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<GatewayEvent> {
|
fn complete_if_needed(&mut self, reason: FinishReason) -> Vec<GatewayEvent> {
|
||||||
if !self.saw_assistant_delta && self.accumulated.is_empty() {
|
if !self.saw_assistant_delta && self.accumulated.is_empty() {
|
||||||
return Vec::new();
|
return Vec::new();
|
||||||
}
|
}
|
||||||
|
self.ensure_message_id();
|
||||||
let content = std::mem::take(&mut self.accumulated);
|
let content = std::mem::take(&mut self.accumulated);
|
||||||
self.saw_assistant_delta = false;
|
self.saw_assistant_delta = false;
|
||||||
self.delta_index = 0;
|
self.delta_index = 0;
|
||||||
|
|
@ -577,6 +645,14 @@ fn attachment_file_name(reference: &str) -> Option<String> {
|
||||||
usable.then(|| name.to_owned())
|
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::<Vec<_>>().join(" ")
|
||||||
|
}
|
||||||
|
|
||||||
fn string_from_value(value: &Value) -> Option<String> {
|
fn string_from_value(value: &Value) -> Option<String> {
|
||||||
match value {
|
match value {
|
||||||
Value::Null => None,
|
Value::Null => None,
|
||||||
|
|
@ -706,6 +782,125 @@ mod tests {
|
||||||
assert_eq!(completed, "Hello world");
|
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<GatewayEvent> {
|
||||||
|
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]
|
#[test]
|
||||||
fn maps_error_frames() {
|
fn maps_error_frames() {
|
||||||
let mut mapper = AstrBotStreamMapper::new("astrbot-test", "stream-a");
|
let mut mapper = AstrBotStreamMapper::new("astrbot-test", "stream-a");
|
||||||
|
|
|
||||||
正在加载…
添加表格
在新工单中引用