crabllm-proxy 0.0.19

HTTP proxy server for the crabllm LLM API gateway
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
//! HTTP handler for `POST /v1/messages` (Anthropic-compatible).
//!
//! Translates the inbound `AnthropicRequest` to the internal
//! `ChatCompletionRequest`, dispatches via the same provider pipeline the
//! OpenAI `/v1/chat/completions` handler uses, then translates the response
//! (or streaming chunk sequence) back to Anthropic's wire format.

use crate::{
    AppState,
    anthropic::{from_chat_completion, to_anthropic_sse, to_chat_completion},
    auth::Principal,
    handlers::{
        emit_usage, emit_usage_error, error_response, error_status, record_duration, record_tokens,
        try_chat_with_retries, try_stream_with_retries,
    },
};
use axum::{
    Extension, Json,
    extract::State,
    http::StatusCode,
    response::{
        IntoResponse, Response,
        sse::{Event, Sse},
    },
};
use crabllm_core::{ApiError, Provider, RequestContext, Storage};
use futures::StreamExt;
use std::sync::{
    Arc, Mutex,
    atomic::{AtomicBool, AtomicU32, Ordering},
};
use std::time::Instant;

const ENDPOINT: &str = "messages";

/// Lightweight peek for routing the Anthropic request.
#[derive(serde::Deserialize)]
struct AnthropicPeek {
    model: String,
    #[serde(default)]
    stream: Option<bool>,
}

