zai-rs 0.5.1

一个 Rust SDK, 用于调用 智谱AI 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
//! Buffered HTTP transport used internally by [`ZaiClient`](super::ZaiClient).
//!
//! Execution pipeline:
//! build request → enforce the JSON request limit → send/retry → buffer and cap
//! the response → let the caller decode bytes or JSON.
//!
//! The default client uses a 10-second connect timeout and a 60-second
//! per-attempt timeout. A configured transport derives its overall deadline as
//! `request_timeout * max_attempts`. Backoff uses full jitter with an injectable
//! [`JitterSource`] so tests can use deterministic delays.
//!
//! JSON, empty-body, binary, and multipart endpoints share authentication,
//! retry, redirect, size-limit and response-decoding behavior.

#![allow(dead_code)]

pub mod decode;
pub mod download;
pub mod limits;
pub mod multipart;
pub mod redaction;
pub mod redirect;
pub mod request;
pub mod retry;

use std::sync::Arc;
use std::time::Duration;

use bytes::Bytes;
use tracing::warn;

use crate::ZaiError;
use crate::ZaiResult;
use crate::client::error::codes;
use crate::client::secret::ApiSecret;
use crate::client::transport::limits::{
    ERROR_BODY_MAX, JSON_REQUEST_MAX, JSON_RESPONSE_MAX, MULTIPART_FILE_BYTES_MAX,
};
use crate::client::transport::redirect::follow as follow_redirect;
use crate::client::transport::request::{PreparedRequest, ResponseMode};
use crate::client::transport::retry::{JitterSource, RetrySafety, backoff_delay};
#[allow(unused_imports)]
use crate::client::transport::retry::{
    is_retryable_outcome, parse_retry_after, reconcile_retry_after,
};

/// Timeout values used by the transport.
///
/// `Default` yields connect 10s, attempt 60s, overall 120s, and stream idle
/// 60s. The internal `Transport::new` constructor replaces `overall` with
/// `request_timeout * max_attempts` from the public transport configuration.
#[derive(Debug, Clone, Copy)]
pub struct TimeoutPolicy {
    /// TCP connection timeout.
    pub connect: Duration,
    /// Deadline for one HTTP attempt.
    pub attempt: Duration,
    /// Deadline covering attempts, redirects, and backoff.
    pub overall: Duration,
    /// Reserved idle deadline for streaming transports.
    pub stream_idle: Duration,
}

impl Default for TimeoutPolicy {
    fn default() -> Self {
        Self {
            connect: Duration::from_secs(10),
            attempt: Duration::from_secs(60),
            overall: Duration::from_secs(120),
            stream_idle: Duration::from_secs(60),
        }
    }
}

/// An injectable clock, so tests advance virtual time instead of sleeping.
pub trait Clock: Send + Sync {
    /// Monotonic instant "now".
    fn now(&self) -> std::time::Instant;
}

/// The real wall clock.
pub struct WallClock;
impl Clock for WallClock {
    fn now(&self) -> std::time::Instant {
        std::time::Instant::now()
    }
}

/// The Transport: holds the shared reqwest client, timeout policy, max attempts
/// and injected jitter/clock. Built once per `ZaiClient`.
pub(crate) struct Transport {
    pub(crate) reqwest: reqwest::Client,
    pub(crate) timeouts: TimeoutPolicy,
    pub(crate) max_attempts: u8,
    pub(crate) jitter: Arc<dyn JitterSource>,
    pub(crate) clock: Arc<dyn Clock>,
    secret: ApiSecret,
    additional_headers: Vec<crate::client::AdditionalHeader>,
}

/// A fully buffered response produced by the transport pipeline.
///
/// Keeping status, headers and bytes together replaces the old dependency on
/// `reqwest::Response` and lets typed JSON and binary endpoints share one path.
pub struct TransportResponse {
    status: u16,
    headers: reqwest::header::HeaderMap,
    body: Bytes,
}

