llmshim 0.12.1

Blazing fast LLM API translation layer in pure Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! Bounded SSE data fields; LLM transports do not retain SSE event IDs or retry metadata.

use crate::error::{Result, ShimError};
use bytes::Bytes;
use futures::{Stream, StreamExt};
use std::pin::Pin;
use std::task::{Context, Poll};

#[derive(Clone, Copy)]
struct StreamLimits {
    total_bytes: usize,
    line_bytes: usize,
    frame_bytes: usize,
    frames: usize,
    chunks: usize,
}

impl Default for StreamLimits {
    fn default() -> Self {
        Self {
            total_bytes: 32 * 1024 * 1024,
            line_bytes: 8 * 1024 * 1024,
            frame_bytes: 8 * 1024 * 1024,
            frames: 100_000,
            chunks: 1_048_576,
        }
    }
}

type ByteStream = Pin<Box<dyn Stream<Item = std::result::Result<Bytes, ()>> + Send>>;

pub(crate) fn data<Source, SourceError>(source: Source) -> DataStream
where
    Source: Stream<Item = std::result::Result<Bytes, SourceError>> + Send + 'static,
    SourceError: Send + 'static,
{
    DataStream::new(
        Box::pin(source.map(|chunk| chunk.map_err(|_| ()))),
        StreamLimits::default(),
    )
}

pub(crate) struct DataStream {
    source: ByteStream,
    limits: StreamLimits,
    current_chunk: Bytes,
    chunk_offset: usize,
    line: Vec<u8>,
    event_data: Vec<u8>,
    total_bytes: usize,
    frame_bytes: usize,
    frames: usize,
    chunks: usize,
    first_line: bool,
    after_carriage_return: bool,
    finished: bool,
}

impl DataStream {
    fn new(source: ByteStream, limits: StreamLimits) -> Self {
        Self {
            source,
            limits,
            current_chunk: Bytes::new(),
            chunk_offset: 0,
            line: Vec::new(),
            event_data: Vec::new(),
            total_bytes: 0,
            frame_bytes: 0,
            frames: 0,
            chunks: 0,
            first_line: true,
            after_carriage_return: false,
            finished: false,
        }
    }

    fn finish(&mut self) {
        self.finished = true;
        self.source = Box::pin(futures::stream::empty());
        self.current_chunk = Bytes::new();
        self.line = Vec::new();
        self.event_data = Vec::new();
    }

    fn fail(&mut self, message: &'static str) -> Poll<Option<Result<String>>> {
        self.finish();
        Poll::Ready(Some(Err(ShimError::Stream(message.into()))))
    }

    fn finish_line(&mut self) -> std::result::Result<Option<String>, &'static str> {
        let line = std::str::from_utf8(&self.line).map_err(|_| "invalid upstream SSE UTF-8")?;
        let line = if self.first_line {
            self.first_line = false;
            line.strip_prefix('\u{feff}').unwrap_or(line)
        } else {
            line
        };
        let event = if line.is_empty() {
            if self.frames >= self.limits.frames {
                return Err("upstream SSE frame count exceeds limit");
            }
            self.frames += 1;
            self.frame_bytes = 0;
            if self.event_data.is_empty() {
                None
            } else {
                self.event_data.pop();
                Some(
                    String::from_utf8(std::mem::take(&mut self.event_data))
                        .map_err(|_| "invalid upstream SSE UTF-8")?,
                )
            }
        } else {
            let (field, value) = line.split_once(':').unwrap_or((line, ""));
            if field == "data" {
                let value = value.strip_prefix(' ').unwrap_or(value);
                self.event_data.extend_from_slice(value.as_bytes());
                self.event_data.push(b'\n');
            }
            None
        };
        self.line.clear();
        Ok(event)
    }
}

impl Stream for DataStream {
    type Item = Result<String>;