/// POST /v1/messages
pub async fn messages<S, P>(
    State(state): State<AppState<S, P>>,
    Extension(principal): Extension<Principal>,
    raw_body: axum::body::Bytes,
) -> Response
where
    S: Storage + 'static,
    P: Provider + 'static,
{
    let peek: AnthropicPeek = match crabllm_core::json::from_slice(&raw_body) {
        Ok(r) => r,
        Err(e) => {
            return (
                StatusCode::BAD_REQUEST,
                Json(ApiError::new(e.to_string(), "invalid_request_error")),
            )
                .into_response();
        }
    };
    let is_stream = peek.stream == Some(true);
    let registry = state.registry();
    let model = registry.resolve(&peek.model).to_string();
    let deployments = match registry.dispatch_list(&model) {
        Some(list) => list,
        None => {
            return (
                StatusCode::NOT_FOUND,
                Json(ApiError::new(
                    format!("model '{model}' not found"),
                    "invalid_request_error",
                )),
            )
                .into_response();
        }
    };

    // Raw byte proxy: non-streaming, no extensions, Anthropic-compatible upstream.
    if !is_stream
        && state.extensions.is_empty()
        && deployments.iter().all(|d| d.provider.is_anthropic_compat())
    {
        return handle_raw_anthropic(&state, principal, &model, &deployments, raw_body).await;
    }

    // Full deserialization + translation for streaming, extensions, or
    // non-Anthropic upstreams.
    let anthropic_req: crabllm_core::AnthropicRequest =
        match crabllm_core::json::from_slice(&raw_body) {
            Ok(r) => r,
            Err(e) => {
                return (
                    StatusCode::BAD_REQUEST,
                    Json(ApiError::new(e.to_string(), "invalid_request_error")),
                )
                    .into_response();
            }
        };
    let mut request = to_chat_completion(anthropic_req);

    let provider_name = registry
        .provider_name(&model)
        .unwrap_or_default()
        .to_string();

    let ctx = RequestContext {
        request_id: uuid::Uuid::new_v4().to_string(),
        model: model.clone(),
        provider: provider_name,
        principal: principal.0,
        is_stream,
        started_at: Instant::now(),
    };

    for ext in state.extensions.iter() {
        if let Err(ext_err) = ext.on_request(&ctx).await {
            return (
                StatusCode::from_u16(ext_err.status).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR),
                Json(ext_err.body),
            )
                .into_response();
        }
    }

    if is_stream {
        request
            .extra
            .entry("stream_options".to_string())
            .or_insert(serde_json::json!({ "include_usage": true }));

        let mut last_err = None;
        for deployment in &deployments {
            match try_stream_with_retries(deployment, &request).await {
                Ok(stream) => {
                    let extensions = state.extensions.clone();
                    let ctx = Arc::new(ctx);
                    let errored = Arc::new(AtomicBool::new(false));
                    let tokens_in = Arc::new(AtomicU32::new(0));
                    let tokens_out = Arc::new(AtomicU32::new(0));
                    let first_error: Arc<Mutex<Option<String>>> = Arc::new(Mutex::new(None));

                    let ctx_done = ctx.clone();
                    let errored_done = errored.clone();
                    let tokens_in_done = tokens_in.clone();
                    let tokens_out_done = tokens_out.clone();
                    let first_error_done = first_error.clone();

                    let observed = stream.then(move |result| {
                        let extensions = extensions.clone();
                        let ctx = ctx.clone();
                        let errored = errored.clone();
                        let tokens_in = tokens_in.clone();
                        let tokens_out = tokens_out.clone();
                        let first_error = first_error.clone();
                        async move {
                            match &result {
                                Ok(chunk) => {
                                    if let Some(ref usage) = chunk.usage {
                                        record_tokens(
                                            &ctx,
                                            usage.prompt_tokens,
                                            usage.completion_tokens,
                                        );
                                        tokens_in.store(usage.prompt_tokens, Ordering::Relaxed);
                                        tokens_out
                                            .store(usage.completion_tokens, Ordering::Relaxed);
                                    }
                                    for ext in extensions.iter() {
                                        ext.on_chunk(&ctx, chunk).await;
                                    }
                                }
                                Err(error) => {
                                    errored.store(true, Ordering::Relaxed);
                                    {
                                        let mut slot = first_error.lock().unwrap();
                                        if slot.is_none() {
                                            *slot = Some(error.to_string());
                                        }
                                    }
                                    for ext in extensions.iter() {
                                        ext.on_error(&ctx, error).await;
                                    }
                                }
                            }
                            result
                        }
                    });

                    let anthropic_events = to_anthropic_sse(Box::pin(observed));

                    let sse_stream = anthropic_events.map(|result| match result {
                        Ok(event) => {
                            let name = event.event_name();
                            let json = crabllm_core::json::to_string(&event).unwrap_or_default();
                            Ok::<_, std::convert::Infallible>(
                                Event::default().event(name).data(json),
                            )
                        }
                        Err(e) => {
                            // Anthropic documents `api_error` / `overloaded_error`
                            // rather than `server_error` in the enum.
                            let json = crabllm_core::json::to_string(&serde_json::json!({
                                "type": "error",
                                "error": {
                                    "type": "api_error",
                                    "message": e.to_string(),
                                },
                            }))
                            .unwrap_or_default();
                            Ok(Event::default().event("error").data(json))
                        }
                    });

                    // Wrap the stream so that emit_usage/record_duration run
                    // exactly once, when the wire stream drains — without
                    // putting a spurious SSE frame on the wire.
                    let finalized = futures::stream::unfold(
                        (
                            Box::pin(sse_stream),
                            Some((
                                state.clone(),
                                ctx_done,
                                tokens_in_done,
                                tokens_out_done,
                                errored_done,
                                first_error_done,
                            )),
                        ),
                        |(mut inner, mut slot)| async move {
                            match inner.next().await {
                                Some(item) => Some((item, (inner, slot))),
                                None => {
                                    if let Some((state, ctx, ti, to, er, fe)) = slot.take() {
                                        let errored = er.load(Ordering::Relaxed);
                                        record_duration(&ctx, if errored { "5xx" } else { "2xx" });
                                        let error = fe.lock().unwrap().take();
                                        let status = if errored { 0 } else { 200 };
                                        emit_usage(
                                            &state,
                                            &ctx,
                                            ENDPOINT,
                                            ti.load(Ordering::Relaxed),
                                            to.load(Ordering::Relaxed),
                                            status,
                                            error,
                                        );
                                    }
                                    None
                                }
                            }
                        },
                    );

                    return Sse::new(finalized)
                        .keep_alive(axum::response::sse::KeepAlive::new())
                        .into_response();
                }
                Err(e) => last_err = Some(e),
            }
        }

        let e = last_err
            .unwrap_or_else(|| crabllm_core::Error::Internal("no providers available".into()));
        for ext in state.extensions.iter() {
            ext.on_error(&ctx, &e).await;
        }
        record_duration(&ctx, "5xx");
        emit_usage_error(&state, &ctx, ENDPOINT, &e);
        return error_response(e);
    }

    // Non-streaming: cache first. A cache hit whose shape can't be translated
    // (missing usage, zero choices) falls through to live dispatch rather than
    // erroring — the cached payload was valid for the OpenAI handler, and the
    // translation gap is our bug, not the client's.
    for ext in state.extensions.iter() {
        if let Some(cached) = ext.on_cache_lookup(&request).await
            && let Ok(resp) = from_chat_completion(cached)
        {
            return Json(resp).into_response();
        }
    }

    let mut last_err = None;
    for deployment in &deployments {
        match try_chat_with_retries(deployment, &request).await {
            Ok(resp) => {
                let (pt, ct) = resp
                    .usage
                    .as_ref()
                    .map(|u| (u.prompt_tokens, u.completion_tokens))
                    .unwrap_or((0, 0));
                if pt > 0 || ct > 0 {
                    record_tokens(&ctx, pt, ct);
                }
                record_duration(&ctx, "2xx");
                emit_usage(&state, &ctx, ENDPOINT, pt, ct, 200, None);
                for ext in state.extensions.iter() {
                    ext.on_response(&ctx, &request, &resp).await;
                }
                return match from_chat_completion(resp) {
                    Ok(anthropic) => Json(anthropic).into_response(),
                    Err(e) => {
                        for ext in state.extensions.iter() {
                            ext.on_error(&ctx, &e).await;
                        }
                        record_duration(&ctx, error_status(&e));
                        emit_usage_error(&state, &ctx, ENDPOINT, &e);
                        error_response(e)
                    }
                };
            }
            Err(e) => last_err = Some(e),
        }
    }

    let e =
        last_err.unwrap_or_else(|| crabllm_core::Error::Internal("no providers available".into()));
    for ext in state.extensions.iter() {
        ext.on_error(&ctx, &e).await;
    }
    record_duration(&ctx, error_status(&e));
    emit_usage_error(&state, &ctx, ENDPOINT, &e);
    error_response(e)
}

