crabllm-proxy 0.0.22

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
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
use crabllm_core::{
    AnthropicRequest, AnthropicResponse, AudioSpeechRequest, ChatCompletionChunk,
    ChatCompletionRequest, ChatCompletionResponse, EmbeddingRequest, EmbeddingResponse,
    ImageRequest, ModelList,
};
use utoipa::openapi::{
    ContentBuilder, HttpMethod, InfoBuilder, PathItem, Paths, PathsBuilder, Ref, RefOr, Required,
    ResponseBuilder, Responses, ResponsesBuilder, Tag,
    path::{OperationBuilder, ParameterBuilder, ParameterIn},
    request_body::{RequestBody, RequestBodyBuilder},
    schema::{ComponentsBuilder, KnownFormat, ObjectBuilder, SchemaFormat, SchemaType, Type},
    security::{HttpAuthScheme, HttpBuilder, SecurityRequirement, SecurityScheme},
};
use utoipa::{PartialSchema, ToSchema};

use crate::admin::{CreateKeyRequest, KeyResponse, KeySummary};
use crate::admin_providers::{CreateProviderRequest, ProviderSummary};
use crate::ext::usage::UsageEntry;

const TAG_API: &str = "API";
const TAG_ADMIN_KEYS: &str = "Admin / Keys";
const TAG_ADMIN_PROVIDERS: &str = "Admin / Providers";
const TAG_ADMIN_USAGE: &str = "Admin / Usage";
const TAG_INFRA: &str = "Infrastructure";

macro_rules! collect_schemas {
    ($($ty:ty),* $(,)?) => {{
        let mut schemas = Vec::<(String, utoipa::openapi::RefOr<utoipa::openapi::schema::Schema>)>::new();
        $(
            <$ty as ToSchema>::schemas(&mut schemas);
            schemas.push((<$ty as ToSchema>::name().into(), <$ty as PartialSchema>::schema()));
        )*
        schemas
    }};
}

fn public_schemas() -> Vec<(String, RefOr<utoipa::openapi::schema::Schema>)> {
    collect_schemas!(
        ChatCompletionRequest,
        ChatCompletionResponse,
        ChatCompletionChunk,
        AnthropicRequest,
        AnthropicResponse,
        EmbeddingRequest,
        EmbeddingResponse,
        ImageRequest,
        AudioSpeechRequest,
        ModelList,
        UsageEntry,
    )
}

fn admin_schemas() -> Vec<(String, RefOr<utoipa::openapi::schema::Schema>)> {
    collect_schemas!(
        CreateKeyRequest,
        KeyResponse,
        KeySummary,
        CreateProviderRequest,
        ProviderSummary,
        UsageEntry,
    )
}

fn op(tag: &str, summary: &str) -> OperationBuilder {
    OperationBuilder::new().summary(Some(summary)).tag(tag)
}

/// Build a PathItem with multiple HTTP methods sharing one tag.
fn multi(tag: &str, ops: &[(HttpMethod, &str, Option<RequestBody>, Responses)]) -> PathItem {
    let mut item = PathItem::default();
    for (method, summary, body, responses) in ops {
        let mut builder = op(tag, summary).responses(responses.clone());
        if let Some(b) = body {
            builder = builder.request_body(Some(b.clone()));
        }
        let operation = builder.build();
        match method {
            HttpMethod::Get => item.get = Some(operation),
            HttpMethod::Post => item.post = Some(operation),
            HttpMethod::Put => item.put = Some(operation),
            HttpMethod::Patch => item.patch = Some(operation),
            HttpMethod::Delete => item.delete = Some(operation),
            _ => {}
        }
    }
    item
}

fn query(name: &str) -> ParameterBuilder {
    ParameterBuilder::new()
        .name(name)
        .parameter_in(ParameterIn::Query)
}

fn schema_ref(name: &str) -> Ref {
    Ref::from_schema_name(name)
}

fn json_body(name: &str) -> RequestBody {
    RequestBodyBuilder::new()
        .content(
            "application/json",
            ContentBuilder::new().schema(Some(schema_ref(name))).build(),
        )
        .required(Some(Required::True))
        .build()
}

fn json_merge_body() -> RequestBody {
    RequestBodyBuilder::new()
        .content(
            "application/json",
            ContentBuilder::new()
                .schema(Some(RefOr::T(
                    ObjectBuilder::new()
                        .description(Some(
                            "JSON Merge Patch — only the fields you want to change",
                        ))
                        .build()
                        .into(),
                )))
                .build(),
        )
        .required(Some(Required::True))
        .build()
}