impl TransportResponse {
    /// Return the HTTP status code from the final response.
    pub fn status(&self) -> u16 {
        self.status
    }
    /// Borrow the response headers from the final response.
    pub fn headers(&self) -> &reqwest::header::HeaderMap {
        &self.headers
    }
    /// Consume the response and return its buffered body.
    ///
    /// A recognized business-error envelope or non-2xx HTTP status is converted
    /// to [`ZaiError`] before bytes are returned.
    pub fn bytes(self) -> ZaiResult<Bytes> {
        if let Ok(text) = std::str::from_utf8(&self.body)
            && let Some(error) = decode::extract_error_envelope(text)
        {
            return Err(api_error(self.status, error));
        }
        if !(200..300).contains(&self.status) {
            return Err(ZaiError::from_api_response(
                self.status,
                0,
                String::from_utf8_lossy(&self.body).into_owned(),
            ));
        }
        Ok(self.body)
    }

    /// Consume the response and deserialize its buffered JSON body.
    ///
    /// Business-error envelopes and non-2xx statuses are handled before JSON
    /// deserialization. This method does not validate the response
    /// `Content-Type` header.
    pub fn json<T: serde::de::DeserializeOwned>(self) -> ZaiResult<T> {
        if let Ok(text) = std::str::from_utf8(&self.body)
            && let Some(error) = decode::extract_error_envelope(text)
        {
            return Err(api_error(self.status, error));
        }
        if !(200..300).contains(&self.status) {
            return Err(ZaiError::from_api_response(
                self.status,
                0,
                String::from_utf8_lossy(&self.body).into_owned(),
            ));
        }
        serde_json::from_slice(&self.body).map_err(ZaiError::from)
    }
}

struct SystemJitter;
impl JitterSource for SystemJitter {
    fn jitter(&self, upper: Duration) -> Duration {
        let upper_nanos = upper.as_nanos();
        if upper_nanos == 0 {
            return Duration::ZERO;
        }
        let seed = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map_or(0, |d| d.as_nanos());
        Duration::from_nanos((seed % (upper_nanos + 1)) as u64)
    }
}

/// Outcome of a single send attempt, used by the retry loop.
pub(crate) enum AttemptOutcome {
    /// A final HTTP response (status + headers + body bytes already capped).
    Response {
        status: u16,
        headers: reqwest::header::HeaderMap,
        body: Bytes,
    },
    /// A transient error that may be retried.
    Transient(ZaiError),
    /// A terminal error (auth, non-retryable, etc.).
    Terminal(ZaiError),
}

impl Transport {
    pub(crate) fn new(
        reqwest: reqwest::Client,
        secret: ApiSecret,
        config: &crate::client::HttpTransportConfig,
    ) -> Self {
        Self {
            reqwest,
            timeouts: TimeoutPolicy {
                connect: config.connect_timeout,
                attempt: config.request_timeout,
                overall: config
                    .request_timeout
                    .saturating_mul(u32::from(config.max_attempts)),
                stream_idle: Duration::from_secs(60),
            },
            max_attempts: config.max_attempts,
            jitter: Arc::new(SystemJitter),
            clock: Arc::new(WallClock),
            secret,
            additional_headers: config.additional_headers.clone(),
        }
    }

