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
use std::time::Duration;

use reqwest::{header::HeaderMap, Method, StatusCode};

use crate::{
    errors::{APIError, Error, RetryMetadata},
    REQUEST_ID_HEADER,
};

/// Optional headers for `POST /responses` calls.
#[derive(Clone, Default)]
pub struct ResponseOptions {
    pub request_id: Option<String>,
    pub headers: HeaderList,
    pub timeout: Option<Duration>,
    pub stream_timeouts: StreamTimeouts,
    pub retry: Option<RetryConfig>,
}

impl ResponseOptions {
    pub fn with_request_id(mut self, request_id: impl Into<String>) -> Self {
        self.request_id = Some(request_id.into());
        self
    }

    pub fn with_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers
            .push(HeaderEntry::new(key.into(), value.into()));
        self
    }

    /// Override the overall request timeout for this call.
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = Some(timeout);
        self
    }

    /// Configure streaming timeouts for `/responses` streams.
    pub fn with_stream_timeouts(mut self, timeouts: StreamTimeouts) -> Self {
        self.stream_timeouts = timeouts;
        self
    }

    /// Set the TTFT (time-to-first-content) timeout for streams (0 disables).
    pub fn with_stream_ttft_timeout(mut self, timeout: Duration) -> Self {
        self.stream_timeouts.ttft = Some(timeout);
        self
    }

    /// Set the idle timeout for streams (0 disables).
    pub fn with_stream_idle_timeout(mut self, timeout: Duration) -> Self {
        self.stream_timeouts.idle = Some(timeout);
        self
    }

    /// Set the total stream timeout (0 disables).
    pub fn with_stream_total_timeout(mut self, timeout: Duration) -> Self {
        self.stream_timeouts.total = Some(timeout);
        self
    }

    /// Override the retry policy for this call.
    pub fn with_retry(mut self, retry: RetryConfig) -> Self {
        self.retry = Some(retry);
        self
    }

    /// Disable retries for this call.
    pub fn disable_retry(mut self) -> Self {
        self.retry = Some(RetryConfig::disabled());
        self
    }
}

/// Streaming timeouts for `/responses` NDJSON streams.
#[derive(Clone, Copy, Debug, Default)]
pub struct StreamTimeouts {
    pub ttft: Option<Duration>,
    pub idle: Option<Duration>,
    pub total: Option<Duration>,
}

// StreamFormat enum removed - unified NDJSON is the only streaming format

/// Retry/backoff configuration (defaults use 3 attempts + jittered exponential backoff).
#[derive(Clone, Debug)]
pub struct RetryConfig {
    pub max_attempts: u32,
    pub base_backoff: Duration,
    pub max_backoff: Duration,
    pub retry_post: bool,
}

impl RetryConfig {
    pub fn disabled() -> Self {
        Self {
            max_attempts: 1,
            ..Default::default()
        }
    }

    /// Whether the given status code should trigger a retry for this method.
    pub fn should_retry_status(&self, method: &Method, status: StatusCode) -> bool {
        if status == StatusCode::TOO_MANY_REQUESTS || status == StatusCode::REQUEST_TIMEOUT {
            return self.allow_for_method(method);
        }
        if status.is_server_error() {
            return self.allow_for_method(method);
        }
        false
    }

    /// Whether the given transport error should trigger a retry.
    pub fn should_retry_error(&self, method: &Method, err: &reqwest::Error) -> bool {
        if err.is_timeout() || err.is_connect() || err.is_request() {
            return self.allow_for_method(method);
        }
        false
    }

    /// Jittered exponential backoff for the given attempt (1-indexed).
    pub fn backoff_delay(&self, attempt: u32) -> Duration {
        let exp = if attempt == 0 {
            0
        } else {
            (attempt - 1).min(10)
        };
        let base = self.base_backoff.saturating_mul(2u32.saturating_pow(exp));
        let capped = std::cmp::min(base, self.max_backoff);
        let jitter = 0.5 + fastrand::f64(); // 0.5x .. 1.5x
        let seconds = (capped.as_secs_f64() * jitter).min(self.max_backoff.as_secs_f64());
        Duration::from_secs_f64(seconds)
    }

