llmshim 0.11.0

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
//! Trusted, out-of-band policy hooks for individual provider attempts.

use crate::error::ShimError;
use crate::reasoning::{ReplayTarget, WireFormat};
use serde_json::Value;
use std::{future::Future, pin::Pin, sync::Arc, time::Duration};

pub type AttemptPolicyFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;

#[derive(Clone)]
pub struct DispatchPolicyContext {
    policy: Arc<dyn AttemptPolicy>,
}

impl DispatchPolicyContext {
    pub fn new(policy: Arc<dyn AttemptPolicy>) -> Self {
        Self { policy }
    }

    pub(crate) async fn acquire(
        &self,
        kind: AttemptKind,
        resolved_model: &str,
        prepared_target: &ReplayTarget,
        endpoint: &str,
        native_body: &Value,
    ) -> Result<AttemptTracker, AttemptPolicyRefusal> {
        let identity = AttemptIdentity {
            id: uuid::Uuid::new_v4(),
            kind,
            provider_name: prepared_target.provider.clone(),
            resolved_model: resolved_model.to_owned(),
            native_model: prepared_target.model.clone(),
            wire: prepared_target.wire,
            account_fingerprint: prepared_target.account.clone(),
            endpoint: sanitized_endpoint(endpoint),
        };
        let prepared_attempt = PreparedAttempt {
            identity: &identity,
            method: "POST",
            native_body,
        };
        self.policy.acquire(&prepared_attempt).await?;
        Ok(AttemptTracker {
            context: self.clone(),
            identity,
            usage_observed: false,
            finished: false,
        })
    }

    async fn observe(
        &self,
        identity: &AttemptIdentity,
        event: AttemptEvent<'_>,
    ) -> Result<(), AttemptPolicyError> {
        self.policy.observe(identity, event).await
    }
}

impl std::fmt::Debug for DispatchPolicyContext {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("DispatchPolicyContext")
            .finish_non_exhaustive()
    }
}

pub trait AttemptPolicy: Send + Sync {
    /// Admission must retain any conservative liability until a successful
    /// terminal observation resolves it. Callback failure, cancellation, and
    /// stream abandonment must never release that liability as zero.
    fn acquire<'a>(
        &'a self,
        attempt: &'a PreparedAttempt<'a>,
    ) -> AttemptPolicyFuture<'a, Result<(), AttemptPolicyRefusal>>;

    fn observe<'a>(
        &'a self,
        attempt: &'a AttemptIdentity,
        event: AttemptEvent<'a>,
    ) -> AttemptPolicyFuture<'a, Result<(), AttemptPolicyError>>;

    /// The acquire-time liability remains authoritative when this best-effort
    /// drop notification fails.
    fn observe_abandoned(
        &self,
        attempt: &AttemptIdentity,
        outcome: AttemptOutcome,
    ) -> Result<(), AttemptPolicyError>;
}

pub struct PreparedAttempt<'a> {
    identity: &'a AttemptIdentity,
    method: &'static str,
    native_body: &'a Value,
}

impl<'a> PreparedAttempt<'a> {
    pub fn identity(&self) -> &AttemptIdentity {
        self.identity
    }

    pub fn method(&self) -> &'static str {
        self.method
    }

    pub fn native_body(&self) -> &'a Value {
        self.native_body
    }
}

impl std::fmt::Debug for PreparedAttempt<'_> {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("PreparedAttempt")
            .field("identity", self.identity)
            .field("method", &self.method)
            .field("native_body", &"<redacted>")
            .finish()
    }
}

#[derive(Clone, PartialEq, Eq)]
pub struct AttemptIdentity {
    id: uuid::Uuid,
    kind: AttemptKind,
    provider_name: String,
    resolved_model: String,
    native_model: String,
    wire: WireFormat,
    account_fingerprint: Option<String>,
    endpoint: String,
}

impl AttemptIdentity {
    pub fn id(&self) -> uuid::Uuid {
        self.id
    }

    pub fn kind(&self) -> AttemptKind {
        self.kind
    }

    pub fn provider_name(&self) -> &str {
        &self.provider_name
    }

    pub fn resolved_model(&self) -> &str {
        &self.resolved_model
    }

    pub fn native_model(&self) -> &str {
        &self.native_model
    }

    pub fn wire(&self) -> WireFormat {
        self.wire
    }

    pub fn account_fingerprint(&self) -> Option<&str> {
        self.account_fingerprint.as_deref()
    }

    pub fn endpoint(&self) -> &str {
        &self.endpoint
    }
}