    /// Send a prepared request with the retry/timeout pipeline, returning the
    /// (status, headers, body) of the final attempt. The caller performs the
    /// typed decode.
    ///
    /// Multipart bodies are rebuilt for every attempt. Responses are fully
    /// buffered; this method does not implement SSE streaming.
    #[allow(clippy::too_many_lines)]
    pub(crate) async fn send(&self, prepped: &PreparedRequest<'_>) -> ZaiResult<TransportResponse> {
        // Enforce request body limit up front.
        if let request::BodyKind::Bytes(b) = &prepped.body
            && (b.len() as u64) > JSON_REQUEST_MAX
        {
            return Err(payload_too_large(JSON_REQUEST_MAX));
        }

        let safety = prepped.retry_safety.effective(prepped.retry_override);
        let deadline = self.clock.now() + self.timeouts.overall;
        let max_attempts = self.effective_max_attempts(safety);

        let mut attempt: u8 = 0;
        let mut url = prepped.url.clone();
        let mut hops: u8 = 0;
        loop {
            attempt += 1;
            if self.clock.now() >= deadline {
                return Err(timeout_overall());
            }

            let req = self.build_request(prepped.method, &url, &prepped.body)?;
            let send_result = tokio::time::timeout(self.timeouts.attempt, req.send()).await;

            let outcome = match send_result {
                Err(_) => AttemptOutcome::Transient(timeout_attempt()),
                Ok(Err(e)) => AttemptOutcome::Transient(ZaiError::from(e)),
                Ok(Ok(resp)) => {
                    let status = resp.status().as_u16();
                    let headers = resp.headers().clone();

                    // Redirect handling.
                    if (300..400).contains(&status) {
                        let location = headers
                            .get(reqwest::header::LOCATION)
                            .and_then(|v| v.to_str().ok())
                            .unwrap_or("");
                        match follow_redirect(
                            &url::Url::parse(&url).map_err(|_| invalid("current url parse"))?,
                            status,
                            location,
                            safety,
                            prepped.method,
                            hops,
                        ) {
                            Ok(Some(target)) => {
                                hops += 1;
                                url = target.to_string();
                                continue; // rebuild the request for the accepted same-origin target
                            },
                            Ok(None) | Err(_) => {
                                // Don't follow; treat as terminal with the body.
                                let body = cap_body(resp, ERROR_BODY_MAX).await;
                                return Ok(TransportResponse {
                                    status,
                                    headers,
                                    body,
                                });
                            },
                        }
                    }

                    let limit = if (200..300).contains(&status) {
                        match prepped.response_mode {
                            ResponseMode::Json => JSON_RESPONSE_MAX,
                            ResponseMode::Binary => MULTIPART_FILE_BYTES_MAX,
                        }
                    } else {
                        ERROR_BODY_MAX
                    };
                    let body = cap_body(resp, limit).await;
                    AttemptOutcome::Response {
                        status,
                        headers,
                        body,
                    }
                },
            };

            match outcome {
                AttemptOutcome::Response {
                    status,
                    headers,
                    body,
                } => {
                    let business_code = std::str::from_utf8(&body)
                        .ok()
                        .and_then(decode::extract_error_envelope)
                        .and_then(|e| e.code)
                        .and_then(|v| match v {
                            serde_json::Value::Number(n) => n.as_u64().map(|n| n as u16),
                            serde_json::Value::String(s) => s.parse().ok(),
                            _ => None,
                        });
                    if safety == RetrySafety::Idempotent
                        && is_retryable_outcome(status, business_code)
                        && attempt < max_attempts
                    {
                        let computed = backoff_delay(u32::from(attempt) - 1, self.jitter.as_ref());
                        let hint = headers
                            .get(reqwest::header::RETRY_AFTER)
                            .and_then(|v| v.to_str().ok())
                            .and_then(parse_retry_after);
                        let delay = reconcile_retry_after(hint, computed);
                        if self.clock.now() + delay >= deadline {
                            return Err(timeout_overall());
                        }
                        tokio::time::sleep(delay).await;
                        continue;
                    }
                    return Ok(TransportResponse {
                        status,
                        headers,
                        body,
                    });
                },
                AttemptOutcome::Terminal(e) => return Err(e),
                AttemptOutcome::Transient(e) => {
                    if attempt >= max_attempts {
                        return Err(retry_exhausted(e));
                    }
                    // Network/timeout failures have no HTTP response headers, so
                    // this branch uses jitter without a Retry-After hint.
                    let delay = backoff_delay(u32::from(attempt) - 1, self.jitter.as_ref());
                    if self.clock.now() + delay >= deadline {
                        return Err(timeout_overall());
                    }
                    tokio::time::sleep(delay).await;
                },
            }
        }
    }

    fn effective_max_attempts(&self, safety: RetrySafety) -> u8 {
        match safety {
            RetrySafety::Idempotent => self.max_attempts.max(1),
            RetrySafety::NonIdempotent => 1,
        }
    }