    fn allow_for_method(&self, method: &Method) -> bool {
        if method == Method::POST {
            return self.retry_post;
        }
        true
    }
}

/// Structured header/metadata list with validation.
#[derive(Clone, Debug, Default)]
pub struct HeaderList(Vec<HeaderEntry>);

impl HeaderList {
    pub fn new() -> Self {
        Self(Vec::new())
    }

    /// Add a header entry. Panics if key or value is empty/whitespace-only.
    ///
    /// # Panics
    /// Panics if the header key or value is empty or contains only whitespace.
    /// This is a fail-fast behavior to catch configuration errors early.
    pub fn push(&mut self, entry: HeaderEntry) {
        assert!(
            entry.is_valid(),
            "Invalid header: key and value must be non-empty (got key={:?}, value={:?})",
            entry.key,
            entry.value
        );
        self.0.push(entry);
    }

    pub fn iter(&self) -> impl Iterator<Item = &HeaderEntry> {
        self.0.iter()
    }
}

#[derive(Clone, Debug)]
pub struct HeaderEntry {
    pub key: String,
    pub value: String,
}

impl HeaderEntry {
    pub fn new(key: String, value: String) -> Self {
        Self { key, value }
    }

    pub fn is_valid(&self) -> bool {
        !(self.key.trim().is_empty() || self.value.trim().is_empty())
    }
}

impl Default for RetryConfig {
    fn default() -> Self {
        Self {
            max_attempts: 3,
            base_backoff: Duration::from_millis(300),
            max_backoff: Duration::from_secs(5),
            retry_post: true,
        }
    }
}

pub(crate) fn request_id_from_headers(headers: &HeaderMap) -> Option<String> {
    if let Some(value) = headers.get(REQUEST_ID_HEADER) {
        if let Ok(s) = value.to_str() {
            if !s.is_empty() {
                return Some(s.to_string());
            }
        }
    }
    if let Some(value) = headers.get("X-Request-Id") {
        if let Ok(s) = value.to_str() {
            if !s.is_empty() {
                return Some(s.to_string());
            }
        }
    }
    None
}

/// Validates that the response content-type is NDJSON.
///
/// Returns an error if the content-type is missing or not application/x-ndjson or application/ndjson.
pub(crate) fn validate_ndjson_content_type(
    headers: &HeaderMap,
    status: u16,
) -> std::result::Result<(), crate::errors::Error> {
    let content_type = headers
        .get(reqwest::header::CONTENT_TYPE)
        .and_then(|v| v.to_str().ok())
        .map(|s| s.trim().to_lowercase());
    let is_ndjson = content_type
        .as_deref()
        .map(|ct| ct.starts_with("application/x-ndjson") || ct.starts_with("application/ndjson"))
        .unwrap_or(false);
    if !is_ndjson {
        return Err(crate::errors::Error::StreamContentType {
            expected: "application/x-ndjson",
            received: content_type.unwrap_or_else(|| "<missing>".to_string()),
            status,
        });
    }
    Ok(())
}

