sie-sdk-rs 0.1.0

Rust client for the SIE inference server: embeddings, reranking, extraction and text generation
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
//! End-to-end tests over a mock HTTP server.
//!
//! These cover what unit tests cannot: the request the SDK actually puts on the wire, and
//! the retry loop driving real responses.

use std::time::Duration;

use rmpv::Value as MsgValue;
use serde_json::json;
use sie_sdk::{Client, Error, Item, OutputType};
use wiremock::matchers::{body_json, header, method, path, query_param};
use wiremock::{Mock, MockServer, Request, ResponseTemplate};

/// A client with the retry budget tightened so a failing test finishes in milliseconds.
async fn client(server: &MockServer) -> Client {
    Client::builder(server.uri())
        .api_key("test-key")
        .timeout(Duration::from_secs(2))
        .provision_timeout(Duration::from_secs(2))
        .build()
        .unwrap()
}

/// Build the msgpack-numpy encoding of an `f32` array, as the server sends one.
fn numpy_f32(shape: &[usize], values: &[f32]) -> MsgValue {
    MsgValue::Map(vec![
        (MsgValue::Binary(b"nd".to_vec()), MsgValue::Boolean(true)),
        (
            MsgValue::Binary(b"type".to_vec()),
            MsgValue::String("<f4".into()),
        ),
        (
            MsgValue::Binary(b"shape".to_vec()),
            MsgValue::Array(
                shape
                    .iter()
                    .map(|dim| MsgValue::from(*dim as u64))
                    .collect(),
            ),
        ),
        (
            MsgValue::Binary(b"data".to_vec()),
            MsgValue::Binary(values.iter().flat_map(|v| v.to_le_bytes()).collect()),
        ),
    ])
}

fn encode_response(vectors: &[&[f32]]) -> Vec<u8> {
    let items = vectors
        .iter()
        .enumerate()
        .map(|(index, values)| {
            MsgValue::Map(vec![
                (MsgValue::from("id"), MsgValue::from(index.to_string())),
                (
                    MsgValue::from("dense"),
                    MsgValue::Map(vec![(
                        MsgValue::from("values"),
                        numpy_f32(&[values.len()], values),
                    )]),
                ),
            ])
        })
        .collect();
    let body = MsgValue::Map(vec![
        (MsgValue::from("model"), MsgValue::from("BAAI/bge-m3")),
        (MsgValue::from("items"), MsgValue::Array(items)),
    ]);
    rmp_serde::to_vec(&body).unwrap()
}

fn msgpack_response(body: Vec<u8>) -> ResponseTemplate {
    ResponseTemplate::new(200).set_body_raw(body, "application/msgpack")
}

fn capacity_response(code: &str) -> ResponseTemplate {
    ResponseTemplate::new(503)
        .insert_header("retry-after", "0")
        .set_body_json(json!({"error": {"code": code, "message": "not yet"}}))
}

#[tokio::test]
async fn encode_sends_msgpack_and_decodes_numpy_tensors() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/v1/encode/BAAI/bge-m3"))
        .and(header("content-type", "application/msgpack"))
        .and(header("accept", "application/msgpack"))
        .and(header("authorization", "Bearer test-key"))
        .respond_with(msgpack_response(encode_response(&[&[0.25, -0.5]])))
        .mount(&server)
        .await;

    let result = client(&server)
        .await
        .encode("BAAI/bge-m3", [Item::text("hello")])
        .output_types([OutputType::Dense])
        .send_one()
        .await
        .unwrap();

    assert_eq!(result.model.as_deref(), Some("BAAI/bge-m3"));
    assert_eq!(result.require_dense().unwrap(), &[0.25, -0.5]);
}

#[tokio::test]
async fn routing_headers_carry_the_pool_and_profile() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/v1/encode/m"))
        .and(header("x-sie-machine-profile", "l4"))
        .and(header("x-sie-pool", "prod"))
        .respond_with(msgpack_response(encode_response(&[&[1.0]])))
        .mount(&server)
        .await;

    client(&server)
        .await
        .encode("m", [Item::text("hi")])
        .gpu("prod/l4")
        .send_one()
        .await
        .unwrap();
}

#[tokio::test]
async fn a_result_count_mismatch_is_refused() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .respond_with(msgpack_response(encode_response(&[&[1.0]])))
        .mount(&server)
        .await;

    let err = client(&server)
        .await
        .encode("m", [Item::text("a"), Item::text("b")])
        .send()
        .await
        .unwrap_err();
    assert_eq!(err.code(), Some("ENCODE_RESULT_COUNT_MISMATCH"));
}

