modelrelay 6.5.0

Rust SDK for the ModelRelay API
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
// The mock module's low-level proxy methods are pub(crate) because the public API
// only exposes builders. This module can be used for internal testing or redesigned
// to work with builders in the future.
#![allow(dead_code)]

use std::{
    collections::VecDeque,
    sync::{Arc, Mutex},
};

use crate::{
    errors::{Error, Result},
    types::{
        APIKey, CustomerToken, CustomerTokenRequest, Model, Response, ResponseRequest, StreamEvent,
        Usage,
    },
    ResponseOptions,
};

#[cfg(feature = "streaming")]
use crate::ResponseStreamAdapter;
#[cfg(feature = "streaming")]
use crate::{ndjson::StreamHandle, StreamEventKind};
#[cfg(feature = "blocking")]
use crate::{BlockingStreamHandle, ResponseOptions as BlockingResponseOptions};
use chrono::{Duration, Utc};
use uuid::Uuid;

/// In-memory mock configuration for offline tests.
#[derive(Default)]
pub struct MockConfig {
    pub responses: Vec<Result<Response>>,
    pub stream_sequences: Vec<Vec<Result<StreamEvent>>>,
    pub customer_tokens: Vec<Result<CustomerToken>>,
}

impl MockConfig {
    pub fn with_response(mut self, resp: Response) -> Self {
        self.responses.push(Ok(resp));
        self
    }

    pub fn with_response_error(mut self, err: Error) -> Self {
        self.responses.push(Err(err));
        self
    }

    pub fn with_stream_events(mut self, events: Vec<StreamEvent>) -> Self {
        self.stream_sequences
            .push(events.into_iter().map(Ok).collect());
        self
    }

    pub fn with_stream_results(mut self, events: Vec<Result<StreamEvent>>) -> Self {
        self.stream_sequences.push(events);
        self
    }

    pub fn with_customer_token(mut self, token: CustomerToken) -> Self {
        self.customer_tokens.push(Ok(token));
        self
    }
}

#[derive(Clone)]
pub struct MockClient {
    inner: Arc<MockInner>,
}

impl MockClient {
    pub fn new(cfg: MockConfig) -> Self {
        Self {
            inner: Arc::new(MockInner::new(cfg)),
        }
    }

    pub fn responses(&self) -> MockResponsesClient {
        MockResponsesClient {
            inner: self.inner.clone(),
        }
    }

    #[cfg(feature = "blocking")]
    pub fn blocking_responses(&self) -> MockBlockingResponsesClient {
        MockBlockingResponsesClient {
            inner: self.inner.clone(),
        }
    }

    pub fn auth(&self) -> MockAuthClient {
        MockAuthClient {
            inner: self.inner.clone(),
        }
    }
}

struct MockInner {
    responses: Mutex<VecDeque<Result<Response>>>,
    stream_sequences: Mutex<VecDeque<Vec<Result<StreamEvent>>>>,
    customer_tokens: Mutex<VecDeque<Result<CustomerToken>>>,
}

impl MockInner {
    fn new(cfg: MockConfig) -> Self {
        Self {
            responses: Mutex::new(VecDeque::from(cfg.responses)),
            stream_sequences: Mutex::new(VecDeque::from(cfg.stream_sequences)),
            customer_tokens: Mutex::new(VecDeque::from(cfg.customer_tokens)),
        }
    }

    fn next_response(&self) -> Result<Response> {
        self.responses
            .lock()
            .expect("lock poisoned")
            .pop_front()
            .unwrap_or_else(|| Err(Error::Validation("no mock response queued".into())))
    }

    #[cfg(feature = "streaming")]
    fn next_stream(&self) -> Result<Vec<Result<StreamEvent>>> {
        self.stream_sequences
            .lock()
            .expect("lock poisoned")
            .pop_front()
            .ok_or_else(|| Error::Validation("no mock stream events queued".into()))
    }

    fn next_customer_token(&self) -> Result<CustomerToken> {
        self.customer_tokens
            .lock()
            .expect("lock poisoned")
            .pop_front()
            .unwrap_or_else(|| Err(Error::Validation("no mock customer token queued".into())))
    }
}

#[derive(Clone)]
pub struct MockResponsesClient {
    inner: Arc<MockInner>,
}

#[derive(Clone)]
pub struct MockAuthClient {
    inner: Arc<MockInner>,
}

impl MockAuthClient {
    pub async fn customer_token(&self, _req: CustomerTokenRequest) -> Result<CustomerToken> {
        self.inner.next_customer_token()
    }
}

impl MockResponsesClient {
    pub(crate) async fn create(
        &self,
        req: ResponseRequest,
        options: ResponseOptions,
    ) -> Result<Response> {
        req.validate(true)?;
        let mut resp = self.inner.next_response()?;
        if resp.request_id.is_none() {
            resp.request_id = options.request_id;
        }
        Ok(resp)
    }

