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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
use super::DispatchFailure;
use crate::error::ShimError;
use bytes::Bytes;
use futures::{Stream, StreamExt};

#[derive(Clone, Copy)]
pub(super) struct ResponseBodyLimits {
    pub success_bytes: usize,
    pub error_bytes: usize,
}

impl Default for ResponseBodyLimits {
    fn default() -> Self {
        Self {
            success_bytes: 32 * 1024 * 1024,
            error_bytes: 64 * 1024,
        }
    }
}

#[derive(Debug)]
pub(super) enum BodyReadError {
    Http(reqwest::Error),
    TooLarge,
    Timeout,
    Complexity,
}

impl BodyReadError {
    pub(super) fn into_dispatch_failure(self) -> DispatchFailure {
        match self {
            Self::Http(error) => DispatchFailure::Upstream(error.into()),
            Self::TooLarge => DispatchFailure::Local(ShimError::ProviderError {
                status: 502,
                body: "upstream response body exceeds size limit".into(),
                retry_after: None,
            }),
            Self::Timeout => DispatchFailure::LocalTimeout(ShimError::ProviderError {
                status: 504,
                body: "upstream response body timed out".into(),
                retry_after: None,
            }),
            Self::Complexity => DispatchFailure::Local(ShimError::ProviderError {
                status: 502,
                body: "upstream JSON exceeds complexity limit".into(),
                retry_after: None,
            }),
        }
    }
}

pub(super) async fn read(
    response: reqwest::Response,
    maximum_bytes: usize,
    idle_timeout: std::time::Duration,
    total_deadline: tokio::time::Instant,
) -> Result<Vec<u8>, BodyReadError> {
    if response
        .content_length()
        .is_some_and(|declared_bytes| declared_bytes > maximum_bytes as u64)
    {
        return Err(BodyReadError::TooLarge);
    }
    collect_decoded_chunks(
        response.bytes_stream(),
        maximum_bytes,
        idle_timeout,
        total_deadline,
    )
    .await
}

async fn collect_decoded_chunks(
    response_chunks: impl Stream<Item = Result<Bytes, reqwest::Error>>,
    maximum_bytes: usize,
    idle_timeout: std::time::Duration,
    total_deadline: tokio::time::Instant,
) -> Result<Vec<u8>, BodyReadError> {
    futures::pin_mut!(response_chunks);
    let mut decoded_body = Vec::new();
    loop {
        let idle_deadline = tokio::time::Instant::now()
            .checked_add(idle_timeout)
            .ok_or(BodyReadError::Timeout)?;
        let response_chunk = tokio::select! {
            response_chunk = response_chunks.next() => response_chunk,
            _ = tokio::time::sleep_until(idle_deadline) => return Err(BodyReadError::Timeout),
            _ = tokio::time::sleep_until(total_deadline) => return Err(BodyReadError::Timeout),
        };
        let Some(response_chunk) = response_chunk else {
            break;
        };
        let response_chunk = response_chunk.map_err(BodyReadError::Http)?;
        if response_chunk.len() > maximum_bytes.saturating_sub(decoded_body.len()) {
            return Err(BodyReadError::TooLarge);
        }
        decoded_body.extend_from_slice(&response_chunk);
    }
    Ok(decoded_body)
}

pub(super) async fn read_json(
    response: reqwest::Response,
    maximum_bytes: usize,
    idle_timeout: std::time::Duration,
    total_deadline: tokio::time::Instant,
) -> Result<serde_json::Value, BodyReadError> {
    let decoded_body = read(response, maximum_bytes, idle_timeout, total_deadline).await?;
    match crate::json_bounds::parse_slice(&decoded_body, crate::json_bounds::Limits::UNARY) {
        Ok(value) => return Ok(value),
        Err(crate::json_bounds::ParseError::Complexity) => return Err(BodyReadError::Complexity),
        Err(crate::json_bounds::ParseError::Malformed(_)) => {}
    }
    // Retain reqwest's public decode-error type after the body is bounded.
    reqwest::Response::from(http::Response::new(decoded_body))
        .json()
        .await
        .map_err(BodyReadError::Http)
}

