zai-rs 0.6.0

Type-safe async Rust SDK for Zhipu AI (BigModel) APIs
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
//! Typed responses shared by asynchronous task-submission and polling APIs.

use serde::{Deserialize, Deserializer, Serialize, de::Error as _};

use crate::{
    ZaiResult,
    client::error::{ZaiError, codes},
    model::chat_base_response::{
        Choice, ContentFilterInfo, TaskStatus, Usage, VideoResultItem, WebSearchInfo,
    },
};

fn invalid_response(message: impl Into<String>) -> ZaiError {
    ZaiError::ApiError {
        code: codes::SDK_VALIDATION,
        message: message.into(),
    }
}

/// Acknowledgement returned after an asynchronous task is accepted.
///
/// Every field is optional in the upstream schema. A successful operation must
/// nevertheless return at least one documented non-null field; call
/// [`validate`](Self::validate) after deserializing an untrusted value.
#[derive(Debug, Clone, Serialize)]
pub struct AsyncResponse {
    /// Model that accepted the task.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,
    /// Task identifier used by the async-result endpoint.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    /// Caller-supplied or generated request identifier.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub request_id: Option<String>,
    /// Current task status, when returned at submission time.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub task_status: Option<TaskStatus>,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct AsyncResponseWire {
    model: Option<String>,
    id: Option<String>,
    request_id: Option<String>,
    task_status: Option<TaskStatus>,
}

impl<'de> Deserialize<'de> for AsyncResponse {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let wire = AsyncResponseWire::deserialize(deserializer)?;
        let response = Self {
            model: wire.model,
            id: wire.id,
            request_id: wire.request_id,
            task_status: wire.task_status,
        };
        response
            .validate()
            .map_err(|error| D::Error::custom(error.to_string()))?;
        Ok(response)
    }
}

impl AsyncResponse {
    /// Enforce the operation contract's non-empty response invariant.
    pub fn validate(&self) -> ZaiResult<()> {
        if self.model.is_some()
            || self.id.is_some()
            || self.request_id.is_some()
            || self.task_status.is_some()
        {
            return Ok(());
        }
        Err(invalid_response(
            "async task response contained no documented fields",
        ))
    }

    /// Borrow the task identifier.
    pub fn id(&self) -> Option<&str> {
        self.id.as_deref()
    }

    /// Borrow the request identifier.
    pub fn request_id(&self) -> Option<&str> {
        self.request_id.as_deref()
    }

    /// Borrow the model identifier.
    pub fn model(&self) -> Option<&str> {
        self.model.as_deref()
    }

    /// Borrow the current task status.
    pub const fn status(&self) -> Option<&TaskStatus> {
        self.task_status.as_ref()
    }
}

/// Descriptive alias for the acknowledgement returned by task submissions.
pub type AsyncTaskResponse = AsyncResponse;

/// A completed asynchronous chat-completion result.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AsyncChatTaskResult {
    /// Task identifier.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    /// Request identifier.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub request_id: Option<String>,
    /// Creation time as a Unix timestamp in seconds.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub created: Option<u64>,
    /// Current task status, when returned with the completion payload.
    /// One of `PROCESSING` / `SUCCESS` / `FAIL`; a successful completion
    /// typically carries `SUCCESS` alongside `choices` and `usage`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub task_status: Option<TaskStatus>,
    /// Model that generated the response.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,
    /// Generated choices.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub choices: Option<Vec<Choice>>,
    /// Token usage statistics.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub usage: Option<Usage>,
    /// Web-search sources used by the response.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub web_search: Option<Vec<WebSearchInfo>>,
    /// Content-safety classifications.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content_filter: Option<Vec<ContentFilterInfo>>,
}

impl AsyncChatTaskResult {
    /// Borrow the generated choices.
    pub fn choices(&self) -> Option<&[Choice]> {
        self.choices.as_deref()
    }

    /// Borrow the current task status.
    pub const fn status(&self) -> Option<&TaskStatus> {
        self.task_status.as_ref()
    }