fn json_ok(name: &str, desc: &str) -> Responses {
    ResponsesBuilder::new()
        .response(
            "200",
            ResponseBuilder::new()
                .description(desc)
                .content(
                    "application/json",
                    ContentBuilder::new().schema(Some(schema_ref(name))).build(),
                )
                .build(),
        )
        .build()
}

fn json_array_ok(name: &str, desc: &str) -> Responses {
    let array = ObjectBuilder::new()
        .schema_type(SchemaType::new(Type::Array))
        .build();
    let mut content = ContentBuilder::new()
        .schema(Some(RefOr::T(array.into())))
        .build();
    // Inline an `items` ref via raw schema. Easier: build the array schema
    // with items pointing at the named schema.
    let items_schema = utoipa::openapi::schema::ArrayBuilder::new()
        .items(schema_ref(name))
        .build();
    content.schema = Some(RefOr::T(items_schema.into()));
    ResponsesBuilder::new()
        .response(
            "200",
            ResponseBuilder::new()
                .description(desc)
                .content("application/json", content)
                .build(),
        )
        .build()
}

fn binary_ok(mime: &str, desc: &str) -> Responses {
    let bin = ObjectBuilder::new()
        .schema_type(SchemaType::new(Type::String))
        .format(Some(SchemaFormat::KnownFormat(KnownFormat::Binary)))
        .build();
    ResponsesBuilder::new()
        .response(
            "200",
            ResponseBuilder::new()
                .description(desc)
                .content(
                    mime,
                    ContentBuilder::new()
                        .schema(Some(RefOr::T(bin.into())))
                        .build(),
                )
                .build(),
        )
        .build()
}

fn no_content(desc: &str) -> Responses {
    ResponsesBuilder::new()
        .response("204", ResponseBuilder::new().description(desc).build())
        .build()
}

fn empty_ok(desc: &str) -> Responses {
    ResponsesBuilder::new()
        .response(
            "200",
            ResponseBuilder::new()
                .description(desc)
                .content(
                    "application/json",
                    ContentBuilder::new()
                        .schema(Some(RefOr::T(ObjectBuilder::new().build().into())))
                        .build(),
                )
                .build(),
        )
        .build()
}

/// Spec for the public OpenAI-compatible surface only
/// (`/v1/chat/completions` through `/v1/usage`). For embedders that put
/// crabllm behind their own auth and admin layer.
///
/// `api_tag` overrides the default `"API"` tag on every operation —
/// useful when merging into a host spec that groups the gateway under
/// a different tag name.
pub fn public(api_tag: Option<&str>) -> utoipa::openapi::OpenApi {
    let mut doc = base(public_schemas());
    doc.paths = public_paths();
    if let Some(tag) = api_tag {
        retag(&mut doc, tag);
    }
    doc
}

/// Spec for the admin surface only (`/v1/admin/*`, `/v1/budget`,
/// `/v1/cache`). Embedders that replace the admin surface with their own
/// will skip this; standalone deployments serve it alongside [`public`].
pub fn admin() -> utoipa::openapi::OpenApi {
    let mut doc = base(admin_schemas());
    doc.paths = admin_paths();
    doc
}

/// Spec for the infra surface only (`/health`, `/metrics`). Embedders
/// usually have their own probes and will skip this.
pub fn infra() -> utoipa::openapi::OpenApi {
    let mut doc = base(vec![]);
    doc.paths = infra_paths();
    doc
}

/// Combined spec: [`public`] + [`admin`] + [`infra`]. What standalone
/// crabllm deployments serve at `/openapi.json`.
pub fn spec() -> utoipa::openapi::OpenApi {
    let mut schemas = public_schemas();
    schemas.extend(admin_schemas());
    let mut doc = base(schemas);
    let mut all = public_paths();
    for (path, item) in admin_paths().paths {
        all.paths.insert(path, item);
    }
    for (path, item) in infra_paths().paths {
        all.paths.insert(path, item);
    }
    doc.paths = all;
    doc
}

