litellm/litellm-rust/crates/framer/tests/chaining.rs
Yujong Lee edebb26e57 refactor(rust): own the SSE parser and yield smithy messages from the framing crate
The AWS event stream framer now yields aws_smithy_types Message directly
instead of copying headers and payload into a second struct, which halves
its allocations (16 to 8 per frame) and cuts wall time by about 30%. It
also parses from a slice and advances the buffer rather than splitting and
freezing it.

SSE parsing no longer goes through sse-stream. The new parser follows the
WHATWG event stream grammar (unknown fields ignored, colonless lines are
empty fields, last event line wins, non-digit retry and NUL ids ignored,
events without data are not dispatched), caps an event at MAX_FRAME_BYTES
so an unterminated upstream cannot grow memory without bound, and reports
upstream body failures as Error::Body like the AWS path does. Allocations
stay at two per event and wall time is within a few percent.

The Framer trait had no generic consumer, so both parsers are plain
functions now. MissingStreamData in litellm-core is gone because a
dispatched SSE event always carries data.
2026-09-17 15:49:14 -07:00

32 lines
890 B
Rust

#![cfg(all(feature = "aws", feature = "sse"))]
mod support;
use std::io;
use futures_util::TryStreamExt;
use litellm_framing::sse::SseFrame;
use litellm_framing::{aws_event_stream, sse};
use support::encode;
#[tokio::test]
async fn hosting_payloads_feed_the_same_sse_parser_across_envelope_boundaries() {
let bytes = [encode(b"event: delta\ndata: hel"), encode(b"lo\nid: 7\n\n")].concat();
let envelopes = aws_event_stream::frames(futures_util::stream::iter(
bytes.chunks(3).map(Ok::<_, io::Error>),
));
let frames = sse::frames(envelopes.map_ok(|message| message.payload().clone()))
.try_collect::<Vec<_>>()
.await
.unwrap();
assert_eq!(
frames,
vec![SseFrame {
event: Some("delta".into()),
data: "hello".into(),
id: Some("7".into()),
retry: None,
}]
);
}