pub(crate) fn parse_api_error_parts(
    status: StatusCode,
    headers: &HeaderMap,
    body: String,
    retries: Option<RetryMetadata>,
) -> Error {
    let request_id = request_id_from_headers(headers);
    let status_code = status.as_u16();
    let status_text = status
        .canonical_reason()
        .unwrap_or("request failed")
        .to_string();

    if body.is_empty() {
        return APIError {
            status: status_code,
            code: None,
            message: status_text,
            request_id,
            fields: Vec::new(),
            retries,
            raw_body: None,
        }
        .into();
    }

    if let Ok(value) = serde_json::from_str::<serde_json::Value>(&body) {
        if status == StatusCode::BAD_REQUEST {
            if let Ok(mut verr) =
                serde_json::from_value::<crate::errors::WorkflowValidationError>(value.clone())
            {
                if !verr.issues.is_empty() {
                    verr.request_id = request_id.clone();
                    verr.retries = retries.clone();
                    return verr.into();
                }
            }
        }

        if let Some(err_obj) = value.get("error").and_then(|v| v.as_object()) {
            let code = err_obj
                .get("code")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string());
            let message = err_obj
                .get("message")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string())
                .unwrap_or_else(|| status_text.clone());
            let fields = err_obj
                .get("fields")
                .and_then(|v| {
                    serde_json::from_value::<Vec<crate::errors::FieldError>>(v.clone()).ok()
                })
                .unwrap_or_default();
            let status_override = err_obj
                .get("status")
                .and_then(|v| v.as_u64())
                .map(|v| v as u16)
                .unwrap_or(status_code);
            return APIError {
                status: status_override,
                code,
                message,
                request_id: value
                    .get("request_id")
                    .or_else(|| value.get("requestId"))
                    .and_then(|v| v.as_str())
                    .map(|s| s.to_string())
                    .or(request_id),
                fields,
                retries,
                raw_body: Some(body.clone()),
            }
            .into();
        }

        if let Some(message) = value.get("message").and_then(|v| v.as_str()) {
            let code = value
                .get("code")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string());
            let fields = value
                .get("fields")
                .and_then(|v| {
                    serde_json::from_value::<Vec<crate::errors::FieldError>>(v.clone()).ok()
                })
                .unwrap_or_default();
            let req_id = value
                .get("request_id")
                .or_else(|| value.get("requestId"))
                .and_then(|v| v.as_str())
                .map(|s| s.to_string())
                .or(request_id);
            return APIError {
                status: status_code,
                code,
                message: message.to_string(),
                request_id: req_id,
                fields,
                retries,
                raw_body: Some(body.clone()),
            }
            .into();
        }
    }

    APIError {
        status: status_code,
        code: None,
        message: body.clone(),
        request_id,
        fields: Vec::new(),
        retries,
        raw_body: Some(body),
    }
    .into()
}

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

    #[test]
    fn backoff_respects_max_and_jitter() {
        let retry = RetryConfig {
            max_attempts: 3,
            base_backoff: Duration::from_millis(500),
            max_backoff: Duration::from_secs(1),
            retry_post: true,
        };

        let delay = retry.backoff_delay(5);
        assert!(delay <= Duration::from_secs(1));
        assert!(delay >= Duration::from_millis(250));
    }

    #[test]
    fn retry_post_toggle_honored() {
        let retry = RetryConfig {
            retry_post: false,
            ..Default::default()
        };
        assert!(!retry.should_retry_status(&Method::POST, StatusCode::INTERNAL_SERVER_ERROR));
        assert!(retry.should_retry_status(&Method::GET, StatusCode::INTERNAL_SERVER_ERROR));
    }

    #[test]
    fn response_options_disable_retry_sets_single_attempt() {
        let opts = ResponseOptions::default().disable_retry();
        assert_eq!(opts.retry.unwrap().max_attempts, 1);
    }

    #[test]
    fn header_list_accepts_valid_entries() {
        let mut list = HeaderList::new();
        list.push(HeaderEntry::new(
            "X-Custom".to_string(),
            "value".to_string(),
        ));
        assert_eq!(list.iter().count(), 1);
    }

    #[test]
    #[should_panic(expected = "Invalid header")]
    fn header_list_panics_on_empty_key() {
        let mut list = HeaderList::new();
        list.push(HeaderEntry::new("".to_string(), "value".to_string()));
    }

    #[test]
    #[should_panic(expected = "Invalid header")]
    fn header_list_panics_on_empty_value() {
        let mut list = HeaderList::new();
        list.push(HeaderEntry::new("key".to_string(), "".to_string()));
    }

    #[test]
    #[should_panic(expected = "Invalid header")]
    fn header_list_panics_on_whitespace_only() {
        let mut list = HeaderList::new();
        list.push(HeaderEntry::new("   ".to_string(), "value".to_string()));
    }
}