#[tokio::test]
async fn capacity_responses_are_retried_until_the_server_is_ready() {
    // A `Retry-After: 0` is honoured verbatim on the first OOM attempt but only floors the
    // exponential schedule afterwards, so that branch is exercised with a single failure.
    for (code, failures) in [
        ("PROVISIONING", 2),
        ("MODEL_LOADING", 2),
        ("LORA_LOADING", 2),
        ("RESOURCE_EXHAUSTED", 1),
    ] {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .respond_with(capacity_response(code))
            .up_to_n_times(failures)
            .mount(&server)
            .await;
        Mock::given(method("POST"))
            .respond_with(msgpack_response(encode_response(&[&[1.0]])))
            .mount(&server)
            .await;

        let result = client(&server)
            .await
            .encode("m", [Item::text("hi")])
            .send_one()
            .await
            .unwrap_or_else(|err| panic!("{code} should have been retried: {err}"));
        assert_eq!(result.request.unwrap().retries, failures as u32, "{code}");
    }
}

#[tokio::test]
async fn fail_fast_surfaces_the_capacity_error_instead_of_waiting() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .respond_with(capacity_response("PROVISIONING"))
        .mount(&server)
        .await;

    let err = client(&server)
        .await
        .encode("m", [Item::text("hi")])
        .wait_for_capacity(false)
        .send_one()
        .await
        .unwrap_err();
    assert!(matches!(err, Error::Provisioning { .. }), "{err:?}");
    assert_eq!(server.received_requests().await.unwrap().len(), 1);
}

#[tokio::test]
async fn a_model_load_failure_is_terminal() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .respond_with(ResponseTemplate::new(502).set_body_json(json!({
            "error": {"code": "MODEL_LOAD_FAILED", "message": "gated repo",
                      "error_class": "GATED", "permanent": true, "attempts": 2}
        })))
        .mount(&server)
        .await;

    let err = client(&server)
        .await
        .encode("m", [Item::text("hi")])
        .send_one()
        .await
        .unwrap_err();
    match err {
        Error::ModelLoadFailed {
            error_class,
            permanent,
            attempts,
            ..
        } => {
            assert_eq!(error_class, sie_sdk::ModelLoadErrorClass::Gated);
            assert!(permanent);
            assert_eq!(attempts, 2);
        }
        other => panic!("unexpected: {other:?}"),
    }
    // Terminal means one attempt, not one plus the retry budget.
    assert_eq!(server.received_requests().await.unwrap().len(), 1);
}

#[tokio::test]
async fn generation_never_replays_a_gateway_timeout() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/v1/generate/org__model"))
        .respond_with(
            ResponseTemplate::new(504).set_body_json(json!({"error": {"message": "timed out"}})),
        )
        .mount(&server)
        .await;

    let err = client(&server)
        .await
        .generate("org/model", "hi", 16)
        .send()
        .await
        .unwrap_err();
    assert_eq!(err.status(), Some(504));
    assert!(err.to_string().contains("double-bill"), "{err}");
    assert_eq!(server.received_requests().await.unwrap().len(), 1);
}

#[tokio::test]
async fn request_metadata_is_read_off_the_response() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .respond_with(
            msgpack_response(encode_response(&[&[1.0]]))
                .insert_header("x-sie-request-id", "req_123")
                .insert_header("x-sie-units-input-tokens", "42")
                .insert_header("x-sie-credits-debited", "7")
                .insert_header("x-sie-model-revision", "rev-9"),
        )
        .mount(&server)
        .await;

    let result = client(&server)
        .await
        .encode("m", [Item::text("hi")])
        .send_one()
        .await
        .unwrap();
    let metadata = result.request.unwrap();
    assert_eq!(metadata.id.as_deref(), Some("req_123"));
    assert_eq!(metadata.usage.unwrap().input_tokens, Some(42));
    assert_eq!(metadata.credits_debited, Some(7));
    assert_eq!(metadata.model_revision.as_deref(), Some("rev-9"));
}

#[tokio::test]
async fn chat_sends_json_and_returns_the_completion() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/v1/chat/completions"))
        .and(body_json(json!({
            "model": "qwen3",
            "messages": [{"role": "user", "content": "hi"}],
            "temperature": 0.2
        })))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "id": "cmpl-1", "object": "chat.completion", "created": 1, "model": "qwen3",
            "choices": [{"index": 0, "message": {"role": "assistant", "content": "hello"},
                         "finish_reason": "stop"}],
            "usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2}
        })))
        .mount(&server)
        .await;

    let completion = client(&server)
        .await
        .chat("qwen3", [sie_sdk::types::ChatMessage::user("hi")])
        .temperature(0.2)
        .send()
        .await
        .unwrap();
    assert_eq!(completion.text(), Some("hello"));
}

#[tokio::test]
async fn chat_streams_chunks_and_stops_at_the_done_sentinel() {
    use futures_util::StreamExt;

    let server = MockServer::start().await;
    let body = concat!(
        "data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"he\"}}]}\n\n",
        "data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"llo\"}}]}\n\n",
        "data: [DONE]\n\n",
        "data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"ignored\"}}]}\n\n",
    );
    Mock::given(method("POST"))
        .and(path("/v1/chat/completions"))
        .and(header("accept", "text/event-stream"))
        .respond_with(ResponseTemplate::new(200).set_body_raw(body, "text/event-stream"))
        .mount(&server)
        .await;

    let stream = client(&server)
        .await
        .chat("m", [sie_sdk::types::ChatMessage::user("hi")])
        .stream()
        .unwrap();
    let deltas: Vec<String> = stream
        .map(|chunk| chunk.unwrap().delta().unwrap_or_default().to_string())
        .collect()
        .await;
    assert_eq!(deltas, vec!["he", "llo"]);
}