/// Non-streaming raw byte proxy for Anthropic-compatible providers.
async fn handle_raw_anthropic<S: Storage, P: Provider>(
    state: &AppState<S, P>,
    principal: Principal,
    model: &str,
    deployments: &[&crabllm_provider::Deployment<P>],
    raw_body: axum::body::Bytes,
) -> Response {
    use crate::handlers::with_timeout;

    #[derive(serde::Deserialize)]
    struct AnthropicUsagePeek {
        usage: Option<AnthropicUsageFields>,
    }
    #[derive(serde::Deserialize)]
    struct AnthropicUsageFields {
        #[serde(default)]
        input_tokens: u32,
        #[serde(default)]
        output_tokens: u32,
    }

    let registry = state.registry();
    let provider_name = registry
        .provider_name(model)
        .unwrap_or_default()
        .to_string();

    let ctx = RequestContext {
        request_id: uuid::Uuid::new_v4().to_string(),
        model: model.to_string(),
        provider: provider_name,
        principal: principal.0,
        is_stream: false,
        started_at: Instant::now(),
    };

    let mut last_err = None;
    for deployment in deployments {
        match with_timeout(
            deployment.timeout,
            deployment.provider.anthropic_messages_raw(raw_body.clone()),
        )
        .await
        {
            Ok(resp_bytes) => {
                let (pt, ct) = crabllm_core::json::from_slice::<AnthropicUsagePeek>(&resp_bytes)
                    .ok()
                    .and_then(|p| p.usage)
                    .map(|u| (u.input_tokens, u.output_tokens))
                    .unwrap_or((0, 0));
                if pt > 0 || ct > 0 {
                    record_tokens(&ctx, pt, ct);
                }
                record_duration(&ctx, "2xx");
                emit_usage(state, &ctx, ENDPOINT, pt, ct, 200, None);
                return (
                    [(axum::http::header::CONTENT_TYPE, "application/json")],
                    resp_bytes,
                )
                    .into_response();
            }
            Err(e) => {
                if !e.is_transient() {
                    record_duration(&ctx, error_status(&e));
                    emit_usage_error(state, &ctx, ENDPOINT, &e);
                    return error_response(e);
                }
                last_err = Some(e);
            }
        }
    }

    let e = last_err
        .unwrap_or_else(|| crabllm_core::Error::Internal("no providers available".to_string()));
    record_duration(&ctx, error_status(&e));
    emit_usage_error(state, &ctx, ENDPOINT, &e);
    error_response(e)
}