    fn has_documented_value(&self) -> bool {
        self.id.is_some()
            || self.request_id.is_some()
            || self.created.is_some()
            || self.task_status.is_some()
            || self.model.is_some()
            || self.choices.is_some()
            || self.usage.is_some()
            || self.web_search.is_some()
            || self.content_filter.is_some()
    }
}

/// A completed asynchronous video-generation result.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AsyncVideoTaskResult {
    /// Model that generated the video.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,
    /// Current task status.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub task_status: Option<TaskStatus>,
    /// Generated videos.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub video_result: Option<Vec<VideoResultItem>>,
    /// Request identifier.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub request_id: Option<String>,
}

impl AsyncVideoTaskResult {
    /// Borrow the current task status.
    pub const fn status(&self) -> Option<&TaskStatus> {
        self.task_status.as_ref()
    }

    /// Borrow the generated video items.
    pub fn videos(&self) -> Option<&[VideoResultItem]> {
        self.video_result.as_deref()
    }

    fn has_documented_value(&self) -> bool {
        self.model.is_some()
            || self.task_status.is_some()
            || self.video_result.is_some()
            || self.request_id.is_some()
    }
}

/// One generated image returned by an asynchronous image task.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AsyncImageResultItem {
    /// URL of the generated image.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,
}

impl AsyncImageResultItem {
    /// Borrow the generated image URL.
    pub fn url(&self) -> Option<&str> {
        self.url.as_deref()
    }
}

/// A completed asynchronous image-generation result.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AsyncImageTaskResult {
    /// Model that generated the image.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,
    /// Current task status.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub task_status: Option<TaskStatus>,
    /// Generated images.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub image_result: Option<Vec<AsyncImageResultItem>>,
    /// Request identifier.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub request_id: Option<String>,
}

impl AsyncImageTaskResult {
    /// Borrow the current task status.
    pub const fn status(&self) -> Option<&TaskStatus> {
        self.task_status.as_ref()
    }

    /// Borrow the generated image items.
    pub fn images(&self) -> Option<&[AsyncImageResultItem]> {
        self.image_result.as_deref()
    }

    fn has_documented_value(&self) -> bool {
        self.model.is_some()
            || self.task_status.is_some()
            || self.image_result.is_some()
            || self.request_id.is_some()
    }
}

/// Status-only task result used while a task is pending or after it fails.
///
/// Chat, image, and video tasks share this wire shape, so it cannot be assigned
/// to one media-specific variant until a result field is present.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AsyncTaskState {
    /// Task identifier, when supplied by the service.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    /// Model handling the task.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,
    /// Request identifier.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub request_id: Option<String>,
    /// Creation time as a Unix timestamp in seconds, when supplied by the
    /// service while a task is still processing.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub created: Option<u64>,
    /// Current task status.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub task_status: Option<TaskStatus>,
}

impl AsyncTaskState {
    /// Borrow the current task status.
    pub const fn status(&self) -> Option<&TaskStatus> {
        self.task_status.as_ref()
    }

    /// Return whether the task is still processing.
    pub fn is_processing(&self) -> bool {
        matches!(self.task_status, Some(TaskStatus::Processing))
    }

    /// Return whether the task completed successfully without a typed payload.
    pub fn is_success(&self) -> bool {
        matches!(self.task_status, Some(TaskStatus::Success))
    }

    /// Return whether the task failed.
    pub fn is_failed(&self) -> bool {
        matches!(self.task_status, Some(TaskStatus::Fail))
    }

    fn has_documented_value(&self) -> bool {
        self.id.is_some()
            || self.model.is_some()
            || self.request_id.is_some()
            || self.created.is_some()
            || self.task_status.is_some()
    }
}