    #[cfg(feature = "streaming")]
    pub(crate) async fn stream(
        &self,
        req: ResponseRequest,
        options: ResponseOptions,
    ) -> Result<StreamHandle> {
        req.validate(true)?;
        let results = self.inner.next_stream()?;
        let mut events = Vec::new();
        for res in results {
            events.push(res?);
        }
        let req_id = options.request_id.clone().or_else(|| {
            events
                .iter()
                .find_map(|evt| evt.request_id.clone().filter(|v| !v.is_empty()))
        });
        let events = events
            .into_iter()
            .map(|mut evt| {
                if evt.request_id.is_none() {
                    evt.request_id = req_id.clone();
                }
                evt
            })
            .collect::<Vec<_>>();
        Ok(StreamHandle::from_events_with_request_id(events, req_id))
    }

    #[cfg(feature = "streaming")]
    pub(crate) async fn stream_deltas(
        &self,
        req: ResponseRequest,
        options: ResponseOptions,
    ) -> Result<std::pin::Pin<Box<dyn futures_core::Stream<Item = Result<String>> + Send>>> {
        let stream = self.stream(req, options).await?;
        Ok(Box::pin(
            ResponseStreamAdapter::<crate::StreamHandle>::new(stream).into_stream(),
        ))
    }
}

#[cfg(feature = "blocking")]
#[derive(Clone)]
pub struct MockBlockingResponsesClient {
    inner: Arc<MockInner>,
}

#[cfg(feature = "blocking")]
impl MockBlockingResponsesClient {
    pub(crate) fn create(
        &self,
        req: ResponseRequest,
        options: BlockingResponseOptions,
    ) -> Result<Response> {
        req.validate(true)?;
        let mut resp = self.inner.next_response()?;
        if resp.request_id.is_none() {
            resp.request_id = options.request_id;
        }
        Ok(resp)
    }

    #[cfg(feature = "streaming")]
    pub(crate) fn stream(
        &self,
        req: ResponseRequest,
        options: BlockingResponseOptions,
    ) -> Result<BlockingStreamHandle> {
        req.validate(true)?;
        let results = self.inner.next_stream()?;
        let mut events = Vec::new();
        for res in results {
            events.push(res?);
        }
        let req_id = options.request_id.clone().or_else(|| {
            events
                .iter()
                .find_map(|evt| evt.request_id.clone().filter(|v| !v.is_empty()))
        });
        let events = events
            .into_iter()
            .map(|mut evt| {
                if evt.request_id.is_none() {
                    evt.request_id = req_id.clone();
                }
                evt
            })
            .collect::<Vec<_>>();
        Ok(BlockingStreamHandle::from_events_with_request_id(
            events, req_id,
        ))
    }

    #[cfg(all(feature = "blocking", feature = "streaming"))]
    pub(crate) fn stream_deltas(
        &self,
        req: ResponseRequest,
        options: BlockingResponseOptions,
    ) -> Result<Box<dyn Iterator<Item = Result<String>>>> {
        let stream = self.stream(req, options)?;
        Ok(Box::new(
            crate::ResponseStreamAdapter::<crate::BlockingStreamHandle>::new(stream).into_iter(),
        ))
    }
}

pub mod fixtures {
    use super::*;

    pub fn simple_response() -> Response {
        Response {
            id: "resp_mock_123".into(),
            stop_reason: Some(crate::StopReason::Stop),
            model: Model::from("gpt-4o-mini"),
            output: vec![crate::OutputItem::Message {
                role: crate::MessageRole::Assistant,
                content: vec![crate::ContentPart::text("hello world")],
                tool_calls: None,
            }],
            usage: Usage {
                input_tokens: 10,
                output_tokens: 5,
                total_tokens: 15,
            },
            request_id: Some("req_mock_123".into()),
            provider: None,
            citations: None,
            decoding_warnings: None,
        }
    }

    #[cfg(feature = "streaming")]
    pub fn simple_stream_events() -> Vec<StreamEvent> {
        vec![
            StreamEvent {
                kind: StreamEventKind::MessageStart,
                event: "message_start".into(),
                data: None,
                text_delta: None,
                tool_call_delta: None,
                tool_calls: None,
                tool_result: None,
                response_id: Some("resp_stream_mock".into()),
                model: Some(Model::from("gpt-4o-mini")),
                stop_reason: None,
                usage: None,
                request_id: Some("req_stream_mock".into()),
                raw: "{}".into(),
            },
            StreamEvent {
                kind: StreamEventKind::MessageDelta,
                event: "message_delta".into(),
                data: None,
                text_delta: Some("hello".into()),
                tool_call_delta: None,
                tool_calls: None,
                tool_result: None,
                response_id: Some("resp_stream_mock".into()),
                model: Some(Model::from("gpt-4o-mini")),
                stop_reason: None,
                usage: None,
                request_id: Some("req_stream_mock".into()),
                raw: "{}".into(),
            },
            StreamEvent {
                kind: StreamEventKind::MessageStop,
                event: "message_stop".into(),
                data: None,
                text_delta: None,
                tool_call_delta: None,
                tool_calls: None,
                tool_result: None,
                response_id: Some("resp_stream_mock".into()),
                model: Some(Model::from("gpt-4o-mini")),
                stop_reason: Some(crate::StopReason::Completed),
                usage: Some(Usage {
                    input_tokens: 10,
                    output_tokens: 5,
                    total_tokens: 15,
                }),
                request_id: Some("req_stream_mock".into()),
                raw: "{}".into(),
            },
        ]
    }