#[tokio::test]
async fn a_capacity_error_as_the_first_stream_event_reconnects() {
    use futures_util::StreamExt;

    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .respond_with(
            ResponseTemplate::new(200)
                .insert_header("retry-after", "0")
                .set_body_raw(
                    "data: {\"error\":{\"code\":\"MODEL_LOADING\",\"message\":\"loading\"}}\n\n",
                    "text/event-stream",
                ),
        )
        .up_to_n_times(1)
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .respond_with(ResponseTemplate::new(200).set_body_raw(
            "data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"ok\"}}]}\ndata: [DONE]\n",
            "text/event-stream",
        ))
        .mount(&server)
        .await;

    let stream = client(&server)
        .await
        .chat("m", [sie_sdk::types::ChatMessage::user("hi")])
        .stream()
        .unwrap();
    let chunks: Vec<_> = stream.collect().await;
    assert_eq!(chunks.len(), 1);
    assert_eq!(chunks[0].as_ref().unwrap().delta(), Some("ok"));
}

#[tokio::test]
async fn files_upload_raw_bytes_with_metadata_in_the_query() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/v1/files"))
        .and(query_param("purpose", "batch"))
        .and(query_param("filename", "in.jsonl"))
        .and(header("content-type", "application/jsonl"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "id": "file_1", "object": "file", "bytes": 5, "created_at": 1,
            "filename": "in.jsonl", "purpose": "batch", "status": "processed"
        })))
        .mount(&server)
        .await;

    let file = client(&server)
        .await
        .files()
        .upload(b"{}\n{}".to_vec())
        .filename("in.jsonl")
        .send()
        .await
        .unwrap();
    assert_eq!(file.id, "file_1");

    let request: &Request = &server.received_requests().await.unwrap()[0];
    assert_eq!(request.body, b"{}\n{}");
}

#[tokio::test]
async fn a_connector_job_carries_its_idempotency_key() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/v1/jobs"))
        .and(header("idempotency-key", "run-1"))
        .and(body_json(json!({
            "operation": "encode", "model": "m",
            "src": "postgres://warehouse?query=SELECT+1",
            "connection": "warehouse", "execution": "run"
        })))
        .respond_with(ResponseTemplate::new(201).set_body_json(json!({
            "id": "job_1", "object": "job", "operation": "encode", "model": "m", "state": "queued"
        })))
        .mount(&server)
        .await;

    let job = client(&server)
        .await
        .jobs()
        .submit(
            sie_sdk::client::JobSource::connector("postgres://warehouse?query=SELECT+1"),
            "m",
        )
        .execution(sie_sdk::types::JobExecution::Run)
        .idempotency_key("run-1")
        .send()
        .await
        .unwrap();
    assert_eq!(job.id, "job_1");
}

#[tokio::test]
async fn capacity_filters_workers_by_gpu_type() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/health"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "status": "healthy", "type": "gateway",
            "cluster": {"worker_count": 3, "gpu_count": 3, "models_loaded": 1, "total_qps": 0.0},
            "workers": [{"name": "a", "gpu": "l4"}, {"name": "b", "gpu": "a100-80gb"}],
            "configured_gpu_types": ["l4"], "live_gpu_types": ["l4"]
        })))
        .mount(&server)
        .await;

    let client = client(&server).await;
    assert_eq!(client.get_capacity(None).await.unwrap().worker_count, 3);
    let filtered = client.get_capacity(Some("L4")).await.unwrap();
    assert_eq!(filtered.worker_count, 1);
    assert_eq!(filtered.workers[0].name, "a");
}

#[tokio::test]
async fn capacity_refuses_a_worker_endpoint() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/health"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_json(json!({"status": "healthy", "type": "worker"})),
        )
        .mount(&server)
        .await;

    let err = client(&server).await.get_capacity(None).await.unwrap_err();
    assert_eq!(err.code(), Some("not_gateway"));
}

#[tokio::test]
async fn metadata_endpoints_do_not_sit_inside_the_provisioning_budget() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .respond_with(capacity_response("PROVISIONING"))
        .mount(&server)
        .await;

    let err = client(&server).await.list_models().await.unwrap_err();
    assert_eq!(err.status(), Some(503));
    assert_eq!(server.received_requests().await.unwrap().len(), 1);
}

#[tokio::test]
async fn edge_headers_never_leave_the_base_origin() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/v1/models"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({"models": []})))
        .mount(&server)
        .await;

    // The mock server is plain HTTP, so edge headers are refused at construction.
    let err = Client::builder(server.uri())
        .base_url_headers(std::collections::HashMap::from([(
            "Modal-Key".to_string(),
            "k".to_string(),
        )]))
        .build()
        .unwrap_err();
    assert!(err.to_string().contains("https base_url"), "{err}");
}