/// Closed set of payloads returned by the asynchronous result endpoint.
#[derive(Debug, Clone, Serialize)]
#[serde(untagged)]
pub enum AsyncTaskResult {
    /// Completed chat-completion payload.
    Chat(AsyncChatTaskResult),
    /// Completed video-generation payload.
    Video(AsyncVideoTaskResult),
    /// Completed image-generation payload.
    Image(AsyncImageTaskResult),
    /// Shared status-only payload for pending, failed, or result-less tasks.
    State(AsyncTaskState),
}

impl AsyncTaskResult {
    fn has_documented_value(&self) -> bool {
        match self {
            Self::Chat(result) => result.has_documented_value(),
            Self::Video(result) => result.has_documented_value(),
            Self::Image(result) => result.has_documented_value(),
            Self::State(result) => result.has_documented_value(),
        }
    }

    /// Borrow the status supplied by media and status-only payloads.
    pub const fn status(&self) -> Option<&TaskStatus> {
        match self {
            Self::Chat(_) => None,
            Self::Video(result) => result.task_status.as_ref(),
            Self::Image(result) => result.task_status.as_ref(),
            Self::State(result) => result.task_status.as_ref(),
        }
    }

    /// Borrow the chat payload when this is a completed chat task.
    pub const fn as_chat(&self) -> Option<&AsyncChatTaskResult> {
        match self {
            Self::Chat(result) => Some(result),
            _ => None,
        }
    }

    /// Borrow the video payload when this is a completed video task.
    pub const fn as_video(&self) -> Option<&AsyncVideoTaskResult> {
        match self {
            Self::Video(result) => Some(result),
            _ => None,
        }
    }

    /// Borrow the image payload when this is a completed image task.
    pub const fn as_image(&self) -> Option<&AsyncImageTaskResult> {
        match self {
            Self::Image(result) => Some(result),
            _ => None,
        }
    }

    /// Borrow the status-only payload.
    pub const fn as_state(&self) -> Option<&AsyncTaskState> {
        match self {
            Self::State(result) => Some(result),
            _ => None,
        }
    }
}

impl<'de> Deserialize<'de> for AsyncTaskResult {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = serde_json::Value::deserialize(deserializer)?;
        let object = value
            .as_object()
            .ok_or_else(|| D::Error::custom("async task result must be a JSON object"))?;

        let result = if object.contains_key("image_result") {
            Self::Image(
                serde_json::from_value(value)
                    .map_err(|error| D::Error::custom(error.to_string()))?,
            )
        } else if object.contains_key("video_result") {
            Self::Video(
                serde_json::from_value(value)
                    .map_err(|error| D::Error::custom(error.to_string()))?,
            )
        } else if [
            "choices",
            "usage",
            "web_search",
            "content_filter",
        ]
        .iter()
        .any(|field| object.contains_key(*field))
            || (object.contains_key("id")
                && !object.contains_key("task_status")
                && !object.contains_key("model")
                && !object.contains_key("request_id"))
        {
            Self::Chat(
                serde_json::from_value(value)
                    .map_err(|error| D::Error::custom(error.to_string()))?,
            )
        } else if ["id", "model", "request_id", "created", "task_status"]
            .iter()
            .any(|field| object.get(*field).is_some_and(|value| !value.is_null()))
        {
            Self::State(
                serde_json::from_value(value)
                    .map_err(|error| D::Error::custom(error.to_string()))?,
            )
        } else {
            return Err(D::Error::custom(
                "async task result contained no documented non-null fields",
            ));
        };

        if !result.has_documented_value() {
            return Err(D::Error::custom(
                "async task result contained no documented non-null fields",
            ));
        }