    pub fn customer_token() -> CustomerToken {
        CustomerToken {
            token: "mr_eut_mock".into(),
            expires_at: Utc::now() + Duration::hours(1),
            expires_in: 3600,
            project_id: Uuid::new_v4(),
            customer_id: Some(Uuid::new_v4()),
            customer_external_id: "customer_mock_123".into(),
            tier_code: Some("free".parse().expect("valid tier code")),
        }
    }

    pub fn api_key(label: &str) -> APIKey {
        APIKey {
            id: Uuid::new_v4(),
            label: label.into(),
            kind: "secret".into(),
            created_at: Utc::now(),
            expires_at: None,
            last_used_at: None,
            redacted_key: format!("mr_sk_{label}_redacted"),
            secret_key: Some(format!("mr_sk_{label}")),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[cfg(feature = "streaming")]
    use futures_util::StreamExt;

    /// Helper to create a simple ResponseRequest for testing.
    fn simple_request(model: &str) -> ResponseRequest {
        ResponseRequest {
            provider: None,
            model: Some(Model::from(model)),
            input: vec![crate::InputItem::user("hi")],
            output_format: None,
            max_output_tokens: None,
            temperature: None,
            stop: None,
            tools: None,
            tool_choice: None,
        }
    }

    #[tokio::test]
    async fn responses_returns_queued_response() {
        let cfg = MockConfig::default().with_response(fixtures::simple_response());
        let client = MockClient::new(cfg);
        let resp = client
            .responses()
            .create(
                simple_request("gpt-4o-mini"),
                ResponseOptions::default().with_request_id("req_override"),
            )
            .await
            .unwrap();
        let text = resp
            .output
            .iter()
            .filter_map(|item| match item {
                crate::OutputItem::Message { role, content, .. } => {
                    (*role == crate::MessageRole::Assistant).then_some(content)
                }
            })
            .flatten()
            .filter_map(|p| match p {
                crate::ContentPart::Text { text } => Some(text.as_str()),
                crate::ContentPart::File { .. } => None,
            })
            .collect::<String>();
        assert_eq!(text, "hello world");
        assert_eq!(resp.request_id.as_deref(), Some("req_mock_123"));
    }

    #[cfg(feature = "streaming")]
    #[tokio::test]
    async fn responses_stream_yields_events() {
        let cfg = MockConfig::default().with_stream_events(fixtures::simple_stream_events());
        let client = MockClient::new(cfg);
        let mut stream = client
            .responses()
            .stream(simple_request("gpt-4o-mini"), ResponseOptions::default())
            .await
            .unwrap();

        let mut deltas = String::new();
        while let Some(evt) = stream.next().await {
            let evt = evt.unwrap();
            if let Some(text) = evt.text_delta {
                deltas.push_str(&text);
            }
        }
        assert_eq!(deltas, "hello");
    }

    #[cfg(feature = "streaming")]
    #[tokio::test]
    async fn responses_stream_delta_adapter_collects() {
        use futures_util::StreamExt;

        let cfg = MockConfig::default().with_stream_events(fixtures::simple_stream_events());
        let client = MockClient::new(cfg);
        let mut deltas = String::new();
        let stream = client
            .responses()
            .stream_deltas(simple_request("gpt-4o-mini"), ResponseOptions::default())
            .await
            .unwrap();
        futures_util::pin_mut!(stream);
        while let Some(chunk) = stream.next().await {
            deltas.push_str(&chunk.unwrap());
        }
        assert_eq!(deltas, "hello");
    }

    #[tokio::test]
    async fn responses_smoke_test() {
        let mut resp = fixtures::simple_response();
        resp.request_id = None;
        let cfg = MockConfig::default().with_response(resp);
        let client = MockClient::new(cfg);

        let resp = client
            .responses()
            .create(
                simple_request("openai/gpt-4o-mini"),
                ResponseOptions::default().with_request_id("req_test"),
            )
            .await
            .unwrap();
        assert_eq!(resp.id, "resp_mock_123");
        assert_eq!(resp.request_id.as_deref(), Some("req_test"));
    }

    #[cfg(feature = "blocking")]
    #[test]
    fn blocking_responses_returns_response() {
        let cfg = MockConfig::default().with_response(fixtures::simple_response());
        let client = MockClient::new(cfg);
        let resp = client
            .blocking_responses()
            .create(
                simple_request("openai/gpt-4o-mini"),
                BlockingResponseOptions::default(),
            )
            .unwrap();
        assert_eq!(resp.model, Model::from("gpt-4o-mini"));
    }
}