    fn build_request(
        &self,
        method: &str,
        url: &str,
        body: &request::BodyKind<'_>,
    ) -> ZaiResult<reqwest::RequestBuilder> {
        let m = reqwest::Method::from_bytes(method.as_bytes()).unwrap_or(reqwest::Method::GET);
        let mut auth =
            reqwest::header::HeaderValue::from_str(&format!("Bearer {}", self.secret.expose()))
                .map_err(|_| invalid("invalid authorization header"))?;
        auth.set_sensitive(true);
        let mut rb = self
            .reqwest
            .request(m, url)
            .header(reqwest::header::AUTHORIZATION, auth)
            .header(reqwest::header::ACCEPT, "application/json")
            .header(
                reqwest::header::USER_AGENT,
                concat!("zai-rs/", env!("CARGO_PKG_VERSION")),
            );
        for header in &self.additional_headers {
            rb = rb.header(header.name(), header.value());
        }
        Ok(match body {
            request::BodyKind::None => rb,
            request::BodyKind::Json(v) => rb
                .header(reqwest::header::CONTENT_TYPE, "application/json")
                .body(serde_json::to_vec(v).map_err(ZaiError::from)?),
            request::BodyKind::Bytes(b) => rb
                .header(reqwest::header::CONTENT_TYPE, "application/json")
                .body((*b).clone()),
            request::BodyKind::Multipart(factory) => rb.multipart(factory.build()?),
        })
    }
}

fn api_error(status: u16, error: decode::BusinessError) -> ZaiError {
    let code = error
        .code
        .and_then(|v| match v {
            serde_json::Value::Number(n) => n.as_u64().map(|n| n as u16),
            serde_json::Value::String(s) => s.parse().ok(),
            _ => None,
        })
        .unwrap_or(0);
    ZaiError::from_api_response(status, code, error.message)
}

/// Buffer at most `limit` response bytes.
///
/// Oversized bodies are truncated. A body-read error or 60-second read timeout
/// produces an empty buffer; callers will subsequently surface a status or
/// decode error.
async fn cap_body(resp: reqwest::Response, limit: u64) -> Bytes {
    // Read up to limit+1 to detect overflow.
    match tokio::time::timeout(Duration::from_secs(60), resp.bytes()).await {
        Ok(Ok(b)) if (b.len() as u64) > limit => {
            warn!(
                bytes = b.len(),
                limit, "response body exceeded limit; truncated"
            );
            b.slice(..limit as usize)
        },
        Ok(Ok(b)) => b,
        Ok(Err(_)) | Err(_) => Bytes::new(),
    }
}

fn payload_too_large(limit: u64) -> ZaiError {
    ZaiError::ApiError {
        code: codes::SDK_VALIDATION,
        message: format!("request body exceeded limit ({limit} bytes)"),
    }
}

fn timeout_attempt() -> ZaiError {
    ZaiError::ApiError {
        code: codes::SDK_TIMEOUT,
        message: "per-attempt timeout exceeded".to_string(),
    }
}

fn timeout_overall() -> ZaiError {
    ZaiError::ApiError {
        code: codes::SDK_TIMEOUT,
        message: "overall deadline exceeded".to_string(),
    }
}

fn retry_exhausted(prev: ZaiError) -> ZaiError {
    ZaiError::ApiError {
        code: codes::SDK_TIMEOUT,
        message: format!("retry exhausted: {prev}"),
    }
}

fn invalid(msg: &str) -> ZaiError {
    ZaiError::ApiError {
        code: codes::SDK_CONFIG,
        message: msg.to_string(),
    }
}

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

    #[test]
    fn timeout_defaults_match_plan() {
        let t = TimeoutPolicy::default();
        assert_eq!(t.connect, Duration::from_secs(10));
        assert_eq!(t.attempt, Duration::from_secs(60));
        assert_eq!(t.overall, Duration::from_secs(120));
        assert_eq!(t.stream_idle, Duration::from_secs(60));
    }

    #[test]
    fn nonidempotent_max_attempts_is_one() {
        // A constructed Transport is heavy (needs a reqwest client); test the
        // pure logic instead.
        let safety = RetrySafety::NonIdempotent;
        assert_eq!(effective_max_attempts_for(safety, 3), 1);
        let safety = RetrySafety::Idempotent;
        assert_eq!(effective_max_attempts_for(safety, 3), 3);
        assert_eq!(effective_max_attempts_for(safety, 1), 1);
    }

    fn effective_max_attempts_for(safety: RetrySafety, configured: u8) -> u8 {
        match safety {
            RetrySafety::Idempotent => configured.max(1),
            RetrySafety::NonIdempotent => 1,
        }
    }
}