    fn poll_next(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        if self.finished {
            return Poll::Ready(None);
        }
        for _ in 0..65_536 {
            if self.chunk_offset == self.current_chunk.len() {
                match self.source.as_mut().poll_next(context) {
                    Poll::Ready(Some(Ok(chunk))) => {
                        if self.chunks >= self.limits.chunks {
                            return self.fail("upstream SSE chunk count exceeds limit");
                        }
                        self.chunks += 1;
                        self.current_chunk = chunk;
                        self.chunk_offset = 0;
                        continue;
                    }
                    Poll::Ready(Some(Err(()))) => {
                        return self.fail("could not read upstream SSE");
                    }
                    Poll::Ready(None) => {
                        if std::str::from_utf8(&self.line).is_err() {
                            return self.fail("invalid upstream SSE UTF-8");
                        }
                        self.finish();
                        return Poll::Ready(None);
                    }
                    Poll::Pending => return Poll::Pending,
                }
            }

            if self.total_bytes >= self.limits.total_bytes {
                return self.fail("upstream SSE decoded bytes exceed limit");
            }
            let byte = self.current_chunk[self.chunk_offset];
            self.chunk_offset += 1;
            self.total_bytes += 1;

            let follows_carriage_return = self.after_carriage_return;
            self.after_carriage_return = false;
            // Count both transport bytes, but only one logical CRLF line ending.
            if follows_carriage_return && byte == b'\n' {
                continue;
            }
            if self.frame_bytes >= self.limits.frame_bytes {
                return self.fail("upstream SSE frame exceeds size limit");
            }
            self.frame_bytes += 1;
            if byte == b'\n' || byte == b'\r' {
                self.after_carriage_return = byte == b'\r';
                match self.finish_line() {
                    Ok(Some(event)) => return Poll::Ready(Some(Ok(event))),
                    Ok(None) => {}
                    Err(message) => return self.fail(message),
                }
            } else {
                if self.line.len() >= self.limits.line_bytes {
                    return self.fail("upstream SSE line exceeds size limit");
                }
                self.line.push(byte);
            }
        }
        context.waker().wake_by_ref();
        Poll::Pending
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use eventsource_stream::Eventsource;
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::sync::Arc;
    use std::time::Duration;

    fn small_limits() -> StreamLimits {
        StreamLimits {
            total_bytes: 512,
            line_bytes: 128,
            frame_bytes: 256,
            frames: 16,
            chunks: 512,
        }
    }

    fn byte_source(bytes: &[u8], chunk_size: usize) -> ByteStream {
        let chunks: Vec<_> = bytes
            .chunks(chunk_size)
            .map(|chunk| Ok(Bytes::copy_from_slice(chunk)))
            .collect();
        Box::pin(futures::stream::iter(chunks))
    }

    async fn collect_values(mut stream: DataStream) -> Vec<String> {
        let mut values = Vec::new();
        while let Some(value) = stream.next().await {
            values.push(value.unwrap());
        }
        values
    }

    #[tokio::test]
    async fn standard_data_fields_match_the_previous_parser_across_fragmentation() {
        for ending in ["\n", "\r\n"] {
            let fixture = [
                ": comment",
                "id: identity",
                "event: custom",
                "retry: 100",
                "data: é雪🙂",
                "data: second",
                "",
                "data",
                "",
                "data:  leading space",
                "",
                "",
            ]
            .join(ending);
            for chunk_size in [1, 2, 7, fixture.len()] {
                let expected: Vec<_> = byte_source(fixture.as_bytes(), chunk_size)
                    .eventsource()
                    .map(|event| event.unwrap().data)
                    .collect()
                    .await;
                let actual = collect_values(DataStream::new(
                    byte_source(fixture.as_bytes(), chunk_size),
                    small_limits(),
                ))
                .await;
                assert_eq!(actual, expected);
                assert_eq!(actual, ["é雪🙂\nsecond", "", " leading space"]);
            }
        }
    }

    #[tokio::test]
    async fn bom_and_carriage_return_frames_complete_without_waiting_for_eof() {
        let source = byte_source("\u{feff}data: é雪🙂\rdata: second\r\r".as_bytes(), 1)
            .chain(futures::stream::pending());
        let mut stream = DataStream::new(Box::pin(source), small_limits());
        let event = tokio::time::timeout(Duration::from_secs(1), stream.next())
            .await
            .unwrap()
            .unwrap()
            .unwrap();
        assert_eq!(event, "é雪🙂\nsecond");
    }

    #[tokio::test]
    async fn line_limit_applies_before_newline_to_data_and_unused_metadata() {
        for prefix in ["data:", "id:", "event:", "retry:", ":"] {
            let fixture = format!("{prefix} ordinary field text");
            let source = byte_source(fixture.as_bytes(), 1).chain(futures::stream::pending());
            let mut stream = DataStream::new(
                Box::pin(source),
                StreamLimits {
                    line_bytes: 8,
                    ..small_limits()
                },
            );
            let error = tokio::time::timeout(Duration::from_secs(1), stream.next())
                .await
                .unwrap()
                .unwrap()
                .unwrap_err();
            assert!(error.to_string().contains("line exceeds size limit"));
            assert!(stream.next().await.is_none());
            assert!(stream.line.is_empty() && stream.event_data.is_empty());
        }
    }

    #[tokio::test]
    async fn multiple_data_lines_share_one_frame_budget() {
        let mut stream = DataStream::new(
            byte_source(b"data:a\ndata:b\n\n", 1),
            StreamLimits {
                frame_bytes: 12,
                ..small_limits()
            },
        );
        let error = stream.next().await.unwrap().unwrap_err();
        assert!(error.to_string().contains("frame exceeds size limit"));
        assert!(stream.next().await.is_none());
    }

    #[tokio::test]
    async fn total_bytes_include_crlf_and_preserve_prior_completed_frames() {
        let limits = StreamLimits {
            total_bytes: 10,
            line_bytes: 6,
            frame_bytes: 8,
            ..small_limits()
        };
        let exact = DataStream::new(byte_source(b"data:a\r\n\r\n", 1), limits);
        assert_eq!(collect_values(exact).await, ["a"]);

        let mut overflowing = DataStream::new(byte_source(b"data:a\r\n\r\n:", 11), limits);
        assert_eq!(overflowing.next().await.unwrap().unwrap(), "a");
        let error = overflowing.next().await.unwrap().unwrap_err();
        assert!(error.to_string().contains("decoded bytes exceed limit"));
        assert!(overflowing.next().await.is_none());
    }

    #[tokio::test]
    async fn empty_frames_still_consume_the_frame_count_budget() {
        let mut stream = DataStream::new(
            byte_source(b": comment\n\n: comment\n\n", 2),
            StreamLimits {
                frames: 1,
                ..small_limits()
            },
        );
        let error = stream.next().await.unwrap().unwrap_err();
        assert!(error.to_string().contains("frame count exceeds limit"));
    }

    #[tokio::test]
    async fn empty_transport_chunks_cannot_bypass_the_work_budget() {
        let source = futures::stream::repeat_with(|| Ok(Bytes::new()));
        let mut stream = DataStream::new(
            Box::pin(source),
            StreamLimits {
                chunks: 3,
                ..small_limits()
            },
        );
        let error = tokio::time::timeout(Duration::from_secs(1), stream.next())
            .await
            .unwrap()
            .unwrap()
            .unwrap_err();
        assert!(error.to_string().contains("chunk count exceeds limit"));
    }

    #[tokio::test]
    async fn eof_discards_unterminated_frames_and_rejects_incomplete_utf8() {
        for fixture in [b"data: unfinished".as_slice(), b"data: unfinished\n"] {
            assert!(
                collect_values(DataStream::new(byte_source(fixture, 1), small_limits()))
                    .await
                    .is_empty()
            );
        }
        let mut stream = DataStream::new(
            byte_source(&[b'd', b'a', b't', b'a', b':', 0xf0], 1),
            small_limits(),
        );
        assert!(stream
            .next()
            .await
            .unwrap()
            .unwrap_err()
            .to_string()
            .contains("invalid upstream SSE UTF-8"));
        assert!(stream.next().await.is_none());
    }

    #[tokio::test]
    async fn source_errors_do_not_expose_transport_details() {
        let mut stream = data(futures::stream::iter([Err::<Bytes, _>(
            "private transport data",
        )]));
        let error = stream.next().await.unwrap().unwrap_err().to_string();
        assert!(error.contains("could not read upstream SSE"));
        assert!(!error.contains("private transport data"));
        assert!(stream.next().await.is_none());
    }

    struct SourceGuard(Arc<AtomicBool>);

    impl Drop for SourceGuard {
        fn drop(&mut self) {
            self.0.store(true, Ordering::SeqCst);
        }
    }

    #[tokio::test]
    async fn limit_failure_releases_the_source_without_another_poll() {
        let released = Arc::new(AtomicBool::new(false));
        let source = futures::stream::unfold(SourceGuard(released.clone()), |guard| async move {
            Some((Ok(Bytes::from_static(b"data: ordinary field text")), guard))
        });
        let mut stream = DataStream::new(
            Box::pin(source),
            StreamLimits {
                line_bytes: 8,
                ..small_limits()
            },
        );
        assert!(!released.load(Ordering::SeqCst));
        assert!(stream.next().await.unwrap().is_err());
        assert!(released.load(Ordering::SeqCst));
    }
}