pub(super) async fn read_text_and_bytes(
    response: reqwest::Response,
    maximum_bytes: usize,
    idle_timeout: std::time::Duration,
    total_deadline: tokio::time::Instant,
) -> Result<(String, Vec<u8>), BodyReadError> {
    let content_type = response.headers().get(http::header::CONTENT_TYPE).cloned();
    let decoded_body = read(response, maximum_bytes, idle_timeout, total_deadline).await?;
    let mut bounded_response = http::Response::new(decoded_body.clone());
    if let Some(content_type) = content_type {
        bounded_response
            .headers_mut()
            .insert(http::header::CONTENT_TYPE, content_type);
    }
    let text = reqwest::Response::from(bounded_response)
        .text()
        .await
        .map_err(BodyReadError::Http)?;
    Ok((text, decoded_body))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::client::{RetryConfig, ShimClient};
    use crate::policy::{
        AttemptAccounting, AttemptEvent, AttemptIdentity, AttemptOutcome, AttemptPolicy,
        AttemptPolicyError, AttemptPolicyFuture, AttemptPolicyRefusal, DispatchPolicyContext,
        PreparedAttempt,
    };
    use crate::provider::ProviderRequest;
    use serde_json::json;
    use std::sync::{Arc, Mutex};
    use std::time::Duration;

    fn bounded_client() -> ShimClient {
        ShimClient {
            response_body_limits: ResponseBodyLimits {
                success_bytes: 64,
                error_bytes: 16,
            },
            retry: RetryConfig {
                max_retries: 1,
                base: Duration::ZERO,
                cap: Duration::ZERO,
            },
            ..ShimClient::new()
        }
    }

    fn request(server_url: &str) -> ProviderRequest {
        ProviderRequest {
            url: format!("{server_url}/completion"),
            headers: vec![],
            body: json!({"messages": [{"role": "user", "content": "test"}]}),
        }
    }

    #[tokio::test]
    async fn decoded_chunks_accept_the_exact_limit_and_stop_before_eof() {
        let exact_chunks = futures::stream::iter([
            Ok(Bytes::from_static(b"abc")),
            Ok(Bytes::from_static(b"def")),
        ]);
        assert_eq!(
            collect_decoded_chunks(
                exact_chunks,
                6,
                Duration::from_secs(1),
                tokio::time::Instant::now() + Duration::from_secs(1),
            )
            .await
            .unwrap(),
            b"abcdef"
        );

        let overflowing_chunks = futures::stream::iter([
            Ok(Bytes::from_static(b"abc")),
            Ok(Bytes::from_static(b"def")),
        ])
        .chain(futures::stream::pending());
        let result = tokio::time::timeout(
            Duration::from_secs(1),
            collect_decoded_chunks(
                overflowing_chunks,
                5,
                Duration::from_secs(1),
                tokio::time::Instant::now() + Duration::from_secs(1),
            ),
        )
        .await
        .expect("the reader must not wait for EOF after crossing the limit");
        assert!(matches!(result, Err(BodyReadError::TooLarge)));
    }

    #[tokio::test(start_paused = true)]
    async fn decoded_body_idle_timeout_is_independent_of_size_limit() {
        let result = collect_decoded_chunks(
            futures::stream::pending(),
            64,
            Duration::from_millis(5),
            tokio::time::Instant::now() + Duration::from_secs(1),
        )
        .await;
        assert!(matches!(result, Err(BodyReadError::Timeout)));
    }

    #[tokio::test(start_paused = true)]
    async fn decoded_body_total_timeout_does_not_reset_on_progress() {
        let chunks = futures::stream::unfold((), |_| async {
            tokio::time::sleep(Duration::from_millis(4)).await;
            Some((Ok(Bytes::from_static(b"x")), ()))
        });
        let result = collect_decoded_chunks(
            chunks,
            64,
            Duration::from_millis(5),
            tokio::time::Instant::now() + Duration::from_millis(10),
        )
        .await;
        assert!(matches!(result, Err(BodyReadError::Timeout)));
    }

    #[tokio::test]
    async fn gzip_is_limited_after_decoding() {
        let compressed_body = vec![
            31, 139, 8, 0, 0, 0, 0, 0, 2, 255, 75, 76, 28, 88, 0, 0, 140, 54, 43, 241, 128, 0, 0, 0,
        ];
        assert!(compressed_body.len() < 64);
        let mut upstream_server = mockito::Server::new_async().await;
        let response_mock = upstream_server
            .mock("GET", "/compressed")
            .with_header("content-encoding", "gzip")
            .with_body(compressed_body)
            .expect(2)
            .create_async()
            .await;
        for maximum_bytes in [64, 128] {
            let response = reqwest::Client::new()
                .get(format!("{}/compressed", upstream_server.url()))
                .send()
                .await
                .unwrap();
            let result = read(
                response,
                maximum_bytes,
                Duration::from_secs(1),
                tokio::time::Instant::now() + Duration::from_secs(1),
            )
            .await;
            if maximum_bytes == 64 {
                assert!(matches!(result, Err(BodyReadError::TooLarge)));
            } else {
                assert_eq!(result.unwrap(), vec![b'a'; 128]);
            }
        }
        response_mock.assert_async().await;
    }

    #[tokio::test]
    async fn oversized_final_error_uses_a_fixed_diagnostic() {
        let mut upstream_server = mockito::Server::new_async().await;
        let response_mock = upstream_server
            .mock("POST", "/completion")
            .with_status(401)
            .with_chunked_body(|writer| writer.write_all(b"provider-specific diagnostic text"))
            .expect(1)
            .create_async()
            .await;
        let error = bounded_client()
            .send(&request(&upstream_server.url()))
            .await
            .unwrap_err();
        assert!(
            matches!(error, ShimError::ProviderError { status: 502, ref body, .. }
            if body == "upstream response body exceeds size limit")
        );
        response_mock.assert_async().await;
    }

    #[tokio::test]
    async fn oversized_retry_body_is_dropped_before_the_next_attempt() {
        let mut upstream_server = mockito::Server::new_async().await;
        let retry_mock = upstream_server
            .mock("POST", "/completion")
            .with_status(500)
            .with_chunked_body(|writer| writer.write_all(b"retry body beyond the small test limit"))
            .expect(1)
            .create_async()
            .await;
        let success_mock = upstream_server
            .mock("POST", "/completion")
            .with_status(200)
            .with_body("ok")
            .expect(1)
            .create_async()
            .await;
        let response = bounded_client()
            .send(&request(&upstream_server.url()))
            .await
            .unwrap();
        assert_eq!(response.text().await.unwrap(), "ok");
        retry_mock.assert_async().await;
        success_mock.assert_async().await;
    }

    #[tokio::test]
    async fn bounded_json_preserves_reqwest_decode_errors() {
        let mut upstream_server = mockito::Server::new_async().await;
        let response_mock = upstream_server
            .mock("GET", "/invalid-json")
            .with_body("not json")
            .create_async()
            .await;
        let response = reqwest::Client::new()
            .get(format!("{}/invalid-json", upstream_server.url()))
            .send()
            .await
            .unwrap();
        assert!(matches!(read_json(
            response,
            64,
            Duration::from_secs(1),
            tokio::time::Instant::now() + Duration::from_secs(1),
        ).await,
            Err(BodyReadError::Http(error)) if error.is_decode()));
        response_mock.assert_async().await;
    }

    #[tokio::test]
    async fn bounded_text_preserves_reqwest_character_decoding() {
        let mut upstream_server = mockito::Server::new_async().await;
        let response_mock = upstream_server
            .mock("GET", "/encoded-text")
            .with_header("content-type", "text/plain; charset=iso-8859-1")
            .with_body(vec![b'c', b'a', b'f', 0xe9])
            .expect(2)
            .create_async()
            .await;
        let response_url = format!("{}/encoded-text", upstream_server.url());
        let http_client = reqwest::Client::new();
        let original_text = http_client
            .get(&response_url)
            .send()
            .await
            .unwrap()
            .text()
            .await
            .unwrap();
        let bounded_text = read_text_and_bytes(
            http_client.get(response_url).send().await.unwrap(),
            4,
            Duration::from_secs(1),
            tokio::time::Instant::now() + Duration::from_secs(1),
        )
        .await
        .unwrap()
        .0;
        assert_eq!(bounded_text, original_text);
        response_mock.assert_async().await;
    }

    #[derive(Default)]
    struct RecordingPolicy {
        outcomes: Mutex<Vec<AttemptOutcome>>,
    }

    struct PendingAcquirePolicy;

    impl AttemptPolicy for PendingAcquirePolicy {
        fn acquire<'a>(
            &'a self,
            _attempt: &'a PreparedAttempt<'a>,
        ) -> AttemptPolicyFuture<'a, Result<(), AttemptPolicyRefusal>> {
            Box::pin(futures::future::pending())
        }

        fn observe<'a>(
            &'a self,
            _attempt: &'a AttemptIdentity,
            _event: AttemptEvent<'a>,
        ) -> AttemptPolicyFuture<'a, Result<(), AttemptPolicyError>> {
            Box::pin(async { Ok(()) })
        }

        fn observe_abandoned(
            &self,
            _attempt: &AttemptIdentity,
            _outcome: AttemptOutcome,
        ) -> Result<(), AttemptPolicyError> {
            Ok(())
        }
    }

    #[derive(Clone, Copy, Debug)]
    enum PendingObservation {
        Headers,
        Usage,
        Finish,
    }

    struct PendingObservationPolicy {
        pending: PendingObservation,
        abandoned: std::sync::atomic::AtomicBool,
    }

    impl AttemptPolicy for PendingObservationPolicy {
        fn acquire<'a>(
            &'a self,
            _attempt: &'a PreparedAttempt<'a>,
        ) -> AttemptPolicyFuture<'a, Result<(), AttemptPolicyRefusal>> {
            Box::pin(async { Ok(()) })
        }

        fn observe<'a>(
            &'a self,
            _attempt: &'a AttemptIdentity,
            event: AttemptEvent<'a>,
        ) -> AttemptPolicyFuture<'a, Result<(), AttemptPolicyError>> {
            let should_wait = matches!(
                (self.pending, event),
                (
                    PendingObservation::Headers,
                    AttemptEvent::ResponseHeaders { .. }
                ) | (PendingObservation::Usage, AttemptEvent::Usage { .. })
                    | (PendingObservation::Finish, AttemptEvent::Finished(_))
            );
            Box::pin(async move {
                if should_wait {
                    futures::future::pending().await
                } else {
                    Ok(())
                }
            })
        }

        fn observe_abandoned(
            &self,
            _attempt: &AttemptIdentity,
            _outcome: AttemptOutcome,
        ) -> Result<(), AttemptPolicyError> {
            self.abandoned
                .store(true, std::sync::atomic::Ordering::SeqCst);
            Ok(())
        }
    }

    #[tokio::test(start_paused = true)]
    async fn pending_policy_acquire_returns_local_503_without_sending() {
        let mut upstream_server = mockito::Server::new_async().await;
        let response_mock = upstream_server
            .mock("POST", "/chat/completions")
            .expect(0)
            .create_async()
            .await;
        let provider = crate::providers::openai_compat::OpenAiCompatible::new(
            "test-provider",
            upstream_server.url(),
            None,
        );
        let deadlines = crate::client::AttemptDeadlines::default()
            .with_policy_callback_timeout(Duration::from_millis(5))
            .unwrap();
        let client = ShimClient::new().with_attempt_deadlines(deadlines).unwrap();
        let context = DispatchPolicyContext::new(Arc::new(PendingAcquirePolicy));
        let error = client
            .completion_with_policy(
                &provider,
                "test-model",
                &json!({"messages": [{"role": "user", "content": "test"}]}),
                &context,
            )
            .await
            .unwrap_err();
        assert!(matches!(
            error,
            ShimError::ProviderError { status: 503, .. }
        ));
        response_mock.assert_async().await;
    }

    #[tokio::test]
    async fn pending_header_usage_and_finish_callbacks_are_bounded_and_abandoned() {
        for pending in [
            PendingObservation::Headers,
            PendingObservation::Usage,
            PendingObservation::Finish,
        ] {
            let mut upstream_server = mockito::Server::new_async().await;
            let response_mock = upstream_server
                .mock("POST", "/chat/completions")
                .with_status(200)
                .with_body(
                    json!({
                        "id": "response",
                        "choices": [{
                            "index": 0,
                            "message": {"role": "assistant", "content": "ok"},
                            "finish_reason": "stop"
                        }],
                        "usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2}
                    })
                    .to_string(),
                )
                .expect(1)
                .create_async()
                .await;
            let provider = crate::providers::openai_compat::OpenAiCompatible::new(
                "test-provider",
                upstream_server.url(),
                None,
            );
            let deadlines = crate::client::AttemptDeadlines::default()
                .with_policy_callback_timeout(Duration::from_millis(20))
                .unwrap();
            let client = ShimClient::new().with_attempt_deadlines(deadlines).unwrap();
            let policy = Arc::new(PendingObservationPolicy {
                pending,
                abandoned: std::sync::atomic::AtomicBool::new(false),
            });
            let context = DispatchPolicyContext::new(policy.clone());
            let error = client
                .completion_with_policy(
                    &provider,
                    "test-model",
                    &json!({"messages": [{"role": "user", "content": "test"}]}),
                    &context,
                )
                .await
                .unwrap_err();
            assert!(
                matches!(error, ShimError::ProviderError { status: 503, .. }),
                "unexpected {pending:?} error: {error:?}"
            );
            assert!(policy.abandoned.load(std::sync::atomic::Ordering::SeqCst));
            response_mock.assert_async().await;
        }
    }

    impl AttemptPolicy for RecordingPolicy {
        fn acquire<'a>(
            &'a self,
            _attempt: &'a PreparedAttempt<'a>,
        ) -> AttemptPolicyFuture<'a, Result<(), AttemptPolicyRefusal>> {
            Box::pin(async { Ok(()) })
        }

        fn observe<'a>(
            &'a self,
            _attempt: &'a AttemptIdentity,
            event: AttemptEvent<'a>,
        ) -> AttemptPolicyFuture<'a, Result<(), AttemptPolicyError>> {
            Box::pin(async move {
                if let AttemptEvent::Finished(outcome) = event {
                    self.outcomes.lock().unwrap().push(outcome);
                }
                Ok(())
            })
        }

        fn observe_abandoned(
            &self,
            _attempt: &AttemptIdentity,
            outcome: AttemptOutcome,
        ) -> Result<(), AttemptPolicyError> {
            self.outcomes.lock().unwrap().push(outcome);
            Ok(())
        }
    }

    #[tokio::test]
    async fn oversized_success_finishes_the_attempt_with_unknown_accounting() {
        let mut upstream_server = mockito::Server::new_async().await;
        let response_mock = upstream_server
            .mock("POST", "/chat/completions")
            .with_status(200)
            .with_body("a".repeat(65))
            .expect(1)
            .create_async()
            .await;
        let provider = crate::providers::openai_compat::OpenAiCompatible::new(
            "test-provider",
            upstream_server.url(),
            None,
        );
        let policy = Arc::new(RecordingPolicy::default());
        let context = DispatchPolicyContext::new(policy.clone());
        let error = bounded_client()
            .completion_with_policy(
                &provider,
                "test-model",
                &json!({"messages": [{"role": "user", "content": "test"}]}),
                &context,
            )
            .await
            .unwrap_err();
        assert!(matches!(
            error,
            ShimError::ProviderError { status: 502, .. }
        ));
        assert_eq!(
            *policy.outcomes.lock().unwrap(),
            vec![AttemptOutcome::InvalidResponse {
                accounting: AttemptAccounting::Unknown
            }]
        );
        response_mock.assert_async().await;
    }
}