fn base(
    schemas: Vec<(String, RefOr<utoipa::openapi::schema::Schema>)>,
) -> utoipa::openapi::OpenApi {
    let components = ComponentsBuilder::new()
        .schemas_from_iter(schemas)
        .security_scheme(
            "BearerAuth",
            SecurityScheme::Http(HttpBuilder::new().scheme(HttpAuthScheme::Bearer).build()),
        )
        .build();
    let mut doc = utoipa::openapi::OpenApiBuilder::new()
        .components(Some(components))
        .build();
    doc.info = InfoBuilder::new()
        .title("CrabLLM API")
        .version(env!("CARGO_PKG_VERSION"))
        .description(Some("High-performance LLM API gateway"))
        .build();
    doc.tags = Some(vec![
        Tag::new(TAG_API),
        Tag::new(TAG_ADMIN_KEYS),
        Tag::new(TAG_ADMIN_PROVIDERS),
        Tag::new(TAG_ADMIN_USAGE),
        Tag::new(TAG_INFRA),
    ]);
    doc.security = Some(vec![SecurityRequirement::new(
        "BearerAuth",
        Vec::<String>::new(),
    )]);

    doc
}

/// Override every operation's tag (and the doc-level tags list) with a
/// single name. Used by [`public`] to retag the OpenAI-compatible surface
/// when an embedder groups it under their own tag.
fn retag(doc: &mut utoipa::openapi::OpenApi, tag: &str) {
    for item in doc.paths.paths.values_mut() {
        for op in [
            &mut item.get,
            &mut item.post,
            &mut item.put,
            &mut item.patch,
            &mut item.delete,
            &mut item.head,
            &mut item.options,
            &mut item.trace,
        ]
        .into_iter()
        .flatten()
        {
            op.tags = Some(vec![tag.to_string()]);
        }
    }
    doc.tags = Some(vec![Tag::new(tag)]);
}

fn public_paths() -> Paths {
    PathsBuilder::new()
        .path(
            "/v1/chat/completions",
            PathItem::new(
                HttpMethod::Post,
                op(TAG_API, "Create a chat completion")
                    .description(Some(
                        "Returns a single JSON response, or an SSE stream of \
                         `ChatCompletionChunk` events when `stream=true`.",
                    ))
                    .request_body(Some(json_body("ChatCompletionRequest")))
                    .responses(json_ok(
                        "ChatCompletionResponse",
                        "Chat completion (or SSE stream when stream=true)",
                    )),
            ),
        )
        .path(
            "/v1/messages",
            PathItem::new(
                HttpMethod::Post,
                op(TAG_API, "Create a message (Anthropic format)")
                    .description(Some(
                        "Anthropic-style messages endpoint. Body and response follow \
                         the Anthropic Messages API; SSE is returned when stream=true.",
                    ))
                    .request_body(Some(json_body("AnthropicRequest")))
                    .responses(json_ok(
                        "AnthropicResponse",
                        "Message response (or SSE stream when stream=true)",
                    )),
            ),
        )
        .path(
            "/v1/embeddings",
            PathItem::new(
                HttpMethod::Post,
                op(TAG_API, "Create embeddings")
                    .request_body(Some(json_body("EmbeddingRequest")))
                    .responses(json_ok("EmbeddingResponse", "Embedding vectors")),
            ),
        )
        .path(
            "/v1/images/generations",
            PathItem::new(
                HttpMethod::Post,
                op(TAG_API, "Generate images")
                    .request_body(Some(json_body("ImageRequest")))
                    .responses(binary_ok("image/png", "Generated image bytes")),
            ),
        )
        .path(
            "/v1/audio/speech",
            PathItem::new(
                HttpMethod::Post,
                op(TAG_API, "Generate speech audio")
                    .request_body(Some(json_body("AudioSpeechRequest")))
                    .responses(binary_ok("audio/mpeg", "Synthesized audio bytes")),
            ),
        )
        .path(
            "/v1/audio/transcriptions",
            PathItem::new(
                HttpMethod::Post,
                op(TAG_API, "Transcribe audio")
                    .description(Some(
                        "Multipart form upload: `model` field plus an audio file.",
                    ))
                    .request_body(Some(
                        RequestBodyBuilder::new()
                            .content(
                                "multipart/form-data",
                                ContentBuilder::new()
                                    .schema(Some(RefOr::T(ObjectBuilder::new().build().into())))
                                    .build(),
                            )
                            .required(Some(Required::True))
                            .build(),
                    ))
                    .responses(empty_ok("Transcription result")),
            ),
        )
        .path(
            "/v1/models",
            PathItem::new(
                HttpMethod::Get,
                op(TAG_API, "List available models")
                    .responses(json_ok("ModelList", "Models the caller can access")),
            ),
        )
        .path(
            "/v1/usage",
            PathItem::new(
                HttpMethod::Get,
                op(TAG_API, "Get usage for the authenticated key")
                    .parameter(query("model"))
                    .responses(json_array_ok("UsageEntry", "Usage rows for this key")),
            ),
        )
        .build()
}