        Ok(result)
    }
}

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

    #[test]
    fn task_acknowledgement_requires_a_documented_value() {
        assert!(serde_json::from_str::<AsyncResponse>("{}").is_err());
        assert!(serde_json::from_str::<AsyncResponse>(r#"{"id":null}"#).is_err());

        let response: AsyncResponse = serde_json::from_value(serde_json::json!({
            "id": "task-1",
            "task_status": "PROCESSING"
        }))
        .unwrap();
        assert_eq!(response.id(), Some("task-1"));
        assert!(matches!(response.status(), Some(TaskStatus::Processing)));
        assert!(response.validate().is_ok());
    }

    #[test]
    fn task_result_rejects_empty_and_unknown_payloads() {
        assert!(serde_json::from_str::<AsyncTaskResult>("{}").is_err());
        assert!(serde_json::from_str::<AsyncTaskResult>(r#"{"task_id":"legacy"}"#).is_err());
        assert!(serde_json::from_str::<AsyncTaskResult>(r#"{"choices":null}"#).is_err());
        assert!(serde_json::from_str::<AsyncTaskResult>(r#"{"video_result":null}"#).is_err());
        assert!(serde_json::from_str::<AsyncTaskResult>(r#"{"image_result":null}"#).is_err());
    }

    #[test]
    fn task_result_selects_each_closed_variant() {
        let chat: AsyncTaskResult = serde_json::from_value(serde_json::json!({
            "id": "chat-1",
            "choices": [{"index": 0, "message": {"role": "assistant", "content": "done"}}]
        }))
        .unwrap();
        assert_eq!(
            chat.as_chat()
                .and_then(|value| value.choices())
                .map(<[_]>::len),
            Some(1)
        );

        let video: AsyncTaskResult = serde_json::from_value(serde_json::json!({
            "model": "cogvideox-3",
            "task_status": "SUCCESS",
            "video_result": [{"url": "https://example.com/video.mp4"}]
        }))
        .unwrap();
        assert_eq!(
            video
                .as_video()
                .and_then(|value| value.videos())
                .map(<[_]>::len),
            Some(1)
        );

        let image: AsyncTaskResult = serde_json::from_value(serde_json::json!({
            "model": "glm-image",
            "task_status": "SUCCESS",
            "image_result": [{"url": "https://example.com/image.png"}]
        }))
        .unwrap();
        assert_eq!(
            image
                .as_image()
                .and_then(|value| value.images())
                .map(<[_]>::len),
            Some(1)
        );

        let state: AsyncTaskResult = serde_json::from_value(serde_json::json!({
            "request_id": "request-1",
            "task_status": "PROCESSING"
        }))
        .unwrap();
        assert!(state.as_state().is_some_and(AsyncTaskState::is_processing));
    }

    /// The async-result endpoint returns `task_status` while a chat task is
    /// still processing, and alongside `choices`/`usage` once it succeeds.
    /// Both shapes must deserialize; the processing payload routes to the
    /// shared `State` variant and the success payload to `Chat`. Regression
    /// test for the "unknown field `task_status`" failure seen while polling.
    #[test]
    fn task_result_handles_chat_task_status_at_every_stage() {
        // Processing chat task: the polling endpoint echoes the creation
        // timestamp and task_status but no completion payload.
        let processing: AsyncTaskResult = serde_json::from_value(serde_json::json!({
            "id": "task-1",
            "model": "glm-5.2",
            "created": 1_700_000_000_u64,
            "request_id": "request-1",
            "task_status": "PROCESSING"
        }))
        .unwrap();
        let processing_state = processing
            .as_state()
            .expect("processing chat task must route to the State variant");
        assert!(processing_state.is_processing());

        // Completed chat task: the success payload now carries task_status.
        let done: AsyncTaskResult = serde_json::from_value(serde_json::json!({
            "id": "task-1",
            "model": "glm-5.2",
            "created": 1_700_000_000_u64,
            "request_id": "request-1",
            "task_status": "SUCCESS",
            "choices": [{"index": 0, "message": {"role": "assistant", "content": "done"}}]
        }))
        .unwrap();
        let chat = done
            .as_chat()
            .expect("completed chat task must route to the Chat variant");
        assert_eq!(
            chat.status(),
            Some(&TaskStatus::Success),
            "completed chat result should expose its task_status"
        );
        assert_eq!(
            chat.choices().map(<[_]>::len),
            Some(1),
            "completed chat result should expose its choices"
        );
    }
}