impl std::fmt::Debug for AttemptIdentity {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("AttemptIdentity")
            .field("id", &self.id)
            .field("kind", &self.kind)
            .field("provider_name", &self.provider_name)
            .field("resolved_model", &self.resolved_model)
            .field("native_model", &self.native_model)
            .field("wire", &self.wire)
            .field(
                "account_fingerprint",
                &self.account_fingerprint.as_ref().map(|_| "<redacted>"),
            )
            .field("endpoint", &"<redacted>")
            .finish()
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AttemptKind {
    Completion,
    Stream,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AttemptAccounting {
    UsageObserved,
    NoUsageReported,
    Unknown,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AttemptOutcome {
    Completed {
        accounting: AttemptAccounting,
    },
    HttpFailure {
        status: u16,
        accounting: AttemptAccounting,
    },
    TransportFailure {
        accounting: AttemptAccounting,
    },
    InvalidResponse {
        accounting: AttemptAccounting,
    },
    StreamFailure {
        accounting: AttemptAccounting,
    },
    Abandoned {
        kind: AttemptKind,
        accounting: AttemptAccounting,
    },
}

#[derive(Debug)]
pub enum AttemptEvent<'a> {
    ResponseHeaders {
        status: u16,
    },
    /// A cumulative accounting snapshot. Streams may update it more than once;
    /// consumers must key settlement by [`AttemptIdentity::id`].
    Usage {
        usage: &'a Value,
    },
    Finished(AttemptOutcome),
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AttemptPolicyRefusalKind {
    /// Provider-wide capacity refusal. Fallback may only advance to a target
    /// resolved to a different provider.
    ProviderLimit,
    TenantLimit,
    Budget,
    Unpriceable,
    CoordinatorUnavailable,
    Other,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct AttemptPolicyRefusal {
    kind: AttemptPolicyRefusalKind,
    retry_after: Option<Duration>,
}

impl AttemptPolicyRefusal {
    pub fn new(kind: AttemptPolicyRefusalKind, retry_after: Option<Duration>) -> Self {
        Self { kind, retry_after }
    }

    pub fn kind(&self) -> AttemptPolicyRefusalKind {
        self.kind
    }

    pub fn retry_after(&self) -> Option<Duration> {
        self.retry_after
    }

    pub(crate) fn into_shim_error(self) -> ShimError {
        let (status, body) = match self.kind {
            AttemptPolicyRefusalKind::ProviderLimit => (429, "provider attempt limit exceeded"),
            AttemptPolicyRefusalKind::TenantLimit => (429, "tenant attempt limit exceeded"),
            AttemptPolicyRefusalKind::Budget => (429, "attempt budget exhausted"),
            AttemptPolicyRefusalKind::Unpriceable => {
                (400, "attempt cannot be admitted under the active policy")
            }
            AttemptPolicyRefusalKind::CoordinatorUnavailable => {
                (503, "attempt policy coordinator unavailable")
            }
            AttemptPolicyRefusalKind::Other => (429, "attempt refused by policy"),
        };
        ShimError::ProviderError {
            status,
            body: body.to_owned(),
            retry_after: self.retry_after,
        }
    }
}

impl std::fmt::Display for AttemptPolicyRefusal {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(formatter, "attempt refused by policy ({:?})", self.kind)
    }
}

impl std::error::Error for AttemptPolicyRefusal {}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AttemptPolicyErrorKind {
    CoordinatorUnavailable,
    Other,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct AttemptPolicyError {
    kind: AttemptPolicyErrorKind,
}

impl AttemptPolicyError {
    pub fn new(kind: AttemptPolicyErrorKind) -> Self {
        Self { kind }
    }

    pub fn kind(&self) -> AttemptPolicyErrorKind {
        self.kind
    }

    pub(crate) fn into_shim_error(self) -> ShimError {
        ShimError::ProviderError {
            status: 503,
            body: match self.kind {
                AttemptPolicyErrorKind::CoordinatorUnavailable => {
                    "attempt policy coordinator unavailable"
                }
                AttemptPolicyErrorKind::Other => "attempt policy observation failed",
            }
            .to_owned(),
            retry_after: None,
        }
    }
}

impl std::fmt::Display for AttemptPolicyError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            formatter,
            "attempt policy observation failed ({:?})",
            self.kind
        )
    }
}

impl std::error::Error for AttemptPolicyError {}

pub(crate) struct AttemptTracker {
    context: DispatchPolicyContext,
    identity: AttemptIdentity,
    usage_observed: bool,
    finished: bool,
}

impl AttemptTracker {
    pub(crate) async fn response_headers(&self, status: u16) -> Result<(), AttemptPolicyError> {
        self.context
            .observe(&self.identity, AttemptEvent::ResponseHeaders { status })
            .await
    }

    pub(crate) async fn usage(&mut self, usage: &Value) -> Result<(), AttemptPolicyError> {
        self.context
            .observe(&self.identity, AttemptEvent::Usage { usage })
            .await?;
        self.usage_observed = true;
        Ok(())
    }

    pub(crate) fn accounting(&self, completed: bool) -> AttemptAccounting {
        if self.usage_observed {
            AttemptAccounting::UsageObserved
        } else if completed {
            AttemptAccounting::NoUsageReported
        } else {
            AttemptAccounting::Unknown
        }
    }

    pub(crate) async fn finish(
        &mut self,
        outcome: AttemptOutcome,
    ) -> Result<(), AttemptPolicyError> {
        if self.finished {
            return Ok(());
        }
        self.context
            .observe(&self.identity, AttemptEvent::Finished(outcome))
            .await?;
        self.finished = true;
        Ok(())
    }
}

impl Drop for AttemptTracker {
    fn drop(&mut self) {
        if self.finished {
            return;
        }
        let accounting = self.accounting(false);
        let _ = self.context.policy.observe_abandoned(
            &self.identity,
            AttemptOutcome::Abandoned {
                kind: self.identity.kind,
                accounting,
            },
        );
        self.finished = true;
    }
}

fn sanitized_endpoint(endpoint: &str) -> String {
    let Ok(mut parsed) = reqwest::Url::parse(endpoint) else {
        return "<invalid-endpoint>".to_owned();
    };
    let _ = parsed.set_username("");
    let _ = parsed.set_password(None);
    parsed.set_query(None);
    parsed.set_fragment(None);
    parsed.to_string()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn endpoint_metadata_never_keeps_query_credentials() {
        assert_eq!(
            sanitized_endpoint("https://example.test/models/m:generate?key=secret&alt=sse"),
            "https://example.test/models/m:generate"
        );
        assert_eq!(
            sanitized_endpoint("https://user:password@example.test/v1"),
            "https://example.test/v1"
        );
        assert_eq!(sanitized_endpoint("not a url"), "<invalid-endpoint>");
    }
}