fn admin_paths() -> Paths {
    PathsBuilder::new()
        .path(
            "/v1/admin/keys",
            multi(
                TAG_ADMIN_KEYS,
                &[
                    (
                        HttpMethod::Post,
                        "Create a virtual API key",
                        Some(json_body("CreateKeyRequest")),
                        json_ok(
                            "KeyResponse",
                            "Newly created key (full secret returned once)",
                        ),
                    ),
                    (
                        HttpMethod::Get,
                        "List all virtual keys",
                        None,
                        json_array_ok("KeySummary", "All known keys with masked secrets"),
                    ),
                ],
            ),
        )
        .path(
            "/v1/admin/keys/{name}",
            multi(
                TAG_ADMIN_KEYS,
                &[
                    (
                        HttpMethod::Get,
                        "Get key details",
                        None,
                        json_ok("KeySummary", "Key details"),
                    ),
                    (
                        HttpMethod::Patch,
                        "Update a key (models, rate_limit)",
                        Some(json_merge_body()),
                        json_ok("KeySummary", "Updated key"),
                    ),
                    (
                        HttpMethod::Delete,
                        "Revoke a virtual key",
                        None,
                        no_content("Key revoked"),
                    ),
                ],
            ),
        )
        .path(
            "/v1/admin/providers",
            multi(
                TAG_ADMIN_PROVIDERS,
                &[
                    (
                        HttpMethod::Post,
                        "Create a provider",
                        Some(json_body("CreateProviderRequest")),
                        json_ok("ProviderSummary", "Newly created provider (secrets masked)"),
                    ),
                    (
                        HttpMethod::Get,
                        "List all providers",
                        None,
                        json_array_ok("ProviderSummary", "All known providers (secrets masked)"),
                    ),
                ],
            ),
        )
        .path(
            "/v1/admin/providers/{name}",
            multi(
                TAG_ADMIN_PROVIDERS,
                &[
                    (
                        HttpMethod::Get,
                        "Get provider details",
                        None,
                        json_ok("ProviderSummary", "Provider details (secrets masked)"),
                    ),
                    (
                        HttpMethod::Patch,
                        "Update a provider",
                        Some(json_merge_body()),
                        json_ok("ProviderSummary", "Updated provider (secrets masked)"),
                    ),
                    (
                        HttpMethod::Delete,
                        "Delete a provider",
                        None,
                        no_content("Provider deleted"),
                    ),
                ],
            ),
        )
        .path(
            "/v1/admin/usage",
            PathItem::new(
                HttpMethod::Get,
                op(TAG_ADMIN_USAGE, "Global usage view")
                    .parameter(query("name"))
                    .parameter(query("model"))
                    .responses(json_array_ok("UsageEntry", "Usage rows across all keys")),
            ),
        )
        .path(
            "/v1/admin/logs",
            PathItem::new(
                HttpMethod::Get,
                op(TAG_ADMIN_USAGE, "Query audit logs")
                    .parameter(query("key"))
                    .parameter(query("model"))
                    .parameter(query("since"))
                    .parameter(query("until"))
                    .parameter(query("limit"))
                    .responses(empty_ok("Audit log entries")),
            ),
        )
        .path(
            "/v1/budget",
            PathItem::new(
                HttpMethod::Get,
                op(TAG_ADMIN_USAGE, "Get budget status per key")
                    .responses(empty_ok("Budget status per key")),
            ),
        )
        .path(
            "/v1/cache",
            PathItem::new(
                HttpMethod::Delete,
                op(TAG_ADMIN_USAGE, "Clear response cache").responses(no_content("Cache cleared")),
            ),
        )
        .build()
}

fn infra_paths() -> Paths {
    PathsBuilder::new()
        .path(
            "/health",
            PathItem::new(
                HttpMethod::Get,
                op(TAG_INFRA, "Health check").responses(empty_ok("Service healthy")),
            ),
        )
        .path(
            "/metrics",
            PathItem::new(
                HttpMethod::Get,
                op(TAG_INFRA, "Prometheus metrics").responses(binary_ok(
                    "text/plain; version=0.0.4",
                    "Prometheus exposition format",
                )),
            ),
        )
        .build()
}